virtio_net: invoke softirqs after __napi_schedule
[linux-flexiantxendom0-3.2.10.git] / drivers / net / virtio_net.c
index b8225f3..cbefe67 100644 (file)
@@ -30,7 +30,7 @@
 static int napi_weight = 128;
 module_param(napi_weight, int, 0444);
 
-static int csum = 1, gso = 1;
+static bool csum = true, gso = true;
 module_param(csum, bool, 0444);
 module_param(gso, bool, 0444);
 
@@ -39,6 +39,7 @@ module_param(gso, bool, 0444);
 #define GOOD_COPY_LEN  128
 
 #define VIRTNET_SEND_COMMAND_SG_MAX    2
+#define VIRTNET_DRIVER_VERSION "1.0.0"
 
 struct virtnet_stats {
        struct u64_stats_sync syncp;
@@ -143,20 +144,19 @@ static void skb_xmit_done(struct virtqueue *svq)
 static void set_skb_frag(struct sk_buff *skb, struct page *page,
                         unsigned int offset, unsigned int *len)
 {
+       int size = min((unsigned)PAGE_SIZE - offset, *len);
        int i = skb_shinfo(skb)->nr_frags;
-       skb_frag_t *f;
 
-       f = &skb_shinfo(skb)->frags[i];
-       f->size = min((unsigned)PAGE_SIZE - offset, *len);
-       f->page_offset = offset;
-       __skb_frag_set_page(f, page);
+       __skb_fill_page_desc(skb, i, page, offset, size);
 
-       skb->data_len += f->size;
-       skb->len += f->size;
+       skb->data_len += size;
+       skb->len += size;
+       skb->truesize += PAGE_SIZE;
        skb_shinfo(skb)->nr_frags++;
-       *len -= f->size;
+       *len -= size;
 }
 
+/* Called from bottom half context */
 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
                                   struct page *page, unsigned int len)
 {
@@ -255,7 +255,7 @@ static int receive_mergeable(struct virtnet_info *vi, struct sk_buff *skb)
 static void receive_buf(struct net_device *dev, void *buf, unsigned int len)
 {
        struct virtnet_info *vi = netdev_priv(dev);
-       struct virtnet_stats __percpu *stats = this_cpu_ptr(vi->stats);
+       struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
        struct sk_buff *skb;
        struct page *page;
        struct skb_vnet_hdr *hdr;
@@ -290,7 +290,6 @@ static void receive_buf(struct net_device *dev, void *buf, unsigned int len)
        }
 
        hdr = skb_vnet_hdr(skb);
-       skb->truesize += skb->data_len;
 
        u64_stats_update_begin(&stats->syncp);
        stats->rx_bytes += skb->len;
@@ -360,7 +359,7 @@ static int add_recvbuf_small(struct virtnet_info *vi, gfp_t gfp)
        struct skb_vnet_hdr *hdr;
        int err;
 
-       skb = netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN);
+       skb = __netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN, gfp);
        if (unlikely(!skb))
                return -ENOMEM;
 
@@ -371,7 +370,7 @@ static int add_recvbuf_small(struct virtnet_info *vi, gfp_t gfp)
 
        skb_to_sgvec(skb, vi->rx_sg + 1, 0, skb->len);
 
-       err = virtqueue_add_buf_gfp(vi->rvq, vi->rx_sg, 0, 2, skb, gfp);
+       err = virtqueue_add_buf(vi->rvq, vi->rx_sg, 0, 2, skb, gfp);
        if (err < 0)
                dev_kfree_skb(skb);
 
@@ -416,8 +415,8 @@ static int add_recvbuf_big(struct virtnet_info *vi, gfp_t gfp)
 
        /* chain first in list head */
        first->private = (unsigned long)list;
-       err = virtqueue_add_buf_gfp(vi->rvq, vi->rx_sg, 0, MAX_SKB_FRAGS + 2,
-                                   first, gfp);
+       err = virtqueue_add_buf(vi->rvq, vi->rx_sg, 0, MAX_SKB_FRAGS + 2,
+                               first, gfp);
        if (err < 0)
                give_pages(vi, first);
 
@@ -435,14 +434,20 @@ static int add_recvbuf_mergeable(struct virtnet_info *vi, gfp_t gfp)
 
        sg_init_one(vi->rx_sg, page_address(page), PAGE_SIZE);
 
