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>
37 #include <xtensa/simcall.h>
39 #define DRIVER_NAME "iss-netdev"
40 #define ETH_MAX_PACKET 1500
41 #define ETH_HEADER_OTHER 14
42 #define ISS_NET_TIMER_VALUE (2 * HZ)
45 static DEFINE_SPINLOCK(opened_lock);
46 static LIST_HEAD(opened);
48 static DEFINE_SPINLOCK(devices_lock);
49 static LIST_HEAD(devices);
51 /* ------------------------------------------------------------------------- */
53 /* We currently only support the TUNTAP transport protocol. */
55 #define TRANSPORT_TUNTAP_NAME "tuntap"
56 #define TRANSPORT_TUNTAP_MTU ETH_MAX_PACKET
59 char dev_name[IFNAMSIZ];
61 unsigned char gw[ETH_ALEN];
65 /* ------------------------------------------------------------------------- */
68 /* This structure contains out private information for the driver. */
70 struct iss_net_private {
72 struct list_head device_list;
73 struct list_head opened_list;
76 struct net_device *dev;
77 struct platform_device pdev;
79 struct net_device_stats stats;
81 struct timer_list timer;
82 unsigned int timer_val;
87 unsigned char mac[ETH_ALEN];
92 struct tuntap_info tuntap;
95 int (*open)(struct iss_net_private *lp);
96 void (*close)(struct iss_net_private *lp);
97 int (*read)(struct iss_net_private *lp, struct sk_buff **skb);
98 int (*write)(struct iss_net_private *lp, struct sk_buff **skb);
99 unsigned short (*protocol)(struct sk_buff *skb);
100 int (*poll)(struct iss_net_private *lp);
105 /* ======================= ISS SIMCALL INTERFACE =========================== */
107 /* Note: __simc must _not_ be declared inline! */
111 static int __simc (int a, int b, int c, int d, int e, int f)
114 __asm__ __volatile__ ("simcall\n"
116 "mov %1, a3\n" : "=a" (ret), "=a" (errno)
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, ...)
162 while ((arg = va_arg(ap, char**)) != NULL) {
165 end = strchr(str, ',');
181 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
183 if ((skb != NULL) && (skb_tailroom(skb) < extra)) {
184 struct sk_buff *skb2;
186 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
197 /* Return the IP address as a string for a given device. */
199 static void dev_ip_addr(void *d, char *buf, char *bin_buf)
201 struct net_device *dev = d;
202 struct in_device *ip = dev->ip_ptr;
203 struct in_ifaddr *in;
206 if ((ip == NULL) || ((in = ip->ifa_list) == NULL)) {
207 printk(KERN_WARNING "Device not assigned an IP address!\n");
211 addr = in->ifa_address;
212 sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
213 (addr >> 16) & 0xff, addr >> 24);
216 bin_buf[0] = addr & 0xff;
217 bin_buf[1] = (addr >> 8) & 0xff;
218 bin_buf[2] = (addr >> 16) & 0xff;
219 bin_buf[3] = addr >> 24;
223 /* Set Ethernet address of the specified device. */
225 static void inline set_ether_mac(void *d, unsigned char *addr)
227 struct net_device *dev = d;
228 memcpy(dev->dev_addr, addr, ETH_ALEN);
232 /* ======================= TUNTAP TRANSPORT INTERFACE ====================== */
234 static int tuntap_open(struct iss_net_private *lp)
237 char *dev_name = lp->tp.info.tuntap.dev_name;
241 /* We currently only support a fixed configuration. */
243 if (!lp->tp.info.tuntap.fixed_config)
246 if ((fd = simc_open("/dev/net/tun", 02, 0)) < 0) { /* O_RDWR */
247 printk("Failed to open /dev/net/tun, returned %d "
248 "(errno = %d)\n", fd, errno);
252 memset(&ifr, 0, sizeof ifr);
253 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
254 strlcpy(ifr.ifr_name, dev_name, sizeof ifr.ifr_name - 1);
256 if ((err = simc_ioctl(fd, TUNSETIFF, (void*) &ifr)) < 0) {
257 printk("Failed to set interface, returned %d "
258 "(errno = %d)\n", err, errno);
263 lp->tp.info.tuntap.fd = fd;
267 static void tuntap_close(struct iss_net_private *lp)
270 if (lp->tp.info.tuntap.fixed_config)
271 iter_addresses(lp->tp.info.tuntap.dev, close_addr, lp->host.dev_name);
273 simc_close(lp->tp.info.tuntap.fd);
274 lp->tp.info.tuntap.fd = -1;
277 static int tuntap_read (struct iss_net_private *lp, struct sk_buff **skb)
280 *skb = ether_adjust_skb(*skb, ETH_HEADER_OTHER);
285 return simc_read(lp->tp.info.tuntap.fd,
286 (*skb)->data, (*skb)->dev->mtu + ETH_HEADER_OTHER);
289 static int tuntap_write (struct iss_net_private *lp, struct sk_buff **skb)
291 return simc_write(lp->tp.info.tuntap.fd, (*skb)->data, (*skb)->len);
294 unsigned short tuntap_protocol(struct sk_buff *skb)
296 return eth_type_trans(skb, skb->dev);
299 static int tuntap_poll(struct iss_net_private *lp)
301 return simc_poll(lp->tp.info.tuntap.fd);
305 * Currently only a device name is supported.
306 * ethX=tuntap[,[mac address][,[device name]]]
309 static int tuntap_probe(struct iss_net_private *lp, int index, char *init)
311 const int len = strlen(TRANSPORT_TUNTAP_NAME);
312 char *dev_name = NULL, *mac_str = NULL, *rem = NULL;
314 /* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */
316 if (strncmp(init, TRANSPORT_TUNTAP_NAME, len))
319 if (*(init += strlen(TRANSPORT_TUNTAP_NAME)) == ',') {
320 if ((rem=split_if_spec(init+1, &mac_str, &dev_name)) != NULL) {
321 printk("Extra garbage on specification : '%s'\n", rem);
324 } else if (*init != '\0') {
325 printk("Invalid argument: %s. Skipping device!\n", init);
330 strncpy(lp->tp.info.tuntap.dev_name, dev_name,
331 sizeof lp->tp.info.tuntap.dev_name);
332 lp->tp.info.tuntap.fixed_config = 1;
334 strcpy(lp->tp.info.tuntap.dev_name, TRANSPORT_TUNTAP_NAME);
338 if (setup_etheraddr(mac_str, lp->mac))
341 lp->mtu = TRANSPORT_TUNTAP_MTU;
343 //lp->info.tuntap.gate_addr = gate_addr;
345 lp->tp.info.tuntap.fd = -1;
347 lp->tp.open = tuntap_open;
348 lp->tp.close = tuntap_close;
349 lp->tp.read = tuntap_read;
350 lp->tp.write = tuntap_write;
351 lp->tp.protocol = tuntap_protocol;
352 lp->tp.poll = tuntap_poll;
354 printk("TUN/TAP backend - ");
356 if (lp->host.gate_addr != NULL)
357 printk("IP = %s", lp->host.gate_addr);
364 /* ================================ ISS NET ================================ */
366 static int iss_net_rx(struct net_device *dev)
368 struct iss_net_private *lp = dev->priv;
372 /* Check if there is any new data. */
374 if (lp->tp.poll(lp) == 0)
377 /* Try to allocate memory, if it fails, try again next round. */
379 if ((skb = dev_alloc_skb(dev->mtu + 2 + ETH_HEADER_OTHER)) == NULL) {
380 lp->stats.rx_dropped++;
389 skb->mac.raw = skb->data;
390 pkt_len = lp->tp.read(lp, &skb);
391 skb_put(skb, pkt_len);
394 skb_trim(skb, pkt_len);
395 skb->protocol = lp->tp.protocol(skb);
399 lp->stats.rx_bytes += skb->len;
400 lp->stats.rx_packets++;
407 static int iss_net_poll(void)
409 struct list_head *ele;
412 spin_lock(&opened_lock);
414 list_for_each(ele, &opened) {
415 struct iss_net_private *lp;
417 lp = list_entry(ele, struct iss_net_private, opened_list);
419 if (!netif_running(lp->dev))
422 spin_lock(&lp->lock);
424 while ((err = iss_net_rx(lp->dev)) > 0)
427 spin_unlock(&lp->lock);
430 printk(KERN_ERR "Device '%s' read returned %d, "
431 "shutting it down\n", lp->dev->name, err);
434 // FIXME reactivate_fd(lp->fd, ISS_ETH_IRQ);
438 spin_unlock(&opened_lock);
443 static void iss_net_timer(unsigned long priv)
445 struct iss_net_private* lp = (struct iss_net_private*) priv;
447 spin_lock(&lp->lock);
451 mod_timer(&lp->timer, jiffies + lp->timer_val);
453 spin_unlock(&lp->lock);
457 static int iss_net_open(struct net_device *dev)
459 struct iss_net_private *lp = dev->priv;
460 char addr[sizeof "255.255.255.255\0"];
463 spin_lock(&lp->lock);
465 if ((err = lp->tp.open(lp)) < 0)
469 dev_ip_addr(dev, addr, &lp->mac[2]);
470 set_ether_mac(dev, lp->mac);
473 netif_start_queue(dev);
475 /* clear buffer - it can happen that the host side of the interface
476 * is full when we gethere. In this case, new data is never queued,
477 * SIGIOs never arrive, and the net never works.
479 while ((err = iss_net_rx(dev)) > 0)
482 spin_lock(&opened_lock);
483 list_add(&lp->opened_list, &opened);
484 spin_unlock(&opened_lock);
486 init_timer(&lp->timer);
487 lp->timer_val = ISS_NET_TIMER_VALUE;
488 lp->timer.data = (unsigned long) lp;
489 lp->timer.function = iss_net_timer;
490 mod_timer(&lp->timer, jiffies + lp->timer_val);
493 spin_unlock(&lp->lock);
497 static int iss_net_close(struct net_device *dev)
499 struct iss_net_private *lp = dev->priv;
500 printk("iss_net_close!\n");
501 netif_stop_queue(dev);
502 spin_lock(&lp->lock);
504 spin_lock(&opened_lock);
506 spin_unlock(&opened_lock);
508 del_timer_sync(&lp->timer);
512 spin_unlock(&lp->lock);
516 static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
518 struct iss_net_private *lp = dev->priv;
522 netif_stop_queue(dev);
523 spin_lock_irqsave(&lp->lock, flags);
525 len = lp->tp.write(lp, &skb);
527 if (len == skb->len) {
528 lp->stats.tx_packets++;
529 lp->stats.tx_bytes += skb->len;
530 dev->trans_start = jiffies;
531 netif_start_queue(dev);
533 /* this is normally done in the interrupt when tx finishes */
534 netif_wake_queue(dev);
536 } else if (len == 0) {
537 netif_start_queue(dev);
538 lp->stats.tx_dropped++;
541 netif_start_queue(dev);
542 printk(KERN_ERR "iss_net_start_xmit: failed(%d)\n", len);
545 spin_unlock_irqrestore(&lp->lock, flags);
552 static struct net_device_stats *iss_net_get_stats(struct net_device *dev)
554 struct iss_net_private *lp = dev->priv;
558 static void iss_net_set_multicast_list(struct net_device *dev)
561 if (dev->flags & IFF_PROMISC)
563 else if (dev->mc_count)
564 dev->flags |= IFF_ALLMULTI;
566 dev->flags &= ~IFF_ALLMULTI;
570 static void iss_net_tx_timeout(struct net_device *dev)
573 dev->trans_start = jiffies;
574 netif_wake_queue(dev);
578 static int iss_net_set_mac(struct net_device *dev, void *addr)
581 struct iss_net_private *lp = dev->priv;
582 struct sockaddr *hwaddr = addr;
584 spin_lock(&lp->lock);
585 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
586 spin_unlock(&lp->lock);
592 static int iss_net_change_mtu(struct net_device *dev, int new_mtu)
595 struct iss_net_private *lp = dev->priv;
598 spin_lock(&lp->lock);
600 // FIXME not needed new_mtu = transport_set_mtu(new_mtu, &lp->user);
607 spin_unlock(&lp->lock);
613 static int iss_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
616 static const struct ethtool_drvinfo info = {
617 .cmd = ETHTOOL_GDRVINFO,
618 .driver = DRIVER_NAME,
626 useraddr = ifr->ifr_data;
627 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
631 case ETHTOOL_GDRVINFO:
632 if (copy_to_user(useraddr, &info, sizeof(info)))
645 void iss_net_user_timer_expire(unsigned long _conn)
650 static struct device_driver iss_net_driver = {
652 .bus = &platform_bus_type,
655 static int driver_registered;
657 static int iss_net_configure(int index, char *init)
659 struct net_device *dev;
660 struct iss_net_private *lp;
663 if ((dev = alloc_etherdev(sizeof *lp)) == NULL) {
664 printk(KERN_ERR "eth_configure: failed to allocate device\n");
668 /* Initialize private element. */
671 *lp = ((struct iss_net_private) {
672 .device_list = LIST_HEAD_INIT(lp->device_list),
673 .opened_list = LIST_HEAD_INIT(lp->opened_list),
674 .lock = SPIN_LOCK_UNLOCKED,
678 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0 },
683 * Try all transport protocols.
684 * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
687 if (!tuntap_probe(lp, index, init)) {
688 printk("Invalid arguments. Skipping device!\n");
692 printk(KERN_INFO "Netdevice %d ", index);
694 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
695 lp->mac[0], lp->mac[1],
696 lp->mac[2], lp->mac[3],
697 lp->mac[4], lp->mac[5]);
702 if (!driver_registered) {
703 driver_register(&iss_net_driver);
704 driver_registered = 1;
707 spin_lock(&devices_lock);
708 list_add(&lp->device_list, &devices);
709 spin_unlock(&devices_lock);
712 lp->pdev.name = DRIVER_NAME;
713 platform_device_register(&lp->pdev);
714 SET_NETDEV_DEV(dev,&lp->pdev.dev);
717 * If this name ends up conflicting with an existing registered
718 * netdevice, that is OK, register_netdev{,ice}() will notice this
721 snprintf(dev->name, sizeof dev->name, "eth%d", index);
724 dev->open = iss_net_open;
725 dev->hard_start_xmit = iss_net_start_xmit;
726 dev->stop = iss_net_close;
727 dev->get_stats = iss_net_get_stats;
728 dev->set_multicast_list = iss_net_set_multicast_list;
729 dev->tx_timeout = iss_net_tx_timeout;
730 dev->set_mac_address = iss_net_set_mac;
731 dev->change_mtu = iss_net_change_mtu;
732 dev->do_ioctl = iss_net_ioctl;
733 dev->watchdog_timeo = (HZ >> 1);
737 err = register_netdevice(dev);
741 printk("Error registering net device!\n");
742 /* XXX: should we call ->remove() here? */
748 lp->tl.function = iss_net_user_timer_expire;
752 set_ether_mac(dev, lp->mac);
757 // FIXME: unregister; free, etc..
762 /* ------------------------------------------------------------------------- */
764 /* Filled in during early boot */
766 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
768 struct iss_net_init {
769 struct list_head list;
770 char *init; /* init string */
775 * Parse the command line and look for 'ethX=...' fields, and register all
776 * those fields. They will be later initialized in iss_net_init.
779 #define ERR KERN_ERR "iss_net_setup: "
781 static int iss_net_setup(char *str)
783 struct iss_net_private *device = NULL;
784 struct iss_net_init *new;
785 struct list_head *ele;
789 n = simple_strtoul(str, &end, 0);
791 printk(ERR "Failed to parse '%s'\n", str);
795 printk(ERR "Device %d is negative\n", n);
798 if (*(str = end) != '=') {
799 printk(ERR "Expected '=' after device number\n");
803 spin_lock(&devices_lock);
805 list_for_each(ele, &devices) {
806 device = list_entry(ele, struct iss_net_private, device_list);
807 if (device->index == n)
811 spin_unlock(&devices_lock);
813 if (device && device->index == n) {
814 printk(ERR "Device %d already configured\n", n);
818 if ((new = alloc_bootmem(sizeof new)) == NULL) {
819 printk("Alloc_bootmem failed\n");
823 INIT_LIST_HEAD(&new->list);
827 list_add_tail(&new->list, ð_cmd_line);
833 __setup("eth", iss_net_setup);
836 * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
839 static int iss_net_init(void)
841 struct list_head *ele, *next;
843 /* Walk through all Ethernet devices specified in the command line. */
845 list_for_each_safe(ele, next, ð_cmd_line) {
846 struct iss_net_init *eth;
847 eth = list_entry(ele, struct iss_net_init, list);
848 iss_net_configure(eth->index, eth->init);
854 module_init(iss_net_init);