virtio-pci: do not oops on config change if driver not loaded
[safe/jmp/linux-2.6] / drivers / virtio / virtio_pci.c
index 192687e..330aacb 100644 (file)
@@ -37,7 +37,7 @@ struct virtio_pci_device
        struct pci_dev *pci_dev;
 
        /* the IO mapping for the PCI config space */
-       void *ioaddr;
+       void __iomem *ioaddr;
 
        /* a list of queues so we can dispatch IRQs */
        spinlock_t lock;
@@ -73,13 +73,7 @@ MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
 /* A PCI device has it's own struct device and so does a virtio device so
  * we create a place for the virtio devices to show up in sysfs.  I think it
  * would make more sense for virtio to not insist on having it's own device. */
-static struct device virtio_pci_root = {
-       .parent         = NULL,
-       .bus_id         = "virtio-pci",
-};
-
-/* Unique numbering for devices under the kvm root */
-static unsigned int dev_index;
+static struct device *virtio_pci_root;
 
 /* Convert a generic virtio device to our structure */
 static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
@@ -87,23 +81,27 @@ static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
        return container_of(vdev, struct virtio_pci_device, vdev);
 }
 
-/* virtio config->feature() implementation */
-static bool vp_feature(struct virtio_device *vdev, unsigned bit)
+/* virtio config->get_features() implementation */
+static u32 vp_get_features(struct virtio_device *vdev)
 {
        struct virtio_pci_device *vp_dev = to_vp_device(vdev);
-       u32 mask;
-
-       /* Since this function is supposed to have the side effect of
-        * enabling a queried feature, we simulate that by doing a read
-        * from the host feature bitmask and then writing to the guest
-        * feature bitmask */
-       mask = ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
-       if (mask & (1 << bit)) {
-               mask |= (1 << bit);
-               iowrite32(mask, vp_dev->ioaddr + VIRTIO_PCI_GUEST_FEATURES);
-       }
 
-       return !!(mask & (1 << bit));
+       /* When someone needs more than 32 feature bits, we'll need to
+        * steal a bit to indicate that the rest are somewhere else. */
+       return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
+}
+
+/* virtio config->finalize_features() implementation */
+static void vp_finalize_features(struct virtio_device *vdev)
+{
+       struct virtio_pci_device *vp_dev = to_vp_device(vdev);
+
+       /* Give virtio_ring a chance to accept features. */
+       vring_transport_features(vdev);
+
+       /* We only support 32 feature bits. */
+       BUILD_BUG_ON(ARRAY_SIZE(vdev->features) != 1);
+       iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES);
 }
 
 /* virtio config->get() implementation */
