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
60 char dev_name[IFNAMSIZ];
62 unsigned char gw[ETH_ALEN];
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;
77 struct net_device *dev;
78 struct platform_device pdev;
80 struct net_device_stats stats;
82 struct timer_list timer;
83 unsigned int timer_val;
88 unsigned char mac[ETH_ALEN];
93 struct tuntap_info tuntap;
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);
106 /* ======================= ISS SIMCALL INTERFACE =========================== */
108 /* Note: __simc must _not_ be declared inline! */
112 static int __simc (int a, int b, int c, int d, int e, int f)
115 __asm__ __volatile__ ("simcall\n"
117 "mov %1, a3\n" : "=a" (ret), "=a" (errno)
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, ...)
163 while ((arg = va_arg(ap, char**)) != NULL) {
166 end = strchr(str, ',');
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);
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;
207 if ((ip == NULL) || ((in = ip->ifa_list) == NULL)) {
208 printk(KERN_WARNING "Device not assigned an IP address!\n");
212 addr = in->ifa_address;
213 sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
214 (addr >> 16) & 0xff, addr >> 24);
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)
238 char *dev_name = lp->tp.info.tuntap.dev_name;
242 /* We currently only support a fixed configuration. */
244 if (!lp->tp.info.tuntap.fixed_config)
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);
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);
264 lp->tp.info.tuntap.fd = fd;
268 static void tuntap_close(struct iss_net_private *lp)
271 if (lp->tp.info.tuntap.fixed_config)
272 iter_addresses(lp->tp.info.tuntap.dev, close_addr, lp->host.dev_name);
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)
281 *skb = ether_adjust_skb(*skb, ETH_HEADER_OTHER);
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))
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);
325 } else if (*init != '\0') {
326 printk("Invalid argument: %s. Skipping device!\n", init);
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;
335 strcpy(lp->tp.info.tuntap.dev_name, TRANSPORT_TUNTAP_NAME);
339 if (setup_etheraddr(mac_str, lp->mac))
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 - ");
357 if (lp->host.gate_addr != NULL)
358 printk("IP = %s", lp->host.gate_addr);
365 /* ================================ ISS NET ================================ */
367 static int iss_net_rx(struct net_device *dev)
369 struct iss_net_private *lp = dev->priv;
373 /* Check if there is any new data. */
375 if (lp->tp.poll(lp) == 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++;
390 skb->mac.raw = skb->data;
391 pkt_len = lp->tp.read(lp, &skb);
392 skb_put(skb, pkt_len);
395 skb_trim(skb, pkt_len);
396 skb->protocol = lp->tp.protocol(skb);
400 lp->stats.rx_bytes += skb->len;
401 lp->stats.rx_packets++;
408 static int iss_net_poll(void)
410 struct list_head *ele;
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))
423 spin_lock(&lp->lock);
425 while ((err = iss_net_rx(lp->dev)) > 0)
428 spin_unlock(&lp->lock);
431 printk(KERN_ERR "Device '%s' read returned %d, "
432 "shutting it down\n", lp->dev->name, err);
435 // FIXME reactivate_fd(lp->fd, ISS_ETH_IRQ);
439 spin_unlock(&opened_lock);
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);
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"];
464 spin_lock(&lp->lock);
466 if ((err = lp->tp.open(lp)) < 0)
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);
494 spin_unlock(&lp->lock);
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);
507 spin_unlock(&opened_lock);
509 del_timer_sync(&lp->timer);
513 spin_unlock(&lp->lock);
517 static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
519 struct iss_net_private *lp = dev->priv;
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++;
542 netif_start_queue(dev);
543 printk(KERN_ERR "iss_net_start_xmit: failed(%d)\n", len);
546 spin_unlock_irqrestore(&lp->lock, flags);
553 static struct net_device_stats *iss_net_get_stats(struct net_device *dev)
555 struct iss_net_private *lp = dev->priv;
559 static void iss_net_set_multicast_list(struct net_device *dev)
562 if (dev->flags & IFF_PROMISC)
564 else if (dev->mc_count)
565 dev->flags |= IFF_ALLMULTI;
567 dev->flags &= ~IFF_ALLMULTI;
571 static void iss_net_tx_timeout(struct net_device *dev)
574 dev->trans_start = jiffies;
575 netif_wake_queue(dev);
579 static int iss_net_set_mac(struct net_device *dev, void *addr)
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);
593 static int iss_net_change_mtu(struct net_device *dev, int new_mtu)
596 struct iss_net_private *lp = dev->priv;
599 spin_lock(&lp->lock);
601 // FIXME not needed new_mtu = transport_set_mtu(new_mtu, &lp->user);
608 spin_unlock(&lp->lock);
614 void iss_net_user_timer_expire(unsigned long _conn)
619 static struct device_driver iss_net_driver = {
621 .bus = &platform_bus_type,
624 static int driver_registered;
626 static int iss_net_configure(int index, char *init)
628 struct net_device *dev;
629 struct iss_net_private *lp;
632 if ((dev = alloc_etherdev(sizeof *lp)) == NULL) {
633 printk(KERN_ERR "eth_configure: failed to allocate device\n");
637 /* Initialize private element. */
640 *lp = ((struct iss_net_private) {
641 .device_list = LIST_HEAD_INIT(lp->device_list),
642 .opened_list = LIST_HEAD_INIT(lp->opened_list),
643 .lock = SPIN_LOCK_UNLOCKED,
647 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0 },
652 * Try all transport protocols.
653 * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
656 if (!tuntap_probe(lp, index, init)) {
657 printk("Invalid arguments. Skipping device!\n");
661 printk(KERN_INFO "Netdevice %d ", index);
663 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
664 lp->mac[0], lp->mac[1],
665 lp->mac[2], lp->mac[3],
666 lp->mac[4], lp->mac[5]);
671 if (!driver_registered) {
672 driver_register(&iss_net_driver);
673 driver_registered = 1;
676 spin_lock(&devices_lock);
677 list_add(&lp->device_list, &devices);
678 spin_unlock(&devices_lock);
681 lp->pdev.name = DRIVER_NAME;
682 platform_device_register(&lp->pdev);
683 SET_NETDEV_DEV(dev,&lp->pdev.dev);
686 * If this name ends up conflicting with an existing registered
687 * netdevice, that is OK, register_netdev{,ice}() will notice this
690 snprintf(dev->name, sizeof dev->name, "eth%d", index);
693 dev->open = iss_net_open;
694 dev->hard_start_xmit = iss_net_start_xmit;
695 dev->stop = iss_net_close;
696 dev->get_stats = iss_net_get_stats;
697 dev->set_multicast_list = iss_net_set_multicast_list;
698 dev->tx_timeout = iss_net_tx_timeout;
699 dev->set_mac_address = iss_net_set_mac;
700 dev->change_mtu = iss_net_change_mtu;
701 dev->watchdog_timeo = (HZ >> 1);
705 err = register_netdevice(dev);
709 printk("Error registering net device!\n");
710 /* XXX: should we call ->remove() here? */
716 lp->tl.function = iss_net_user_timer_expire;
720 set_ether_mac(dev, lp->mac);
725 // FIXME: unregister; free, etc..
730 /* ------------------------------------------------------------------------- */
732 /* Filled in during early boot */
734 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
736 struct iss_net_init {
737 struct list_head list;
738 char *init; /* init string */
743 * Parse the command line and look for 'ethX=...' fields, and register all
744 * those fields. They will be later initialized in iss_net_init.
747 #define ERR KERN_ERR "iss_net_setup: "
749 static int iss_net_setup(char *str)
751 struct iss_net_private *device = NULL;
752 struct iss_net_init *new;
753 struct list_head *ele;
757 n = simple_strtoul(str, &end, 0);
759 printk(ERR "Failed to parse '%s'\n", str);
763 printk(ERR "Device %d is negative\n", n);
766 if (*(str = end) != '=') {
767 printk(ERR "Expected '=' after device number\n");
771 spin_lock(&devices_lock);
773 list_for_each(ele, &devices) {
774 device = list_entry(ele, struct iss_net_private, device_list);
775 if (device->index == n)
779 spin_unlock(&devices_lock);
781 if (device && device->index == n) {
782 printk(ERR "Device %d already configured\n", n);
786 if ((new = alloc_bootmem(sizeof new)) == NULL) {
787 printk("Alloc_bootmem failed\n");
791 INIT_LIST_HEAD(&new->list);
795 list_add_tail(&new->list, ð_cmd_line);
801 __setup("eth", iss_net_setup);
804 * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
807 static int iss_net_init(void)
809 struct list_head *ele, *next;
811 /* Walk through all Ethernet devices specified in the command line. */
813 list_for_each_safe(ele, next, ð_cmd_line) {
814 struct iss_net_init *eth;
815 eth = list_entry(ele, struct iss_net_init, list);
816 iss_net_configure(eth->index, eth->init);
822 module_init(iss_net_init);