SUNRPC: rpc_pipefs cleanup
[safe/jmp/linux-2.6] / drivers / net / virtio_net.c
index daab9c9..2a6e81d 100644 (file)
@@ -283,10 +283,11 @@ static void try_fill_recv_maxbufs(struct virtnet_info *vi)
        for (;;) {
                struct virtio_net_hdr *hdr;
 
-               skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
+               skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN + NET_IP_ALIGN);
                if (unlikely(!skb))
                        break;
 
+               skb_reserve(skb, NET_IP_ALIGN);
                skb_put(skb, MAX_PACKET_LEN);
 
                hdr = skb_vnet_hdr(skb);
@@ -470,7 +471,7 @@ static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
        }
 
        if (skb_is_gso(skb)) {
-               hdr->hdr_len = skb_transport_header(skb) - skb->data;
+               hdr->hdr_len = skb_headlen(skb);
                hdr->gso_size = skb_shinfo(skb)->gso_size;
                if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
                        hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
@@ -565,6 +566,23 @@ stop_queue:
        goto done;
 }
 
+static int virtnet_set_mac_address(struct net_device *dev, void *p)
+{
+       struct virtnet_info *vi = netdev_priv(dev);
+       struct virtio_device *vdev = vi->vdev;
+       int ret;
+
+       ret = eth_mac_addr(dev, p);
+       if (ret)
+               return ret;
+
+       if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
+               vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
+                                 dev->dev_addr, dev->addr_len);
+
+       return 0;
+}
+
 #ifdef CONFIG_NET_POLL_CONTROLLER
 static void virtnet_netpoll(struct net_device *dev)
 {
@@ -599,17 +617,15 @@ static int virtnet_open(struct net_device *dev)
 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
                                 struct scatterlist *data, int out, int in)
 {
-       struct scatterlist sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
+       struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
        struct virtio_net_ctrl_hdr ctrl;
        virtio_net_ctrl_ack status = ~0;
        unsigned int tmp;
+       int i;
 
-       if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
-               BUG();  /* Caller should know better */
-               return false;
-       }
-
-       BUG_ON(out + in > VIRTNET_SEND_COMMAND_SG_MAX);
+       /* Caller should know better */
+       BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
+               (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
 
        out++; /* Add header */
        in++; /* Add return status */
@@ -620,11 +636,11 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
        sg_init_table(sg, out + in);
 
        sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
-       memcpy(&sg[1], data, sizeof(struct scatterlist) * (out + in - 2));
+       for_each_sg(data, s, out + in - 2, i)
+               sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
        sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
 
-       if (vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) != 0)
-               BUG();
+       BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi));
 
        vi->cvq->vq_ops->kick(vi->cvq);
 
@@ -665,6 +681,7 @@ static void virtnet_set_rx_mode(struct net_device *dev)
        u8 promisc, allmulti;
        struct virtio_net_ctrl_mac *mac_data;
        struct dev_addr_list *addr;
+       struct netdev_hw_addr *ha;
        void *buf;
        int i;
 
@@ -675,7 +692,7 @@ static void virtnet_set_rx_mode(struct net_device *dev)
        promisc = ((dev->flags & IFF_PROMISC) != 0);
        allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
 
-       sg_set_buf(sg, &promisc, sizeof(promisc));
+       sg_init_one(sg, &promisc, sizeof(promisc));
 
        if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
                                  VIRTIO_NET_CTRL_RX_PROMISC,
@@ -683,7 +700,7 @@ static void virtnet_set_rx_mode(struct net_device *dev)
                dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
                         promisc ? "en" : "dis");
 
-       sg_set_buf(sg, &allmulti, sizeof(allmulti));
+       sg_init_one(sg, &allmulti, sizeof(allmulti));
 
        if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
                                  VIRTIO_NET_CTRL_RX_ALLMULTI,
@@ -692,24 +709,26 @@ static void virtnet_set_rx_mode(struct net_device *dev)
                         allmulti ? "en" : "dis");
 
        /* MAC filter - use one buffer for both lists */
-       mac_data = buf = kzalloc(((dev->uc_count + dev->mc_count) * ETH_ALEN) +
+       mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) +
                                 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
        if (!buf) {
                dev_warn(&dev->dev, "No memory for MAC address buffer\n");
                return;
        }
 
+       sg_init_table(sg, 2);
+
        /* Store the unicast list and count in the front of the buffer */
