fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / um / drivers / net_kern.c
index 4eeaf88..afe3d42 100644 (file)
@@ -5,7 +5,6 @@
  * Licensed under the GPL.
  */
 
-#include "linux/config.h"
 #include "linux/kernel.h"
 #include "linux/netdevice.h"
 #include "linux/rtnetlink.h"
@@ -20,6 +19,7 @@
 #include "linux/ctype.h"
 #include "linux/bootmem.h"
 #include "linux/ethtool.h"
+#include "linux/platform_device.h"
 #include "asm/uaccess.h"
 #include "user_util.h"
 #include "kern_util.h"
 #include "irq_user.h"
 #include "irq_kern.h"
 
+static inline void set_ether_mac(struct net_device *dev, unsigned char *addr)
+{
+       memcpy(dev->dev_addr, addr, ETH_ALEN);
+}
+
 #define DRIVER_NAME "uml-netdev"
 
 static DEFINE_SPINLOCK(opened_lock);
-LIST_HEAD(opened);
+static LIST_HEAD(opened);
 
 static int uml_net_rx(struct net_device *dev)
 {
@@ -67,7 +72,14 @@ static int uml_net_rx(struct net_device *dev)
        return pkt_len;
 }
 
-irqreturn_t uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+static void uml_dev_close(struct work_struct *work)
+{
+       struct uml_net_private *lp =
+               container_of(work, struct uml_net_private, work);
+       dev_close(lp->dev);
+}
+
+irqreturn_t uml_net_interrupt(int irq, void *dev_id)
 {
        struct net_device *dev = dev_id;
        struct uml_net_private *lp = dev->priv;
@@ -82,12 +94,19 @@ irqreturn_t uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
                printk(KERN_ERR 
                       "Device '%s' read returned %d, shutting it down\n", 
                       dev->name, err);
-               dev_close(dev);
+               /* dev_close can't be called in interrupt context, and takes
+                * again lp->lock.
+                * And dev_close() can be safely called multiple times on the
+                * same device, since it tests for (dev->flags & IFF_UP). So
+                * there's no harm in delaying the device shutdown.
+                * Furthermore, the workqueue will not re-enqueue an already
+                * enqueued work item. */
+               schedule_work(&lp->work);
                goto out;
        }
        reactivate_fd(lp->fd, UM_ETH_IRQ);
 
- out:
+out:
        spin_unlock(&lp->lock);
        return(IRQ_HANDLED);
 }
@@ -95,21 +114,13 @@ irqreturn_t uml_net_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 static int uml_net_open(struct net_device *dev)
 {
        struct uml_net_private *lp = dev->priv;
-       char addr[sizeof("255.255.255.255\0")];
        int err;
 
-       spin_lock(&lp->lock);
-
        if(lp->fd >= 0){
                err = -ENXIO;
                goto out;
        }
 
-       if(!lp->have_mac){
-               dev_ip_addr(dev, addr, &lp->mac[2]);
-               set_ether_mac(dev, lp->mac);
-       }
-
        lp->fd = (*lp->open)(&lp->user);
        if(lp->fd < 0){
                err = lp->fd;
@@ -117,12 +128,11 @@ static int uml_net_open(struct net_device *dev)
        }
 
        err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
-                            SA_INTERRUPT | SA_SHIRQ, dev->name, dev);
+                            IRQF_DISABLED | IRQF_SHARED, dev->name, dev);
        if(err != 0){
                printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
-               if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
-               lp->fd = -1;
                err = -ENETUNREACH;
+               goto out_close;
        }
 
        lp->tl.data = (unsigned long) &lp->user;
@@ -134,9 +144,16 @@ static int uml_net_open(struct net_device *dev)
         */
        while((err = uml_net_rx(dev)) > 0) ;
 
- out:
-       spin_unlock(&lp->lock);
-       return(err);
+       spin_lock(&opened_lock);
+       list_add(&lp->list, &opened);
+       spin_unlock(&opened_lock);
+
+       return 0;
+out_close:
+       if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
+       lp->fd = -1;
+out:
+       return err;
 }
 
 static int uml_net_close(struct net_device *dev)