-       err = virtqueue_add_buf_gfp(vi->rvq, vi->rx_sg, 0, 1, page, gfp);
+       err = virtqueue_add_buf(vi->rvq, vi->rx_sg, 0, 1, page, gfp);
        if (err < 0)
                give_pages(vi, page);
 
        return err;
 }
 
-/* Returns false if we couldn't fill entirely (OOM). */
+/*
+ * Returns false if we couldn't fill entirely (OOM).
+ *
+ * Normally run in the receive path, but can also be run from ndo_open
+ * before we're receiving packets, or from refill_work which is
+ * careful to disable receiving (using napi_disable).
+ */
 static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
 {
        int err;
@@ -487,7 +492,9 @@ static void virtnet_napi_enable(struct virtnet_info *vi)
         * We synchronize against interrupts via NAPI_STATE_SCHED */
        if (napi_schedule_prep(&vi->napi)) {
                virtqueue_disable_cb(vi->rvq);
+               local_bh_disable();
                __napi_schedule(&vi->napi);
+               local_bh_enable();
        }
 }
 
@@ -504,7 +511,7 @@ static void refill_work(struct work_struct *work)
        /* In theory, this can happen: if we don't get any buffers in
         * we will *never* try to fill again. */
        if (still_empty)
-               schedule_delayed_work(&vi->refill, HZ/2);
+               queue_delayed_work(system_nrt_wq, &vi->refill, HZ/2);
 }
 
 static int virtnet_poll(struct napi_struct *napi, int budget)
@@ -523,7 +530,7 @@ again:
 
        if (vi->num < vi->max / 2) {
                if (!try_fill_recv(vi, GFP_ATOMIC))
-                       schedule_delayed_work(&vi->refill, 0);
+                       queue_delayed_work(system_nrt_wq, &vi->refill, 0);
        }
 
        /* Out of packets? */
@@ -544,7 +551,7 @@ static unsigned int free_old_xmit_skbs(struct virtnet_info *vi)
 {
        struct sk_buff *skb;
        unsigned int len, tot_sgs = 0;
-       struct virtnet_stats __percpu *stats = this_cpu_ptr(vi->stats);
+       struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
 
        while ((skb = virtqueue_get_buf(vi->svq, &len)) != NULL) {
                pr_debug("Sent skb %p\n", skb);
@@ -604,7 +611,7 @@ static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
 
        hdr->num_sg = skb_to_sgvec(skb, vi->tx_sg + 1, 0, skb->len) + 1;
        return virtqueue_add_buf(vi->svq, vi->tx_sg, hdr->num_sg,
-                                       0, skb);
+                                0, skb, GFP_ATOMIC);
 }
 
 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
