linux/drivers/virtio/virtio.c
<<
>>
Prefs
   1#include <linux/virtio.h>
   2#include <linux/spinlock.h>
   3#include <linux/virtio_config.h>
   4#include <linux/module.h>
   5#include <linux/idr.h>
   6#include <uapi/linux/virtio_ids.h>
   7
   8/* Unique numbering for virtio devices. */
   9static DEFINE_IDA(virtio_index_ida);
  10
  11static ssize_t device_show(struct device *_d,
  12                           struct device_attribute *attr, char *buf)
  13{
  14        struct virtio_device *dev = dev_to_virtio(_d);
  15        return sprintf(buf, "0x%04x\n", dev->id.device);
  16}
  17static ssize_t vendor_show(struct device *_d,
  18                           struct device_attribute *attr, char *buf)
  19{
  20        struct virtio_device *dev = dev_to_virtio(_d);
  21        return sprintf(buf, "0x%04x\n", dev->id.vendor);
  22}
  23static ssize_t status_show(struct device *_d,
  24                           struct device_attribute *attr, char *buf)
  25{
  26        struct virtio_device *dev = dev_to_virtio(_d);
  27        return sprintf(buf, "0x%08x\n", dev->config->get_status(dev));
  28}
  29static ssize_t modalias_show(struct device *_d,
  30                             struct device_attribute *attr, char *buf)
  31{
  32        struct virtio_device *dev = dev_to_virtio(_d);
  33        return sprintf(buf, "virtio:d%08Xv%08X\n",
  34                       dev->id.device, dev->id.vendor);
  35}
  36static ssize_t features_show(struct device *_d,
  37                             struct device_attribute *attr, char *buf)
  38{
  39        struct virtio_device *dev = dev_to_virtio(_d);
  40        unsigned int i;
  41        ssize_t len = 0;
  42
  43        /* We actually represent this as a bitstring, as it could be
  44         * arbitrary length in future. */
  45        for (i = 0; i < sizeof(dev->features)*8; i++)
  46                len += sprintf(buf+len, "%c",
  47                               __virtio_test_bit(dev, i) ? '1' : '0');
  48        len += sprintf(buf+len, "\n");
  49        return len;
  50}
  51static struct device_attribute virtio_dev_attrs[] = {
  52        __ATTR_RO(device),
  53        __ATTR_RO(vendor),
  54        __ATTR_RO(status),
  55        __ATTR_RO(modalias),
  56        __ATTR_RO(features),
  57        __ATTR_NULL
  58};
  59
  60static inline int virtio_id_match(const struct virtio_device *dev,
  61                                  const struct virtio_device_id *id)
  62{
  63        if (id->device != dev->id.device && id->device != VIRTIO_DEV_ANY_ID)
  64                return 0;
  65
  66        return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor == dev->id.vendor;
  67}
  68
  69/* This looks through all the IDs a driver claims to support.  If any of them
  70 * match, we return 1 and the kernel will call virtio_dev_probe(). */
  71static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
  72{
  73        unsigned int i;
  74        struct virtio_device *dev = dev_to_virtio(_dv);
  75        const struct virtio_device_id *ids;
  76
  77        ids = drv_to_virtio(_dr)->id_table;
  78        for (i = 0; ids[i].device; i++)
  79                if (virtio_id_match(dev, &ids[i]))
  80                        return 1;
  81        return 0;
  82}
  83
  84static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
  85{
  86        struct virtio_device *dev = dev_to_virtio(_dv);
  87
  88        return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
  89                              dev->id.device, dev->id.vendor);
  90}
  91
  92static void add_status(struct virtio_device *dev, unsigned status)
  93{
  94        dev->config->set_status(dev, dev->config->get_status(dev) | status);
  95}
  96
  97void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
  98                                         unsigned int fbit)
  99{
 100        unsigned int i;
 101        struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver);
 102
 103        for (i = 0; i < drv->feature_table_size; i++)
 104                if (drv->feature_table[i] == fbit)
 105                        return;
 106
 107        if (drv->feature_table_legacy) {
 108                for (i = 0; i < drv->feature_table_size_legacy; i++)
 109                        if (drv->feature_table_legacy[i] == fbit)
 110                                return;
 111        }
 112
 113        BUG();
 114}
 115EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
 116
 117static void __virtio_config_changed(struct virtio_device *dev)
 118{
 119        struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
 120
 121        if (!dev->config_enabled)
 122                dev->config_change_pending = true;
 123        else if (drv && drv->config_changed)
 124                drv->config_changed(dev);
 125}
 126
 127void virtio_config_changed(struct virtio_device *dev)
 128{
 129        unsigned long flags;
 130
 131        spin_lock_irqsave(&dev->config_lock, flags);
 132        __virtio_config_changed(dev);
 133        spin_unlock_irqrestore(&dev->config_lock, flags);
 134}
 135EXPORT_SYMBOL_GPL(virtio_config_changed);
 136
 137static void virtio_config_disable(struct virtio_device *dev)
 138{
 139        spin_lock_irq(&dev->config_lock);
 140        dev->config_enabled = false;
 141        spin_unlock_irq(&dev->config_lock);
 142}
 143
 144static void virtio_config_enable(struct virtio_device *dev)
 145{
 146        spin_lock_irq(&dev->config_lock);
 147        dev->config_enabled = true;
 148        if (dev->config_change_pending)
 149                __virtio_config_changed(dev);
 150        dev->config_change_pending = false;
 151        spin_unlock_irq(&dev->config_lock);
 152}
 153
 154static int virtio_finalize_features(struct virtio_device *dev)
 155{
 156        int ret = dev->config->finalize_features(dev);
 157        unsigned status;
 158
 159        if (ret)
 160                return ret;
 161
 162        if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1))
 163                return 0;
 164
 165        add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
 166        status = dev->config->get_status(dev);
 167        if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
 168                dev_err(&dev->dev, "virtio: device refuses features: %x\n",
 169                        status);
 170                return -ENODEV;
 171        }
 172        return 0;
 173}
 174
 175static int virtio_dev_probe(struct device *_d)
 176{
 177        int err, i;
 178        struct virtio_device *dev = dev_to_virtio(_d);
 179        struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
 180        u64 device_features;
 181        u64 driver_features;
 182        u64 driver_features_legacy;
 183
 184        /* We have a driver! */
 185        add_status(dev, VIRTIO_CONFIG_S_DRIVER);
 186
 187        /* Figure out what features the device supports. */
 188        device_features = dev->config->get_features(dev);
 189
 190        /* Figure out what features the driver supports. */
 191        driver_features = 0;
 192        for (i = 0; i < drv->feature_table_size; i++) {
 193                unsigned int f = drv->feature_table[i];
 194                BUG_ON(f >= 64);
 195                driver_features |= (1ULL << f);
 196        }
 197
 198        /* Some drivers have a separate feature table for virtio v1.0 */
 199        if (drv->feature_table_legacy) {
 200                driver_features_legacy = 0;
 201                for (i = 0; i < drv->feature_table_size_legacy; i++) {
 202                        unsigned int f = drv->feature_table_legacy[i];
 203                        BUG_ON(f >= 64);
 204                        driver_features_legacy |= (1ULL << f);
 205                }
 206        } else {
 207                driver_features_legacy = driver_features;
 208        }
 209
 210        if (device_features & (1ULL << VIRTIO_F_VERSION_1))
 211                dev->features = driver_features & device_features;
 212        else
 213                dev->features = driver_features_legacy & device_features;
 214
 215        /* Transport features always preserved to pass to finalize_features. */
 216        for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
 217                if (device_features & (1ULL << i))
 218                        __virtio_set_bit(dev, i);
 219
 220        if (drv->validate) {
 221                err = drv->validate(dev);
 222                if (err)
 223                        goto err;
 224        }
 225
 226        err = virtio_finalize_features(dev);
 227        if (err)
 228                goto err;
 229
 230        err = drv->probe(dev);
 231        if (err)
 232                goto err;
 233
 234        /* If probe didn't do it, mark device DRIVER_OK ourselves. */
 235        if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
 236                virtio_device_ready(dev);
 237
 238        if (drv->scan)
 239                drv->scan(dev);
 240
 241        virtio_config_enable(dev);
 242
 243        return 0;
 244err:
 245        add_status(dev, VIRTIO_CONFIG_S_FAILED);
 246        return err;
 247
 248}
 249
 250static int virtio_dev_remove(struct device *_d)
 251{
 252        struct virtio_device *dev = dev_to_virtio(_d);
 253        struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
 254
 255        virtio_config_disable(dev);
 256
 257        drv->remove(dev);
 258
 259        /* Driver should have reset device. */
 260        WARN_ON_ONCE(dev->config->get_status(dev));
 261
 262        /* Acknowledge the device's existence again. */
 263        add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
 264        return 0;
 265}
 266
 267static struct bus_type virtio_bus = {
 268        .name  = "virtio",
 269        .match = virtio_dev_match,
 270        .dev_attrs = virtio_dev_attrs,
 271        .uevent = virtio_uevent,
 272        .probe = virtio_dev_probe,
 273        .remove = virtio_dev_remove,
 274};
 275
 276int register_virtio_driver(struct virtio_driver *driver)
 277{
 278        /* Catch this early. */
 279        BUG_ON(driver->feature_table_size && !driver->feature_table);
 280        driver->driver.bus = &virtio_bus;
 281        return driver_register(&driver->driver);
 282}
 283EXPORT_SYMBOL_GPL(register_virtio_driver);
 284
 285void unregister_virtio_driver(struct virtio_driver *driver)
 286{
 287        driver_unregister(&driver->driver);
 288}
 289EXPORT_SYMBOL_GPL(unregister_virtio_driver);
 290
 291int register_virtio_device(struct virtio_device *dev)
 292{
 293        int err;
 294
 295        dev->dev.bus = &virtio_bus;
 296
 297        /* Assign a unique device index and hence name. */
 298        err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
 299        if (err < 0)
 300                goto out;
 301
 302        dev->index = err;
 303        dev_set_name(&dev->dev, "virtio%u", dev->index);
 304
 305        spin_lock_init(&dev->config_lock);
 306        dev->config_enabled = false;
 307        dev->config_change_pending = false;
 308
 309        /* We always start by resetting the device, in case a previous
 310         * driver messed it up.  This also tests that code path a little. */
 311        dev->config->reset(dev);
 312
 313        /* Acknowledge that we've seen the device. */
 314        add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
 315
 316        INIT_LIST_HEAD(&dev->vqs);
 317
 318        /* device_register() causes the bus infrastructure to look for a
 319         * matching driver. */
 320        err = device_register(&dev->dev);
 321out:
 322        if (err)
 323                add_status(dev, VIRTIO_CONFIG_S_FAILED);
 324        return err;
 325}
 326EXPORT_SYMBOL_GPL(register_virtio_device);
 327
 328void unregister_virtio_device(struct virtio_device *dev)
 329{
 330        int index = dev->index; /* save for after device release */
 331
 332        device_unregister(&dev->dev);
 333        ida_simple_remove(&virtio_index_ida, index);
 334}
 335EXPORT_SYMBOL_GPL(unregister_virtio_device);
 336
 337#ifdef CONFIG_PM_SLEEP
 338int virtio_device_freeze(struct virtio_device *dev)
 339{
 340        struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
 341
 342        dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
 343
 344        if (drv && drv->freeze)
 345                return drv->freeze(dev);
 346
 347        return 0;
 348}
 349EXPORT_SYMBOL_GPL(virtio_device_freeze);
 350
 351int virtio_device_restore(struct virtio_device *dev)
 352{
 353        struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
 354        int ret;
 355
 356        /* We always start by resetting the device, in case a previous
 357         * driver messed it up. */
 358        dev->config->reset(dev);
 359
 360        /* Acknowledge that we've seen the device. */
 361        add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
 362
 363        /* Maybe driver failed before freeze.
 364         * Restore the failed status, for debugging. */
 365        if (dev->failed)
 366                add_status(dev, VIRTIO_CONFIG_S_FAILED);
 367
 368        if (!drv)
 369                return 0;
 370
 371        /* We have a driver! */
 372        add_status(dev, VIRTIO_CONFIG_S_DRIVER);
 373
 374        ret = virtio_finalize_features(dev);
 375        if (ret)
 376                goto err;
 377
 378        if (drv->restore) {
 379                ret = drv->restore(dev);
 380                if (ret)
 381                        goto err;
 382        }
 383
 384        /* Finally, tell the device we're all set */
 385        add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
 386
 387        return 0;
 388
 389err:
 390        add_status(dev, VIRTIO_CONFIG_S_FAILED);
 391        return ret;
 392}
 393EXPORT_SYMBOL_GPL(virtio_device_restore);
 394#endif
 395
 396static int virtio_init(void)
 397{
 398        if (bus_register(&virtio_bus) != 0)
 399                panic("virtio bus registration failed");
 400        return 0;
 401}
 402
 403static void __exit virtio_exit(void)
 404{
 405        bus_unregister(&virtio_bus);
 406}
 407core_initcall(virtio_init);
 408module_exit(virtio_exit);
 409
 410MODULE_LICENSE("GPL");
 411