diff options
Diffstat (limited to 'drivers/xen/xenbus/xenbus_probe_frontend.c')
-rw-r--r-- | drivers/xen/xenbus/xenbus_probe_frontend.c | 275 |
1 files changed, 275 insertions, 0 deletions
diff --git a/drivers/xen/xenbus/xenbus_probe_frontend.c b/drivers/xen/xenbus/xenbus_probe_frontend.c new file mode 100644 index 00000000000..04c4f012352 --- /dev/null +++ b/drivers/xen/xenbus/xenbus_probe_frontend.c | |||
@@ -0,0 +1,275 @@ | |||
1 | #define DPRINTK(fmt, args...) \ | ||
2 | pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \ | ||
3 | __func__, __LINE__, ##args) | ||
4 | |||
5 | #include <linux/kernel.h> | ||
6 | #include <linux/err.h> | ||
7 | #include <linux/string.h> | ||
8 | #include <linux/ctype.h> | ||
9 | #include <linux/fcntl.h> | ||
10 | #include <linux/mm.h> | ||
11 | #include <linux/proc_fs.h> | ||
12 | #include <linux/notifier.h> | ||
13 | #include <linux/kthread.h> | ||
14 | #include <linux/mutex.h> | ||
15 | #include <linux/io.h> | ||
16 | |||
17 | #include <asm/page.h> | ||
18 | #include <asm/pgtable.h> | ||
19 | #include <asm/xen/hypervisor.h> | ||
20 | #include <xen/xenbus.h> | ||
21 | #include <xen/events.h> | ||
22 | #include <xen/page.h> | ||
23 | |||
24 | #include <xen/platform_pci.h> | ||
25 | |||
26 | #include "xenbus_comms.h" | ||
27 | #include "xenbus_probe.h" | ||
28 | |||
29 | |||
30 | /* device/<type>/<id> => <type>-<id> */ | ||
31 | static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename) | ||
32 | { | ||
33 | nodename = strchr(nodename, '/'); | ||
34 | if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) { | ||
35 | printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename); | ||
36 | return -EINVAL; | ||
37 | } | ||
38 | |||
39 | strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE); | ||
40 | if (!strchr(bus_id, '/')) { | ||
41 | printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id); | ||
42 | return -EINVAL; | ||
43 | } | ||
44 | *strchr(bus_id, '/') = '-'; | ||
45 | return 0; | ||
46 | } | ||
47 | |||
48 | /* device/<typename>/<name> */ | ||
49 | static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type, const char *name) | ||
50 | { | ||
51 | char *nodename; | ||
52 | int err; | ||
53 | |||
54 | nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name); | ||
55 | if (!nodename) | ||
56 | return -ENOMEM; | ||
57 | |||
58 | DPRINTK("%s", nodename); | ||
59 | |||
60 | err = xenbus_probe_node(bus, type, nodename); | ||
61 | kfree(nodename); | ||
62 | return err; | ||
63 | } | ||
64 | |||
65 | static void backend_changed(struct xenbus_watch *watch, | ||
66 | const char **vec, unsigned int len) | ||
67 | { | ||
68 | xenbus_otherend_changed(watch, vec, len, 1); | ||
69 | } | ||
70 | |||
71 | static struct device_attribute xenbus_frontend_dev_attrs[] = { | ||
72 | __ATTR_NULL | ||
73 | }; | ||
74 | |||
75 | static struct xen_bus_type xenbus_frontend = { | ||
76 | .root = "device", | ||
77 | .levels = 2, /* device/type/<id> */ | ||
78 | .get_bus_id = frontend_bus_id, | ||
79 | .probe = xenbus_probe_frontend, | ||
80 | .otherend_changed = backend_changed, | ||
81 | .bus = { | ||
82 | .name = "xen", | ||
83 | .match = xenbus_match, | ||
84 | .uevent = xenbus_uevent, | ||
85 | .probe = xenbus_dev_probe, | ||
86 | .remove = xenbus_dev_remove, | ||
87 | .shutdown = xenbus_dev_shutdown, | ||
88 | .dev_attrs= xenbus_frontend_dev_attrs, | ||
89 | |||
90 | .suspend = xenbus_dev_suspend, | ||
91 | .resume = xenbus_dev_resume, | ||
92 | }, | ||
93 | }; | ||
94 | |||
95 | static void frontend_changed(struct xenbus_watch *watch, | ||
96 | const char **vec, unsigned int len) | ||
97 | { | ||
98 | DPRINTK(""); | ||
99 | |||
100 | xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend); | ||
101 | } | ||
102 | |||
103 | |||
104 | /* We watch for devices appearing and vanishing. */ | ||
105 | static struct xenbus_watch fe_watch = { | ||
106 | .node = "device", | ||
107 | .callback = frontend_changed, | ||
108 | }; | ||
109 | |||
110 | static int read_backend_details(struct xenbus_device *xendev) | ||
111 | { | ||
112 | return xenbus_read_otherend_details(xendev, "backend-id", "backend"); | ||
113 | } | ||
114 | |||
115 | static int is_device_connecting(struct device *dev, void *data) | ||
116 | { | ||
117 | struct xenbus_device *xendev = to_xenbus_device(dev); | ||
118 | struct device_driver *drv = data; | ||
119 | struct xenbus_driver *xendrv; | ||
120 | |||
121 | /* | ||
122 | * A device with no driver will never connect. We care only about | ||
123 | * devices which should currently be in the process of connecting. | ||
124 | */ | ||
125 | if (!dev->driver) | ||
126 | return 0; | ||
127 | |||
128 | /* Is this search limited to a particular driver? */ | ||
129 | if (drv && (dev->driver != drv)) | ||
130 | return 0; | ||
131 | |||
132 | xendrv = to_xenbus_driver(dev->driver); | ||
133 | return (xendev->state < XenbusStateConnected || | ||
134 | (xendev->state == XenbusStateConnected && | ||
135 | xendrv->is_ready && !xendrv->is_ready(xendev))); | ||
136 | } | ||
137 | |||
138 | static int exists_connecting_device(struct device_driver *drv) | ||
139 | { | ||
140 | return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, | ||
141 | is_device_connecting); | ||
142 | } | ||
143 | |||
144 | static int print_device_status(struct device *dev, void *data) | ||
145 | { | ||
146 | struct xenbus_device *xendev = to_xenbus_device(dev); | ||
147 | struct device_driver *drv = data; | ||
148 | |||
149 | /* Is this operation limited to a particular driver? */ | ||
150 | if (drv && (dev->driver != drv)) | ||
151 | return 0; | ||
152 | |||
153 | if (!dev->driver) { | ||
154 | /* Information only: is this too noisy? */ | ||
155 | printk(KERN_INFO "XENBUS: Device with no driver: %s\n", | ||
156 | xendev->nodename); | ||
157 | } else if (xendev->state < XenbusStateConnected) { | ||
158 | enum xenbus_state rstate = XenbusStateUnknown; | ||
159 | if (xendev->otherend) | ||
160 | rstate = xenbus_read_driver_state(xendev->otherend); | ||
161 | printk(KERN_WARNING "XENBUS: Timeout connecting " | ||
162 | "to device: %s (local state %d, remote state %d)\n", | ||
163 | xendev->nodename, xendev->state, rstate); | ||
164 | } | ||
165 | |||
166 | return 0; | ||
167 | } | ||
168 | |||
169 | /* We only wait for device setup after most initcalls have run. */ | ||
170 | static int ready_to_wait_for_devices; | ||
171 | |||
172 | /* | ||
173 | * On a 5-minute timeout, wait for all devices currently configured. We need | ||
174 | * to do this to guarantee that the filesystems and / or network devices | ||
175 | * needed for boot are available, before we can allow the boot to proceed. | ||
176 | * | ||
177 | * This needs to be on a late_initcall, to happen after the frontend device | ||
178 | * drivers have been initialised, but before the root fs is mounted. | ||
179 | * | ||
180 | * A possible improvement here would be to have the tools add a per-device | ||
181 | * flag to the store entry, indicating whether it is needed at boot time. | ||
182 | * This would allow people who knew what they were doing to accelerate their | ||
183 | * boot slightly, but of course needs tools or manual intervention to set up | ||
184 | * those flags correctly. | ||
185 | */ | ||
186 | static void wait_for_devices(struct xenbus_driver *xendrv) | ||
187 | { | ||
188 | unsigned long start = jiffies; | ||
189 | struct device_driver *drv = xendrv ? &xendrv->driver : NULL; | ||
190 | unsigned int seconds_waited = 0; | ||
191 | |||
192 | if (!ready_to_wait_for_devices || !xen_domain()) | ||
193 | return; | ||
194 | |||
195 | while (exists_connecting_device(drv)) { | ||
196 | if (time_after(jiffies, start + (seconds_waited+5)*HZ)) { | ||
197 | if (!seconds_waited) | ||
198 | printk(KERN_WARNING "XENBUS: Waiting for " | ||
199 | "devices to initialise: "); | ||
200 | seconds_waited += 5; | ||
201 | printk("%us...", 300 - seconds_waited); | ||
202 | if (seconds_waited == 300) | ||
203 | break; | ||
204 | } | ||
205 | |||
206 | schedule_timeout_interruptible(HZ/10); | ||
207 | } | ||
208 | |||
209 | if (seconds_waited) | ||
210 | printk("\n"); | ||
211 | |||
212 | bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, | ||
213 | print_device_status); | ||
214 | } | ||
215 | |||
216 | int __xenbus_register_frontend(struct xenbus_driver *drv, | ||
217 | struct module *owner, const char *mod_name) | ||
218 | { | ||
219 | int ret; | ||
220 | |||
221 | drv->read_otherend_details = read_backend_details; | ||
222 | |||
223 | ret = xenbus_register_driver_common(drv, &xenbus_frontend, | ||
224 | owner, mod_name); | ||
225 | if (ret) | ||
226 | return ret; | ||
227 | |||
228 | /* If this driver is loaded as a module wait for devices to attach. */ | ||
229 | wait_for_devices(drv); | ||
230 | |||
231 | return 0; | ||
232 | } | ||
233 | EXPORT_SYMBOL_GPL(__xenbus_register_frontend); | ||
234 | |||
235 | static int __init xenbus_probe_frontend_init(void) | ||
236 | { | ||
237 | int err; | ||
238 | |||
239 | DPRINTK(""); | ||
240 | |||
241 | /* Register ourselves with the kernel bus subsystem */ | ||
242 | err = bus_register(&xenbus_frontend.bus); | ||
243 | if (err) { | ||
244 | printk(KERN_CRIT "%s didn't register bus!\n", __func__); | ||
245 | return err; | ||
246 | } | ||
247 | printk(KERN_CRIT "%s bus registered ok\n", __func__); | ||
248 | |||
249 | if (!xen_initial_domain()) { | ||
250 | /* Enumerate devices in xenstore and watch for changes. */ | ||
251 | xenbus_probe_devices(&xenbus_frontend); | ||
252 | printk(KERN_CRIT "%s devices probed ok\n", __func__); | ||
253 | register_xenbus_watch(&fe_watch); | ||
254 | printk(KERN_CRIT "%s watch add ok ok\n", __func__); | ||
255 | printk(KERN_CRIT "%s all done\n", __func__); | ||
256 | } | ||
257 | |||
258 | return 0; | ||
259 | } | ||
260 | |||
261 | module_init(xenbus_probe_frontend_init); | ||
262 | |||
263 | #ifndef MODULE | ||
264 | static int __init boot_wait_for_devices(void) | ||
265 | { | ||
266 | if (xen_hvm_domain() && !xen_platform_pci_unplug) | ||
267 | return -ENODEV; | ||
268 | |||
269 | ready_to_wait_for_devices = 1; | ||
270 | wait_for_devices(NULL); | ||
271 | return 0; | ||
272 | } | ||
273 | |||
274 | late_initcall(boot_wait_for_devices); | ||
275 | #endif | ||