virtio.c 5.9 KB

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