@@ -144,15 +161,16 @@ static int uml_net_close(struct net_device *dev)
        struct uml_net_private *lp = dev->priv;
        
        netif_stop_queue(dev);
-       spin_lock(&lp->lock);
 
-       free_irq_by_irq_and_dev(dev->irq, dev);
        free_irq(dev->irq, dev);
        if(lp->close != NULL)
                (*lp->close)(lp->fd, &lp->user);
        lp->fd = -1;
 
-       spin_unlock(&lp->lock);
+       spin_lock(&opened_lock);
+       list_del(&lp->list);
+       spin_unlock(&opened_lock);
+
        return 0;
 }
 
@@ -217,9 +235,9 @@ static int uml_net_set_mac(struct net_device *dev, void *addr)
        struct uml_net_private *lp = dev->priv;
        struct sockaddr *hwaddr = addr;
 
-       spin_lock(&lp->lock);
-       memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
-       spin_unlock(&lp->lock);
+       spin_lock_irq(&lp->lock);
+       set_ether_mac(dev, hwaddr->sa_data);
+       spin_unlock_irq(&lp->lock);
 
        return(0);
 }
@@ -229,7 +247,7 @@ static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
        struct uml_net_private *lp = dev->priv;
        int err = 0;
 
-       spin_lock(&lp->lock);
+       spin_lock_irq(&lp->lock);
 
        new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
        if(new_mtu < 0){
@@ -240,38 +258,22 @@ static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
        dev->mtu = new_mtu;
 
  out:
-       spin_unlock(&lp->lock);
+       spin_unlock_irq(&lp->lock);
        return err;
 }
 
