virtio.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 = container_of(_d,struct virtio_device,dev);
  12. return sprintf(buf, "0x%04x\n", dev->id.device);
  13. }
  14. static ssize_t vendor_show(struct device *_d,
  15. struct device_attribute *attr, char *buf)
  16. {
  17. struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
  18. return sprintf(buf, "0x%04x\n", dev->id.vendor);
  19. }
  20. static ssize_t status_show(struct device *_d,
  21. struct device_attribute *attr, char *buf)
  22. {
  23. struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
  24. return sprintf(buf, "0x%08x\n", dev->config->get_status(dev));
  25. }
  26. static ssize_t modalias_show(struct device *_d,
  27. struct device_attribute *attr, char *buf)
  28. {
  29. struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
  30. return sprintf(buf, "virtio:d%08Xv%08X\n",
  31. dev->id.device, dev->id.vendor);
  32. }
  33. static ssize_t features_show(struct device *_d,
  34. struct device_attribute *attr, char *buf)
  35. {
  36. struct virtio_device *dev = container_of(_d, struct virtio_device, dev);
  37. unsigned int i;
  38. ssize_t len = 0;
  39. /* We actually represent this as a bitstring, as it could be
  40. * arbitrary length in future. */
  41. for (i = 0; i < ARRAY_SIZE(dev->features)*BITS_PER_LONG; i++)
  42. len += sprintf(buf+len, "%c",
  43. test_bit(i, dev->features) ? '1' : '0');
  44. len += sprintf(buf+len, "\n");
  45. return len;
  46. }
  47. static struct device_attribute virtio_dev_attrs[] = {
  48. __ATTR_RO(device),
  49. __ATTR_RO(vendor),
  50. __ATTR_RO(status),
  51. __ATTR_RO(modalias),
  52. __ATTR_RO(features),
  53. __ATTR_NULL
  54. };
  55. static inline int virtio_id_match(const struct virtio_device *dev,
  56. const struct virtio_device_id *id)
  57. {
  58. if (id->device != dev->id.device && id->device != VIRTIO_DEV_ANY_ID)
  59. return 0;
  60. return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor == dev->id.vendor;
  61. }
  62. /* This looks through all the IDs a driver claims to support. If any of them
  63. * match, we return 1 and the kernel will call virtio_dev_probe(). */
  64. static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
  65. {
  66. unsigned int i;
  67. struct virtio_device *dev = container_of(_dv,struct virtio_device,dev);
  68. const struct virtio_device_id *ids;
  69. ids = container_of(_dr, struct virtio_driver, driver)->id_table;
  70. for (i = 0; ids[i].device; i++)
  71. if (virtio_id_match(dev, &ids[i]))
  72. return 1;
  73. return 0;
  74. }
  75. static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
  76. {
  77. struct virtio_device *dev = container_of(_dv,struct virtio_device,dev);
  78. return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
  79. dev->id.device, dev->id.vendor);
  80. }
  81. static void add_status(struct virtio_device *dev, unsigned status)
  82. {
  83. dev->config->set_status(dev, dev->config->get_status(dev) | status);
  84. }
  85. void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
  86. unsigned int fbit)
  87. {
  88. unsigned int i;
  89. struct virtio_driver *drv = container_of(vdev->dev.driver,
  90. struct virtio_driver, driver);
  91. for (i = 0; i < drv->feature_table_size; i++)
  92. if (drv->feature_table[i] == fbit)
  93. return;
  94. BUG();
  95. }
  96. EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
  97. static int virtio_dev_probe(struct device *_d)
  98. {
  99. int err, i;
  100. struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
  101. struct virtio_driver *drv = container_of(dev->dev.driver,
  102. struct virtio_driver, driver);
  103. u32 device_features;
  104. /* We have a driver! */
  105. add_status(dev, VIRTIO_CONFIG_S_DRIVER);
  106. /* Figure out what features the device supports. */
  107. device_features = dev->config->get_features(dev);
  108. /* Features supported by both device and driver into dev->features. */
  109. memset(dev->features, 0, sizeof(dev->features));
  110. for (i = 0; i < drv->feature_table_size; i++) {
  111. unsigned int f = drv->feature_table[i];
  112. BUG_ON(f >= 32);
  113. if (device_features & (1 << f))
  114. set_bit(f, dev->features);
  115. }
  116. /* Transport features always preserved to pass to finalize_features. */
  117. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
  118. if (device_features & (1 << i))
  119. set_bit(i, dev->features);
  120. dev->config->finalize_features(dev);
  121. err = drv->probe(dev);
  122. if (err)
  123. add_status(dev, VIRTIO_CONFIG_S_FAILED);
  124. else {
  125. add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
  126. if (drv->scan)
  127. drv->scan(dev);
  128. }
  129. return err;
  130. }
  131. static int virtio_dev_remove(struct device *_d)
  132. {
  133. struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
  134. struct virtio_driver *drv = container_of(dev->dev.driver,
  135. struct virtio_driver, driver);
  136. drv->remove(dev);
  137. /* Driver should have reset device. */
  138. BUG_ON(dev->config->get_status(dev));
  139. /* Acknowledge the device's existence again. */
  140. add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  141. return 0;
  142. }
  143. static struct bus_type virtio_bus = {
  144. .name = "virtio",
  145. .match = virtio_dev_match,
  146. .dev_attrs = virtio_dev_attrs,
  147. .uevent = virtio_uevent,
  148. .probe = virtio_dev_probe,
  149. .remove = virtio_dev_remove,
  150. };
  151. int register_virtio_driver(struct virtio_driver *driver)
  152. {
  153. /* Catch this early. */
  154. BUG_ON(driver->feature_table_size && !driver->feature_table);
  155. driver->driver.bus = &virtio_bus;
  156. return driver_register(&driver->driver);
  157. }
  158. EXPORT_SYMBOL_GPL(register_virtio_driver);
  159. void unregister_virtio_driver(struct virtio_driver *driver)
  160. {
  161. driver_unregister(&driver->driver);
  162. }
  163. EXPORT_SYMBOL_GPL(unregister_virtio_driver);
  164. int register_virtio_device(struct virtio_device *dev)
  165. {
  166. int err;
  167. dev->dev.bus = &virtio_bus;
  168. /* Assign a unique device index and hence name. */
  169. err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
  170. if (err < 0)
  171. goto out;
  172. dev->index = err;
  173. dev_set_name(&dev->dev, "virtio%u", dev->index);
  174. /* We always start by resetting the device, in case a previous
  175. * driver messed it up. This also tests that code path a little. */
  176. dev->config->reset(dev);
  177. /* Acknowledge that we've seen the device. */
  178. add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  179. INIT_LIST_HEAD(&dev->vqs);
  180. /* device_register() causes the bus infrastructure to look for a
  181. * matching driver. */
  182. err = device_register(&dev->dev);
  183. out:
  184. if (err)
  185. add_status(dev, VIRTIO_CONFIG_S_FAILED);
  186. return err;
  187. }
  188. EXPORT_SYMBOL_GPL(register_virtio_device);
  189. void unregister_virtio_device(struct virtio_device *dev)
  190. {
  191. device_unregister(&dev->dev);
  192. ida_simple_remove(&virtio_index_ida, dev->index);
  193. }
  194. EXPORT_SYMBOL_GPL(unregister_virtio_device);
  195. static int virtio_init(void)
  196. {
  197. if (bus_register(&virtio_bus) != 0)
  198. panic("virtio bus registration failed");
  199. return 0;
  200. }
  201. static void __exit virtio_exit(void)
  202. {
  203. bus_unregister(&virtio_bus);
  204. }
  205. core_initcall(virtio_init);
  206. module_exit(virtio_exit);
  207. MODULE_LICENSE("GPL");