-       mac_data->entries = dev->uc_count;
-       addr = dev->uc_list;
-       for (i = 0; i < dev->uc_count; i++, addr = addr->next)
-               memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
+       mac_data->entries = dev->uc.count;
+       i = 0;
+       list_for_each_entry(ha, &dev->uc.list, list)
+               memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
 
        sg_set_buf(&sg[0], mac_data,
-                  sizeof(mac_data->entries) + (dev->uc_count * ETH_ALEN));
+                  sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN));
 
        /* multicast list and count fill the end */
-       mac_data = (void *)&mac_data->macs[dev->uc_count][0];
+       mac_data = (void *)&mac_data->macs[dev->uc.count][0];
 
        mac_data->entries = dev->mc_count;
        addr = dev->mc_list;
@@ -727,6 +746,30 @@ 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)
+{
+       struct virtnet_info *vi = netdev_priv(dev);
+       struct scatterlist sg;
+
+       sg_init_one(&sg, &vid, sizeof(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);
+}
+
+static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
+{
+       struct virtnet_info *vi = netdev_priv(dev);
+       struct scatterlist sg;
+
+       sg_init_one(&sg, &vid, sizeof(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);
+}
+
 static struct ethtool_ops virtnet_ethtool_ops = {
        .set_tx_csum = virtnet_set_tx_csum,
        .set_sg = ethtool_op_set_sg,
@@ -750,9 +793,11 @@ static const struct net_device_ops virtnet_netdev = {
        .ndo_stop            = virtnet_close,
        .ndo_start_xmit      = start_xmit,
        .ndo_validate_addr   = eth_validate_addr,
-       .ndo_set_mac_address = eth_mac_addr,
+       .ndo_set_mac_address = virtnet_set_mac_address,
        .ndo_set_rx_mode     = virtnet_set_rx_mode,
        .ndo_change_mtu      = virtnet_change_mtu,
+       .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
+       .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
 #ifdef CONFIG_NET_POLL_CONTROLLER
        .ndo_poll_controller = virtnet_netpoll,
 #endif
@@ -798,6 +843,10 @@ 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));
@@ -858,25 +907,22 @@ 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. */
-       vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
-       if (IS_ERR(vi->rvq)) {
-               err = PTR_ERR(vi->rvq);
+       /* 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);
+       if (err)
                goto free;
-       }
 
-       vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
-       if (IS_ERR(vi->svq)) {
-               err = PTR_ERR(vi->svq);
-               goto free_recv;
-       }
+       vi->rvq = vqs[0];
+       vi->svq = vqs[1];
 
        if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
-               vi->cvq = vdev->config->find_vq(vdev, 2, NULL);
-               if (IS_ERR(vi->cvq)) {
-                       err = PTR_ERR(vi->svq);
-                       goto free_send;
-               }
+               vi->cvq = vqs[2];
+
+               if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
+                       dev->features |= NETIF_F_HW_VLAN_FILTER;
        }
 
        /* Initialize our empty receive and send queues. */
@@ -891,7 +937,7 @@ static int virtnet_probe(struct virtio_device *vdev)
        err = register_netdev(dev);
        if (err) {
                pr_debug("virtio_net: registering device failed\n");
-               goto free_ctrl;
+               goto free_vqs;
        }
 
        /* Last of all, set up some receive buffers. */
@@ -905,19 +951,15 @@ static int virtnet_probe(struct virtio_device *vdev)
 
        vi->status = VIRTIO_NET_S_LINK_UP;
        virtnet_update_status(vi);
+       netif_carrier_on(dev);
 
        pr_debug("virtnet: registered device %s\n", dev->name);
        return 0;
 
 unregister:
        unregister_netdev(dev);
-free_ctrl:
-       if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ))
-               vdev->config->del_vq(vi->cvq);
-free_send:
-       vdev->config->del_vq(vi->svq);
-free_recv:
-       vdev->config->del_vq(vi->rvq);
+free_vqs:
+       vdev->config->del_vqs(vdev);
 free:
        free_netdev(dev);
        return err;
@@ -943,12 +985,10 @@ static void virtnet_remove(struct virtio_device *vdev)
 
        BUG_ON(vi->num != 0);
 
-       vdev->config->del_vq(vi->svq);
-       vdev->config->del_vq(vi->rvq);
-       if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ))
-               vdev->config->del_vq(vi->cvq);
        unregister_netdev(vi->dev);
 
+       vdev->config->del_vqs(vi->vdev);
+
        while (vi->pages)
                __free_pages(get_a_page(vi, GFP_KERNEL), 0);
 
@@ -967,7 +1007,7 @@ static unsigned int features[] = {
        VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
        VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
        VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
-       VIRTIO_NET_F_CTRL_RX,
+       VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
        VIRTIO_F_NOTIFY_ON_EMPTY,
 };