virtio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. err = virtio_finalize_features(dev);
  199. if (err)
  200. goto err;
  201. err = drv->probe(dev);
  202. if (err)
  203. goto err;
  204. /* If probe didn't do it, mark device DRIVER_OK ourselves. */
  205. if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
  206. virtio_device_ready(dev);
  207. if (drv->scan)
  208. drv->scan(dev);
  209. virtio_config_enable(dev);
  210. return 0;
  211. err:
  212. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  213. return err;
  214. }
  215. static int virtio_dev_remove(struct device *_d)
  216. {
  217. struct virtio_device *dev = dev_to_virtio(_d);
  218. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  219. virtio_config_disable(dev);
  220. drv->remove(dev);
  221. /* Driver should have reset device. */
  222. WARN_ON_ONCE(dev->config->get_status(dev));
  223. /* Acknowledge the device's existence again. */
  224. virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  225. return 0;
  226. }
  227. static struct bus_type virtio_bus = {
  228. .name = "virtio",
  229. .match = virtio_dev_match,
  230. .dev_groups = virtio_dev_groups,
  231. .uevent = virtio_uevent,
  232. .probe = virtio_dev_probe,
  233. .remove = virtio_dev_remove,
  234. };
  235. int register_virtio_driver(struct virtio_driver *driver)
  236. {
  237. /* Catch this early. */
  238. BUG_ON(driver->feature_table_size && !driver->feature_table);
  239. driver->driver.bus = &virtio_bus;
  240. return driver_register(&driver->driver);
  241. }
  242. EXPORT_SYMBOL_GPL(register_virtio_driver);
  243. void unregister_virtio_driver(struct virtio_driver *driver)
  244. {
  245. driver_unregister(&driver->driver);
  246. }
  247. EXPORT_SYMBOL_GPL(unregister_virtio_driver);
  248. int register_virtio_device(struct virtio_device *dev)
  249. {
  250. int err;
  251. dev->dev.bus = &virtio_bus;
  252. /* Assign a unique device index and hence name. */
  253. err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
  254. if (err < 0)
  255. goto out;
  256. dev->index = err;
  257. dev_set_name(&dev->dev, "virtio%u", dev->index);
  258. spin_lock_init(&dev->config_lock);
  259. dev->config_enabled = false;
  260. dev->config_change_pending = false;
  261. /* We always start by resetting the device, in case a previous
  262. * driver messed it up. This also tests that code path a little. */
  263. dev->config->reset(dev);
  264. /* Acknowledge that we've seen the device. */
  265. virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  266. INIT_LIST_HEAD(&dev->vqs);
  267. /* device_register() causes the bus infrastructure to look for a
  268. * matching driver. */
  269. err = device_register(&dev->dev);
  270. out:
  271. if (err)
  272. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  273. return err;
  274. }
  275. EXPORT_SYMBOL_GPL(register_virtio_device);
  276. void unregister_virtio_device(struct virtio_device *dev)
  277. {
  278. int index = dev->index; /* save for after device release */
  279. device_unregister(&dev->dev);
  280. ida_simple_remove(&virtio_index_ida, index);
  281. }
  282. EXPORT_SYMBOL_GPL(unregister_virtio_device);
  283. #ifdef CONFIG_PM_SLEEP
  284. int virtio_device_freeze(struct virtio_device *dev)
  285. {
  286. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  287. virtio_config_disable(dev);
  288. dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
  289. if (drv && drv->freeze)
  290. return drv->freeze(dev);
  291. return 0;
  292. }
  293. EXPORT_SYMBOL_GPL(virtio_device_freeze);
  294. int virtio_device_restore(struct virtio_device *dev)
  295. {
  296. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  297. int ret;
  298. /* We always start by resetting the device, in case a previous
  299. * driver messed it up. */
  300. dev->config->reset(dev);
  301. /* Acknowledge that we've seen the device. */
  302. virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  303. /* Maybe driver failed before freeze.
  304. * Restore the failed status, for debugging. */
  305. if (dev->failed)
  306. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  307. if (!drv)
  308. return 0;
  309. /* We have a driver! */
  310. virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
  311. ret = virtio_finalize_features(dev);
  312. if (ret)
  313. goto err;
  314. if (drv->restore) {
  315. ret = drv->restore(dev);
  316. if (ret)
  317. goto err;
  318. }
  319. /* Finally, tell the device we're all set */
  320. virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
  321. virtio_config_enable(dev);
  322. return 0;
  323. err:
  324. virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
  325. return ret;
  326. }
  327. EXPORT_SYMBOL_GPL(virtio_device_restore);
  328. #endif
  329. static int virtio_init(void)
  330. {
  331. if (bus_register(&virtio_bus) != 0)
  332. panic("virtio bus registration failed");
  333. return 0;
  334. }
  335. static void __exit virtio_exit(void)
  336. {
  337. bus_unregister(&virtio_bus);
  338. ida_destroy(&virtio_index_ida);
  339. }
  340. core_initcall(virtio_init);
  341. module_exit(virtio_exit);
  342. MODULE_LICENSE("GPL");