virtio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. static void add_status(struct virtio_device *dev, unsigned status)
  89. {
  90. dev->config->set_status(dev, dev->config->get_status(dev) | status);
  91. }
  92. void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
  93. unsigned int fbit)
  94. {
  95. unsigned int i;
  96. struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver);
  97. for (i = 0; i < drv->feature_table_size; i++)
  98. if (drv->feature_table[i] == fbit)
  99. return;
  100. if (drv->feature_table_legacy) {
  101. for (i = 0; i < drv->feature_table_size_legacy; i++)
  102. if (drv->feature_table_legacy[i] == fbit)
  103. return;
  104. }
  105. BUG();
  106. }
  107. EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
  108. static void __virtio_config_changed(struct virtio_device *dev)
  109. {
  110. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  111. if (!dev->config_enabled)
  112. dev->config_change_pending = true;
  113. else if (drv && drv->config_changed)
  114. drv->config_changed(dev);
  115. }
  116. void virtio_config_changed(struct virtio_device *dev)
  117. {
  118. unsigned long flags;
  119. spin_lock_irqsave(&dev->config_lock, flags);
  120. __virtio_config_changed(dev);
  121. spin_unlock_irqrestore(&dev->config_lock, flags);
  122. }
  123. EXPORT_SYMBOL_GPL(virtio_config_changed);
  124. static void virtio_config_disable(struct virtio_device *dev)
  125. {
  126. spin_lock_irq(&dev->config_lock);
  127. dev->config_enabled = false;
  128. spin_unlock_irq(&dev->config_lock);
  129. }
  130. static void virtio_config_enable(struct virtio_device *dev)
  131. {
  132. spin_lock_irq(&dev->config_lock);
  133. dev->config_enabled = true;
  134. if (dev->config_change_pending)
  135. __virtio_config_changed(dev);
  136. dev->config_change_pending = false;
  137. spin_unlock_irq(&dev->config_lock);
  138. }
  139. static int virtio_finalize_features(struct virtio_device *dev)
  140. {
  141. int ret = dev->config->finalize_features(dev);
  142. unsigned status;
  143. if (ret)
  144. return ret;
  145. if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1))
  146. return 0;
  147. add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
  148. status = dev->config->get_status(dev);
  149. if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
  150. dev_err(&dev->dev, "virtio: device refuses features: %x\n",
  151. status);
  152. return -ENODEV;
  153. }
  154. return 0;
  155. }
  156. static int virtio_dev_probe(struct device *_d)
  157. {
  158. int err, i;
  159. struct virtio_device *dev = dev_to_virtio(_d);
  160. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  161. u64 device_features;
  162. u64 driver_features;
  163. u64 driver_features_legacy;
  164. /* We have a driver! */
  165. add_status(dev, VIRTIO_CONFIG_S_DRIVER);
  166. /* Figure out what features the device supports. */
  167. device_features = dev->config->get_features(dev);
  168. /* Figure out what features the driver supports. */
  169. driver_features = 0;
  170. for (i = 0; i < drv->feature_table_size; i++) {
  171. unsigned int f = drv->feature_table[i];
  172. BUG_ON(f >= 64);
  173. driver_features |= (1ULL << f);
  174. }
  175. /* Some drivers have a separate feature table for virtio v1.0 */
  176. if (drv->feature_table_legacy) {
  177. driver_features_legacy = 0;
  178. for (i = 0; i < drv->feature_table_size_legacy; i++) {
  179. unsigned int f = drv->feature_table_legacy[i];
  180. BUG_ON(f >= 64);
  181. driver_features_legacy |= (1ULL << f);
  182. }
  183. } else {
  184. driver_features_legacy = driver_features;
  185. }
  186. if (device_features & (1ULL << VIRTIO_F_VERSION_1))
  187. dev->features = driver_features & device_features;
  188. else
  189. dev->features = driver_features_legacy & device_features;
  190. /* Transport features always preserved to pass to finalize_features. */
  191. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
  192. if (device_features & (1ULL << i))
  193. __virtio_set_bit(dev, i);
  194. err = virtio_finalize_features(dev);
  195. if (err)
  196. goto err;
  197. err = drv->probe(dev);
  198. if (err)
  199. goto err;
  200. /* If probe didn't do it, mark device DRIVER_OK ourselves. */
  201. if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
  202. virtio_device_ready(dev);
  203. if (drv->scan)
  204. drv->scan(dev);
  205. virtio_config_enable(dev);
  206. return 0;
  207. err:
  208. add_status(dev, VIRTIO_CONFIG_S_FAILED);
  209. return err;
  210. }
  211. static int virtio_dev_remove(struct device *_d)
  212. {
  213. struct virtio_device *dev = dev_to_virtio(_d);
  214. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  215. virtio_config_disable(dev);
  216. drv->remove(dev);
  217. /* Driver should have reset device. */
  218. WARN_ON_ONCE(dev->config->get_status(dev));
  219. /* Acknowledge the device's existence again. */
  220. add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  221. return 0;
  222. }
  223. static struct bus_type virtio_bus = {
  224. .name = "virtio",
  225. .match = virtio_dev_match,
  226. .dev_groups = virtio_dev_groups,
  227. .uevent = virtio_uevent,
  228. .probe = virtio_dev_probe,
  229. .remove = virtio_dev_remove,
  230. };
  231. int register_virtio_driver(struct virtio_driver *driver)
  232. {
  233. /* Catch this early. */
  234. BUG_ON(driver->feature_table_size && !driver->feature_table);
  235. driver->driver.bus = &virtio_bus;
  236. return driver_register(&driver->driver);
  237. }
  238. EXPORT_SYMBOL_GPL(register_virtio_driver);
  239. void unregister_virtio_driver(struct virtio_driver *driver)
  240. {
  241. driver_unregister(&driver->driver);
  242. }
  243. EXPORT_SYMBOL_GPL(unregister_virtio_driver);
  244. int register_virtio_device(struct virtio_device *dev)
  245. {
  246. int err;
  247. dev->dev.bus = &virtio_bus;
  248. /* Assign a unique device index and hence name. */
  249. err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
  250. if (err < 0)
  251. goto out;
  252. dev->index = err;
  253. dev_set_name(&dev->dev, "virtio%u", dev->index);
  254. spin_lock_init(&dev->config_lock);
  255. dev->config_enabled = false;
  256. dev->config_change_pending = false;
  257. /* We always start by resetting the device, in case a previous
  258. * driver messed it up. This also tests that code path a little. */
  259. dev->config->reset(dev);
  260. /* Acknowledge that we've seen the device. */
  261. add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  262. INIT_LIST_HEAD(&dev->vqs);
  263. /* device_register() causes the bus infrastructure to look for a
  264. * matching driver. */
  265. err = device_register(&dev->dev);
  266. if (err)
  267. ida_simple_remove(&virtio_index_ida, dev->index);
  268. out:
  269. if (err)
  270. add_status(dev, VIRTIO_CONFIG_S_FAILED);
  271. return err;
  272. }
  273. EXPORT_SYMBOL_GPL(register_virtio_device);
  274. void unregister_virtio_device(struct virtio_device *dev)
  275. {
  276. int index = dev->index; /* save for after device release */
  277. device_unregister(&dev->dev);
  278. ida_simple_remove(&virtio_index_ida, index);
  279. }
  280. EXPORT_SYMBOL_GPL(unregister_virtio_device);
  281. #ifdef CONFIG_PM_SLEEP
  282. int virtio_device_freeze(struct virtio_device *dev)
  283. {
  284. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  285. virtio_config_disable(dev);
  286. dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
  287. if (drv && drv->freeze)
  288. return drv->freeze(dev);
  289. return 0;
  290. }
  291. EXPORT_SYMBOL_GPL(virtio_device_freeze);
  292. int virtio_device_restore(struct virtio_device *dev)
  293. {
  294. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  295. int ret;
  296. /* We always start by resetting the device, in case a previous
  297. * driver messed it up. */
  298. dev->config->reset(dev);
  299. /* Acknowledge that we've seen the device. */
  300. add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  301. /* Maybe driver failed before freeze.
  302. * Restore the failed status, for debugging. */
  303. if (dev->failed)
  304. add_status(dev, VIRTIO_CONFIG_S_FAILED);
  305. if (!drv)
  306. return 0;
  307. /* We have a driver! */
  308. add_status(dev, VIRTIO_CONFIG_S_DRIVER);
  309. ret = virtio_finalize_features(dev);
  310. if (ret)
  311. goto err;
  312. if (drv->restore) {
  313. ret = drv->restore(dev);
  314. if (ret)
  315. goto err;
  316. }
  317. /* Finally, tell the device we're all set */
  318. add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
  319. virtio_config_enable(dev);
  320. return 0;
  321. err:
  322. add_status(dev, VIRTIO_CONFIG_S_FAILED);
  323. return ret;
  324. }
  325. EXPORT_SYMBOL_GPL(virtio_device_restore);
  326. #endif
  327. static int virtio_init(void)
  328. {
  329. if (bus_register(&virtio_bus) != 0)
  330. panic("virtio bus registration failed");
  331. return 0;
  332. }
  333. static void __exit virtio_exit(void)
  334. {
  335. bus_unregister(&virtio_bus);
  336. ida_destroy(&virtio_index_ida);
  337. }
  338. core_initcall(virtio_init);
  339. module_exit(virtio_exit);
  340. MODULE_LICENSE("GPL");