0001
0002 #include <linux/virtio.h>
0003 #include <linux/spinlock.h>
0004 #include <linux/virtio_config.h>
0005 #include <linux/virtio_anchor.h>
0006 #include <linux/module.h>
0007 #include <linux/idr.h>
0008 #include <linux/of.h>
0009 #include <uapi/linux/virtio_ids.h>
0010
0011
0012 static DEFINE_IDA(virtio_index_ida);
0013
0014 static ssize_t device_show(struct device *_d,
0015 struct device_attribute *attr, char *buf)
0016 {
0017 struct virtio_device *dev = dev_to_virtio(_d);
0018 return sprintf(buf, "0x%04x\n", dev->id.device);
0019 }
0020 static DEVICE_ATTR_RO(device);
0021
0022 static ssize_t vendor_show(struct device *_d,
0023 struct device_attribute *attr, char *buf)
0024 {
0025 struct virtio_device *dev = dev_to_virtio(_d);
0026 return sprintf(buf, "0x%04x\n", dev->id.vendor);
0027 }
0028 static DEVICE_ATTR_RO(vendor);
0029
0030 static ssize_t status_show(struct device *_d,
0031 struct device_attribute *attr, char *buf)
0032 {
0033 struct virtio_device *dev = dev_to_virtio(_d);
0034 return sprintf(buf, "0x%08x\n", dev->config->get_status(dev));
0035 }
0036 static DEVICE_ATTR_RO(status);
0037
0038 static ssize_t modalias_show(struct device *_d,
0039 struct device_attribute *attr, char *buf)
0040 {
0041 struct virtio_device *dev = dev_to_virtio(_d);
0042 return sprintf(buf, "virtio:d%08Xv%08X\n",
0043 dev->id.device, dev->id.vendor);
0044 }
0045 static DEVICE_ATTR_RO(modalias);
0046
0047 static ssize_t features_show(struct device *_d,
0048 struct device_attribute *attr, char *buf)
0049 {
0050 struct virtio_device *dev = dev_to_virtio(_d);
0051 unsigned int i;
0052 ssize_t len = 0;
0053
0054
0055
0056 for (i = 0; i < sizeof(dev->features)*8; i++)
0057 len += sprintf(buf+len, "%c",
0058 __virtio_test_bit(dev, i) ? '1' : '0');
0059 len += sprintf(buf+len, "\n");
0060 return len;
0061 }
0062 static DEVICE_ATTR_RO(features);
0063
0064 static struct attribute *virtio_dev_attrs[] = {
0065 &dev_attr_device.attr,
0066 &dev_attr_vendor.attr,
0067 &dev_attr_status.attr,
0068 &dev_attr_modalias.attr,
0069 &dev_attr_features.attr,
0070 NULL,
0071 };
0072 ATTRIBUTE_GROUPS(virtio_dev);
0073
0074 static inline int virtio_id_match(const struct virtio_device *dev,
0075 const struct virtio_device_id *id)
0076 {
0077 if (id->device != dev->id.device && id->device != VIRTIO_DEV_ANY_ID)
0078 return 0;
0079
0080 return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor == dev->id.vendor;
0081 }
0082
0083
0084
0085 static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
0086 {
0087 unsigned int i;
0088 struct virtio_device *dev = dev_to_virtio(_dv);
0089 const struct virtio_device_id *ids;
0090
0091 ids = drv_to_virtio(_dr)->id_table;
0092 for (i = 0; ids[i].device; i++)
0093 if (virtio_id_match(dev, &ids[i]))
0094 return 1;
0095 return 0;
0096 }
0097
0098 static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
0099 {
0100 struct virtio_device *dev = dev_to_virtio(_dv);
0101
0102 return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
0103 dev->id.device, dev->id.vendor);
0104 }
0105
0106 void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
0107 unsigned int fbit)
0108 {
0109 unsigned int i;
0110 struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver);
0111
0112 for (i = 0; i < drv->feature_table_size; i++)
0113 if (drv->feature_table[i] == fbit)
0114 return;
0115
0116 if (drv->feature_table_legacy) {
0117 for (i = 0; i < drv->feature_table_size_legacy; i++)
0118 if (drv->feature_table_legacy[i] == fbit)
0119 return;
0120 }
0121
0122 BUG();
0123 }
0124 EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
0125
0126 static void __virtio_config_changed(struct virtio_device *dev)
0127 {
0128 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
0129
0130 if (!dev->config_enabled)
0131 dev->config_change_pending = true;
0132 else if (drv && drv->config_changed)
0133 drv->config_changed(dev);
0134 }
0135
0136 void virtio_config_changed(struct virtio_device *dev)
0137 {
0138 unsigned long flags;
0139
0140 spin_lock_irqsave(&dev->config_lock, flags);
0141 __virtio_config_changed(dev);
0142 spin_unlock_irqrestore(&dev->config_lock, flags);
0143 }
0144 EXPORT_SYMBOL_GPL(virtio_config_changed);
0145
0146 static void virtio_config_disable(struct virtio_device *dev)
0147 {
0148 spin_lock_irq(&dev->config_lock);
0149 dev->config_enabled = false;
0150 spin_unlock_irq(&dev->config_lock);
0151 }
0152
0153 static void virtio_config_enable(struct virtio_device *dev)
0154 {
0155 spin_lock_irq(&dev->config_lock);
0156 dev->config_enabled = true;
0157 if (dev->config_change_pending)
0158 __virtio_config_changed(dev);
0159 dev->config_change_pending = false;
0160 spin_unlock_irq(&dev->config_lock);
0161 }
0162
0163 void virtio_add_status(struct virtio_device *dev, unsigned int status)
0164 {
0165 might_sleep();
0166 dev->config->set_status(dev, dev->config->get_status(dev) | status);
0167 }
0168 EXPORT_SYMBOL_GPL(virtio_add_status);
0169
0170
0171 static int virtio_features_ok(struct virtio_device *dev)
0172 {
0173 unsigned int status;
0174
0175 might_sleep();
0176
0177 if (virtio_check_mem_acc_cb(dev)) {
0178 if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1)) {
0179 dev_warn(&dev->dev,
0180 "device must provide VIRTIO_F_VERSION_1\n");
0181 return -ENODEV;
0182 }
0183
0184 if (!virtio_has_feature(dev, VIRTIO_F_ACCESS_PLATFORM)) {
0185 dev_warn(&dev->dev,
0186 "device must provide VIRTIO_F_ACCESS_PLATFORM\n");
0187 return -ENODEV;
0188 }
0189 }
0190
0191 if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1))
0192 return 0;
0193
0194 virtio_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
0195 status = dev->config->get_status(dev);
0196 if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
0197 dev_err(&dev->dev, "virtio: device refuses features: %x\n",
0198 status);
0199 return -ENODEV;
0200 }
0201 return 0;
0202 }
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220 void virtio_reset_device(struct virtio_device *dev)
0221 {
0222 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
0223
0224
0225
0226
0227
0228
0229 virtio_break_device(dev);
0230 virtio_synchronize_cbs(dev);
0231 #endif
0232
0233 dev->config->reset(dev);
0234 }
0235 EXPORT_SYMBOL_GPL(virtio_reset_device);
0236
0237 static int virtio_dev_probe(struct device *_d)
0238 {
0239 int err, i;
0240 struct virtio_device *dev = dev_to_virtio(_d);
0241 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
0242 u64 device_features;
0243 u64 driver_features;
0244 u64 driver_features_legacy;
0245
0246
0247 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
0248
0249
0250 device_features = dev->config->get_features(dev);
0251
0252
0253 driver_features = 0;
0254 for (i = 0; i < drv->feature_table_size; i++) {
0255 unsigned int f = drv->feature_table[i];
0256 BUG_ON(f >= 64);
0257 driver_features |= (1ULL << f);
0258 }
0259
0260
0261 if (drv->feature_table_legacy) {
0262 driver_features_legacy = 0;
0263 for (i = 0; i < drv->feature_table_size_legacy; i++) {
0264 unsigned int f = drv->feature_table_legacy[i];
0265 BUG_ON(f >= 64);
0266 driver_features_legacy |= (1ULL << f);
0267 }
0268 } else {
0269 driver_features_legacy = driver_features;
0270 }
0271
0272 if (device_features & (1ULL << VIRTIO_F_VERSION_1))
0273 dev->features = driver_features & device_features;
0274 else
0275 dev->features = driver_features_legacy & device_features;
0276
0277
0278 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
0279 if (device_features & (1ULL << i))
0280 __virtio_set_bit(dev, i);
0281
0282 err = dev->config->finalize_features(dev);
0283 if (err)
0284 goto err;
0285
0286 if (drv->validate) {
0287 u64 features = dev->features;
0288
0289 err = drv->validate(dev);
0290 if (err)
0291 goto err;
0292
0293
0294 if (features != dev->features) {
0295 err = dev->config->finalize_features(dev);
0296 if (err)
0297 goto err;
0298 }
0299 }
0300
0301 err = virtio_features_ok(dev);
0302 if (err)
0303 goto err;
0304
0305 err = drv->probe(dev);
0306 if (err)
0307 goto err;
0308
0309
0310 if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
0311 virtio_device_ready(dev);
0312
0313 if (drv->scan)
0314 drv->scan(dev);
0315
0316 virtio_config_enable(dev);
0317
0318 return 0;
0319 err:
0320 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
0321 return err;
0322
0323 }
0324
0325 static void virtio_dev_remove(struct device *_d)
0326 {
0327 struct virtio_device *dev = dev_to_virtio(_d);
0328 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
0329
0330 virtio_config_disable(dev);
0331
0332 drv->remove(dev);
0333
0334
0335 WARN_ON_ONCE(dev->config->get_status(dev));
0336
0337
0338 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
0339
0340 of_node_put(dev->dev.of_node);
0341 }
0342
0343 static struct bus_type virtio_bus = {
0344 .name = "virtio",
0345 .match = virtio_dev_match,
0346 .dev_groups = virtio_dev_groups,
0347 .uevent = virtio_uevent,
0348 .probe = virtio_dev_probe,
0349 .remove = virtio_dev_remove,
0350 };
0351
0352 int register_virtio_driver(struct virtio_driver *driver)
0353 {
0354
0355 BUG_ON(driver->feature_table_size && !driver->feature_table);
0356 driver->driver.bus = &virtio_bus;
0357 return driver_register(&driver->driver);
0358 }
0359 EXPORT_SYMBOL_GPL(register_virtio_driver);
0360
0361 void unregister_virtio_driver(struct virtio_driver *driver)
0362 {
0363 driver_unregister(&driver->driver);
0364 }
0365 EXPORT_SYMBOL_GPL(unregister_virtio_driver);
0366
0367 static int virtio_device_of_init(struct virtio_device *dev)
0368 {
0369 struct device_node *np, *pnode = dev_of_node(dev->dev.parent);
0370 char compat[] = "virtio,deviceXXXXXXXX";
0371 int ret, count;
0372
0373 if (!pnode)
0374 return 0;
0375
0376 count = of_get_available_child_count(pnode);
0377 if (!count)
0378 return 0;
0379
0380
0381 if (WARN_ON(count > 1))
0382 return -EINVAL;
0383
0384 np = of_get_next_available_child(pnode, NULL);
0385 if (WARN_ON(!np))
0386 return -ENODEV;
0387
0388 ret = snprintf(compat, sizeof(compat), "virtio,device%x", dev->id.device);
0389 BUG_ON(ret >= sizeof(compat));
0390
0391
0392
0393
0394
0395
0396 if (!of_device_is_compatible(np, compat)) {
0397 ret = 0;
0398 goto out;
0399 }
0400
0401 dev->dev.of_node = np;
0402 return 0;
0403
0404 out:
0405 of_node_put(np);
0406 return ret;
0407 }
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418 int register_virtio_device(struct virtio_device *dev)
0419 {
0420 int err;
0421
0422 dev->dev.bus = &virtio_bus;
0423 device_initialize(&dev->dev);
0424
0425
0426 err = ida_alloc(&virtio_index_ida, GFP_KERNEL);
0427 if (err < 0)
0428 goto out;
0429
0430 dev->index = err;
0431 err = dev_set_name(&dev->dev, "virtio%u", dev->index);
0432 if (err)
0433 goto out_ida_remove;
0434
0435 err = virtio_device_of_init(dev);
0436 if (err)
0437 goto out_ida_remove;
0438
0439 spin_lock_init(&dev->config_lock);
0440 dev->config_enabled = false;
0441 dev->config_change_pending = false;
0442
0443 INIT_LIST_HEAD(&dev->vqs);
0444 spin_lock_init(&dev->vqs_list_lock);
0445
0446
0447
0448 virtio_reset_device(dev);
0449
0450
0451 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
0452
0453
0454
0455
0456
0457 err = device_add(&dev->dev);
0458 if (err)
0459 goto out_of_node_put;
0460
0461 return 0;
0462
0463 out_of_node_put:
0464 of_node_put(dev->dev.of_node);
0465 out_ida_remove:
0466 ida_free(&virtio_index_ida, dev->index);
0467 out:
0468 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
0469 return err;
0470 }
0471 EXPORT_SYMBOL_GPL(register_virtio_device);
0472
0473 bool is_virtio_device(struct device *dev)
0474 {
0475 return dev->bus == &virtio_bus;
0476 }
0477 EXPORT_SYMBOL_GPL(is_virtio_device);
0478
0479 void unregister_virtio_device(struct virtio_device *dev)
0480 {
0481 int index = dev->index;
0482
0483 device_unregister(&dev->dev);
0484 ida_free(&virtio_index_ida, index);
0485 }
0486 EXPORT_SYMBOL_GPL(unregister_virtio_device);
0487
0488 #ifdef CONFIG_PM_SLEEP
0489 int virtio_device_freeze(struct virtio_device *dev)
0490 {
0491 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
0492
0493 virtio_config_disable(dev);
0494
0495 dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
0496
0497 if (drv && drv->freeze)
0498 return drv->freeze(dev);
0499
0500 return 0;
0501 }
0502 EXPORT_SYMBOL_GPL(virtio_device_freeze);
0503
0504 int virtio_device_restore(struct virtio_device *dev)
0505 {
0506 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
0507 int ret;
0508
0509
0510
0511 virtio_reset_device(dev);
0512
0513
0514 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
0515
0516
0517
0518 if (dev->failed)
0519 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
0520
0521 if (!drv)
0522 return 0;
0523
0524
0525 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
0526
0527 ret = dev->config->finalize_features(dev);
0528 if (ret)
0529 goto err;
0530
0531 ret = virtio_features_ok(dev);
0532 if (ret)
0533 goto err;
0534
0535 if (drv->restore) {
0536 ret = drv->restore(dev);
0537 if (ret)
0538 goto err;
0539 }
0540
0541
0542 if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
0543 virtio_device_ready(dev);
0544
0545 virtio_config_enable(dev);
0546
0547 return 0;
0548
0549 err:
0550 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
0551 return ret;
0552 }
0553 EXPORT_SYMBOL_GPL(virtio_device_restore);
0554 #endif
0555
0556 static int virtio_init(void)
0557 {
0558 if (bus_register(&virtio_bus) != 0)
0559 panic("virtio bus registration failed");
0560 return 0;
0561 }
0562
0563 static void __exit virtio_exit(void)
0564 {
0565 bus_unregister(&virtio_bus);
0566 ida_destroy(&virtio_index_ida);
0567 }
0568 core_initcall(virtio_init);
0569 module_exit(virtio_exit);
0570
0571 MODULE_LICENSE("GPL");