virtio.c 12 KB

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