@@ -620,16 +627,16 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
 
        /* This can happen with OOM and indirect buffers. */
        if (unlikely(capacity < 0)) {
-               if (net_ratelimit()) {
-                       if (likely(capacity == -ENOMEM)) {
+               if (likely(capacity == -ENOMEM)) {
+                       if (net_ratelimit())
                                dev_warn(&dev->dev,
                                         "TX queue failure: out of memory\n");
-                       } else {
-                               dev->stats.tx_fifo_errors++;
+               } else {
+                       dev->stats.tx_fifo_errors++;
+                       if (net_ratelimit())
                                dev_warn(&dev->dev,
                                         "Unexpected TX queue failure: %d\n",
                                         capacity);
-                       }
                }
                dev->stats.tx_dropped++;
                kfree_skb(skb);
@@ -683,8 +690,7 @@ static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
        unsigned int start;
 
        for_each_possible_cpu(cpu) {
-               struct virtnet_stats __percpu *stats
-                       = per_cpu_ptr(vi->stats, cpu);
+               struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
                u64 tpackets, tbytes, rpackets, rbytes;
 
                do {
@@ -702,6 +708,7 @@ static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
        }
 
        tot->tx_dropped = dev->stats.tx_dropped;
+       tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
        tot->rx_dropped = dev->stats.rx_dropped;
        tot->rx_length_errors = dev->stats.rx_length_errors;
        tot->rx_frame_errors = dev->stats.rx_frame_errors;
@@ -722,6 +729,10 @@ static int virtnet_open(struct net_device *dev)
 {
        struct virtnet_info *vi = netdev_priv(dev);
 
+       /* Make sure we have some buffers: if oom use wq. */
+       if (!try_fill_recv(vi, GFP_KERNEL))
+               queue_delayed_work(system_nrt_wq, &vi->refill, 0);
+
        virtnet_napi_enable(vi);
        return 0;
 }
@@ -757,7 +768,7 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
                sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
        sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
 
-       BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi) < 0);
+       BUG_ON(virtqueue_add_buf(vi->cvq, sg, out, in, vi, GFP_ATOMIC) < 0);
 
        virtqueue_kick(vi->cvq);
 
@@ -775,6 +786,8 @@ static int virtnet_close(struct net_device *dev)
 {
        struct virtnet_info *vi = netdev_priv(dev);
 
+       /* Make sure refill_work doesn't re-enable napi! */
+       cancel_delayed_work_sync(&vi->refill);
        napi_disable(&vi->napi);
 
        return 0;
@@ -856,7 +869,7 @@ static void virtnet_set_rx_mode(struct net_device *dev)
        kfree(buf);
 }
 
-static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
+static int virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
 {
        struct virtnet_info *vi = netdev_priv(dev);
        struct scatterlist sg;
@@ -866,9 +879,10 @@ static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
        if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
                                  VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
                dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
+       return 0;
 }
 
-static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
+static int virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
 {
        struct virtnet_info *vi = netdev_priv(dev);
        struct scatterlist sg;
@@ -878,10 +892,38 @@ static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
        if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
                                  VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
                dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
+       return 0;
+}
+
+static void virtnet_get_ringparam(struct net_device *dev,
+                               struct ethtool_ringparam *ring)
+{
+       struct virtnet_info *vi = netdev_priv(dev);
+
+       ring->rx_max_pending = virtqueue_get_vring_size(vi->rvq);
+       ring->tx_max_pending = virtqueue_get_vring_size(vi->svq);
+       ring->rx_pending = ring->rx_max_pending;
+       ring->tx_pending = ring->tx_max_pending;
+
+}
+
+
+static void virtnet_get_drvinfo(struct net_device *dev,
+                               struct ethtool_drvinfo *info)
+{
+       struct virtnet_info *vi = netdev_priv(dev);
+       struct virtio_device *vdev = vi->vdev;
+
+       strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
+       strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
+       strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
+
 }
 
 static const struct ethtool_ops virtnet_ethtool_ops = {
+       .get_drvinfo = virtnet_get_drvinfo,
        .get_link = ethtool_op_get_link,
+       .get_ringparam = virtnet_get_ringparam,
 };
 
 #define MIN_MTU 68
@@ -915,12 +957,10 @@ static void virtnet_update_status(struct virtnet_info *vi)
 {
        u16 v;
 
-       if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
-               return;
-
-       vi->vdev->config->get(vi->vdev,
+       if (virtio_config_val(vi->vdev, VIRTIO_NET_F_STATUS,
                              offsetof(struct virtio_net_config, status),
-                             &v, sizeof(v));
+                             &v) < 0)
+               return;
 
        /* Ignore unknown (future) status bits */
        v &= VIRTIO_NET_S_LINK_UP;
@@ -946,15 +986,38 @@ static void virtnet_config_changed(struct virtio_device *vdev)
        virtnet_update_status(vi);
 }
 
+static int init_vqs(struct virtnet_info *vi)
+{
+       struct virtqueue *vqs[3];
+       vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
+       const char *names[] = { "input", "output", "control" };
+       int nvqs, err;
+
+       /* We expect two virtqueues, receive then send,
+        * and optionally control. */
+       nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
+
+       err = vi->vdev->config->find_vqs(vi->vdev, nvqs, vqs, callbacks, names);
+       if (err)
+               return err;
+
+       vi->rvq = vqs[0];
+       vi->svq = vqs[1];
+
+       if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
+               vi->cvq = vqs[2];
+
+               if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
+                       vi->dev->features |= NETIF_F_HW_VLAN_FILTER;
+       }
+       return 0;
+}
+
 static int virtnet_probe(struct virtio_device *vdev)
 {
        int err;
        struct net_device *dev;
        struct virtnet_info *vi;
-       struct virtqueue *vqs[3];
-       vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
-       const char *names[] = { "input", "output", "control" };
-       int nvqs;
 
        /* Allocate ourselves a network device with room for our info */
        dev = alloc_etherdev(sizeof(struct virtnet_info));
@@ -996,12 +1059,10 @@ static int virtnet_probe(struct virtio_device *vdev)
        }
 
        /* Configuration may specify what MAC to use.  Otherwise random. */
