]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - drivers/virtio/virtio.c
virtio: add legacy feature table support
[karo-tx-linux.git] / drivers / virtio / virtio.c
index fed0ce198ae3eacf76d747948f54cb051bda8d57..f9ad99c8b35241681393670801f5aaa037cd6538 100644 (file)
@@ -49,9 +49,9 @@ static ssize_t features_show(struct device *_d,
 
        /* We actually represent this as a bitstring, as it could be
         * arbitrary length in future. */
-       for (i = 0; i < ARRAY_SIZE(dev->features)*BITS_PER_LONG; i++)
+       for (i = 0; i < sizeof(dev->features)*8; i++)
                len += sprintf(buf+len, "%c",
-                              test_bit(i, dev->features) ? '1' : '0');
+                              __virtio_test_bit(dev, i) ? '1' : '0');
        len += sprintf(buf+len, "\n");
        return len;
 }
@@ -113,16 +113,63 @@ void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
        for (i = 0; i < drv->feature_table_size; i++)
                if (drv->feature_table[i] == fbit)
                        return;
+
+       if (drv->feature_table_legacy) {
+               for (i = 0; i < drv->feature_table_size_legacy; i++)
+                       if (drv->feature_table_legacy[i] == fbit)
+                               return;
+       }
+
        BUG();
 }
 EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
 
+static void __virtio_config_changed(struct virtio_device *dev)
+{
+       struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
+
+       if (!dev->config_enabled)
+               dev->config_change_pending = true;
+       else if (drv && drv->config_changed)
+               drv->config_changed(dev);
+}
+
+void virtio_config_changed(struct virtio_device *dev)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&dev->config_lock, flags);
+       __virtio_config_changed(dev);
+       spin_unlock_irqrestore(&dev->config_lock, flags);
+}
+EXPORT_SYMBOL_GPL(virtio_config_changed);
+
+static void virtio_config_disable(struct virtio_device *dev)
+{
+       spin_lock_irq(&dev->config_lock);
+       dev->config_enabled = false;
+       spin_unlock_irq(&dev->config_lock);
+}
+
+static void virtio_config_enable(struct virtio_device *dev)
+{
+       spin_lock_irq(&dev->config_lock);
+       dev->config_enabled = true;
+       if (dev->config_change_pending)
+               __virtio_config_changed(dev);
+       dev->config_change_pending = false;
+       spin_unlock_irq(&dev->config_lock);
+}
+
 static int virtio_dev_probe(struct device *_d)
 {
        int err, i;
        struct virtio_device *dev = dev_to_virtio(_d);
        struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
-       u32 device_features;
+       u64 device_features;
+       u64 driver_features;
+       u64 driver_features_legacy;
+       unsigned status;
 
        /* We have a driver! */
        add_status(dev, VIRTIO_CONFIG_S_DRIVER);
@@ -130,32 +177,64 @@ static int virtio_dev_probe(struct device *_d)
        /* Figure out what features the device supports. */
        device_features = dev->config->get_features(dev);
 
-       /* Features supported by both device and driver into dev->features. */
-       memset(dev->features, 0, sizeof(dev->features));
+       /* Figure out what features the driver supports. */
+       driver_features = 0;
        for (i = 0; i < drv->feature_table_size; i++) {
                unsigned int f = drv->feature_table[i];
-               BUG_ON(f >= 32);
-               if (device_features & (1 << f))
-                       set_bit(f, dev->features);
+               BUG_ON(f >= 64);
+               driver_features |= (1ULL << f);
+       }
+
+       /* Some drivers have a separate feature table for virtio v1.0 */
+       if (drv->feature_table_legacy) {
+               driver_features_legacy = 0;
+               for (i = 0; i < drv->feature_table_size_legacy; i++) {
+                       unsigned int f = drv->feature_table_legacy[i];
+                       BUG_ON(f >= 64);
+                       driver_features_legacy |= (1ULL << f);
+               }
+       } else {
+               driver_features_legacy = driver_features;
        }
 
+       if (driver_features & device_features & (1ULL << VIRTIO_F_VERSION_1))
+               dev->features = driver_features & device_features;
+       else
+               dev->features = driver_features_legacy & device_features;
+
        /* Transport features always preserved to pass to finalize_features. */
        for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
-               if (device_features & (1 << i))
-                       set_bit(i, dev->features);
+               if (device_features & (1ULL << i))
+                       __virtio_set_bit(dev, i);
 
        dev->config->finalize_features(dev);
 
+       if (virtio_has_feature(dev, VIRTIO_F_VERSION_1)) {
+               add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
+               status = dev->config->get_status(dev);
+               if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
+                       dev_err(_d, "virtio: device refuses features: %x\n",
+                              status);
+                       err = -ENODEV;
+                       goto err;
+               }
+       }
+
        err = drv->probe(dev);
        if (err)
-               add_status(dev, VIRTIO_CONFIG_S_FAILED);
-       else {
-               add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
-               if (drv->scan)
-                       drv->scan(dev);
-       }
+               goto err;
 
+       add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
+       if (drv->scan)
+               drv->scan(dev);
+
+       virtio_config_enable(dev);
+
+       return 0;
+err:
+       add_status(dev, VIRTIO_CONFIG_S_FAILED);
        return err;
+
 }
 
 static int virtio_dev_remove(struct device *_d)
@@ -163,6 +242,8 @@ static int virtio_dev_remove(struct device *_d)
        struct virtio_device *dev = dev_to_virtio(_d);
        struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
 
+       virtio_config_disable(dev);
+
        drv->remove(dev);
 
        /* Driver should have reset device. */
@@ -211,6 +292,10 @@ int register_virtio_device(struct virtio_device *dev)
        dev->index = err;
        dev_set_name(&dev->dev, "virtio%u", dev->index);
 
+       spin_lock_init(&dev->config_lock);
+       dev->config_enabled = false;
+       dev->config_change_pending = false;
+
        /* We always start by resetting the device, in case a previous
         * driver messed it up.  This also tests that code path a little. */
        dev->config->reset(dev);
@@ -239,6 +324,64 @@ void unregister_virtio_device(struct virtio_device *dev)
 }
 EXPORT_SYMBOL_GPL(unregister_virtio_device);
 
+#ifdef CONFIG_PM_SLEEP
+int virtio_device_freeze(struct virtio_device *dev)
+{
+       struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
+
+       virtio_config_disable(dev);
+
+       dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
+
+       if (drv && drv->freeze)
+               return drv->freeze(dev);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(virtio_device_freeze);
+
+int virtio_device_restore(struct virtio_device *dev)
+{
+       struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
+
+       /* We always start by resetting the device, in case a previous
+        * driver messed it up. */
+       dev->config->reset(dev);
+
+       /* Acknowledge that we've seen the device. */
+       add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
+
+       /* Maybe driver failed before freeze.
+        * Restore the failed status, for debugging. */
+       if (dev->failed)
+               add_status(dev, VIRTIO_CONFIG_S_FAILED);
+
+       if (!drv)
+               return 0;
+
+       /* We have a driver! */
+       add_status(dev, VIRTIO_CONFIG_S_DRIVER);
+
+       dev->config->finalize_features(dev);
+
+       if (drv->restore) {
+               int ret = drv->restore(dev);
+               if (ret) {
+                       add_status(dev, VIRTIO_CONFIG_S_FAILED);
+                       return ret;
+               }
+       }
+
+       /* Finally, tell the device we're all set */
+       add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
+
+       virtio_config_enable(dev);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(virtio_device_restore);
+#endif
+
 static int virtio_init(void)
 {
        if (bus_register(&virtio_bus) != 0)