lguest_bus.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*P:050 Lguest guests use a very simple bus for devices. It's a simple array
  2. * of device descriptors contained just above the top of normal memory. The
  3. * lguest bus is 80% tedious boilerplate code. :*/
  4. #include <linux/init.h>
  5. #include <linux/bootmem.h>
  6. #include <linux/lguest_bus.h>
  7. #include <asm/io.h>
  8. static ssize_t type_show(struct device *_dev,
  9. struct device_attribute *attr, char *buf)
  10. {
  11. struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
  12. return sprintf(buf, "%hu", lguest_devices[dev->index].type);
  13. }
  14. static ssize_t features_show(struct device *_dev,
  15. struct device_attribute *attr, char *buf)
  16. {
  17. struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
  18. return sprintf(buf, "%hx", lguest_devices[dev->index].features);
  19. }
  20. static ssize_t pfn_show(struct device *_dev,
  21. struct device_attribute *attr, char *buf)
  22. {
  23. struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
  24. return sprintf(buf, "%u", lguest_devices[dev->index].pfn);
  25. }
  26. static ssize_t status_show(struct device *_dev,
  27. struct device_attribute *attr, char *buf)
  28. {
  29. struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
  30. return sprintf(buf, "%hx", lguest_devices[dev->index].status);
  31. }
  32. static ssize_t status_store(struct device *_dev, struct device_attribute *attr,
  33. const char *buf, size_t count)
  34. {
  35. struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
  36. if (sscanf(buf, "%hi", &lguest_devices[dev->index].status) != 1)
  37. return -EINVAL;
  38. return count;
  39. }
  40. static struct device_attribute lguest_dev_attrs[] = {
  41. __ATTR_RO(type),
  42. __ATTR_RO(features),
  43. __ATTR_RO(pfn),
  44. __ATTR(status, 0644, status_show, status_store),
  45. __ATTR_NULL
  46. };
  47. /*D:130 The generic bus infrastructure requires a function which says whether a
  48. * device matches a driver. For us, it is simple: "struct lguest_driver"
  49. * contains a "device_type" field which indicates what type of device it can
  50. * handle, so we just cast the args and compare: */
  51. static int lguest_dev_match(struct device *_dev, struct device_driver *_drv)
  52. {
  53. struct lguest_device *dev = container_of(_dev,struct lguest_device,dev);
  54. struct lguest_driver *drv = container_of(_drv,struct lguest_driver,drv);
  55. return (drv->device_type == lguest_devices[dev->index].type);
  56. }
  57. /*:*/
  58. struct lguest_bus {
  59. struct bus_type bus;
  60. struct device dev;
  61. };
  62. static struct lguest_bus lguest_bus = {
  63. .bus = {
  64. .name = "lguest",
  65. .match = lguest_dev_match,
  66. .dev_attrs = lguest_dev_attrs,
  67. },
  68. .dev = {
  69. .parent = NULL,
  70. .bus_id = "lguest",
  71. }
  72. };
  73. /*D:140 This is the callback which occurs once the bus infrastructure matches
  74. * up a device and driver, ie. in response to add_lguest_device() calling
  75. * device_register(), or register_lguest_driver() calling driver_register().
  76. *
  77. * At the moment it's always the latter: the devices are added first, since
  78. * scan_devices() is called from a "core_initcall", and the drivers themselves
  79. * called later as a normal "initcall". But it would work the other way too.
  80. *
  81. * So now we have the happy couple, we add the status bit to indicate that we
  82. * found a driver. If the driver truly loves the device, it will return
  83. * happiness from its probe function (ok, perhaps this wasn't my greatest
  84. * analogy), and we set the final "driver ok" bit so the Host sees it's all
  85. * green. */
  86. static int lguest_dev_probe(struct device *_dev)
  87. {
  88. int ret;
  89. struct lguest_device*dev = container_of(_dev,struct lguest_device,dev);
  90. struct lguest_driver*drv = container_of(dev->dev.driver,
  91. struct lguest_driver, drv);
  92. lguest_devices[dev->index].status |= LGUEST_DEVICE_S_DRIVER;
  93. ret = drv->probe(dev);
  94. if (ret == 0)
  95. lguest_devices[dev->index].status |= LGUEST_DEVICE_S_DRIVER_OK;
  96. return ret;
  97. }
  98. /* The last part of the bus infrastructure is the function lguest drivers use
  99. * to register themselves. Firstly, we do nothing if there's no lguest bus
  100. * (ie. this is not a Guest), otherwise we fill in the embedded generic "struct
  101. * driver" fields and call the generic driver_register(). */
  102. int register_lguest_driver(struct lguest_driver *drv)
  103. {
  104. if (!lguest_devices)
  105. return 0;
  106. drv->drv.bus = &lguest_bus.bus;
  107. drv->drv.name = drv->name;
  108. drv->drv.owner = drv->owner;
  109. drv->drv.probe = lguest_dev_probe;
  110. return driver_register(&drv->drv);
  111. }
  112. /* At the moment we build all the drivers into the kernel because they're so
  113. * simple: 8144 bytes for all three of them as I type this. And as the console
  114. * really needs to be built in, it's actually only 3527 bytes for the network
  115. * and block drivers.
  116. *
  117. * If they get complex it will make sense for them to be modularized, so we
  118. * need to explicitly export the symbol.
  119. *
  120. * I don't think non-GPL modules make sense, so it's a GPL-only export.
  121. */
  122. EXPORT_SYMBOL_GPL(register_lguest_driver);
  123. /*D:120 This is the core of the lguest bus: actually adding a new device.
  124. * It's a separate function because it's neater that way, and because an
  125. * earlier version of the code supported hotplug and unplug. They were removed
  126. * early on because they were never used.
  127. *
  128. * As Andrew Tridgell says, "Untested code is buggy code".
  129. *
  130. * It's worth reading this carefully: we start with an index into the array of
  131. * "struct lguest_device_desc"s indicating the device which is new: */
  132. static void add_lguest_device(unsigned int index)
  133. {
  134. struct lguest_device *new;
  135. /* Each "struct lguest_device_desc" has a "status" field, which the
  136. * Guest updates as the device is probed. In the worst case, the Host
  137. * can look at these bits to tell what part of device setup failed,
  138. * even if the console isn't available. */
  139. lguest_devices[index].status |= LGUEST_DEVICE_S_ACKNOWLEDGE;
  140. new = kmalloc(sizeof(struct lguest_device), GFP_KERNEL);
  141. if (!new) {
  142. printk(KERN_EMERG "Cannot allocate lguest device %u\n", index);
  143. lguest_devices[index].status |= LGUEST_DEVICE_S_FAILED;
  144. return;
  145. }
  146. /* The "struct lguest_device" setup is pretty straight-forward example
  147. * code. */
  148. new->index = index;
  149. new->private = NULL;
  150. memset(&new->dev, 0, sizeof(new->dev));
  151. new->dev.parent = &lguest_bus.dev;
  152. new->dev.bus = &lguest_bus.bus;
  153. sprintf(new->dev.bus_id, "%u", index);
  154. /* device_register() causes the bus infrastructure to look for a
  155. * matching driver. */
  156. if (device_register(&new->dev) != 0) {
  157. printk(KERN_EMERG "Cannot register lguest device %u\n", index);
  158. lguest_devices[index].status |= LGUEST_DEVICE_S_FAILED;
  159. kfree(new);
  160. }
  161. }
  162. /*D:110 scan_devices() simply iterates through the device array. The type 0
  163. * is reserved to mean "no device", and anything else means we have found a
  164. * device: add it. */
  165. static void scan_devices(void)
  166. {
  167. unsigned int i;
  168. for (i = 0; i < LGUEST_MAX_DEVICES; i++)
  169. if (lguest_devices[i].type)
  170. add_lguest_device(i);
  171. }
  172. /*D:100 Fairly early in boot, lguest_bus_init() is called to set up the lguest
  173. * bus. We check that we are a Guest by checking paravirt_ops.name: there are
  174. * other ways of checking, but this seems most obvious to me.
  175. *
  176. * So we can access the array of "struct lguest_device_desc"s easily, we map
  177. * that memory and store the pointer in the global "lguest_devices". Then we
  178. * register the bus with the core. Doing two registrations seems clunky to me,
  179. * but it seems to be the correct sysfs incantation.
  180. *
  181. * Finally we call scan_devices() which adds all the devices found in the
  182. * "struct lguest_device_desc" array. */
  183. static int __init lguest_bus_init(void)
  184. {
  185. if (strcmp(paravirt_ops.name, "lguest") != 0)
  186. return 0;
  187. /* Devices are in a single page above top of "normal" mem */
  188. lguest_devices = lguest_map(max_pfn<<PAGE_SHIFT, 1);
  189. if (bus_register(&lguest_bus.bus) != 0
  190. || device_register(&lguest_bus.dev) != 0)
  191. panic("lguest bus registration failed");
  192. scan_devices();
  193. return 0;
  194. }
  195. /* Do this after core stuff, before devices. */
  196. postcore_initcall(lguest_bus_init);