virtio.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 < ARRAY_SIZE(dev->features)*BITS_PER_LONG; i++)
  46. len += sprintf(buf+len, "%c",
  47. test_bit(i, dev->features) ? '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 int virtio_dev_probe(struct device *_d)
  103. {
  104. int err, i;
  105. struct virtio_device *dev = dev_to_virtio(_d);
  106. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  107. u32 device_features;
  108. /* We have a driver! */
  109. add_status(dev, VIRTIO_CONFIG_S_DRIVER);
  110. /* Figure out what features the device supports. */
  111. device_features = dev->config->get_features(dev);
  112. /* Features supported by both device and driver into dev->features. */
  113. memset(dev->features, 0, sizeof(dev->features));
  114. for (i = 0; i < drv->feature_table_size; i++) {
  115. unsigned int f = drv->feature_table[i];
  116. BUG_ON(f >= 32);
  117. if (device_features & (1 << f))
  118. set_bit(f, dev->features);
  119. }
  120. /* Transport features always preserved to pass to finalize_features. */
  121. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
  122. if (device_features & (1 << i))
  123. set_bit(i, dev->features);
  124. dev->config->finalize_features(dev);
  125. err = drv->probe(dev);
  126. if (err)
  127. add_status(dev, VIRTIO_CONFIG_S_FAILED);
  128. else {
  129. add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
  130. if (drv->scan)
  131. drv->scan(dev);
  132. }
  133. return err;
  134. }
  135. static int virtio_dev_remove(struct device *_d)
  136. {
  137. struct virtio_device *dev = dev_to_virtio(_d);
  138. struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
  139. drv->remove(dev);
  140. /* Driver should have reset device. */
  141. WARN_ON_ONCE(dev->config->get_status(dev));
  142. /* Acknowledge the device's existence again. */
  143. add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  144. return 0;
  145. }
  146. static struct bus_type virtio_bus = {
  147. .name = "virtio",
  148. .match = virtio_dev_match,
  149. .dev_groups = virtio_dev_groups,
  150. .uevent = virtio_uevent,
  151. .probe = virtio_dev_probe,
  152. .remove = virtio_dev_remove,
  153. };
  154. int register_virtio_driver(struct virtio_driver *driver)
  155. {
  156. /* Catch this early. */
  157. BUG_ON(driver->feature_table_size && !driver->feature_table);
  158. driver->driver.bus = &virtio_bus;
  159. return driver_register(&driver->driver);
  160. }
  161. EXPORT_SYMBOL_GPL(register_virtio_driver);
  162. void unregister_virtio_driver(struct virtio_driver *driver)
  163. {
  164. driver_unregister(&driver->driver);
  165. }
  166. EXPORT_SYMBOL_GPL(unregister_virtio_driver);
  167. int register_virtio_device(struct virtio_device *dev)
  168. {
  169. int err;
  170. dev->dev.bus = &virtio_bus;
  171. /* Assign a unique device index and hence name. */
  172. err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
  173. if (err < 0)
  174. goto out;
  175. dev->index = err;
  176. dev_set_name(&dev->dev, "virtio%u", dev->index);
  177. /* We always start by resetting the device, in case a previous
  178. * driver messed it up. This also tests that code path a little. */
  179. dev->config->reset(dev);
  180. /* Acknowledge that we've seen the device. */
  181. add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  182. INIT_LIST_HEAD(&dev->vqs);
  183. /* device_register() causes the bus infrastructure to look for a
  184. * matching driver. */
  185. err = device_register(&dev->dev);
  186. out:
  187. if (err)
  188. add_status(dev, VIRTIO_CONFIG_S_FAILED);
  189. return err;
  190. }
  191. EXPORT_SYMBOL_GPL(register_virtio_device);
  192. void unregister_virtio_device(struct virtio_device *dev)
  193. {
  194. int index = dev->index; /* save for after device release */
  195. device_unregister(&dev->dev);
  196. ida_simple_remove(&virtio_index_ida, index);
  197. }
  198. EXPORT_SYMBOL_GPL(unregister_virtio_device);
  199. static int virtio_init(void)
  200. {
  201. if (bus_register(&virtio_bus) != 0)
  202. panic("virtio bus registration failed");
  203. return 0;
  204. }
  205. static void __exit virtio_exit(void)
  206. {
  207. bus_unregister(&virtio_bus);
  208. }
  209. core_initcall(virtio_init);
  210. module_exit(virtio_exit);
  211. MODULE_LICENSE("GPL");