virtio.c 11 KB

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