virtio.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 are always preserved to pass to set_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. /* They should never have set feature bits beyond 32 */
  108. dev->config->set_features(dev, dev->features[0]);
  109. add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
  110. }
  111. return err;
  112. }
  113. static int virtio_dev_remove(struct device *_d)
  114. {
  115. struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
  116. struct virtio_driver *drv = container_of(dev->dev.driver,
  117. struct virtio_driver, driver);
  118. drv->remove(dev);
  119. /* Driver should have reset device. */
  120. BUG_ON(dev->config->get_status(dev));
  121. /* Acknowledge the device's existence again. */
  122. add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  123. return 0;
  124. }
  125. static struct bus_type virtio_bus = {
  126. .name = "virtio",
  127. .match = virtio_dev_match,
  128. .dev_attrs = virtio_dev_attrs,
  129. .uevent = virtio_uevent,
  130. .probe = virtio_dev_probe,
  131. .remove = virtio_dev_remove,
  132. };
  133. int register_virtio_driver(struct virtio_driver *driver)
  134. {
  135. /* Catch this early. */
  136. BUG_ON(driver->feature_table_size && !driver->feature_table);
  137. driver->driver.bus = &virtio_bus;
  138. return driver_register(&driver->driver);
  139. }
  140. EXPORT_SYMBOL_GPL(register_virtio_driver);
  141. void unregister_virtio_driver(struct virtio_driver *driver)
  142. {
  143. driver_unregister(&driver->driver);
  144. }
  145. EXPORT_SYMBOL_GPL(unregister_virtio_driver);
  146. int register_virtio_device(struct virtio_device *dev)
  147. {
  148. int err;
  149. dev->dev.bus = &virtio_bus;
  150. /* Assign a unique device index and hence name. */
  151. dev->index = dev_index++;
  152. sprintf(dev->dev.bus_id, "virtio%u", dev->index);
  153. /* We always start by resetting the device, in case a previous
  154. * driver messed it up. This also tests that code path a little. */
  155. dev->config->reset(dev);
  156. /* Acknowledge that we've seen the device. */
  157. add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  158. /* device_register() causes the bus infrastructure to look for a
  159. * matching driver. */
  160. err = device_register(&dev->dev);
  161. if (err)
  162. add_status(dev, VIRTIO_CONFIG_S_FAILED);
  163. return err;
  164. }
  165. EXPORT_SYMBOL_GPL(register_virtio_device);
  166. void unregister_virtio_device(struct virtio_device *dev)
  167. {
  168. device_unregister(&dev->dev);
  169. }
  170. EXPORT_SYMBOL_GPL(unregister_virtio_device);
  171. static int virtio_init(void)
  172. {
  173. if (bus_register(&virtio_bus) != 0)
  174. panic("virtio bus registration failed");
  175. return 0;
  176. }
  177. static void __exit virtio_exit(void)
  178. {
  179. bus_unregister(&virtio_bus);
  180. }
  181. core_initcall(virtio_init);
  182. module_exit(virtio_exit);
  183. MODULE_LICENSE("GPL");