virtio.c 6.3 KB

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