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