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