include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / arch / um / drivers / net_kern.c
index b097a24..f053726 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/platform_device.h>
 #include <linux/rtnetlink.h>
 #include <linux/skbuff.h>
+#include <linux/slab.h>
 #include <linux/spinlock.h>
 #include "init.h"
 #include "irq_kern.h"
@@ -34,31 +35,74 @@ static inline void set_ether_mac(struct net_device *dev, unsigned char *addr)
 static DEFINE_SPINLOCK(opened_lock);
 static LIST_HEAD(opened);
 
+/*
+ * The drop_skb is used when we can't allocate an skb.  The
+ * packet is read into drop_skb in order to get the data off the
+ * connection to the host.
+ * It is reallocated whenever a maximum packet size is seen which is
+ * larger than any seen before.  update_drop_skb is called from
+ * eth_configure when a new interface is added.
+ */
+static DEFINE_SPINLOCK(drop_lock);
+static struct sk_buff *drop_skb;
+static int drop_max;
+
+static int update_drop_skb(int max)
+{
+       struct sk_buff *new;
+       unsigned long flags;
+       int err = 0;
+
+       spin_lock_irqsave(&drop_lock, flags);
+
+       if (max <= drop_max)
+               goto out;
+
+       err = -ENOMEM;
+       new = dev_alloc_skb(max);
+       if (new == NULL)
+               goto out;
+
+       skb_put(new, max);
+
+       kfree_skb(drop_skb);
+       drop_skb = new;
+       drop_max = max;
+       err = 0;
+out:
+       spin_unlock_irqrestore(&drop_lock, flags);
+
+       return err;
+}
+
 static int uml_net_rx(struct net_device *dev)
 {
-       struct uml_net_private *lp = dev->priv;
+       struct uml_net_private *lp = netdev_priv(dev);
        int pkt_len;
        struct sk_buff *skb;
 
        /* If we can't allocate memory, try again next round. */
-       skb = dev_alloc_skb(dev->mtu);
+       skb = dev_alloc_skb(lp->max_packet);
        if (skb == NULL) {
-               lp->stats.rx_dropped++;
+               drop_skb->dev = dev;
+               /* Read a packet into drop_skb and don't do anything with it. */
+               (*lp->read)(lp->fd, drop_skb, lp);
+               dev->stats.rx_dropped++;
                return 0;
        }
 
        skb->dev = dev;
-       skb_put(skb, dev->mtu);
+       skb_put(skb, lp->max_packet);
        skb_reset_mac_header(skb);
-       pkt_len = (*lp->read)(lp->fd, &skb, lp);
+       pkt_len = (*lp->read)(lp->fd, skb, lp);
 
        if (pkt_len > 0) {
                skb_trim(skb, pkt_len);
                skb->protocol = (*lp->protocol)(skb);
-               netif_rx(skb);
 
-               lp->stats.rx_bytes += skb->len;
-               lp->stats.rx_packets++;
+               dev->stats.rx_bytes += skb->len;
+               dev->stats.rx_packets++;
+               netif_rx(skb);
                return pkt_len;
        }
 
@@ -73,10 +117,10 @@ static void uml_dev_close(struct work_struct *work)
        dev_close(lp->dev);
 }
 
