virtio.c 8.9 KB

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