virtio.c 6.5 KB

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