-static int uml_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
-{
-       static const struct ethtool_drvinfo info = {
-               .cmd     = ETHTOOL_GDRVINFO,
-               .driver  = DRIVER_NAME,
-               .version = "42",
-       };
-       void *useraddr;
-       u32 ethcmd;
-
-       switch (cmd) {
-       case SIOCETHTOOL:
-               useraddr = ifr->ifr_data;
-               if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
-                       return -EFAULT;
-               switch (ethcmd) {
-               case ETHTOOL_GDRVINFO:
-                       if (copy_to_user(useraddr, &info, sizeof(info)))
-                               return -EFAULT;
-                       return 0;
-               default:
-                       return -EOPNOTSUPP;
-               }
-       default:
-               return -EINVAL;
-       }
+static void uml_net_get_drvinfo(struct net_device *dev,
+                               struct ethtool_drvinfo *info)
+{
+       strcpy(info->driver, DRIVER_NAME);
+       strcpy(info->version, "42");
 }
 
+static struct ethtool_ops uml_net_ethtool_ops = {
+       .get_drvinfo    = uml_net_get_drvinfo,
+       .get_link       = ethtool_op_get_link,
+};
+
 void uml_net_user_timer_expire(unsigned long _conn)
 {
 #ifdef undef
@@ -282,12 +284,44 @@ void uml_net_user_timer_expire(unsigned long _conn)
 #endif
 }
 
+static void setup_etheraddr(char *str, unsigned char *addr)
+{
+       char *end;
+       int i;
+
+       if(str == NULL)
+               goto random;
+
+       for(i=0;i<6;i++){
+               addr[i] = simple_strtoul(str, &end, 16);
+               if((end == str) ||
+                  ((*end != ':') && (*end != ',') && (*end != '\0'))){
+                       printk(KERN_ERR
+                              "setup_etheraddr: failed to parse '%s' "
+                              "as an ethernet address\n", str);
+                       goto random;
+               }
+               str = end + 1;
+       }
+       if(addr[0] & 1){
+               printk(KERN_ERR
+                      "Attempt to assign a broadcast ethernet address to a "
+                      "device disallowed\n");
+               goto random;
+       }
+       return;
+
+random:
+       random_ether_addr(addr);
+}
+
 static DEFINE_SPINLOCK(devices_lock);
-static struct list_head devices = LIST_HEAD_INIT(devices);
+static LIST_HEAD(devices);
 
-static struct device_driver uml_net_driver = {
-       .name  = DRIVER_NAME,
-       .bus   = &platform_bus_type,
+static struct platform_driver uml_net_driver = {
+       .driver = {
+               .name  = DRIVER_NAME,
+       },
 };
 static int driver_registered;
 
@@ -302,13 +336,12 @@ static int eth_configure(int n, void *init, char *mac,
        size = transport->private_size + sizeof(struct uml_net_private) + 
                sizeof(((struct uml_net_private *) 0)->user);
 
-       device = kmalloc(sizeof(*device), GFP_KERNEL);
+       device = kzalloc(sizeof(*device), GFP_KERNEL);
        if (device == NULL) {
                printk(KERN_ERR "eth_configure failed to allocate uml_net\n");
                return(1);
        }
 
-       memset(device, 0, sizeof(*device));
        INIT_LIST_HEAD(&device->list);
        device->index = n;
 
@@ -316,15 +349,13 @@ static int eth_configure(int n, void *init, char *mac,
        list_add(&device->list, &devices);
        spin_unlock(&devices_lock);
 
-       if (setup_etheraddr(mac, device->mac))
-               device->have_mac = 1;
+       setup_etheraddr(mac, device->mac);
 
        printk(KERN_INFO "Netdevice %d ", n);
-       if (device->have_mac)
-               printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
-                      device->mac[0], device->mac[1],
-                      device->mac[2], device->mac[3],
-                      device->mac[4], device->mac[5]);
+       printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
+              device->mac[0], device->mac[1],
+              device->mac[2], device->mac[3],
+              device->mac[4], device->mac[5]);
        printk(": ");
        dev = alloc_etherdev(size);
        if (dev == NULL) {
@@ -332,9 +363,15 @@ static int eth_configure(int n, void *init, char *mac,
                return 1;
        }
 
+       lp = dev->priv;
+       /* This points to the transport private data. It's still clear, but we
+        * must memset it to 0 *now*. Let's help the drivers. */
+       memset(lp, 0, size);
+       INIT_WORK(&lp->work, uml_dev_close);
+
        /* sysfs register */
        if (!driver_registered) {
-               driver_register(&uml_net_driver);
+               platform_driver_register(&uml_net_driver);
                driver_registered = 1;
        }
        device->pdev.id = n;
@@ -360,7 +397,7 @@ static int eth_configure(int n, void *init, char *mac,
        dev->tx_timeout = uml_net_tx_timeout;
        dev->set_mac_address = uml_net_set_mac;
        dev->change_mtu = uml_net_change_mtu;
-       dev->do_ioctl = uml_net_ioctl;
+       dev->ethtool_ops = &uml_net_ethtool_ops;
        dev->watchdog_timeo = (HZ >> 1);
        dev->irq = UM_ETH_IRQ;
 
@@ -373,7 +410,6 @@ static int eth_configure(int n, void *init, char *mac,
                free_netdev(dev);
                return 1;
        }
-       lp = dev->priv;
 
        /* lp.user is the first four bytes of the transport data, which
         * has already been initialized.  This structure assignment will
@@ -386,7 +422,6 @@ static int eth_configure(int n, void *init, char *mac,
                  .dev                  = dev,
                  .fd                   = -1,
                  .mac                  = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
-                 .have_mac             = device->have_mac,
                  .protocol             = transport->kern->protocol,
                  .open                 = transport->user->open,
                  .close                = transport->user->close,
@@ -401,20 +436,14 @@ static int eth_configure(int n, void *init, char *mac,
        init_timer(&lp->tl);
        spin_lock_init(&lp->lock);
        lp->tl.function = uml_net_user_timer_expire;
-       if (lp->have_mac)
-               memcpy(lp->mac, device->mac, sizeof(lp->mac));
+       memcpy(lp->mac, device->mac, sizeof(lp->mac));
 
        if (transport->user->init) 
                (*transport->user->init)(&lp->user, dev);
 
-       if (device->have_mac)
-               set_ether_mac(dev, device->mac);
+       set_ether_mac(dev, device->mac);
 
-       spin_lock(&opened_lock);
-       list_add(&lp->list, &opened);
-       spin_unlock(&opened_lock);
-
-       return(0);
+       return 0;
 }
 
 static struct uml_net *find_device(int n)
@@ -555,12 +584,13 @@ static int eth_setup(char *str)
        int n, err;
 
        err = eth_parse(str, &n, &str);
-       if(err) return(1);
+       if(err)
+               return 1;
 
-       new = alloc_bootmem(sizeof(new));
+       new = alloc_bootmem(sizeof(*new));
        if (new == NULL){
                printk("eth_init : alloc_bootmem failed\n");
-               return(1);
+               return 1;
        }
 
        INIT_LIST_HEAD(&new->list);
@@ -568,7 +598,7 @@ static int eth_setup(char *str)
        new->init = str;
 
        list_add_tail(&new->list, &eth_cmd_line);
-       return(1);
+       return 1;
 }
 
 __setup("eth", eth_setup);
@@ -602,7 +632,7 @@ static int net_config(char *str)
        err = eth_parse(str, &n, &str);
        if(err) return(err);
 
-       str = uml_strdup(str);
+       str = kstrdup(str, GFP_KERNEL);
        if(str == NULL){
                printk(KERN_ERR "net_config failed to strdup string\n");
                return(-1);
@@ -613,25 +643,35 @@ static int net_config(char *str)
        return(err);
 }
 
-static int net_remove(char *str)
+static int net_id(char **str, int *start_out, int *end_out)
+{
+        char *end;
+        int n;
+
+       n = simple_strtoul(*str, &end, 0);
+       if((*end != '\0') || (end == *str))
+               return -1;
+
+        *start_out = n;
+        *end_out = n;
+        *str = end;
+        return n;
+}
+
+static int net_remove(int n)
 {
        struct uml_net *device;
        struct net_device *dev;
        struct uml_net_private *lp;
-       char *end;
-       int n;
-
-       n = simple_strtoul(str, &end, 0);
-       if((*end != '\0') || (end == str))
-               return(-1);
 
        device = find_device(n);
        if(device == NULL)
-               return(0);
+               return -ENODEV;
 
        dev = device->dev;
        lp = dev->priv;
-       if(lp->fd > 0) return(-1);
+       if(lp->fd > 0)
+                return -EBUSY;
        if(lp->remove != NULL) (*lp->remove)(&lp->user);
        unregister_netdev(dev);
        platform_device_unregister(&device->pdev);
@@ -639,13 +679,14 @@ static int net_remove(char *str)
        list_del(&device->list);
        kfree(device);
        free_netdev(dev);
-       return(0);
+       return 0;
 }
 
 static struct mc_device net_mc = {
        .name           = "eth",
        .config         = net_config,
        .get_config     = NULL,
+        .id            = net_id,
        .remove         = net_remove,
 };
 
@@ -653,8 +694,6 @@ static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
                              void *ptr)
 {
        struct in_ifaddr *ifa = ptr;
-       u32 addr = ifa->ifa_address;
-       u32 netmask = ifa->ifa_mask;
        struct net_device *dev = ifa->ifa_dev->dev;
        struct uml_net_private *lp;
        void (*proc)(unsigned char *, unsigned char *, void *);
@@ -674,14 +713,8 @@ static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
                break;
        }
        if(proc != NULL){
-               addr_buf[0] = addr & 0xff;
-               addr_buf[1] = (addr >> 8) & 0xff;
-               addr_buf[2] = (addr >> 16) & 0xff;
-               addr_buf[3] = addr >> 24;
-               netmask_buf[0] = netmask & 0xff;
-               netmask_buf[1] = (netmask >> 8) & 0xff;
-               netmask_buf[2] = (netmask >> 16) & 0xff;
-               netmask_buf[3] = netmask >> 24;
+               memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
+               memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
                (*proc)(addr_buf, netmask_buf, &lp->user);
        }
        return(NOTIFY_DONE);
@@ -728,6 +761,7 @@ static void close_devices(void)
 
        list_for_each(ele, &opened){
                lp = list_entry(ele, struct uml_net_private, list);
+               free_irq(lp->dev->irq, lp->dev);
                if((lp->close != NULL) && (lp->fd >= 0))
                        (*lp->close)(lp->fd, &lp->user);
                if(lp->remove != NULL) (*lp->remove)(&lp->user);
@@ -736,63 +770,6 @@ static void close_devices(void)
 
 __uml_exitcall(close_devices);
 
-int setup_etheraddr(char *str, unsigned char *addr)
-{
-       char *end;
-       int i;
-
-       if(str == NULL)
-               return(0);
-       for(i=0;i<6;i++){
-               addr[i] = simple_strtoul(str, &end, 16);
-               if((end == str) ||
-                  ((*end != ':') && (*end != ',') && (*end != '\0'))){
-                       printk(KERN_ERR 
-                              "setup_etheraddr: failed to parse '%s' "
-                              "as an ethernet address\n", str);
-                       return(0);
-               }
-               str = end + 1;
-       }
-       if(addr[0] & 1){
-               printk(KERN_ERR 
-                      "Attempt to assign a broadcast ethernet address to a "
-                      "device disallowed\n");
-               return(0);
-       }
-       return(1);
-}
-
-void dev_ip_addr(void *d, char *buf, char *bin_buf)
-{
-       struct net_device *dev = d;
-       struct in_device *ip = dev->ip_ptr;
-       struct in_ifaddr *in;
-       u32 addr;
-
-       if((ip == NULL) || ((in = ip->ifa_list) == NULL)){
-               printk(KERN_WARNING "dev_ip_addr - device not assigned an "
-                      "IP address\n");
-               return;
-       }
-       addr = in->ifa_address;
-       sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff, 
-               (addr >> 16) & 0xff, addr >> 24);
-       if(bin_buf){
-               bin_buf[0] = addr & 0xff;
-               bin_buf[1] = (addr >> 8) & 0xff;
-               bin_buf[2] = (addr >> 16) & 0xff;
-               bin_buf[3] = addr >> 24;
-       }
-}
-
-void set_ether_mac(void *d, unsigned char *addr)
-{
-       struct net_device *dev = d;
-
-       memcpy(dev->dev_addr, addr, ETH_ALEN);  
-}
-
 struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
 {
        if((skb != NULL) && (skb_tailroom(skb) < extra)){
@@ -818,14 +795,8 @@ void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
        if(ip == NULL) return;
        in = ip->ifa_list;
        while(in != NULL){
-               address[0] = in->ifa_address & 0xff;
-               address[1] = (in->ifa_address >> 8) & 0xff;
-               address[2] = (in->ifa_address >> 16) & 0xff;
-               address[3] = in->ifa_address >> 24;
-               netmask[0] = in->ifa_mask & 0xff;
-               netmask[1] = (in->ifa_mask >> 8) & 0xff;
-               netmask[2] = (in->ifa_mask >> 16) & 0xff;
-               netmask[3] = in->ifa_mask >> 24;
+               memcpy(address, &in->ifa_address, sizeof(address));
+               memcpy(netmask, &in->ifa_mask, sizeof(netmask));
                (*cb)(address, netmask, arg);
                in = in->ifa_next;
        }
@@ -836,7 +807,7 @@ int dev_netmask(void *d, void *m)
        struct net_device *dev = d;
        struct in_device *ip = dev->ip_ptr;
        struct in_ifaddr *in;
-       __u32 *mask_out = m;
+       __be32 *mask_out = m;
 
        if(ip == NULL) 
                return(1);