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/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 <asm/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
58 char dev_name[IFNAMSIZ];
60 unsigned char gw[ETH_ALEN];
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;
75 struct net_device *dev;
76 struct platform_device pdev;
78 struct net_device_stats stats;
80 struct timer_list timer;
81 unsigned int timer_val;
86 unsigned char mac[ETH_ALEN];
91 struct tuntap_info tuntap;
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);
104 /* ======================= ISS SIMCALL INTERFACE =========================== */
106 /* Note: __simc must _not_ be declared inline! */
110 static int __simc (int a, int b, int c, int d, int e, int f)
113 __asm__ __volatile__ ("simcall\n"
115 "mov %1, a3\n" : "=a" (ret), "=a" (errno)
120 static int inline simc_open(char *file, int flags, int mode)
122 return __simc(SYS_open, (int) file, flags, mode, 0, 0);
125 static int inline simc_close(int fd)
127 return __simc(SYS_close, fd, 0, 0, 0, 0);
130 static int inline simc_ioctl(int fd, int request, void *arg)
132 return __simc(SYS_ioctl, fd, request, (int) arg, 0, 0);
135 static int inline simc_read(int fd, void *buf, size_t count)
137 return __simc(SYS_read, fd, (int) buf, count, 0, 0);
140 static int inline simc_write(int fd, void *buf, size_t count)
142 return __simc(SYS_write, fd, (int) buf, count, 0, 0);
145 static int inline simc_poll(int fd)
147 struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
149 return __simc(SYS_select_one, fd, XTISS_SELECT_ONE_READ, (int)&tv,0,0);
152 /* ================================ HELPERS ================================ */
155 static char *split_if_spec(char *str, ...)
161 while ((arg = va_arg(ap, char**)) != NULL) {
164 end = strchr(str, ',');
180 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
182 if ((skb != NULL) && (skb_tailroom(skb) < extra)) {
183 struct sk_buff *skb2;
185 skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
196 /* Return the IP address as a string for a given device. */
198 static void dev_ip_addr(void *d, char *buf, char *bin_buf)
200 struct net_device *dev = d;
201 struct in_device *ip = dev->ip_ptr;
202 struct in_ifaddr *in;
205 if ((ip == NULL) || ((in = ip->ifa_list) == NULL)) {
206 printk(KERN_WARNING "Device not assigned an IP address!\n");
210 addr = in->ifa_address;
211 sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
212 (addr >> 16) & 0xff, addr >> 24);
215 bin_buf[0] = addr & 0xff;
216 bin_buf[1] = (addr >> 8) & 0xff;
217 bin_buf[2] = (addr >> 16) & 0xff;
218 bin_buf[3] = addr >> 24;
222 /* Set Ethernet address of the specified device. */
224 static void inline set_ether_mac(void *d, unsigned char *addr)
226 struct net_device *dev = d;
227 memcpy(dev->dev_addr, addr, ETH_ALEN);
231 /* ======================= TUNTAP TRANSPORT INTERFACE ====================== */
233 static int tuntap_open(struct iss_net_private *lp)
236 char *dev_name = lp->tp.info.tuntap.dev_name;
240 /* We currently only support a fixed configuration. */
242 if (!lp->tp.info.tuntap.fixed_config)
245 if ((fd = simc_open("/dev/net/tun", 02, 0)) < 0) { /* O_RDWR */
246 printk("Failed to open /dev/net/tun, returned %d "
247 "(errno = %d)\n", fd, errno);
251 memset(&ifr, 0, sizeof ifr);
252 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
253 strlcpy(ifr.ifr_name, dev_name, sizeof ifr.ifr_name);
255 if ((err = simc_ioctl(fd, TUNSETIFF, (void*) &ifr)) < 0) {
256 printk("Failed to set interface, returned %d "
257 "(errno = %d)\n", err, errno);
262 lp->tp.info.tuntap.fd = fd;
266 static void tuntap_close(struct iss_net_private *lp)
269 if (lp->tp.info.tuntap.fixed_config)
270 iter_addresses(lp->tp.info.tuntap.dev, close_addr, lp->host.dev_name);
272 simc_close(lp->tp.info.tuntap.fd);
273 lp->tp.info.tuntap.fd = -1;
276 static int tuntap_read (struct iss_net_private *lp, struct sk_buff **skb)
279 *skb = ether_adjust_skb(*skb, ETH_HEADER_OTHER);
284 return simc_read(lp->tp.info.tuntap.fd,
285 (*skb)->data, (*skb)->dev->mtu + ETH_HEADER_OTHER);
288 static int tuntap_write (struct iss_net_private *lp, struct sk_buff **skb)
290 return simc_write(lp->tp.info.tuntap.fd, (*skb)->data, (*skb)->len);
293 unsigned short tuntap_protocol(struct sk_buff *skb)
295 return eth_type_trans(skb, skb->dev);
298 static int tuntap_poll(struct iss_net_private *lp)
300 return simc_poll(lp->tp.info.tuntap.fd);
304 * Currently only a device name is supported.
305 * ethX=tuntap[,[mac address][,[device name]]]
308 static int tuntap_probe(struct iss_net_private *lp, int index, char *init)
310 const int len = strlen(TRANSPORT_TUNTAP_NAME);
311 char *dev_name = NULL, *mac_str = NULL, *rem = NULL;
313 /* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */
315 if (strncmp(init, TRANSPORT_TUNTAP_NAME, len))
318 if (*(init += strlen(TRANSPORT_TUNTAP_NAME)) == ',') {
319 if ((rem=split_if_spec(init+1, &mac_str, &dev_name)) != NULL) {
320 printk("Extra garbage on specification : '%s'\n", rem);
323 } else if (*init != '\0') {
324 printk("Invalid argument: %s. Skipping device!\n", init);
329 strncpy(lp->tp.info.tuntap.dev_name, dev_name,
330 sizeof lp->tp.info.tuntap.dev_name);
331 lp->tp.info.tuntap.fixed_config = 1;
333 strcpy(lp->tp.info.tuntap.dev_name, TRANSPORT_TUNTAP_NAME);
337 if (setup_etheraddr(mac_str, lp->mac))
340 lp->mtu = TRANSPORT_TUNTAP_MTU;
342 //lp->info.tuntap.gate_addr = gate_addr;
344 lp->tp.info.tuntap.fd = -1;
346 lp->tp.open = tuntap_open;
347 lp->tp.close = tuntap_close;
348 lp->tp.read = tuntap_read;
349 lp->tp.write = tuntap_write;
350 lp->tp.protocol = tuntap_protocol;
351 lp->tp.poll = tuntap_poll;
353 printk("TUN/TAP backend - ");
355 if (lp->host.gate_addr != NULL)
356 printk("IP = %s", lp->host.gate_addr);
363 /* ================================ ISS NET ================================ */
365 static int iss_net_rx(struct net_device *dev)
367 struct iss_net_private *lp = dev->priv;
371 /* Check if there is any new data. */
373 if (lp->tp.poll(lp) == 0)
376 /* Try to allocate memory, if it fails, try again next round. */
378 if ((skb = dev_alloc_skb(dev->mtu + 2 + ETH_HEADER_OTHER)) == NULL) {
379 lp->stats.rx_dropped++;
388 skb_reset_mac_header(skb);
389 pkt_len = lp->tp.read(lp, &skb);
390 skb_put(skb, pkt_len);
393 skb_trim(skb, pkt_len);
394 skb->protocol = lp->tp.protocol(skb);
396 lp->stats.rx_bytes += skb->len;
397 lp->stats.rx_packets++;
406 static int iss_net_poll(void)
408 struct list_head *ele;
411 spin_lock(&opened_lock);
413 list_for_each(ele, &opened) {
414 struct iss_net_private *lp;
416 lp = list_entry(ele, struct iss_net_private, opened_list);
418 if (!netif_running(lp->dev))
421 spin_lock(&lp->lock);
423 while ((err = iss_net_rx(lp->dev)) > 0)
426 spin_unlock(&lp->lock);
429 printk(KERN_ERR "Device '%s' read returned %d, "
430 "shutting it down\n", lp->dev->name, err);
433 // FIXME reactivate_fd(lp->fd, ISS_ETH_IRQ);
437 spin_unlock(&opened_lock);
442 static void iss_net_timer(unsigned long priv)
444 struct iss_net_private* lp = (struct iss_net_private*) priv;
446 spin_lock(&lp->lock);
450 mod_timer(&lp->timer, jiffies + lp->timer_val);
452 spin_unlock(&lp->lock);
456 static int iss_net_open(struct net_device *dev)
458 struct iss_net_private *lp = dev->priv;
459 char addr[sizeof "255.255.255.255\0"];
462 spin_lock(&lp->lock);
464 if ((err = lp->tp.open(lp)) < 0)
468 dev_ip_addr(dev, addr, &lp->mac[2]);
469 set_ether_mac(dev, lp->mac);
472 netif_start_queue(dev);
474 /* clear buffer - it can happen that the host side of the interface
475 * is full when we get here. In this case, new data is never queued,
476 * SIGIOs never arrive, and the net never works.
478 while ((err = iss_net_rx(dev)) > 0)
481 spin_lock(&opened_lock);
482 list_add(&lp->opened_list, &opened);
483 spin_unlock(&opened_lock);
485 init_timer(&lp->timer);
486 lp->timer_val = ISS_NET_TIMER_VALUE;
487 lp->timer.data = (unsigned long) lp;
488 lp->timer.function = iss_net_timer;
489 mod_timer(&lp->timer, jiffies + lp->timer_val);
492 spin_unlock(&lp->lock);
496 static int iss_net_close(struct net_device *dev)
498 struct iss_net_private *lp = dev->priv;
499 printk("iss_net_close!\n");
500 netif_stop_queue(dev);
501 spin_lock(&lp->lock);
503 spin_lock(&opened_lock);
505 spin_unlock(&opened_lock);
507 del_timer_sync(&lp->timer);
511 spin_unlock(&lp->lock);
515 static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
517 struct iss_net_private *lp = dev->priv;
521 netif_stop_queue(dev);
522 spin_lock_irqsave(&lp->lock, flags);
524 len = lp->tp.write(lp, &skb);
526 if (len == skb->len) {
527 lp->stats.tx_packets++;
528 lp->stats.tx_bytes += skb->len;
529 dev->trans_start = jiffies;
530 netif_start_queue(dev);
532 /* this is normally done in the interrupt when tx finishes */
533 netif_wake_queue(dev);
535 } else if (len == 0) {
536 netif_start_queue(dev);
537 lp->stats.tx_dropped++;
540 netif_start_queue(dev);
541 printk(KERN_ERR "iss_net_start_xmit: failed(%d)\n", len);
544 spin_unlock_irqrestore(&lp->lock, flags);
551 static struct net_device_stats *iss_net_get_stats(struct net_device *dev)
553 struct iss_net_private *lp = dev->priv;
557 static void iss_net_set_multicast_list(struct net_device *dev)
560 if (dev->flags & IFF_PROMISC)
562 else if (dev->mc_count)
563 dev->flags |= IFF_ALLMULTI;
565 dev->flags &= ~IFF_ALLMULTI;
569 static void iss_net_tx_timeout(struct net_device *dev)
572 dev->trans_start = jiffies;
573 netif_wake_queue(dev);
577 static int iss_net_set_mac(struct net_device *dev, void *addr)
580 struct iss_net_private *lp = dev->priv;
581 struct sockaddr *hwaddr = addr;
583 spin_lock(&lp->lock);
584 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
585 spin_unlock(&lp->lock);
591 static int iss_net_change_mtu(struct net_device *dev, int new_mtu)
594 struct iss_net_private *lp = dev->priv;
597 spin_lock(&lp->lock);
599 // FIXME not needed new_mtu = transport_set_mtu(new_mtu, &lp->user);
606 spin_unlock(&lp->lock);
612 void iss_net_user_timer_expire(unsigned long _conn)
617 static struct platform_driver iss_net_driver = {
623 static int driver_registered;
625 static int iss_net_configure(int index, char *init)
627 struct net_device *dev;
628 struct iss_net_private *lp;
631 if ((dev = alloc_etherdev(sizeof *lp)) == NULL) {
632 printk(KERN_ERR "eth_configure: failed to allocate device\n");
636 /* Initialize private element. */
639 *lp = ((struct iss_net_private) {
640 .device_list = LIST_HEAD_INIT(lp->device_list),
641 .opened_list = LIST_HEAD_INIT(lp->opened_list),
642 .lock = SPIN_LOCK_UNLOCKED,
646 .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0 },
651 * Try all transport protocols.
652 * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
655 if (!tuntap_probe(lp, index, init)) {
656 printk("Invalid arguments. Skipping device!\n");
660 printk(KERN_INFO "Netdevice %d ", index);
662 printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
663 lp->mac[0], lp->mac[1],
664 lp->mac[2], lp->mac[3],
665 lp->mac[4], lp->mac[5]);
670 if (!driver_registered) {
671 platform_driver_register(&iss_net_driver);
672 driver_registered = 1;
675 spin_lock(&devices_lock);
676 list_add(&lp->device_list, &devices);
677 spin_unlock(&devices_lock);
680 lp->pdev.name = DRIVER_NAME;
681 platform_device_register(&lp->pdev);
682 SET_NETDEV_DEV(dev,&lp->pdev.dev);
685 * If this name ends up conflicting with an existing registered
686 * netdevice, that is OK, register_netdev{,ice}() will notice this
689 snprintf(dev->name, sizeof dev->name, "eth%d", index);
692 dev->open = iss_net_open;
693 dev->hard_start_xmit = iss_net_start_xmit;
694 dev->stop = iss_net_close;
695 dev->get_stats = iss_net_get_stats;
696 dev->set_multicast_list = iss_net_set_multicast_list;
697 dev->tx_timeout = iss_net_tx_timeout;
698 dev->set_mac_address = iss_net_set_mac;
699 dev->change_mtu = iss_net_change_mtu;
700 dev->watchdog_timeo = (HZ >> 1);
704 err = register_netdevice(dev);
708 printk("Error registering net device!\n");
709 /* XXX: should we call ->remove() here? */
715 lp->tl.function = iss_net_user_timer_expire;
719 set_ether_mac(dev, lp->mac);
724 // FIXME: unregister; free, etc..
729 /* ------------------------------------------------------------------------- */
731 /* Filled in during early boot */
733 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
735 struct iss_net_init {
736 struct list_head list;
737 char *init; /* init string */
742 * Parse the command line and look for 'ethX=...' fields, and register all
743 * those fields. They will be later initialized in iss_net_init.
746 #define ERR KERN_ERR "iss_net_setup: "
748 static int iss_net_setup(char *str)
750 struct iss_net_private *device = NULL;
751 struct iss_net_init *new;
752 struct list_head *ele;
756 n = simple_strtoul(str, &end, 0);
758 printk(ERR "Failed to parse '%s'\n", str);
762 printk(ERR "Device %d is negative\n", n);
765 if (*(str = end) != '=') {
766 printk(ERR "Expected '=' after device number\n");
770 spin_lock(&devices_lock);
772 list_for_each(ele, &devices) {
773 device = list_entry(ele, struct iss_net_private, device_list);
774 if (device->index == n)
778 spin_unlock(&devices_lock);
780 if (device && device->index == n) {
781 printk(ERR "Device %d already configured\n", n);
785 if ((new = alloc_bootmem(sizeof new)) == NULL) {
786 printk("Alloc_bootmem failed\n");
790 INIT_LIST_HEAD(&new->list);
794 list_add_tail(&new->list, ð_cmd_line);
800 __setup("eth=", iss_net_setup);
803 * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
806 static int iss_net_init(void)
808 struct list_head *ele, *next;
810 /* Walk through all Ethernet devices specified in the command line. */
812 list_for_each_safe(ele, next, ð_cmd_line) {
813 struct iss_net_init *eth;
814 eth = list_entry(ele, struct iss_net_init, list);
815 iss_net_configure(eth->index, eth->init);
821 module_init(iss_net_init);