GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / xtensa / platforms / iss / network.c
blobd974d7f9045230d5700c4b32484dc7f82c760dd3
1 /*
3 * arch/xtensa/platforms/iss/network.c
5 * Platform specific initialization.
7 * Authors: Chris Zankel <chris@zankel.net>
8 * Based on work form the UML team.
10 * Copyright 2005 Tensilica Inc.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
19 #include <linux/list.h>
20 #include <linux/irq.h>
21 #include <linux/spinlock.h>
22 #include <linux/slab.h>
23 #include <linux/timer.h>
24 #include <linux/if_ether.h>
25 #include <linux/inetdevice.h>
26 #include <linux/init.h>
27 #include <linux/if_tun.h>
28 #include <linux/etherdevice.h>
29 #include <linux/interrupt.h>
30 #include <linux/ioctl.h>
31 #include <linux/bootmem.h>
32 #include <linux/ethtool.h>
33 #include <linux/rtnetlink.h>
34 #include <linux/platform_device.h>
36 #include <platform/simcall.h>
38 #define DRIVER_NAME "iss-netdev"
39 #define ETH_MAX_PACKET 1500
40 #define ETH_HEADER_OTHER 14
41 #define ISS_NET_TIMER_VALUE (2 * HZ)
44 static DEFINE_SPINLOCK(opened_lock);
45 static LIST_HEAD(opened);
47 static DEFINE_SPINLOCK(devices_lock);
48 static LIST_HEAD(devices);
50 /* ------------------------------------------------------------------------- */
52 /* We currently only support the TUNTAP transport protocol. */
54 #define TRANSPORT_TUNTAP_NAME "tuntap"
55 #define TRANSPORT_TUNTAP_MTU ETH_MAX_PACKET
57 struct tuntap_info {
58 char dev_name[IFNAMSIZ];
59 int fixed_config;
60 unsigned char gw[ETH_ALEN];
61 int fd;
64 /* ------------------------------------------------------------------------- */
67 /* This structure contains out private information for the driver. */
69 struct iss_net_private {
71 struct list_head device_list;
72 struct list_head opened_list;
74 spinlock_t lock;
75 struct net_device *dev;
76 struct platform_device pdev;
77 struct timer_list tl;
78 struct net_device_stats stats;
80 struct timer_list timer;
81 unsigned int timer_val;
83 int index;
84 int mtu;
86 unsigned char mac[ETH_ALEN];
87 int have_mac;
89 struct {
90 union {
91 struct tuntap_info tuntap;
92 } info;
94 int (*open)(struct iss_net_private *lp);
95 void (*close)(struct iss_net_private *lp);
96 int (*read)(struct iss_net_private *lp, struct sk_buff **skb);
97 int (*write)(struct iss_net_private *lp, struct sk_buff **skb);
98 unsigned short (*protocol)(struct sk_buff *skb);
99 int (*poll)(struct iss_net_private *lp);
100 } tp;
104 /* ======================= ISS SIMCALL INTERFACE =========================== */
106 /* Note: __simc must _not_ be declared inline! */
108 static int errno;
110 static int __simc (int a, int b, int c, int d, int e, int f) __attribute__((__noinline__));
111 static int __simc (int a, int b, int c, int d, int e, int f)
113 int ret;
114 __asm__ __volatile__ ("simcall\n"
115 "mov %0, a2\n"
116 "mov %1, a3\n" : "=a" (ret), "=a" (errno)
117 : : "a2", "a3");
118 return ret;
121 static int inline simc_open(char *file, int flags, int mode)
123 return __simc(SYS_open, (int) file, flags, mode, 0, 0);
126 static int inline simc_close(int fd)
128 return __simc(SYS_close, fd, 0, 0, 0, 0);
131 static int inline simc_ioctl(int fd, int request, void *arg)
133 return __simc(SYS_ioctl, fd, request, (int) arg, 0, 0);
136 static int inline simc_read(int fd, void *buf, size_t count)
138 return __simc(SYS_read, fd, (int) buf, count, 0, 0);
141 static int inline simc_write(int fd, void *buf, size_t count)
143 return __simc(SYS_write, fd, (int) buf, count, 0, 0);
146 static int inline simc_poll(int fd)
148 struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
150 return __simc(SYS_select_one, fd, XTISS_SELECT_ONE_READ, (int)&tv,0,0);
153 /* ================================ HELPERS ================================ */
156 static char *split_if_spec(char *str, ...)
158 char **arg, *end;
159 va_list ap;
161 va_start(ap, str);
162 while ((arg = va_arg(ap, char**)) != NULL) {
163 if (*str == '\0')
164 return NULL;
165 end = strchr(str, ',');
166 if (end != str)
167 *arg = str;
168 if (end == NULL)
169 return NULL;
170 *end ++ = '\0';
171 str = end;
173 va_end(ap);
174 return str;
179 /* Return the IP address as a string for a given device. */
181 static void dev_ip_addr(void *d, char *buf, char *bin_buf)
183 struct net_device *dev = d;
184 struct in_device *ip = dev->ip_ptr;
185 struct in_ifaddr *in;
186 __be32 addr;
188 if ((ip == NULL) || ((in = ip->ifa_list) == NULL)) {
189 printk(KERN_WARNING "Device not assigned an IP address!\n");
190 return;
193 addr = in->ifa_address;
194 sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
195 (addr >> 16) & 0xff, addr >> 24);
197 if (bin_buf) {
198 bin_buf[0] = addr & 0xff;
199 bin_buf[1] = (addr >> 8) & 0xff;
200 bin_buf[2] = (addr >> 16) & 0xff;
201 bin_buf[3] = addr >> 24;
205 /* Set Ethernet address of the specified device. */
207 static void inline set_ether_mac(void *d, unsigned char *addr)
209 struct net_device *dev = d;
210 memcpy(dev->dev_addr, addr, ETH_ALEN);
214 /* ======================= TUNTAP TRANSPORT INTERFACE ====================== */
216 static int tuntap_open(struct iss_net_private *lp)
218 struct ifreq ifr;
219 char *dev_name = lp->tp.info.tuntap.dev_name;
220 int err = -EINVAL;
221 int fd;
223 /* We currently only support a fixed configuration. */
225 if (!lp->tp.info.tuntap.fixed_config)
226 return -EINVAL;
228 if ((fd = simc_open("/dev/net/tun", 02, 0)) < 0) { /* O_RDWR */
229 printk("Failed to open /dev/net/tun, returned %d "
230 "(errno = %d)\n", fd, errno);
231 return fd;
234 memset(&ifr, 0, sizeof ifr);
235 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
236 strlcpy(ifr.ifr_name, dev_name, sizeof ifr.ifr_name);
238 if ((err = simc_ioctl(fd, TUNSETIFF, (void*) &ifr)) < 0) {
239 printk("Failed to set interface, returned %d "
240 "(errno = %d)\n", err, errno);
241 simc_close(fd);
242 return err;
245 lp->tp.info.tuntap.fd = fd;
246 return err;
249 static void tuntap_close(struct iss_net_private *lp)
251 simc_close(lp->tp.info.tuntap.fd);
252 lp->tp.info.tuntap.fd = -1;
255 static int tuntap_read (struct iss_net_private *lp, struct sk_buff **skb)
258 return simc_read(lp->tp.info.tuntap.fd,
259 (*skb)->data, (*skb)->dev->mtu + ETH_HEADER_OTHER);
262 static int tuntap_write (struct iss_net_private *lp, struct sk_buff **skb)
264 return simc_write(lp->tp.info.tuntap.fd, (*skb)->data, (*skb)->len);
267 unsigned short tuntap_protocol(struct sk_buff *skb)
269 return eth_type_trans(skb, skb->dev);
272 static int tuntap_poll(struct iss_net_private *lp)
274 return simc_poll(lp->tp.info.tuntap.fd);
278 * Currently only a device name is supported.
279 * ethX=tuntap[,[mac address][,[device name]]]
282 static int tuntap_probe(struct iss_net_private *lp, int index, char *init)
284 const int len = strlen(TRANSPORT_TUNTAP_NAME);
285 char *dev_name = NULL, *mac_str = NULL, *rem = NULL;
287 /* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */
289 if (strncmp(init, TRANSPORT_TUNTAP_NAME, len))
290 return 0;
292 if (*(init += strlen(TRANSPORT_TUNTAP_NAME)) == ',') {
293 if ((rem=split_if_spec(init+1, &mac_str, &dev_name)) != NULL) {
294 printk("Extra garbage on specification : '%s'\n", rem);
295 return 0;
297 } else if (*init != '\0') {
298 printk("Invalid argument: %s. Skipping device!\n", init);
299 return 0;
302 if (dev_name) {
303 strncpy(lp->tp.info.tuntap.dev_name, dev_name,
304 sizeof lp->tp.info.tuntap.dev_name);
305 lp->tp.info.tuntap.fixed_config = 1;
306 } else
307 strcpy(lp->tp.info.tuntap.dev_name, TRANSPORT_TUNTAP_NAME);
310 lp->mtu = TRANSPORT_TUNTAP_MTU;
312 //lp->info.tuntap.gate_addr = gate_addr;
314 lp->tp.info.tuntap.fd = -1;
316 lp->tp.open = tuntap_open;
317 lp->tp.close = tuntap_close;
318 lp->tp.read = tuntap_read;
319 lp->tp.write = tuntap_write;
320 lp->tp.protocol = tuntap_protocol;
321 lp->tp.poll = tuntap_poll;
323 printk("TUN/TAP backend - ");
324 printk("\n");
326 return 1;
329 /* ================================ ISS NET ================================ */
331 static int iss_net_rx(struct net_device *dev)
333 struct iss_net_private *lp = netdev_priv(dev);
334 int pkt_len;
335 struct sk_buff *skb;
337 /* Check if there is any new data. */
339 if (lp->tp.poll(lp) == 0)
340 return 0;
342 /* Try to allocate memory, if it fails, try again next round. */
344 if ((skb = dev_alloc_skb(dev->mtu + 2 + ETH_HEADER_OTHER)) == NULL) {
345 lp->stats.rx_dropped++;
346 return 0;
349 skb_reserve(skb, 2);
351 /* Setup skb */
353 skb->dev = dev;
354 skb_reset_mac_header(skb);
355 pkt_len = lp->tp.read(lp, &skb);
356 skb_put(skb, pkt_len);
358 if (pkt_len > 0) {
359 skb_trim(skb, pkt_len);
360 skb->protocol = lp->tp.protocol(skb);
362 lp->stats.rx_bytes += skb->len;
363 lp->stats.rx_packets++;
364 // netif_rx(skb);
365 netif_rx_ni(skb);
366 return pkt_len;
368 kfree_skb(skb);
369 return pkt_len;
372 static int iss_net_poll(void)
374 struct list_head *ele;
375 int err, ret = 0;
377 spin_lock(&opened_lock);
379 list_for_each(ele, &opened) {
380 struct iss_net_private *lp;
382 lp = list_entry(ele, struct iss_net_private, opened_list);
384 if (!netif_running(lp->dev))
385 break;
387 spin_lock(&lp->lock);
389 while ((err = iss_net_rx(lp->dev)) > 0)
390 ret++;
392 spin_unlock(&lp->lock);
394 if (err < 0) {
395 printk(KERN_ERR "Device '%s' read returned %d, "
396 "shutting it down\n", lp->dev->name, err);
397 dev_close(lp->dev);
398 } else {
402 spin_unlock(&opened_lock);
403 return ret;
407 static void iss_net_timer(unsigned long priv)
409 struct iss_net_private* lp = (struct iss_net_private*) priv;
411 spin_lock(&lp->lock);
413 iss_net_poll();
415 mod_timer(&lp->timer, jiffies + lp->timer_val);
417 spin_unlock(&lp->lock);
421 static int iss_net_open(struct net_device *dev)
423 struct iss_net_private *lp = netdev_priv(dev);
424 char addr[sizeof "255.255.255.255\0"];
425 int err;
427 spin_lock(&lp->lock);
429 if ((err = lp->tp.open(lp)) < 0)
430 goto out;
432 if (!lp->have_mac) {
433 dev_ip_addr(dev, addr, &lp->mac[2]);
434 set_ether_mac(dev, lp->mac);
437 netif_start_queue(dev);
439 /* clear buffer - it can happen that the host side of the interface
440 * is full when we get here. In this case, new data is never queued,
441 * SIGIOs never arrive, and the net never works.
443 while ((err = iss_net_rx(dev)) > 0)
446 spin_lock(&opened_lock);
447 list_add(&lp->opened_list, &opened);
448 spin_unlock(&opened_lock);
450 init_timer(&lp->timer);
451 lp->timer_val = ISS_NET_TIMER_VALUE;
452 lp->timer.data = (unsigned long) lp;
453 lp->timer.function = iss_net_timer;
454 mod_timer(&lp->timer, jiffies + lp->timer_val);
456 out:
457 spin_unlock(&lp->lock);
458 return err;
461 static int iss_net_close(struct net_device *dev)
463 struct iss_net_private *lp = netdev_priv(dev);
464 printk("iss_net_close!\n");
465 netif_stop_queue(dev);
466 spin_lock(&lp->lock);
468 spin_lock(&opened_lock);
469 list_del(&opened);
470 spin_unlock(&opened_lock);
472 del_timer_sync(&lp->timer);
474 lp->tp.close(lp);
476 spin_unlock(&lp->lock);
477 return 0;
480 static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
482 struct iss_net_private *lp = netdev_priv(dev);
483 unsigned long flags;
484 int len;
486 netif_stop_queue(dev);
487 spin_lock_irqsave(&lp->lock, flags);
489 len = lp->tp.write(lp, &skb);
491 if (len == skb->len) {
492 lp->stats.tx_packets++;
493 lp->stats.tx_bytes += skb->len;
494 dev->trans_start = jiffies;
495 netif_start_queue(dev);
497 /* this is normally done in the interrupt when tx finishes */
498 netif_wake_queue(dev);
500 } else if (len == 0) {
501 netif_start_queue(dev);
502 lp->stats.tx_dropped++;
504 } else {
505 netif_start_queue(dev);
506 printk(KERN_ERR "iss_net_start_xmit: failed(%d)\n", len);
509 spin_unlock_irqrestore(&lp->lock, flags);
511 dev_kfree_skb(skb);
512 return NETDEV_TX_OK;
516 static struct net_device_stats *iss_net_get_stats(struct net_device *dev)
518 struct iss_net_private *lp = netdev_priv(dev);
519 return &lp->stats;
522 static void iss_net_set_multicast_list(struct net_device *dev)
526 static void iss_net_tx_timeout(struct net_device *dev)
530 static int iss_net_set_mac(struct net_device *dev, void *addr)
533 return 0;
536 static int iss_net_change_mtu(struct net_device *dev, int new_mtu)
538 return -EINVAL;
541 void iss_net_user_timer_expire(unsigned long _conn)
546 static struct platform_driver iss_net_driver = {
547 .driver = {
548 .name = DRIVER_NAME,
552 static int driver_registered;
554 static const struct net_device_ops iss_netdev_ops = {
555 .ndo_open = iss_net_open,
556 .ndo_stop = iss_net_close,
557 .ndo_get_stats = iss_net_get_stats,
558 .ndo_start_xmit = iss_net_start_xmit,
559 .ndo_validate_addr = eth_validate_addr,
560 .ndo_change_mtu = iss_net_change_mtu,
561 .ndo_set_mac_address = iss_net_set_mac,
562 //.ndo_do_ioctl = iss_net_ioctl,
563 .ndo_tx_timeout = iss_net_tx_timeout,
564 .ndo_set_multicast_list = iss_net_set_multicast_list,
567 static int iss_net_configure(int index, char *init)
569 struct net_device *dev;
570 struct iss_net_private *lp;
571 int err;
573 if ((dev = alloc_etherdev(sizeof *lp)) == NULL) {
574 printk(KERN_ERR "eth_configure: failed to allocate device\n");
575 return 1;
578 /* Initialize private element. */
580 lp = netdev_priv(dev);
581 *lp = ((struct iss_net_private) {
582 .device_list = LIST_HEAD_INIT(lp->device_list),
583 .opened_list = LIST_HEAD_INIT(lp->opened_list),
584 .lock = __SPIN_LOCK_UNLOCKED(lp.lock),
585 .dev = dev,
586 .index = index,
587 //.fd = -1,
588 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0 },
589 .have_mac = 0,
593 * Try all transport protocols.
594 * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
597 if (!tuntap_probe(lp, index, init)) {
598 printk("Invalid arguments. Skipping device!\n");
599 goto errout;
602 printk(KERN_INFO "Netdevice %d ", index);
603 if (lp->have_mac)
604 printk("(%pM) ", lp->mac);
605 printk(": ");
607 /* sysfs register */
609 if (!driver_registered) {
610 platform_driver_register(&iss_net_driver);
611 driver_registered = 1;
614 spin_lock(&devices_lock);
615 list_add(&lp->device_list, &devices);
616 spin_unlock(&devices_lock);
618 lp->pdev.id = index;
619 lp->pdev.name = DRIVER_NAME;
620 platform_device_register(&lp->pdev);
621 SET_NETDEV_DEV(dev,&lp->pdev.dev);
624 * If this name ends up conflicting with an existing registered
625 * netdevice, that is OK, register_netdev{,ice}() will notice this
626 * and fail.
628 snprintf(dev->name, sizeof dev->name, "eth%d", index);
630 dev->netdev_ops = &iss_netdev_ops;
631 dev->mtu = lp->mtu;
632 dev->watchdog_timeo = (HZ >> 1);
633 dev->irq = -1;
635 rtnl_lock();
636 err = register_netdevice(dev);
637 rtnl_unlock();
639 if (err) {
640 printk("Error registering net device!\n");
641 free_netdev(dev);
642 return 1;
645 init_timer(&lp->tl);
646 lp->tl.function = iss_net_user_timer_expire;
648 return 0;
650 errout:
651 return -EIO;
655 /* ------------------------------------------------------------------------- */
657 /* Filled in during early boot */
659 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
661 struct iss_net_init {
662 struct list_head list;
663 char *init; /* init string */
664 int index;
668 * Parse the command line and look for 'ethX=...' fields, and register all
669 * those fields. They will be later initialized in iss_net_init.
672 #define ERR KERN_ERR "iss_net_setup: "
674 static int iss_net_setup(char *str)
676 struct iss_net_private *device = NULL;
677 struct iss_net_init *new;
678 struct list_head *ele;
679 char *end;
680 int n;
682 n = simple_strtoul(str, &end, 0);
683 if (end == str) {
684 printk(ERR "Failed to parse '%s'\n", str);
685 return 1;
687 if (n < 0) {
688 printk(ERR "Device %d is negative\n", n);
689 return 1;
691 if (*(str = end) != '=') {
692 printk(ERR "Expected '=' after device number\n");
693 return 1;
696 spin_lock(&devices_lock);
698 list_for_each(ele, &devices) {
699 device = list_entry(ele, struct iss_net_private, device_list);
700 if (device->index == n)
701 break;
704 spin_unlock(&devices_lock);
706 if (device && device->index == n) {
707 printk(ERR "Device %d already configured\n", n);
708 return 1;
711 if ((new = alloc_bootmem(sizeof new)) == NULL) {
712 printk("Alloc_bootmem failed\n");
713 return 1;
716 INIT_LIST_HEAD(&new->list);
717 new->index = n;
718 new->init = str + 1;
720 list_add_tail(&new->list, &eth_cmd_line);
721 return 1;
724 #undef ERR
726 __setup("eth=", iss_net_setup);
729 * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
732 static int iss_net_init(void)
734 struct list_head *ele, *next;
736 /* Walk through all Ethernet devices specified in the command line. */
738 list_for_each_safe(ele, next, &eth_cmd_line) {
739 struct iss_net_init *eth;
740 eth = list_entry(ele, struct iss_net_init, list);
741 iss_net_configure(eth->index, eth->init);
744 return 1;
747 module_init(iss_net_init);