[MIPS] Always pass -msoft-float.
[linux-2.6/openmoko-kernel/knife-kernel.git] / arch / xtensa / platform-iss / network.c
blob0dc55cc8691bf1cd3917daa10ec38d1f38109a56
1 /*
3 * arch/xtensa/platform-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/config.h>
20 #include <linux/list.h>
21 #include <linux/irq.h>
22 #include <linux/spinlock.h>
23 #include <linux/slab.h>
24 #include <linux/timer.h>
25 #include <linux/if_ether.h>
26 #include <linux/inetdevice.h>
27 #include <linux/init.h>
28 #include <linux/if_tun.h>
29 #include <linux/etherdevice.h>
30 #include <linux/interrupt.h>
31 #include <linux/ioctl.h>
32 #include <linux/bootmem.h>
33 #include <linux/ethtool.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/timer.h>
36 #include <linux/platform_device.h>
38 #include <xtensa/simcall.h>
40 #define DRIVER_NAME "iss-netdev"
41 #define ETH_MAX_PACKET 1500
42 #define ETH_HEADER_OTHER 14
43 #define ISS_NET_TIMER_VALUE (2 * HZ)
46 static DEFINE_SPINLOCK(opened_lock);
47 static LIST_HEAD(opened);
49 static DEFINE_SPINLOCK(devices_lock);
50 static LIST_HEAD(devices);
52 /* ------------------------------------------------------------------------- */
54 /* We currently only support the TUNTAP transport protocol. */
56 #define TRANSPORT_TUNTAP_NAME "tuntap"
57 #define TRANSPORT_TUNTAP_MTU ETH_MAX_PACKET
59 struct tuntap_info {
60 char dev_name[IFNAMSIZ];
61 int fixed_config;
62 unsigned char gw[ETH_ALEN];
63 int fd;
66 /* ------------------------------------------------------------------------- */
69 /* This structure contains out private information for the driver. */
71 struct iss_net_private {
73 struct list_head device_list;
74 struct list_head opened_list;
76 spinlock_t lock;
77 struct net_device *dev;
78 struct platform_device pdev;
79 struct timer_list tl;
80 struct net_device_stats stats;
82 struct timer_list timer;
83 unsigned int timer_val;
85 int index;
86 int mtu;
88 unsigned char mac[ETH_ALEN];
89 int have_mac;
91 struct {
92 union {
93 struct tuntap_info tuntap;
94 } info;
96 int (*open)(struct iss_net_private *lp);
97 void (*close)(struct iss_net_private *lp);
98 int (*read)(struct iss_net_private *lp, struct sk_buff **skb);
99 int (*write)(struct iss_net_private *lp, struct sk_buff **skb);
100 unsigned short (*protocol)(struct sk_buff *skb);
101 int (*poll)(struct iss_net_private *lp);
102 } tp;
106 /* ======================= ISS SIMCALL INTERFACE =========================== */
108 /* Note: __simc must _not_ be declared inline! */
110 static int errno;
112 static int __simc (int a, int b, int c, int d, int e, int f)
114 int ret;
115 __asm__ __volatile__ ("simcall\n"
116 "mov %0, a2\n"
117 "mov %1, a3\n" : "=a" (ret), "=a" (errno)
118 : : "a2", "a3");
119 return ret;
122 static int inline simc_open(char *file, int flags, int mode)
124 return __simc(SYS_open, (int) file, flags, mode, 0, 0);
127 static int inline simc_close(int fd)
129 return __simc(SYS_close, fd, 0, 0, 0, 0);
132 static int inline simc_ioctl(int fd, int request, void *arg)
134 return __simc(SYS_ioctl, fd, request, (int) arg, 0, 0);
137 static int inline simc_read(int fd, void *buf, size_t count)
139 return __simc(SYS_read, fd, (int) buf, count, 0, 0);
142 static int inline simc_write(int fd, void *buf, size_t count)
144 return __simc(SYS_write, fd, (int) buf, count, 0, 0);
147 static int inline simc_poll(int fd)
149 struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
151 return __simc(SYS_select_one, fd, XTISS_SELECT_ONE_READ, (int)&tv,0,0);
154 /* ================================ HELPERS ================================ */
157 static char *split_if_spec(char *str, ...)
159 char **arg, *end;
160 va_list ap;
162 va_start(ap, str);
163 while ((arg = va_arg(ap, char**)) != NULL) {
164 if (*str == '\0')
165 return NULL;
166 end = strchr(str, ',');
167 if (end != str)
168 *arg = str;
169 if (end == NULL)
170 return NULL;
171 *end ++ = '\0';
172 str = end;
174 va_end(ap);
175 return str;
179 #if 0
180 /* Adjust SKB. */
182 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
184 if ((skb != NULL) && (skb_tailroom(skb) < extra)) {
185 struct sk_buff *skb2;
187 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
188 dev_kfree_skb(skb);
189 skb = skb2;
191 if (skb != NULL)
192 skb_put(skb, extra);
194 return skb;
196 #endif
198 /* Return the IP address as a string for a given device. */
200 static void dev_ip_addr(void *d, char *buf, char *bin_buf)
202 struct net_device *dev = d;
203 struct in_device *ip = dev->ip_ptr;
204 struct in_ifaddr *in;
205 u32 addr;
207 if ((ip == NULL) || ((in = ip->ifa_list) == NULL)) {
208 printk(KERN_WARNING "Device not assigned an IP address!\n");
209 return;
212 addr = in->ifa_address;
213 sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
214 (addr >> 16) & 0xff, addr >> 24);
216 if (bin_buf) {
217 bin_buf[0] = addr & 0xff;
218 bin_buf[1] = (addr >> 8) & 0xff;
219 bin_buf[2] = (addr >> 16) & 0xff;
220 bin_buf[3] = addr >> 24;
224 /* Set Ethernet address of the specified device. */
226 static void inline set_ether_mac(void *d, unsigned char *addr)
228 struct net_device *dev = d;
229 memcpy(dev->dev_addr, addr, ETH_ALEN);
233 /* ======================= TUNTAP TRANSPORT INTERFACE ====================== */
235 static int tuntap_open(struct iss_net_private *lp)
237 struct ifreq ifr;
238 char *dev_name = lp->tp.info.tuntap.dev_name;
239 int err = -EINVAL;
240 int fd;
242 /* We currently only support a fixed configuration. */
244 if (!lp->tp.info.tuntap.fixed_config)
245 return -EINVAL;
247 if ((fd = simc_open("/dev/net/tun", 02, 0)) < 0) { /* O_RDWR */
248 printk("Failed to open /dev/net/tun, returned %d "
249 "(errno = %d)\n", fd, errno);
250 return fd;
253 memset(&ifr, 0, sizeof ifr);
254 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
255 strlcpy(ifr.ifr_name, dev_name, sizeof ifr.ifr_name - 1);
257 if ((err = simc_ioctl(fd, TUNSETIFF, (void*) &ifr)) < 0) {
258 printk("Failed to set interface, returned %d "
259 "(errno = %d)\n", err, errno);
260 simc_close(fd);
261 return err;
264 lp->tp.info.tuntap.fd = fd;
265 return err;
268 static void tuntap_close(struct iss_net_private *lp)
270 #if 0
271 if (lp->tp.info.tuntap.fixed_config)
272 iter_addresses(lp->tp.info.tuntap.dev, close_addr, lp->host.dev_name);
273 #endif
274 simc_close(lp->tp.info.tuntap.fd);
275 lp->tp.info.tuntap.fd = -1;
278 static int tuntap_read (struct iss_net_private *lp, struct sk_buff **skb)
280 #if 0
281 *skb = ether_adjust_skb(*skb, ETH_HEADER_OTHER);
282 if (*skb == NULL)
283 return -ENOMEM;
284 #endif
286 return simc_read(lp->tp.info.tuntap.fd,
287 (*skb)->data, (*skb)->dev->mtu + ETH_HEADER_OTHER);
290 static int tuntap_write (struct iss_net_private *lp, struct sk_buff **skb)
292 return simc_write(lp->tp.info.tuntap.fd, (*skb)->data, (*skb)->len);
295 unsigned short tuntap_protocol(struct sk_buff *skb)
297 return eth_type_trans(skb, skb->dev);
300 static int tuntap_poll(struct iss_net_private *lp)
302 return simc_poll(lp->tp.info.tuntap.fd);
306 * Currently only a device name is supported.
307 * ethX=tuntap[,[mac address][,[device name]]]
310 static int tuntap_probe(struct iss_net_private *lp, int index, char *init)
312 const int len = strlen(TRANSPORT_TUNTAP_NAME);
313 char *dev_name = NULL, *mac_str = NULL, *rem = NULL;
315 /* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */
317 if (strncmp(init, TRANSPORT_TUNTAP_NAME, len))
318 return 0;
320 if (*(init += strlen(TRANSPORT_TUNTAP_NAME)) == ',') {
321 if ((rem=split_if_spec(init+1, &mac_str, &dev_name)) != NULL) {
322 printk("Extra garbage on specification : '%s'\n", rem);
323 return 0;
325 } else if (*init != '\0') {
326 printk("Invalid argument: %s. Skipping device!\n", init);
327 return 0;
330 if (dev_name) {
331 strncpy(lp->tp.info.tuntap.dev_name, dev_name,
332 sizeof lp->tp.info.tuntap.dev_name);
333 lp->tp.info.tuntap.fixed_config = 1;
334 } else
335 strcpy(lp->tp.info.tuntap.dev_name, TRANSPORT_TUNTAP_NAME);
338 #if 0
339 if (setup_etheraddr(mac_str, lp->mac))
340 lp->have_mac = 1;
341 #endif
342 lp->mtu = TRANSPORT_TUNTAP_MTU;
344 //lp->info.tuntap.gate_addr = gate_addr;
346 lp->tp.info.tuntap.fd = -1;
348 lp->tp.open = tuntap_open;
349 lp->tp.close = tuntap_close;
350 lp->tp.read = tuntap_read;
351 lp->tp.write = tuntap_write;
352 lp->tp.protocol = tuntap_protocol;
353 lp->tp.poll = tuntap_poll;
355 printk("TUN/TAP backend - ");
356 #if 0
357 if (lp->host.gate_addr != NULL)
358 printk("IP = %s", lp->host.gate_addr);
359 #endif
360 printk("\n");
362 return 1;
365 /* ================================ ISS NET ================================ */
367 static int iss_net_rx(struct net_device *dev)
369 struct iss_net_private *lp = dev->priv;
370 int pkt_len;
371 struct sk_buff *skb;
373 /* Check if there is any new data. */
375 if (lp->tp.poll(lp) == 0)
376 return 0;
378 /* Try to allocate memory, if it fails, try again next round. */
380 if ((skb = dev_alloc_skb(dev->mtu + 2 + ETH_HEADER_OTHER)) == NULL) {
381 lp->stats.rx_dropped++;
382 return 0;
385 skb_reserve(skb, 2);
387 /* Setup skb */
389 skb->dev = dev;
390 skb->mac.raw = skb->data;
391 pkt_len = lp->tp.read(lp, &skb);
392 skb_put(skb, pkt_len);
394 if (pkt_len > 0) {
395 skb_trim(skb, pkt_len);
396 skb->protocol = lp->tp.protocol(skb);
397 // netif_rx(skb);
398 netif_rx_ni(skb);
400 lp->stats.rx_bytes += skb->len;
401 lp->stats.rx_packets++;
402 return pkt_len;
404 kfree_skb(skb);
405 return pkt_len;
408 static int iss_net_poll(void)
410 struct list_head *ele;
411 int err, ret = 0;
413 spin_lock(&opened_lock);
415 list_for_each(ele, &opened) {
416 struct iss_net_private *lp;
418 lp = list_entry(ele, struct iss_net_private, opened_list);
420 if (!netif_running(lp->dev))
421 break;
423 spin_lock(&lp->lock);
425 while ((err = iss_net_rx(lp->dev)) > 0)
426 ret++;
428 spin_unlock(&lp->lock);
430 if (err < 0) {
431 printk(KERN_ERR "Device '%s' read returned %d, "
432 "shutting it down\n", lp->dev->name, err);
433 dev_close(lp->dev);
434 } else {
435 // FIXME reactivate_fd(lp->fd, ISS_ETH_IRQ);
439 spin_unlock(&opened_lock);
440 return ret;
444 static void iss_net_timer(unsigned long priv)
446 struct iss_net_private* lp = (struct iss_net_private*) priv;
448 spin_lock(&lp->lock);
450 iss_net_poll();
452 mod_timer(&lp->timer, jiffies + lp->timer_val);
454 spin_unlock(&lp->lock);
458 static int iss_net_open(struct net_device *dev)
460 struct iss_net_private *lp = dev->priv;
461 char addr[sizeof "255.255.255.255\0"];
462 int err;
464 spin_lock(&lp->lock);
466 if ((err = lp->tp.open(lp)) < 0)
467 goto out;
469 if (!lp->have_mac) {
470 dev_ip_addr(dev, addr, &lp->mac[2]);
471 set_ether_mac(dev, lp->mac);
474 netif_start_queue(dev);
476 /* clear buffer - it can happen that the host side of the interface
477 * is full when we gethere. In this case, new data is never queued,
478 * SIGIOs never arrive, and the net never works.
480 while ((err = iss_net_rx(dev)) > 0)
483 spin_lock(&opened_lock);
484 list_add(&lp->opened_list, &opened);
485 spin_unlock(&opened_lock);
487 init_timer(&lp->timer);
488 lp->timer_val = ISS_NET_TIMER_VALUE;
489 lp->timer.data = (unsigned long) lp;
490 lp->timer.function = iss_net_timer;
491 mod_timer(&lp->timer, jiffies + lp->timer_val);
493 out:
494 spin_unlock(&lp->lock);
495 return err;
498 static int iss_net_close(struct net_device *dev)
500 struct iss_net_private *lp = dev->priv;
501 printk("iss_net_close!\n");
502 netif_stop_queue(dev);
503 spin_lock(&lp->lock);
505 spin_lock(&opened_lock);
506 list_del(&opened);
507 spin_unlock(&opened_lock);
509 del_timer_sync(&lp->timer);
511 lp->tp.close(lp);
513 spin_unlock(&lp->lock);
514 return 0;
517 static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
519 struct iss_net_private *lp = dev->priv;
520 unsigned long flags;
521 int len;
523 netif_stop_queue(dev);
524 spin_lock_irqsave(&lp->lock, flags);
526 len = lp->tp.write(lp, &skb);
528 if (len == skb->len) {
529 lp->stats.tx_packets++;
530 lp->stats.tx_bytes += skb->len;
531 dev->trans_start = jiffies;
532 netif_start_queue(dev);
534 /* this is normally done in the interrupt when tx finishes */
535 netif_wake_queue(dev);
537 } else if (len == 0) {
538 netif_start_queue(dev);
539 lp->stats.tx_dropped++;
541 } else {
542 netif_start_queue(dev);
543 printk(KERN_ERR "iss_net_start_xmit: failed(%d)\n", len);
546 spin_unlock_irqrestore(&lp->lock, flags);
548 dev_kfree_skb(skb);
549 return 0;
553 static struct net_device_stats *iss_net_get_stats(struct net_device *dev)
555 struct iss_net_private *lp = dev->priv;
556 return &lp->stats;
559 static void iss_net_set_multicast_list(struct net_device *dev)
561 #if 0
562 if (dev->flags & IFF_PROMISC)
563 return;
564 else if (dev->mc_count)
565 dev->flags |= IFF_ALLMULTI;
566 else
567 dev->flags &= ~IFF_ALLMULTI;
568 #endif
571 static void iss_net_tx_timeout(struct net_device *dev)
573 #if 0
574 dev->trans_start = jiffies;
575 netif_wake_queue(dev);
576 #endif
579 static int iss_net_set_mac(struct net_device *dev, void *addr)
581 #if 0
582 struct iss_net_private *lp = dev->priv;
583 struct sockaddr *hwaddr = addr;
585 spin_lock(&lp->lock);
586 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
587 spin_unlock(&lp->lock);
588 #endif
590 return 0;
593 static int iss_net_change_mtu(struct net_device *dev, int new_mtu)
595 #if 0
596 struct iss_net_private *lp = dev->priv;
597 int err = 0;
599 spin_lock(&lp->lock);
601 // FIXME not needed new_mtu = transport_set_mtu(new_mtu, &lp->user);
603 if (new_mtu < 0)
604 err = new_mtu;
605 else
606 dev->mtu = new_mtu;
608 spin_unlock(&lp->lock);
609 return err;
610 #endif
611 return -EINVAL;
614 void iss_net_user_timer_expire(unsigned long _conn)
619 static struct platform_driver iss_net_driver = {
620 .driver = {
621 .name = DRIVER_NAME,
625 static int driver_registered;
627 static int iss_net_configure(int index, char *init)
629 struct net_device *dev;
630 struct iss_net_private *lp;
631 int err;
633 if ((dev = alloc_etherdev(sizeof *lp)) == NULL) {
634 printk(KERN_ERR "eth_configure: failed to allocate device\n");
635 return 1;
638 /* Initialize private element. */
640 lp = dev->priv;
641 *lp = ((struct iss_net_private) {
642 .device_list = LIST_HEAD_INIT(lp->device_list),
643 .opened_list = LIST_HEAD_INIT(lp->opened_list),
644 .lock = SPIN_LOCK_UNLOCKED,
645 .dev = dev,
646 .index = index,
647 //.fd = -1,
648 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0 },
649 .have_mac = 0,
653 * Try all transport protocols.
654 * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
657 if (!tuntap_probe(lp, index, init)) {
658 printk("Invalid arguments. Skipping device!\n");
659 goto errout;
662 printk(KERN_INFO "Netdevice %d ", index);
663 if (lp->have_mac)
664 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
665 lp->mac[0], lp->mac[1],
666 lp->mac[2], lp->mac[3],
667 lp->mac[4], lp->mac[5]);
668 printk(": ");
670 /* sysfs register */
672 if (!driver_registered) {
673 platform_driver_register(&iss_net_driver);
674 driver_registered = 1;
677 spin_lock(&devices_lock);
678 list_add(&lp->device_list, &devices);
679 spin_unlock(&devices_lock);
681 lp->pdev.id = index;
682 lp->pdev.name = DRIVER_NAME;
683 platform_device_register(&lp->pdev);
684 SET_NETDEV_DEV(dev,&lp->pdev.dev);
687 * If this name ends up conflicting with an existing registered
688 * netdevice, that is OK, register_netdev{,ice}() will notice this
689 * and fail.
691 snprintf(dev->name, sizeof dev->name, "eth%d", index);
693 dev->mtu = lp->mtu;
694 dev->open = iss_net_open;
695 dev->hard_start_xmit = iss_net_start_xmit;
696 dev->stop = iss_net_close;
697 dev->get_stats = iss_net_get_stats;
698 dev->set_multicast_list = iss_net_set_multicast_list;
699 dev->tx_timeout = iss_net_tx_timeout;
700 dev->set_mac_address = iss_net_set_mac;
701 dev->change_mtu = iss_net_change_mtu;
702 dev->watchdog_timeo = (HZ >> 1);
703 dev->irq = -1;
705 rtnl_lock();
706 err = register_netdevice(dev);
707 rtnl_unlock();
709 if (err) {
710 printk("Error registering net device!\n");
711 /* XXX: should we call ->remove() here? */
712 free_netdev(dev);
713 return 1;
716 init_timer(&lp->tl);
717 lp->tl.function = iss_net_user_timer_expire;
719 #if 0
720 if (lp->have_mac)
721 set_ether_mac(dev, lp->mac);
722 #endif
723 return 0;
725 errout:
726 // FIXME: unregister; free, etc..
727 return -EIO;
731 /* ------------------------------------------------------------------------- */
733 /* Filled in during early boot */
735 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
737 struct iss_net_init {
738 struct list_head list;
739 char *init; /* init string */
740 int index;
744 * Parse the command line and look for 'ethX=...' fields, and register all
745 * those fields. They will be later initialized in iss_net_init.
748 #define ERR KERN_ERR "iss_net_setup: "
750 static int iss_net_setup(char *str)
752 struct iss_net_private *device = NULL;
753 struct iss_net_init *new;
754 struct list_head *ele;
755 char *end;
756 int n;
758 n = simple_strtoul(str, &end, 0);
759 if (end == str) {
760 printk(ERR "Failed to parse '%s'\n", str);
761 return 1;
763 if (n < 0) {
764 printk(ERR "Device %d is negative\n", n);
765 return 1;
767 if (*(str = end) != '=') {
768 printk(ERR "Expected '=' after device number\n");
769 return 1;
772 spin_lock(&devices_lock);
774 list_for_each(ele, &devices) {
775 device = list_entry(ele, struct iss_net_private, device_list);
776 if (device->index == n)
777 break;
780 spin_unlock(&devices_lock);
782 if (device && device->index == n) {
783 printk(ERR "Device %d already configured\n", n);
784 return 1;
787 if ((new = alloc_bootmem(sizeof new)) == NULL) {
788 printk("Alloc_bootmem failed\n");
789 return 1;
792 INIT_LIST_HEAD(&new->list);
793 new->index = n;
794 new->init = str + 1;
796 list_add_tail(&new->list, &eth_cmd_line);
797 return 1;
800 #undef ERR
802 __setup("eth", iss_net_setup);
805 * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
808 static int iss_net_init(void)
810 struct list_head *ele, *next;
812 /* Walk through all Ethernet devices specified in the command line. */
814 list_for_each_safe(ele, next, &eth_cmd_line) {
815 struct iss_net_init *eth;
816 eth = list_entry(ele, struct iss_net_init, list);
817 iss_net_configure(eth->index, eth->init);
820 return 1;
823 module_init(iss_net_init);