-       if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
-               vdev->config->get(vdev,
+       if (virtio_config_val_len(vdev, VIRTIO_NET_F_MAC,
                                  offsetof(struct virtio_net_config, mac),
-                                 dev->dev_addr, dev->addr_len);
-       } else
-               random_ether_addr(dev->dev_addr);
+                                 dev->dev_addr, dev->addr_len) < 0)
+               eth_hw_addr_random(dev);
 
        /* Set up our device-specific information */
        vi = netdev_priv(dev);
@@ -1028,24 +1089,10 @@ static int virtnet_probe(struct virtio_device *vdev)
        if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
                vi->mergeable_rx_bufs = true;
 
-       /* We expect two virtqueues, receive then send,
-        * and optionally control. */
-       nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
-
-       err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
+       err = init_vqs(vi);
        if (err)
                goto free_stats;
 
-       vi->rvq = vqs[0];
-       vi->svq = vqs[1];
-
-       if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
-               vi->cvq = vqs[2];
-
-               if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
-                       dev->features |= NETIF_F_HW_VLAN_FILTER;
-       }
-
        err = register_netdev(dev);
        if (err) {
                pr_debug("virtio_net: registering device failed\n");
@@ -1076,7 +1123,6 @@ static int virtnet_probe(struct virtio_device *vdev)
 
 unregister:
        unregister_netdev(dev);
-       cancel_delayed_work_sync(&vi->refill);
 free_vqs:
        vdev->config->del_vqs(vdev);
 free_stats:
@@ -1108,29 +1154,73 @@ static void free_unused_bufs(struct virtnet_info *vi)
        BUG_ON(vi->num != 0);
 }
 
-static void __devexit virtnet_remove(struct virtio_device *vdev)
+static void remove_vq_common(struct virtnet_info *vi)
 {
-       struct virtnet_info *vi = vdev->priv;
-
-       /* Stop all the virtqueues. */
-       vdev->config->reset(vdev);
-
-
-       unregister_netdev(vi->dev);
-       cancel_delayed_work_sync(&vi->refill);
+       vi->vdev->config->reset(vi->vdev);
 
        /* Free unused buffers in both send and recv, if any. */
        free_unused_bufs(vi);
 
-       vdev->config->del_vqs(vi->vdev);
+       vi->vdev->config->del_vqs(vi->vdev);
 
        while (vi->pages)
                __free_pages(get_a_page(vi, GFP_KERNEL), 0);
+}
+
+static void __devexit virtnet_remove(struct virtio_device *vdev)
+{
+       struct virtnet_info *vi = vdev->priv;
+
+       unregister_netdev(vi->dev);
+
+       remove_vq_common(vi);
 
        free_percpu(vi->stats);
        free_netdev(vi->dev);
 }
 
+#ifdef CONFIG_PM
+static int virtnet_freeze(struct virtio_device *vdev)
+{
+       struct virtnet_info *vi = vdev->priv;
+
+       virtqueue_disable_cb(vi->rvq);
+       virtqueue_disable_cb(vi->svq);
+       if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ))
+               virtqueue_disable_cb(vi->cvq);
+
+       netif_device_detach(vi->dev);
+       cancel_delayed_work_sync(&vi->refill);
+
+       if (netif_running(vi->dev))
+               napi_disable(&vi->napi);
+
+       remove_vq_common(vi);
+
+       return 0;
+}
+
+static int virtnet_restore(struct virtio_device *vdev)
+{
+       struct virtnet_info *vi = vdev->priv;
+       int err;
+
+       err = init_vqs(vi);
+       if (err)
+               return err;
+
+       if (netif_running(vi->dev))
+               virtnet_napi_enable(vi);
+
+       netif_device_attach(vi->dev);
+
+       if (!try_fill_recv(vi, GFP_KERNEL))
+               queue_delayed_work(system_nrt_wq, &vi->refill, 0);
+
+       return 0;
+}
+#endif
+
 static struct virtio_device_id id_table[] = {
        { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
        { 0 },
@@ -1155,6 +1245,10 @@ static struct virtio_driver virtio_net_driver = {
        .probe =        virtnet_probe,
        .remove =       __devexit_p(virtnet_remove),
        .config_changed = virtnet_config_changed,
+#ifdef CONFIG_PM
+       .freeze =       virtnet_freeze,
+       .restore =      virtnet_restore,
+#endif
 };
 
 static int __init init(void)