-irqreturn_t uml_net_interrupt(int irq, void *dev_id)
+static irqreturn_t uml_net_interrupt(int irq, void *dev_id)
 {
        struct net_device *dev = dev_id;
-       struct uml_net_private *lp = dev->priv;
+       struct uml_net_private *lp = netdev_priv(dev);
        int err;
 
        if (!netif_running(dev))
@@ -107,7 +151,7 @@ out:
 
 static int uml_net_open(struct net_device *dev)
 {
-       struct uml_net_private *lp = dev->priv;
+       struct uml_net_private *lp = netdev_priv(dev);
        int err;
 
        if (lp->fd >= 0) {
@@ -152,7 +196,7 @@ out:
 
 static int uml_net_close(struct net_device *dev)
 {
-       struct uml_net_private *lp = dev->priv;
+       struct uml_net_private *lp = netdev_priv(dev);
 
        netif_stop_queue(dev);
 
@@ -170,7 +214,7 @@ static int uml_net_close(struct net_device *dev)
 
 static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
 {
-       struct uml_net_private *lp = dev->priv;
+       struct uml_net_private *lp = netdev_priv(dev);
        unsigned long flags;
        int len;
 
@@ -178,11 +222,11 @@ static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
 
        spin_lock_irqsave(&lp->lock, flags);
 
-       len = (*lp->write)(lp->fd, &skb, lp);
+       len = (*lp->write)(lp->fd, skb, lp);
 
        if (len == skb->len) {
-               lp->stats.tx_packets++;
-               lp->stats.tx_bytes += skb->len;
+               dev->stats.tx_packets++;
+               dev->stats.tx_bytes += skb->len;
                dev->trans_start = jiffies;
                netif_start_queue(dev);
 
@@ -191,7 +235,7 @@ static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
        }
        else if (len == 0) {
                netif_start_queue(dev);
-               lp->stats.tx_dropped++;
+               dev->stats.tx_dropped++;
        }
        else {
                netif_start_queue(dev);
@@ -202,22 +246,12 @@ static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
 
        dev_kfree_skb(skb);
 
-       return 0;
-}
-
-static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
-{
-       struct uml_net_private *lp = dev->priv;
-       return &lp->stats;
+       return NETDEV_TX_OK;
 }
 
 static void uml_net_set_multicast_list(struct net_device *dev)
 {
-       if (dev->flags & IFF_PROMISC)
-               return;
-       else if (dev->mc_count)
-               dev->flags |= IFF_ALLMULTI;
-       else dev->flags &= ~IFF_ALLMULTI;
+       return;
 }
 
 static void uml_net_tx_timeout(struct net_device *dev)
@@ -228,7 +262,7 @@ static void uml_net_tx_timeout(struct net_device *dev)
 
 static int uml_net_set_mac(struct net_device *dev, void *addr)
 {
-       struct uml_net_private *lp = dev->priv;
+       struct uml_net_private *lp = netdev_priv(dev);
        struct sockaddr *hwaddr = addr;
 
        spin_lock_irq(&lp->lock);
@@ -240,22 +274,9 @@ static int uml_net_set_mac(struct net_device *dev, void *addr)
 
 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_irq(&lp->lock);
-
-       new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
-       if (new_mtu < 0) {
-               err = new_mtu;
-               goto out;
-       }
-
        dev->mtu = new_mtu;
 
- out:
-       spin_unlock_irq(&lp->lock);
-       return err;
+       return 0;
 }
 
 static void uml_net_get_drvinfo(struct net_device *dev,
@@ -265,12 +286,12 @@ static void uml_net_get_drvinfo(struct net_device *dev,
        strcpy(info->version, "42");
 }
 
-static struct ethtool_ops uml_net_ethtool_ops = {
+static const 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)
+static void uml_net_user_timer_expire(unsigned long _conn)
 {
 #ifdef undef
        struct connection *conn = (struct connection *)_conn;
@@ -288,7 +309,7 @@ static void setup_etheraddr(char *str, unsigned char *addr, char *name)
        if (str == NULL)
                goto random;
 
-       for (i = 0;i < 6; i++) {
+       for (i = 0; i < 6; i++) {
                addr[i] = simple_strtoul(str, &end, 16);
                if ((end == str) ||
                   ((*end != ':') && (*end != ',') && (*end != '\0'))) {
@@ -313,14 +334,13 @@ static void setup_etheraddr(char *str, unsigned char *addr, char *name)
        }
        if (!is_local_ether_addr(addr)) {
                printk(KERN_WARNING
-                      "Warning: attempt to assign a globally valid ethernet "
+                      "Warning: Assigning a globally valid ethernet "
                       "address to a device\n");
-               printk(KERN_WARNING "You should better enable the 2nd "
-                      "rightmost bit in the first byte of the MAC,\n");
+               printk(KERN_WARNING "You should set the 2nd rightmost bit in "
+                      "the first byte of the MAC,\n");
                printk(KERN_WARNING "i.e. %02x:%02x:%02x:%02x:%02x:%02x\n",
                       addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4],
                       addr[5]);
-               goto random;
        }
        return;
 
@@ -338,13 +358,12 @@ static struct platform_driver uml_net_driver = {
                .name  = DRIVER_NAME,
        },
 };
-static int driver_registered;
 
 static void net_device_release(struct device *dev)
 {
-       struct uml_net *device = dev->driver_data;
+       struct uml_net *device = dev_get_drvdata(dev);
        struct net_device *netdev = device->dev;
-       struct uml_net_private *lp = netdev->priv;
+       struct uml_net_private *lp = netdev_priv(netdev);
 
        if (lp->remove != NULL)
                (*lp->remove)(&lp->user);
@@ -353,6 +372,24 @@ static void net_device_release(struct device *dev)
        free_netdev(netdev);
 }
 
+static const struct net_device_ops uml_netdev_ops = {
+       .ndo_open               = uml_net_open,
+       .ndo_stop               = uml_net_close,
+       .ndo_start_xmit         = uml_net_start_xmit,
+       .ndo_set_multicast_list = uml_net_set_multicast_list,
+       .ndo_tx_timeout         = uml_net_tx_timeout,
+       .ndo_set_mac_address    = uml_net_set_mac,
+       .ndo_change_mtu         = uml_net_change_mtu,
+       .ndo_set_mac_address    = eth_mac_addr,
+       .ndo_validate_addr      = eth_validate_addr,
+};
+
+/*
+ * Ensures that platform_driver_register is called only once by
+ * eth_configure.  Will be set in an initcall.
+ */
+static int driver_registered;
+
 static void eth_configure(int n, void *init, char *mac,
                          struct transport *transport)
 {
@@ -388,14 +425,9 @@ static void eth_configure(int n, void *init, char *mac,
 
        setup_etheraddr(mac, device->mac, dev->name);
 
-       printk(KERN_INFO "Netdevice %d ", n);
-       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(": ");
+       printk(KERN_INFO "Netdevice %d (%pM) : ", n, device->mac);
 
-       lp = dev->priv;
+       lp = netdev_priv(dev);
        /* 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);
@@ -409,7 +441,7 @@ static void eth_configure(int n, void *init, char *mac,
        device->pdev.id = n;
        device->pdev.name = DRIVER_NAME;
        device->pdev.dev.release = net_device_release;
-       device->pdev.dev.driver_data = device;
+       dev_set_drvdata(&device->pdev.dev, device);
        if (platform_device_register(&device->pdev))
                goto out_free_netdev;
        SET_NETDEV_DEV(dev,&device->pdev.dev);
@@ -427,6 +459,7 @@ static void eth_configure(int n, void *init, char *mac,
                  .dev                  = dev,
                  .fd                   = -1,
                  .mac                  = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
+                 .max_packet           = transport->user->max_packet,
                  .protocol             = transport->kern->protocol,
                  .open                 = transport->user->open,
                  .close                = transport->user->close,
@@ -434,8 +467,7 @@ static void eth_configure(int n, void *init, char *mac,
                  .read                 = transport->kern->read,
                  .write                = transport->kern->write,
                  .add_address          = transport->user->add_address,
-                 .delete_address       = transport->user->delete_address,
-                 .set_mtu              = transport->user->set_mtu });
+                 .delete_address       = transport->user->delete_address });
 
        init_timer(&lp->tl);
        spin_lock_init(&lp->lock);
@@ -447,19 +479,16 @@ static void eth_configure(int n, void *init, char *mac,
                goto out_unregister;
 
        set_ether_mac(dev, device->mac);
-       dev->mtu = transport->user->max_packet;
-       dev->open = uml_net_open;
-       dev->hard_start_xmit = uml_net_start_xmit;
-       dev->stop = uml_net_close;
-       dev->get_stats = uml_net_get_stats;
-       dev->set_multicast_list = uml_net_set_multicast_list;
-       dev->tx_timeout = uml_net_tx_timeout;
-       dev->set_mac_address = uml_net_set_mac;
-       dev->change_mtu = uml_net_change_mtu;
+       dev->mtu = transport->user->mtu;
+       dev->netdev_ops = &uml_netdev_ops;
        dev->ethtool_ops = &uml_net_ethtool_ops;
        dev->watchdog_timeo = (HZ >> 1);
        dev->irq = UM_ETH_IRQ;
 
+       err = update_drop_skb(lp->max_packet);
+       if (err)
+               goto out_undo_user_init;
+
        rtnl_lock();
        err = register_netdevice(dev);
        rtnl_unlock();
@@ -505,7 +534,7 @@ static int eth_parse(char *str, int *index_out, char **str_out,
                     char **error_out)
 {
        char *end;
-       int n, err = -EINVAL;;
+       int n, err = -EINVAL;
 
        n = simple_strtoul(str, &end, 0);
        if (end == str) {
@@ -701,7 +730,7 @@ static int net_remove(int n, char **error_out)
                return -ENODEV;
 
        dev = device->dev;
-       lp = dev->priv;
+       lp = netdev_priv(dev);
        if (lp->fd > 0)
                return -EBUSY;
        unregister_netdev(dev);
@@ -719,6 +748,7 @@ static struct mc_device net_mc = {
        .remove         = net_remove,
 };
 
+#ifdef CONFIG_INET
 static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
                              void *ptr)
 {
@@ -728,10 +758,10 @@ static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
        void (*proc)(unsigned char *, unsigned char *, void *);
        unsigned char addr_buf[4], netmask_buf[4];
 
-       if (dev->open != uml_net_open)
+       if (dev->netdev_ops->ndo_open != uml_net_open)
                return NOTIFY_DONE;
 
-       lp = dev->priv;
+       lp = netdev_priv(dev);
 
        proc = NULL;
        switch (event) {
@@ -751,18 +781,17 @@ static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
 }
 
 /* uml_net_init shouldn't be called twice on two CPUs at the same time */
-struct notifier_block uml_inetaddr_notifier = {
+static struct notifier_block uml_inetaddr_notifier = {
        .notifier_call          = uml_inetaddr_event,
 };
 
-static int uml_net_init(void)
+static void inet_register(void)
 {
        struct list_head *ele;
        struct uml_net_private *lp;
        struct in_device *ip;
        struct in_ifaddr *in;
 
-       mconsole_register_dev(&net_mc);
        register_inetaddr_notifier(&uml_inetaddr_notifier);
 
        /* Devices may have been opened already, so the uml_inetaddr_notifier
@@ -782,7 +811,17 @@ static int uml_net_init(void)
                }
        }
        spin_unlock(&opened_lock);
+}
+#else
+static inline void inet_register(void)
+{
+}
+#endif
 
+static int uml_net_init(void)
+{
+       mconsole_register_dev(&net_mc);
+       inet_register();
        return 0;
 }
 
@@ -807,19 +846,6 @@ static void close_devices(void)
 
 __uml_exitcall(close_devices);
 
-struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
-{
-       if ((skb != NULL) && (skb_tailroom(skb) < extra)) {
-               struct sk_buff *skb2;
-
-               skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
-               dev_kfree_skb(skb);
-               skb = skb2;
-       }
-       if (skb != NULL) skb_put(skb, extra);
-       return skb;
-}
-
 void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
                                        void *),
                    void *arg)