Commit | Line | Data |
---|---|---|
4bac07c9 JF |
1 | /****************************************************************************** |
2 | * Talks to Xen Store to figure out what devices we have. | |
3 | * | |
4 | * Copyright (C) 2005 Rusty Russell, IBM Corporation | |
5 | * Copyright (C) 2005 Mike Wray, Hewlett-Packard | |
6 | * Copyright (C) 2005, 2006 XenSource Ltd | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License version 2 | |
10 | * as published by the Free Software Foundation; or, when distributed | |
11 | * separately from the Linux kernel or incorporated into other | |
12 | * software packages, subject to the following license: | |
13 | * | |
14 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
15 | * of this source file (the "Software"), to deal in the Software without | |
16 | * restriction, including without limitation the rights to use, copy, modify, | |
17 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
18 | * and to permit persons to whom the Software is furnished to do so, subject to | |
19 | * the following conditions: | |
20 | * | |
21 | * The above copyright notice and this permission notice shall be included in | |
22 | * all copies or substantial portions of the Software. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
29 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
30 | * IN THE SOFTWARE. | |
31 | */ | |
32 | ||
33 | #define DPRINTK(fmt, args...) \ | |
34 | pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \ | |
35 | __func__, __LINE__, ##args) | |
36 | ||
37 | #include <linux/kernel.h> | |
38 | #include <linux/err.h> | |
39 | #include <linux/string.h> | |
40 | #include <linux/ctype.h> | |
41 | #include <linux/fcntl.h> | |
42 | #include <linux/mm.h> | |
1107ba88 | 43 | #include <linux/proc_fs.h> |
4bac07c9 JF |
44 | #include <linux/notifier.h> |
45 | #include <linux/kthread.h> | |
46 | #include <linux/mutex.h> | |
47 | #include <linux/io.h> | |
5a0e3ad6 | 48 | #include <linux/slab.h> |
4bac07c9 JF |
49 | |
50 | #include <asm/page.h> | |
51 | #include <asm/pgtable.h> | |
52 | #include <asm/xen/hypervisor.h> | |
1ccbf534 JF |
53 | |
54 | #include <xen/xen.h> | |
4bac07c9 JF |
55 | #include <xen/xenbus.h> |
56 | #include <xen/events.h> | |
57 | #include <xen/page.h> | |
58 | ||
bee6ab53 SY |
59 | #include <xen/hvm.h> |
60 | ||
4bac07c9 JF |
61 | #include "xenbus_comms.h" |
62 | #include "xenbus_probe.h" | |
63 | ||
1107ba88 | 64 | |
4bac07c9 | 65 | int xen_store_evtchn; |
1107ba88 AZ |
66 | EXPORT_SYMBOL(xen_store_evtchn); |
67 | ||
4bac07c9 JF |
68 | struct xenstore_domain_interface *xen_store_interface; |
69 | static unsigned long xen_store_mfn; | |
70 | ||
71 | static BLOCKING_NOTIFIER_HEAD(xenstore_chain); | |
72 | ||
73 | static void wait_for_devices(struct xenbus_driver *xendrv); | |
74 | ||
75 | static int xenbus_probe_frontend(const char *type, const char *name); | |
76 | ||
77 | static void xenbus_dev_shutdown(struct device *_dev); | |
78 | ||
de5b31bd IC |
79 | static int xenbus_dev_suspend(struct device *dev, pm_message_t state); |
80 | static int xenbus_dev_resume(struct device *dev); | |
81 | ||
4bac07c9 JF |
82 | /* If something in array of ids matches this device, return it. */ |
83 | static const struct xenbus_device_id * | |
84 | match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev) | |
85 | { | |
86 | for (; *arr->devicetype != '\0'; arr++) { | |
87 | if (!strcmp(arr->devicetype, dev->devicetype)) | |
88 | return arr; | |
89 | } | |
90 | return NULL; | |
91 | } | |
92 | ||
93 | int xenbus_match(struct device *_dev, struct device_driver *_drv) | |
94 | { | |
95 | struct xenbus_driver *drv = to_xenbus_driver(_drv); | |
96 | ||
97 | if (!drv->ids) | |
98 | return 0; | |
99 | ||
100 | return match_device(drv->ids, to_xenbus_device(_dev)) != NULL; | |
101 | } | |
102 | ||
d2f0c52b MM |
103 | static int xenbus_uevent(struct device *_dev, struct kobj_uevent_env *env) |
104 | { | |
105 | struct xenbus_device *dev = to_xenbus_device(_dev); | |
106 | ||
107 | if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype)) | |
108 | return -ENOMEM; | |
109 | ||
110 | return 0; | |
111 | } | |
112 | ||
4bac07c9 | 113 | /* device/<type>/<id> => <type>-<id> */ |
2a678cc5 | 114 | static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename) |
4bac07c9 JF |
115 | { |
116 | nodename = strchr(nodename, '/'); | |
2a678cc5 | 117 | if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) { |
4bac07c9 JF |
118 | printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename); |
119 | return -EINVAL; | |
120 | } | |
121 | ||
2a678cc5 | 122 | strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE); |
4bac07c9 JF |
123 | if (!strchr(bus_id, '/')) { |
124 | printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id); | |
125 | return -EINVAL; | |
126 | } | |
127 | *strchr(bus_id, '/') = '-'; | |
128 | return 0; | |
129 | } | |
130 | ||
131 | ||
132 | static void free_otherend_details(struct xenbus_device *dev) | |
133 | { | |
134 | kfree(dev->otherend); | |
135 | dev->otherend = NULL; | |
136 | } | |
137 | ||
138 | ||
139 | static void free_otherend_watch(struct xenbus_device *dev) | |
140 | { | |
141 | if (dev->otherend_watch.node) { | |
142 | unregister_xenbus_watch(&dev->otherend_watch); | |
143 | kfree(dev->otherend_watch.node); | |
144 | dev->otherend_watch.node = NULL; | |
145 | } | |
146 | } | |
147 | ||
148 | ||
149 | int read_otherend_details(struct xenbus_device *xendev, | |
150 | char *id_node, char *path_node) | |
151 | { | |
152 | int err = xenbus_gather(XBT_NIL, xendev->nodename, | |
153 | id_node, "%i", &xendev->otherend_id, | |
154 | path_node, NULL, &xendev->otherend, | |
155 | NULL); | |
156 | if (err) { | |
157 | xenbus_dev_fatal(xendev, err, | |
158 | "reading other end details from %s", | |
159 | xendev->nodename); | |
160 | return err; | |
161 | } | |
162 | if (strlen(xendev->otherend) == 0 || | |
163 | !xenbus_exists(XBT_NIL, xendev->otherend, "")) { | |
164 | xenbus_dev_fatal(xendev, -ENOENT, | |
165 | "unable to read other end from %s. " | |
166 | "missing or inaccessible.", | |
167 | xendev->nodename); | |
168 | free_otherend_details(xendev); | |
169 | return -ENOENT; | |
170 | } | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
175 | ||
176 | static int read_backend_details(struct xenbus_device *xendev) | |
177 | { | |
178 | return read_otherend_details(xendev, "backend-id", "backend"); | |
179 | } | |
180 | ||
1107ba88 AZ |
181 | static struct device_attribute xenbus_dev_attrs[] = { |
182 | __ATTR_NULL | |
183 | }; | |
4bac07c9 JF |
184 | |
185 | /* Bus type for frontend drivers. */ | |
186 | static struct xen_bus_type xenbus_frontend = { | |
187 | .root = "device", | |
188 | .levels = 2, /* device/type/<id> */ | |
189 | .get_bus_id = frontend_bus_id, | |
190 | .probe = xenbus_probe_frontend, | |
191 | .bus = { | |
1107ba88 AZ |
192 | .name = "xen", |
193 | .match = xenbus_match, | |
194 | .uevent = xenbus_uevent, | |
195 | .probe = xenbus_dev_probe, | |
196 | .remove = xenbus_dev_remove, | |
197 | .shutdown = xenbus_dev_shutdown, | |
198 | .dev_attrs = xenbus_dev_attrs, | |
de5b31bd IC |
199 | |
200 | .suspend = xenbus_dev_suspend, | |
201 | .resume = xenbus_dev_resume, | |
4bac07c9 JF |
202 | }, |
203 | }; | |
204 | ||
205 | static void otherend_changed(struct xenbus_watch *watch, | |
206 | const char **vec, unsigned int len) | |
207 | { | |
208 | struct xenbus_device *dev = | |
209 | container_of(watch, struct xenbus_device, otherend_watch); | |
210 | struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver); | |
211 | enum xenbus_state state; | |
212 | ||
213 | /* Protect us against watches firing on old details when the otherend | |
214 | details change, say immediately after a resume. */ | |
215 | if (!dev->otherend || | |
216 | strncmp(dev->otherend, vec[XS_WATCH_PATH], | |
217 | strlen(dev->otherend))) { | |
898eb71c JP |
218 | dev_dbg(&dev->dev, "Ignoring watch at %s\n", |
219 | vec[XS_WATCH_PATH]); | |
4bac07c9 JF |
220 | return; |
221 | } | |
222 | ||
223 | state = xenbus_read_driver_state(dev->otherend); | |
224 | ||
898eb71c | 225 | dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n", |
4bac07c9 JF |
226 | state, xenbus_strstate(state), dev->otherend_watch.node, |
227 | vec[XS_WATCH_PATH]); | |
228 | ||
229 | /* | |
230 | * Ignore xenbus transitions during shutdown. This prevents us doing | |
231 | * work that can fail e.g., when the rootfs is gone. | |
232 | */ | |
233 | if (system_state > SYSTEM_RUNNING) { | |
234 | struct xen_bus_type *bus = bus; | |
235 | bus = container_of(dev->dev.bus, struct xen_bus_type, bus); | |
236 | /* If we're frontend, drive the state machine to Closed. */ | |
237 | /* This should cause the backend to release our resources. */ | |
238 | if ((bus == &xenbus_frontend) && (state == XenbusStateClosing)) | |
239 | xenbus_frontend_closed(dev); | |
240 | return; | |
241 | } | |
242 | ||
243 | if (drv->otherend_changed) | |
244 | drv->otherend_changed(dev, state); | |
245 | } | |
246 | ||
247 | ||
248 | static int talk_to_otherend(struct xenbus_device *dev) | |
249 | { | |
250 | struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver); | |
251 | ||
252 | free_otherend_watch(dev); | |
253 | free_otherend_details(dev); | |
254 | ||
255 | return drv->read_otherend_details(dev); | |
256 | } | |
257 | ||
258 | ||
259 | static int watch_otherend(struct xenbus_device *dev) | |
260 | { | |
261 | return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed, | |
262 | "%s/%s", dev->otherend, "state"); | |
263 | } | |
264 | ||
265 | ||
266 | int xenbus_dev_probe(struct device *_dev) | |
267 | { | |
268 | struct xenbus_device *dev = to_xenbus_device(_dev); | |
269 | struct xenbus_driver *drv = to_xenbus_driver(_dev->driver); | |
270 | const struct xenbus_device_id *id; | |
271 | int err; | |
272 | ||
273 | DPRINTK("%s", dev->nodename); | |
274 | ||
275 | if (!drv->probe) { | |
276 | err = -ENODEV; | |
277 | goto fail; | |
278 | } | |
279 | ||
280 | id = match_device(drv->ids, dev); | |
281 | if (!id) { | |
282 | err = -ENODEV; | |
283 | goto fail; | |
284 | } | |
285 | ||
286 | err = talk_to_otherend(dev); | |
287 | if (err) { | |
288 | dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n", | |
289 | dev->nodename); | |
290 | return err; | |
291 | } | |
292 | ||
293 | err = drv->probe(dev, id); | |
294 | if (err) | |
295 | goto fail; | |
296 | ||
297 | err = watch_otherend(dev); | |
298 | if (err) { | |
299 | dev_warn(&dev->dev, "watch_otherend on %s failed.\n", | |
300 | dev->nodename); | |
301 | return err; | |
302 | } | |
303 | ||
304 | return 0; | |
305 | fail: | |
306 | xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename); | |
307 | xenbus_switch_state(dev, XenbusStateClosed); | |
308 | return -ENODEV; | |
309 | } | |
310 | ||
311 | int xenbus_dev_remove(struct device *_dev) | |
312 | { | |
313 | struct xenbus_device *dev = to_xenbus_device(_dev); | |
314 | struct xenbus_driver *drv = to_xenbus_driver(_dev->driver); | |
315 | ||
316 | DPRINTK("%s", dev->nodename); | |
317 | ||
318 | free_otherend_watch(dev); | |
319 | free_otherend_details(dev); | |
320 | ||
321 | if (drv->remove) | |
322 | drv->remove(dev); | |
323 | ||
324 | xenbus_switch_state(dev, XenbusStateClosed); | |
325 | return 0; | |
326 | } | |
327 | ||
328 | static void xenbus_dev_shutdown(struct device *_dev) | |
329 | { | |
330 | struct xenbus_device *dev = to_xenbus_device(_dev); | |
331 | unsigned long timeout = 5*HZ; | |
332 | ||
333 | DPRINTK("%s", dev->nodename); | |
334 | ||
335 | get_device(&dev->dev); | |
336 | if (dev->state != XenbusStateConnected) { | |
337 | printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__, | |
338 | dev->nodename, xenbus_strstate(dev->state)); | |
339 | goto out; | |
340 | } | |
341 | xenbus_switch_state(dev, XenbusStateClosing); | |
342 | timeout = wait_for_completion_timeout(&dev->down, timeout); | |
343 | if (!timeout) | |
344 | printk(KERN_INFO "%s: %s timeout closing device\n", | |
345 | __func__, dev->nodename); | |
346 | out: | |
347 | put_device(&dev->dev); | |
348 | } | |
349 | ||
350 | int xenbus_register_driver_common(struct xenbus_driver *drv, | |
351 | struct xen_bus_type *bus, | |
352 | struct module *owner, | |
353 | const char *mod_name) | |
354 | { | |
355 | drv->driver.name = drv->name; | |
356 | drv->driver.bus = &bus->bus; | |
357 | drv->driver.owner = owner; | |
358 | drv->driver.mod_name = mod_name; | |
359 | ||
360 | return driver_register(&drv->driver); | |
361 | } | |
362 | ||
363 | int __xenbus_register_frontend(struct xenbus_driver *drv, | |
364 | struct module *owner, const char *mod_name) | |
365 | { | |
366 | int ret; | |
367 | ||
368 | drv->read_otherend_details = read_backend_details; | |
369 | ||
370 | ret = xenbus_register_driver_common(drv, &xenbus_frontend, | |
371 | owner, mod_name); | |
372 | if (ret) | |
373 | return ret; | |
374 | ||
375 | /* If this driver is loaded as a module wait for devices to attach. */ | |
376 | wait_for_devices(drv); | |
377 | ||
378 | return 0; | |
379 | } | |
380 | EXPORT_SYMBOL_GPL(__xenbus_register_frontend); | |
381 | ||
382 | void xenbus_unregister_driver(struct xenbus_driver *drv) | |
383 | { | |
384 | driver_unregister(&drv->driver); | |
385 | } | |
386 | EXPORT_SYMBOL_GPL(xenbus_unregister_driver); | |
387 | ||
388 | struct xb_find_info | |
389 | { | |
390 | struct xenbus_device *dev; | |
391 | const char *nodename; | |
392 | }; | |
393 | ||
394 | static int cmp_dev(struct device *dev, void *data) | |
395 | { | |
396 | struct xenbus_device *xendev = to_xenbus_device(dev); | |
397 | struct xb_find_info *info = data; | |
398 | ||
399 | if (!strcmp(xendev->nodename, info->nodename)) { | |
400 | info->dev = xendev; | |
401 | get_device(dev); | |
402 | return 1; | |
403 | } | |
404 | return 0; | |
405 | } | |
406 | ||
407 | struct xenbus_device *xenbus_device_find(const char *nodename, | |
408 | struct bus_type *bus) | |
409 | { | |
410 | struct xb_find_info info = { .dev = NULL, .nodename = nodename }; | |
411 | ||
412 | bus_for_each_dev(bus, NULL, &info, cmp_dev); | |
413 | return info.dev; | |
414 | } | |
415 | ||
416 | static int cleanup_dev(struct device *dev, void *data) | |
417 | { | |
418 | struct xenbus_device *xendev = to_xenbus_device(dev); | |
419 | struct xb_find_info *info = data; | |
420 | int len = strlen(info->nodename); | |
421 | ||
422 | DPRINTK("%s", info->nodename); | |
423 | ||
424 | /* Match the info->nodename path, or any subdirectory of that path. */ | |
425 | if (strncmp(xendev->nodename, info->nodename, len)) | |
426 | return 0; | |
427 | ||
428 | /* If the node name is longer, ensure it really is a subdirectory. */ | |
429 | if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/')) | |
430 | return 0; | |
431 | ||
432 | info->dev = xendev; | |
433 | get_device(dev); | |
434 | return 1; | |
435 | } | |
436 | ||
437 | static void xenbus_cleanup_devices(const char *path, struct bus_type *bus) | |
438 | { | |
439 | struct xb_find_info info = { .nodename = path }; | |
440 | ||
441 | do { | |
442 | info.dev = NULL; | |
443 | bus_for_each_dev(bus, NULL, &info, cleanup_dev); | |
444 | if (info.dev) { | |
445 | device_unregister(&info.dev->dev); | |
446 | put_device(&info.dev->dev); | |
447 | } | |
448 | } while (info.dev); | |
449 | } | |
450 | ||
451 | static void xenbus_dev_release(struct device *dev) | |
452 | { | |
453 | if (dev) | |
454 | kfree(to_xenbus_device(dev)); | |
455 | } | |
456 | ||
457 | static ssize_t xendev_show_nodename(struct device *dev, | |
458 | struct device_attribute *attr, char *buf) | |
459 | { | |
460 | return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename); | |
461 | } | |
db05fed0 | 462 | static DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL); |
4bac07c9 JF |
463 | |
464 | static ssize_t xendev_show_devtype(struct device *dev, | |
465 | struct device_attribute *attr, char *buf) | |
466 | { | |
467 | return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype); | |
468 | } | |
db05fed0 | 469 | static DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL); |
4bac07c9 | 470 | |
d2f0c52b MM |
471 | static ssize_t xendev_show_modalias(struct device *dev, |
472 | struct device_attribute *attr, char *buf) | |
473 | { | |
474 | return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype); | |
475 | } | |
db05fed0 | 476 | static DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL); |
4bac07c9 JF |
477 | |
478 | int xenbus_probe_node(struct xen_bus_type *bus, | |
479 | const char *type, | |
480 | const char *nodename) | |
481 | { | |
2a678cc5 | 482 | char devname[XEN_BUS_ID_SIZE]; |
4bac07c9 JF |
483 | int err; |
484 | struct xenbus_device *xendev; | |
485 | size_t stringlen; | |
486 | char *tmpstring; | |
487 | ||
488 | enum xenbus_state state = xenbus_read_driver_state(nodename); | |
489 | ||
490 | if (state != XenbusStateInitialising) { | |
491 | /* Device is not new, so ignore it. This can happen if a | |
492 | device is going away after switching to Closed. */ | |
493 | return 0; | |
494 | } | |
495 | ||
496 | stringlen = strlen(nodename) + 1 + strlen(type) + 1; | |
497 | xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL); | |
498 | if (!xendev) | |
499 | return -ENOMEM; | |
500 | ||
501 | xendev->state = XenbusStateInitialising; | |
502 | ||
503 | /* Copy the strings into the extra space. */ | |
504 | ||
505 | tmpstring = (char *)(xendev + 1); | |
506 | strcpy(tmpstring, nodename); | |
507 | xendev->nodename = tmpstring; | |
508 | ||
509 | tmpstring += strlen(tmpstring) + 1; | |
510 | strcpy(tmpstring, type); | |
511 | xendev->devicetype = tmpstring; | |
512 | init_completion(&xendev->down); | |
513 | ||
514 | xendev->dev.bus = &bus->bus; | |
515 | xendev->dev.release = xenbus_dev_release; | |
516 | ||
2a678cc5 | 517 | err = bus->get_bus_id(devname, xendev->nodename); |
4bac07c9 JF |
518 | if (err) |
519 | goto fail; | |
520 | ||
2a678cc5 KS |
521 | dev_set_name(&xendev->dev, devname); |
522 | ||
4bac07c9 JF |
523 | /* Register with generic device framework. */ |
524 | err = device_register(&xendev->dev); | |
525 | if (err) | |
526 | goto fail; | |
527 | ||
528 | err = device_create_file(&xendev->dev, &dev_attr_nodename); | |
529 | if (err) | |
530 | goto fail_unregister; | |
531 | ||
532 | err = device_create_file(&xendev->dev, &dev_attr_devtype); | |
533 | if (err) | |
d2f0c52b MM |
534 | goto fail_remove_nodename; |
535 | ||
536 | err = device_create_file(&xendev->dev, &dev_attr_modalias); | |
537 | if (err) | |
538 | goto fail_remove_devtype; | |
4bac07c9 JF |
539 | |
540 | return 0; | |
d2f0c52b MM |
541 | fail_remove_devtype: |
542 | device_remove_file(&xendev->dev, &dev_attr_devtype); | |
543 | fail_remove_nodename: | |
4bac07c9 JF |
544 | device_remove_file(&xendev->dev, &dev_attr_nodename); |
545 | fail_unregister: | |
546 | device_unregister(&xendev->dev); | |
547 | fail: | |
548 | kfree(xendev); | |
549 | return err; | |
550 | } | |
551 | ||
552 | /* device/<typename>/<name> */ | |
553 | static int xenbus_probe_frontend(const char *type, const char *name) | |
554 | { | |
555 | char *nodename; | |
556 | int err; | |
557 | ||
558 | nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", | |
559 | xenbus_frontend.root, type, name); | |
560 | if (!nodename) | |
561 | return -ENOMEM; | |
562 | ||
563 | DPRINTK("%s", nodename); | |
564 | ||
565 | err = xenbus_probe_node(&xenbus_frontend, type, nodename); | |
566 | kfree(nodename); | |
567 | return err; | |
568 | } | |
569 | ||
570 | static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type) | |
571 | { | |
572 | int err = 0; | |
573 | char **dir; | |
574 | unsigned int dir_n = 0; | |
575 | int i; | |
576 | ||
577 | dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n); | |
578 | if (IS_ERR(dir)) | |
579 | return PTR_ERR(dir); | |
580 | ||
581 | for (i = 0; i < dir_n; i++) { | |
582 | err = bus->probe(type, dir[i]); | |
583 | if (err) | |
584 | break; | |
585 | } | |
586 | kfree(dir); | |
587 | return err; | |
588 | } | |
589 | ||
590 | int xenbus_probe_devices(struct xen_bus_type *bus) | |
591 | { | |
592 | int err = 0; | |
593 | char **dir; | |
594 | unsigned int i, dir_n; | |
595 | ||
596 | dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n); | |
597 | if (IS_ERR(dir)) | |
598 | return PTR_ERR(dir); | |
599 | ||
600 | for (i = 0; i < dir_n; i++) { | |
601 | err = xenbus_probe_device_type(bus, dir[i]); | |
602 | if (err) | |
603 | break; | |
604 | } | |
605 | kfree(dir); | |
606 | return err; | |
607 | } | |
608 | ||
609 | static unsigned int char_count(const char *str, char c) | |
610 | { | |
611 | unsigned int i, ret = 0; | |
612 | ||
613 | for (i = 0; str[i]; i++) | |
614 | if (str[i] == c) | |
615 | ret++; | |
616 | return ret; | |
617 | } | |
618 | ||
619 | static int strsep_len(const char *str, char c, unsigned int len) | |
620 | { | |
621 | unsigned int i; | |
622 | ||
623 | for (i = 0; str[i]; i++) | |
624 | if (str[i] == c) { | |
625 | if (len == 0) | |
626 | return i; | |
627 | len--; | |
628 | } | |
629 | return (len == 0) ? i : -ERANGE; | |
630 | } | |
631 | ||
632 | void xenbus_dev_changed(const char *node, struct xen_bus_type *bus) | |
633 | { | |
634 | int exists, rootlen; | |
635 | struct xenbus_device *dev; | |
2a678cc5 | 636 | char type[XEN_BUS_ID_SIZE]; |
4bac07c9 JF |
637 | const char *p, *root; |
638 | ||
639 | if (char_count(node, '/') < 2) | |
640 | return; | |
641 | ||
642 | exists = xenbus_exists(XBT_NIL, node, ""); | |
643 | if (!exists) { | |
644 | xenbus_cleanup_devices(node, &bus->bus); | |
645 | return; | |
646 | } | |
647 | ||
648 | /* backend/<type>/... or device/<type>/... */ | |
649 | p = strchr(node, '/') + 1; | |
2a678cc5 KS |
650 | snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p); |
651 | type[XEN_BUS_ID_SIZE-1] = '\0'; | |
4bac07c9 JF |
652 | |
653 | rootlen = strsep_len(node, '/', bus->levels); | |
654 | if (rootlen < 0) | |
655 | return; | |
656 | root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node); | |
657 | if (!root) | |
658 | return; | |
659 | ||
660 | dev = xenbus_device_find(root, &bus->bus); | |
661 | if (!dev) | |
662 | xenbus_probe_node(bus, type, root); | |
663 | else | |
664 | put_device(&dev->dev); | |
665 | ||
666 | kfree(root); | |
667 | } | |
c6a960ce | 668 | EXPORT_SYMBOL_GPL(xenbus_dev_changed); |
4bac07c9 JF |
669 | |
670 | static void frontend_changed(struct xenbus_watch *watch, | |
671 | const char **vec, unsigned int len) | |
672 | { | |
673 | DPRINTK(""); | |
674 | ||
675 | xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend); | |
676 | } | |
677 | ||
678 | /* We watch for devices appearing and vanishing. */ | |
679 | static struct xenbus_watch fe_watch = { | |
680 | .node = "device", | |
681 | .callback = frontend_changed, | |
682 | }; | |
683 | ||
de5b31bd | 684 | static int xenbus_dev_suspend(struct device *dev, pm_message_t state) |
4bac07c9 JF |
685 | { |
686 | int err = 0; | |
687 | struct xenbus_driver *drv; | |
688 | struct xenbus_device *xdev; | |
689 | ||
690 | DPRINTK(""); | |
691 | ||
692 | if (dev->driver == NULL) | |
693 | return 0; | |
694 | drv = to_xenbus_driver(dev->driver); | |
695 | xdev = container_of(dev, struct xenbus_device, dev); | |
696 | if (drv->suspend) | |
de5b31bd | 697 | err = drv->suspend(xdev, state); |
4bac07c9 JF |
698 | if (err) |
699 | printk(KERN_WARNING | |
2a678cc5 | 700 | "xenbus: suspend %s failed: %i\n", dev_name(dev), err); |
4bac07c9 JF |
701 | return 0; |
702 | } | |
703 | ||
de5b31bd | 704 | static int xenbus_dev_resume(struct device *dev) |
4bac07c9 JF |
705 | { |
706 | int err; | |
707 | struct xenbus_driver *drv; | |
708 | struct xenbus_device *xdev; | |
709 | ||
710 | DPRINTK(""); | |
711 | ||
712 | if (dev->driver == NULL) | |
713 | return 0; | |
714 | ||
715 | drv = to_xenbus_driver(dev->driver); | |
716 | xdev = container_of(dev, struct xenbus_device, dev); | |
717 | ||
718 | err = talk_to_otherend(xdev); | |
719 | if (err) { | |
720 | printk(KERN_WARNING | |
721 | "xenbus: resume (talk_to_otherend) %s failed: %i\n", | |
2a678cc5 | 722 | dev_name(dev), err); |
4bac07c9 JF |
723 | return err; |
724 | } | |
725 | ||
726 | xdev->state = XenbusStateInitialising; | |
727 | ||
728 | if (drv->resume) { | |
729 | err = drv->resume(xdev); | |
730 | if (err) { | |
731 | printk(KERN_WARNING | |
732 | "xenbus: resume %s failed: %i\n", | |
2a678cc5 | 733 | dev_name(dev), err); |
4bac07c9 JF |
734 | return err; |
735 | } | |
736 | } | |
737 | ||
738 | err = watch_otherend(xdev); | |
739 | if (err) { | |
740 | printk(KERN_WARNING | |
741 | "xenbus_probe: resume (watch_otherend) %s failed: " | |
2a678cc5 | 742 | "%d.\n", dev_name(dev), err); |
4bac07c9 JF |
743 | return err; |
744 | } | |
745 | ||
746 | return 0; | |
747 | } | |
748 | ||
4bac07c9 JF |
749 | /* A flag to determine if xenstored is 'ready' (i.e. has started) */ |
750 | int xenstored_ready = 0; | |
751 | ||
752 | ||
753 | int register_xenstore_notifier(struct notifier_block *nb) | |
754 | { | |
755 | int ret = 0; | |
756 | ||
757 | if (xenstored_ready > 0) | |
758 | ret = nb->notifier_call(nb, 0, NULL); | |
759 | else | |
760 | blocking_notifier_chain_register(&xenstore_chain, nb); | |
761 | ||
762 | return ret; | |
763 | } | |
764 | EXPORT_SYMBOL_GPL(register_xenstore_notifier); | |
765 | ||
766 | void unregister_xenstore_notifier(struct notifier_block *nb) | |
767 | { | |
768 | blocking_notifier_chain_unregister(&xenstore_chain, nb); | |
769 | } | |
770 | EXPORT_SYMBOL_GPL(unregister_xenstore_notifier); | |
771 | ||
772 | void xenbus_probe(struct work_struct *unused) | |
773 | { | |
774 | BUG_ON((xenstored_ready <= 0)); | |
775 | ||
776 | /* Enumerate devices in xenstore and watch for changes. */ | |
777 | xenbus_probe_devices(&xenbus_frontend); | |
778 | register_xenbus_watch(&fe_watch); | |
779 | xenbus_backend_probe_and_watch(); | |
780 | ||
781 | /* Notify others that xenstore is up */ | |
782 | blocking_notifier_call_chain(&xenstore_chain, 0, NULL); | |
783 | } | |
183d03cc | 784 | EXPORT_SYMBOL_GPL(xenbus_probe); |
4bac07c9 | 785 | |
183d03cc SS |
786 | static int __init xenbus_probe_initcall(void) |
787 | { | |
788 | if (!xen_domain()) | |
789 | return -ENODEV; | |
790 | ||
791 | if (xen_initial_domain() || xen_hvm_domain()) | |
792 | return 0; | |
793 | ||
794 | xenbus_probe(NULL); | |
795 | return 0; | |
796 | } | |
797 | ||
798 | device_initcall(xenbus_probe_initcall); | |
799 | ||
800 | static int __init xenbus_init(void) | |
4bac07c9 JF |
801 | { |
802 | int err = 0; | |
803 | ||
804 | DPRINTK(""); | |
805 | ||
806 | err = -ENODEV; | |
6e833587 | 807 | if (!xen_domain()) |
4bac07c9 JF |
808 | goto out_error; |
809 | ||
810 | /* Register ourselves with the kernel bus subsystem */ | |
811 | err = bus_register(&xenbus_frontend.bus); | |
812 | if (err) | |
813 | goto out_error; | |
814 | ||
815 | err = xenbus_backend_bus_register(); | |
816 | if (err) | |
817 | goto out_unreg_front; | |
818 | ||
819 | /* | |
820 | * Domain0 doesn't have a store_evtchn or store_mfn yet. | |
821 | */ | |
6e833587 | 822 | if (xen_initial_domain()) { |
4bac07c9 JF |
823 | /* dom0 not yet supported */ |
824 | } else { | |
bee6ab53 SY |
825 | if (xen_hvm_domain()) { |
826 | uint64_t v = 0; | |
827 | err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v); | |
828 | if (err) | |
829 | goto out_error; | |
830 | xen_store_evtchn = (int)v; | |
831 | err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v); | |
832 | if (err) | |
833 | goto out_error; | |
834 | xen_store_mfn = (unsigned long)v; | |
835 | xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT, PAGE_SIZE); | |
836 | } else { | |
837 | xen_store_evtchn = xen_start_info->store_evtchn; | |
838 | xen_store_mfn = xen_start_info->store_mfn; | |
839 | xen_store_interface = mfn_to_virt(xen_store_mfn); | |
840 | } | |
4bac07c9 | 841 | xenstored_ready = 1; |
4bac07c9 | 842 | } |
4bac07c9 JF |
843 | |
844 | /* Initialize the interface to xenstore. */ | |
845 | err = xs_init(); | |
846 | if (err) { | |
847 | printk(KERN_WARNING | |
848 | "XENBUS: Error initializing xenstore comms: %i\n", err); | |
849 | goto out_unreg_back; | |
850 | } | |
851 | ||
1107ba88 AZ |
852 | #ifdef CONFIG_XEN_COMPAT_XENFS |
853 | /* | |
854 | * Create xenfs mountpoint in /proc for compatibility with | |
855 | * utilities that expect to find "xenbus" under "/proc/xen". | |
856 | */ | |
857 | proc_mkdir("xen", NULL); | |
858 | #endif | |
859 | ||
4bac07c9 JF |
860 | return 0; |
861 | ||
862 | out_unreg_back: | |
863 | xenbus_backend_bus_unregister(); | |
864 | ||
865 | out_unreg_front: | |
866 | bus_unregister(&xenbus_frontend.bus); | |
867 | ||
868 | out_error: | |
869 | return err; | |
870 | } | |
871 | ||
183d03cc | 872 | postcore_initcall(xenbus_init); |
4bac07c9 JF |
873 | |
874 | MODULE_LICENSE("GPL"); | |
875 | ||
c6e19711 | 876 | static int is_device_connecting(struct device *dev, void *data) |
4bac07c9 JF |
877 | { |
878 | struct xenbus_device *xendev = to_xenbus_device(dev); | |
879 | struct device_driver *drv = data; | |
1d78d705 | 880 | struct xenbus_driver *xendrv; |
4bac07c9 JF |
881 | |
882 | /* | |
883 | * A device with no driver will never connect. We care only about | |
884 | * devices which should currently be in the process of connecting. | |
885 | */ | |
886 | if (!dev->driver) | |
887 | return 0; | |
888 | ||
889 | /* Is this search limited to a particular driver? */ | |
890 | if (drv && (dev->driver != drv)) | |
891 | return 0; | |
892 | ||
1d78d705 | 893 | xendrv = to_xenbus_driver(dev->driver); |
c6e19711 PB |
894 | return (xendev->state < XenbusStateConnected || |
895 | (xendev->state == XenbusStateConnected && | |
896 | xendrv->is_ready && !xendrv->is_ready(xendev))); | |
4bac07c9 JF |
897 | } |
898 | ||
c6e19711 | 899 | static int exists_connecting_device(struct device_driver *drv) |
4bac07c9 JF |
900 | { |
901 | return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, | |
c6e19711 | 902 | is_device_connecting); |
4bac07c9 JF |
903 | } |
904 | ||
905 | static int print_device_status(struct device *dev, void *data) | |
906 | { | |
907 | struct xenbus_device *xendev = to_xenbus_device(dev); | |
908 | struct device_driver *drv = data; | |
909 | ||
910 | /* Is this operation limited to a particular driver? */ | |
911 | if (drv && (dev->driver != drv)) | |
912 | return 0; | |
913 | ||
914 | if (!dev->driver) { | |
915 | /* Information only: is this too noisy? */ | |
916 | printk(KERN_INFO "XENBUS: Device with no driver: %s\n", | |
917 | xendev->nodename); | |
f8dc3308 PB |
918 | } else if (xendev->state < XenbusStateConnected) { |
919 | enum xenbus_state rstate = XenbusStateUnknown; | |
920 | if (xendev->otherend) | |
921 | rstate = xenbus_read_driver_state(xendev->otherend); | |
4bac07c9 | 922 | printk(KERN_WARNING "XENBUS: Timeout connecting " |
f8dc3308 PB |
923 | "to device: %s (local state %d, remote state %d)\n", |
924 | xendev->nodename, xendev->state, rstate); | |
4bac07c9 JF |
925 | } |
926 | ||
927 | return 0; | |
928 | } | |
929 | ||
930 | /* We only wait for device setup after most initcalls have run. */ | |
931 | static int ready_to_wait_for_devices; | |
932 | ||
933 | /* | |
ae788801 | 934 | * On a 5-minute timeout, wait for all devices currently configured. We need |
4bac07c9 JF |
935 | * to do this to guarantee that the filesystems and / or network devices |
936 | * needed for boot are available, before we can allow the boot to proceed. | |
937 | * | |
938 | * This needs to be on a late_initcall, to happen after the frontend device | |
939 | * drivers have been initialised, but before the root fs is mounted. | |
940 | * | |
941 | * A possible improvement here would be to have the tools add a per-device | |
942 | * flag to the store entry, indicating whether it is needed at boot time. | |
943 | * This would allow people who knew what they were doing to accelerate their | |
944 | * boot slightly, but of course needs tools or manual intervention to set up | |
945 | * those flags correctly. | |
946 | */ | |
947 | static void wait_for_devices(struct xenbus_driver *xendrv) | |
948 | { | |
ae788801 | 949 | unsigned long start = jiffies; |
4bac07c9 | 950 | struct device_driver *drv = xendrv ? &xendrv->driver : NULL; |
ae788801 | 951 | unsigned int seconds_waited = 0; |
4bac07c9 | 952 | |
6e833587 | 953 | if (!ready_to_wait_for_devices || !xen_domain()) |
4bac07c9 JF |
954 | return; |
955 | ||
c6e19711 | 956 | while (exists_connecting_device(drv)) { |
ae788801 PB |
957 | if (time_after(jiffies, start + (seconds_waited+5)*HZ)) { |
958 | if (!seconds_waited) | |
959 | printk(KERN_WARNING "XENBUS: Waiting for " | |
960 | "devices to initialise: "); | |
961 | seconds_waited += 5; | |
962 | printk("%us...", 300 - seconds_waited); | |
963 | if (seconds_waited == 300) | |
964 | break; | |
965 | } | |
966 | ||
4bac07c9 JF |
967 | schedule_timeout_interruptible(HZ/10); |
968 | } | |
969 | ||
ae788801 PB |
970 | if (seconds_waited) |
971 | printk("\n"); | |
972 | ||
4bac07c9 JF |
973 | bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, |
974 | print_device_status); | |
975 | } | |
976 | ||
977 | #ifndef MODULE | |
978 | static int __init boot_wait_for_devices(void) | |
979 | { | |
980 | ready_to_wait_for_devices = 1; | |
981 | wait_for_devices(NULL); | |
982 | return 0; | |
983 | } | |
984 | ||
985 | late_initcall(boot_wait_for_devices); | |
986 | #endif |