@@ -111,7 +109,7 @@ static void vp_get(struct virtio_device *vdev, unsigned offset,
                   void *buf, unsigned len)
 {
        struct virtio_pci_device *vp_dev = to_vp_device(vdev);
-       void *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
+       void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
        u8 *ptr = buf;
        int i;
 
@@ -125,7 +123,7 @@ static void vp_set(struct virtio_device *vdev, unsigned offset,
                   const void *buf, unsigned len)
 {
        struct virtio_pci_device *vp_dev = to_vp_device(vdev);
-       void *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
+       void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
        const u8 *ptr = buf;
        int i;
 
@@ -145,14 +143,14 @@ static void vp_set_status(struct virtio_device *vdev, u8 status)
        struct virtio_pci_device *vp_dev = to_vp_device(vdev);
        /* We should never be setting status to 0. */
        BUG_ON(status == 0);
-       return iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
+       iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
 }
 
 static void vp_reset(struct virtio_device *vdev)
 {
        struct virtio_pci_device *vp_dev = to_vp_device(vdev);
        /* 0 status means a reset. */
-       return iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
+       iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
 }
 
 /* the notify function used when creating a virt queue */
@@ -177,6 +175,7 @@ static irqreturn_t vp_interrupt(int irq, void *opaque)
        struct virtio_pci_device *vp_dev = opaque;
        struct virtio_pci_vq_info *info;
        irqreturn_t ret = IRQ_NONE;
+       unsigned long flags;
        u8 isr;
 
        /* reading the ISR has the effect of also clearing it so it's very
@@ -193,16 +192,16 @@ static irqreturn_t vp_interrupt(int irq, void *opaque)
                drv = container_of(vp_dev->vdev.dev.driver,
                                   struct virtio_driver, driver);
 
-               if (drv->config_changed)
+               if (drv && drv->config_changed)
                        drv->config_changed(&vp_dev->vdev);
        }
 
-       spin_lock(&vp_dev->lock);
+       spin_lock_irqsave(&vp_dev->lock, flags);
        list_for_each_entry(info, &vp_dev->virtqueues, node) {
                if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
                        ret = IRQ_HANDLED;
        }
-       spin_unlock(&vp_dev->lock);
+       spin_unlock_irqrestore(&vp_dev->lock, flags);
 
        return ret;
 }
@@ -214,6 +213,7 @@ static struct virtqueue *vp_find_vq(struct virtio_device *vdev, unsigned index,
        struct virtio_pci_device *vp_dev = to_vp_device(vdev);
        struct virtio_pci_vq_info *info;
        struct virtqueue *vq;
+       unsigned long flags, size;
        u16 num;
        int err;
 
@@ -234,19 +234,20 @@ static struct virtqueue *vp_find_vq(struct virtio_device *vdev, unsigned index,
        info->queue_index = index;
        info->num = num;
 
-       info->queue = kzalloc(PAGE_ALIGN(vring_size(num,PAGE_SIZE)), GFP_KERNEL);
+       size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
+       info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
        if (info->queue == NULL) {
                err = -ENOMEM;
                goto out_info;
        }
 
        /* activate the queue */
-       iowrite32(virt_to_phys(info->queue) >> PAGE_SHIFT,
+       iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
                  vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
 
        /* create the vring */
-       vq = vring_new_virtqueue(info->num, vdev, info->queue,
-                                vp_notify, callback);
+       vq = vring_new_virtqueue(info->num, VIRTIO_PCI_VRING_ALIGN,
+                                vdev, info->queue, vp_notify, callback);
        if (!vq) {
                err = -ENOMEM;
                goto out_activate_queue;
@@ -255,15 +256,15 @@ static struct virtqueue *vp_find_vq(struct virtio_device *vdev, unsigned index,
        vq->priv = info;
        info->vq = vq;
 
-       spin_lock(&vp_dev->lock);
+       spin_lock_irqsave(&vp_dev->lock, flags);
        list_add(&info->node, &vp_dev->virtqueues);
-       spin_unlock(&vp_dev->lock);
+       spin_unlock_irqrestore(&vp_dev->lock, flags);
 
        return vq;
 
 out_activate_queue:
        iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
-       kfree(info->queue);
+       free_pages_exact(info->queue, size);
 out_info:
        kfree(info);
        return ERR_PTR(err);
@@ -274,10 +275,11 @@ static void vp_del_vq(struct virtqueue *vq)
 {
        struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
        struct virtio_pci_vq_info *info = vq->priv;
+       unsigned long flags, size;
 
-       spin_lock(&vp_dev->lock);
+       spin_lock_irqsave(&vp_dev->lock, flags);
        list_del(&info->node);
-       spin_unlock(&vp_dev->lock);
+       spin_unlock_irqrestore(&vp_dev->lock, flags);
 
        vring_del_virtqueue(vq);
 
@@ -285,12 +287,12 @@ static void vp_del_vq(struct virtqueue *vq)
        iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
        iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
 
-       kfree(info->queue);
+       size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN));
+       free_pages_exact(info->queue, size);
        kfree(info);
 }
 
 static struct virtio_config_ops virtio_pci_config_ops = {
-       .feature        = vp_feature,
        .get            = vp_get,
        .set            = vp_set,
        .get_status     = vp_get_status,
@@ -298,8 +300,24 @@ static struct virtio_config_ops virtio_pci_config_ops = {
        .reset          = vp_reset,
        .find_vq        = vp_find_vq,
        .del_vq         = vp_del_vq,
+       .get_features   = vp_get_features,
+       .finalize_features = vp_finalize_features,
 };
 
+static void virtio_pci_release_dev(struct device *_d)
+{
+       struct virtio_device *dev = container_of(_d, struct virtio_device, dev);
+       struct virtio_pci_device *vp_dev = to_vp_device(dev);
+       struct pci_dev *pci_dev = vp_dev->pci_dev;
+
+       free_irq(pci_dev->irq, vp_dev);
+       pci_set_drvdata(pci_dev, NULL);
+       pci_iounmap(pci_dev, vp_dev->ioaddr);
+       pci_release_regions(pci_dev);
+       pci_disable_device(pci_dev);
+       kfree(vp_dev);
+}
+
 /* the PCI probing function */
 static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
                                      const struct pci_device_id *id)
@@ -311,16 +329,19 @@ static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
        if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
                return -ENODEV;
 
+       if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
+               printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
+                      VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
+               return -ENODEV;
+       }
+
        /* allocate our structure and fill it out */
        vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
        if (vp_dev == NULL)
                return -ENOMEM;
 
-       snprintf(vp_dev->vdev.dev.bus_id, BUS_ID_SIZE, "virtio%d", dev_index);
-       vp_dev->vdev.index = dev_index;
-       dev_index++;
-
-       vp_dev->vdev.dev.parent = &virtio_pci_root;
+       vp_dev->vdev.dev.parent = virtio_pci_root;
+       vp_dev->vdev.dev.release = virtio_pci_release_dev;
        vp_dev->vdev.config = &virtio_pci_config_ops;
        vp_dev->pci_dev = pci_dev;
        INIT_LIST_HEAD(&vp_dev->virtqueues);
@@ -350,7 +371,7 @@ static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
 
        /* register a handler for the queue with the PCI device's interrupt */
        err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
-                         vp_dev->vdev.dev.bus_id, vp_dev);
+                         dev_name(&vp_dev->vdev.dev), vp_dev);
        if (err)
                goto out_set_drvdata;
 
@@ -379,12 +400,7 @@ static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
 {
        struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
 
-       free_irq(pci_dev->irq, vp_dev);
-       pci_set_drvdata(pci_dev, NULL);
-       pci_iounmap(pci_dev, vp_dev->ioaddr);
-       pci_release_regions(pci_dev);
-       pci_disable_device(pci_dev);
-       kfree(vp_dev);
+       unregister_virtio_device(&vp_dev->vdev);
 }
 
 #ifdef CONFIG_PM
@@ -418,13 +434,13 @@ static int __init virtio_pci_init(void)
 {
        int err;
 
-       err = device_register(&virtio_pci_root);
-       if (err)
-               return err;
+       virtio_pci_root = root_device_register("virtio-pci");
+       if (IS_ERR(virtio_pci_root))
+               return PTR_ERR(virtio_pci_root);
 
        err = pci_register_driver(&virtio_pci_driver);
        if (err)
-               device_unregister(&virtio_pci_root);
+               device_unregister(virtio_pci_root);
 
        return err;
 }
@@ -433,8 +449,8 @@ module_init(virtio_pci_init);
 
 static void __exit virtio_pci_exit(void)
 {
-       device_unregister(&virtio_pci_root);
        pci_unregister_driver(&virtio_pci_driver);
+       root_device_unregister(virtio_pci_root);
 }
 
 module_exit(virtio_pci_exit);