]>
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 | } | |
663 | ||
664 | static void frontend_changed(struct xenbus_watch *watch, | |
665 | const char **vec, unsigned int len) | |
666 | { | |
667 | DPRINTK(""); | |
668 | ||
669 | xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend); | |
670 | } | |
671 | ||
672 | /* We watch for devices appearing and vanishing. */ | |
673 | static struct xenbus_watch fe_watch = { | |
674 | .node = "device", | |
675 | .callback = frontend_changed, | |
676 | }; | |
677 | ||
de5b31bd | 678 | static int xenbus_dev_suspend(struct device *dev, pm_message_t state) |
4bac07c9 JF |
679 | { |
680 | int err = 0; | |
681 | struct xenbus_driver *drv; | |
682 | struct xenbus_device *xdev; | |
683 | ||
684 | DPRINTK(""); | |
685 | ||
686 | if (dev->driver == NULL) | |
687 | return 0; | |
688 | drv = to_xenbus_driver(dev->driver); | |
689 | xdev = container_of(dev, struct xenbus_device, dev); | |
690 | if (drv->suspend) | |
de5b31bd | 691 | err = drv->suspend(xdev, state); |
4bac07c9 JF |
692 | if (err) |
693 | printk(KERN_WARNING | |
2a678cc5 | 694 | "xenbus: suspend %s failed: %i\n", dev_name(dev), err); |
4bac07c9 JF |
695 | return 0; |
696 | } | |
697 | ||
de5b31bd | 698 | static int xenbus_dev_resume(struct device *dev) |
4bac07c9 JF |
699 | { |
700 | int err; | |
701 | struct xenbus_driver *drv; | |
702 | struct xenbus_device *xdev; | |
703 | ||
704 | DPRINTK(""); | |
705 | ||
706 | if (dev->driver == NULL) | |
707 | return 0; | |
708 | ||
709 | drv = to_xenbus_driver(dev->driver); | |
710 | xdev = container_of(dev, struct xenbus_device, dev); | |
711 | ||
712 | err = talk_to_otherend(xdev); | |
713 | if (err) { | |
714 | printk(KERN_WARNING | |
715 | "xenbus: resume (talk_to_otherend) %s failed: %i\n", | |
2a678cc5 | 716 | dev_name(dev), err); |
4bac07c9 JF |
717 | return err; |
718 | } | |
719 | ||
720 | xdev->state = XenbusStateInitialising; | |
721 | ||
722 | if (drv->resume) { | |
723 | err = drv->resume(xdev); | |
724 | if (err) { | |
725 | printk(KERN_WARNING | |
726 | "xenbus: resume %s failed: %i\n", | |
2a678cc5 | 727 | dev_name(dev), err); |
4bac07c9 JF |
728 | return err; |
729 | } | |
730 | } | |
731 | ||
732 | err = watch_otherend(xdev); | |
733 | if (err) { | |
734 | printk(KERN_WARNING | |
735 | "xenbus_probe: resume (watch_otherend) %s failed: " | |
2a678cc5 | 736 | "%d.\n", dev_name(dev), err); |
4bac07c9 JF |
737 | return err; |
738 | } | |
739 | ||
740 | return 0; | |
741 | } | |
742 | ||
4bac07c9 JF |
743 | /* A flag to determine if xenstored is 'ready' (i.e. has started) */ |
744 | int xenstored_ready = 0; | |
745 | ||
746 | ||
747 | int register_xenstore_notifier(struct notifier_block *nb) | |
748 | { | |
749 | int ret = 0; | |
750 | ||
751 | if (xenstored_ready > 0) | |
752 | ret = nb->notifier_call(nb, 0, NULL); | |
753 | else | |
754 | blocking_notifier_chain_register(&xenstore_chain, nb); | |
755 | ||
756 | return ret; | |
757 | } | |
758 | EXPORT_SYMBOL_GPL(register_xenstore_notifier); | |
759 | ||
760 | void unregister_xenstore_notifier(struct notifier_block *nb) | |
761 | { | |
762 | blocking_notifier_chain_unregister(&xenstore_chain, nb); | |
763 | } | |
764 | EXPORT_SYMBOL_GPL(unregister_xenstore_notifier); | |
765 | ||
766 | void xenbus_probe(struct work_struct *unused) | |
767 | { | |
768 | BUG_ON((xenstored_ready <= 0)); | |
769 | ||
770 | /* Enumerate devices in xenstore and watch for changes. */ | |
771 | xenbus_probe_devices(&xenbus_frontend); | |
772 | register_xenbus_watch(&fe_watch); | |
773 | xenbus_backend_probe_and_watch(); | |
774 | ||
775 | /* Notify others that xenstore is up */ | |
776 | blocking_notifier_call_chain(&xenstore_chain, 0, NULL); | |
777 | } | |
778 | ||
779 | static int __init xenbus_probe_init(void) | |
780 | { | |
781 | int err = 0; | |
782 | ||
783 | DPRINTK(""); | |
784 | ||
785 | err = -ENODEV; | |
6e833587 | 786 | if (!xen_domain()) |
4bac07c9 JF |
787 | goto out_error; |
788 | ||
789 | /* Register ourselves with the kernel bus subsystem */ | |
790 | err = bus_register(&xenbus_frontend.bus); | |
791 | if (err) | |
792 | goto out_error; | |
793 | ||
794 | err = xenbus_backend_bus_register(); | |
795 | if (err) | |
796 | goto out_unreg_front; | |
797 | ||
798 | /* | |
799 | * Domain0 doesn't have a store_evtchn or store_mfn yet. | |
800 | */ | |
6e833587 | 801 | if (xen_initial_domain()) { |
4bac07c9 JF |
802 | /* dom0 not yet supported */ |
803 | } else { | |
804 | xenstored_ready = 1; | |
805 | xen_store_evtchn = xen_start_info->store_evtchn; | |
806 | xen_store_mfn = xen_start_info->store_mfn; | |
807 | } | |
808 | xen_store_interface = mfn_to_virt(xen_store_mfn); | |
809 | ||
810 | /* Initialize the interface to xenstore. */ | |
811 | err = xs_init(); | |
812 | if (err) { | |
813 | printk(KERN_WARNING | |
814 | "XENBUS: Error initializing xenstore comms: %i\n", err); | |
815 | goto out_unreg_back; | |
816 | } | |
817 | ||
6e833587 | 818 | if (!xen_initial_domain()) |
4bac07c9 JF |
819 | xenbus_probe(NULL); |
820 | ||
1107ba88 AZ |
821 | #ifdef CONFIG_XEN_COMPAT_XENFS |
822 | /* | |
823 | * Create xenfs mountpoint in /proc for compatibility with | |
824 | * utilities that expect to find "xenbus" under "/proc/xen". | |
825 | */ | |
826 | proc_mkdir("xen", NULL); | |
827 | #endif | |
828 | ||
4bac07c9 JF |
829 | return 0; |
830 | ||
831 | out_unreg_back: | |
832 | xenbus_backend_bus_unregister(); | |
833 | ||
834 | out_unreg_front: | |
835 | bus_unregister(&xenbus_frontend.bus); | |
836 | ||
837 | out_error: | |
838 | return err; | |
839 | } | |
840 | ||
841 | postcore_initcall(xenbus_probe_init); | |
842 | ||
843 | MODULE_LICENSE("GPL"); | |
844 | ||
845 | static int is_disconnected_device(struct device *dev, void *data) | |
846 | { | |
847 | struct xenbus_device *xendev = to_xenbus_device(dev); | |
848 | struct device_driver *drv = data; | |
1d78d705 | 849 | struct xenbus_driver *xendrv; |
4bac07c9 JF |
850 | |
851 | /* | |
852 | * A device with no driver will never connect. We care only about | |
853 | * devices which should currently be in the process of connecting. | |
854 | */ | |
855 | if (!dev->driver) | |
856 | return 0; | |
857 | ||
858 | /* Is this search limited to a particular driver? */ | |
859 | if (drv && (dev->driver != drv)) | |
860 | return 0; | |
861 | ||
1d78d705 CL |
862 | xendrv = to_xenbus_driver(dev->driver); |
863 | return (xendev->state != XenbusStateConnected || | |
864 | (xendrv->is_ready && !xendrv->is_ready(xendev))); | |
4bac07c9 JF |
865 | } |
866 | ||
867 | static int exists_disconnected_device(struct device_driver *drv) | |
868 | { | |
869 | return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, | |
870 | is_disconnected_device); | |
871 | } | |
872 | ||
873 | static int print_device_status(struct device *dev, void *data) | |
874 | { | |
875 | struct xenbus_device *xendev = to_xenbus_device(dev); | |
876 | struct device_driver *drv = data; | |
877 | ||
878 | /* Is this operation limited to a particular driver? */ | |
879 | if (drv && (dev->driver != drv)) | |
880 | return 0; | |
881 | ||
882 | if (!dev->driver) { | |
883 | /* Information only: is this too noisy? */ | |
884 | printk(KERN_INFO "XENBUS: Device with no driver: %s\n", | |
885 | xendev->nodename); | |
886 | } else if (xendev->state != XenbusStateConnected) { | |
887 | printk(KERN_WARNING "XENBUS: Timeout connecting " | |
888 | "to device: %s (state %d)\n", | |
889 | xendev->nodename, xendev->state); | |
890 | } | |
891 | ||
892 | return 0; | |
893 | } | |
894 | ||
895 | /* We only wait for device setup after most initcalls have run. */ | |
896 | static int ready_to_wait_for_devices; | |
897 | ||
898 | /* | |
899 | * On a 10 second timeout, wait for all devices currently configured. We need | |
900 | * to do this to guarantee that the filesystems and / or network devices | |
901 | * needed for boot are available, before we can allow the boot to proceed. | |
902 | * | |
903 | * This needs to be on a late_initcall, to happen after the frontend device | |
904 | * drivers have been initialised, but before the root fs is mounted. | |
905 | * | |
906 | * A possible improvement here would be to have the tools add a per-device | |
907 | * flag to the store entry, indicating whether it is needed at boot time. | |
908 | * This would allow people who knew what they were doing to accelerate their | |
909 | * boot slightly, but of course needs tools or manual intervention to set up | |
910 | * those flags correctly. | |
911 | */ | |
912 | static void wait_for_devices(struct xenbus_driver *xendrv) | |
913 | { | |
914 | unsigned long timeout = jiffies + 10*HZ; | |
915 | struct device_driver *drv = xendrv ? &xendrv->driver : NULL; | |
916 | ||
6e833587 | 917 | if (!ready_to_wait_for_devices || !xen_domain()) |
4bac07c9 JF |
918 | return; |
919 | ||
920 | while (exists_disconnected_device(drv)) { | |
921 | if (time_after(jiffies, timeout)) | |
922 | break; | |
923 | schedule_timeout_interruptible(HZ/10); | |
924 | } | |
925 | ||
926 | bus_for_each_dev(&xenbus_frontend.bus, NULL, drv, | |
927 | print_device_status); | |
928 | } | |
929 | ||
930 | #ifndef MODULE | |
931 | static int __init boot_wait_for_devices(void) | |
932 | { | |
933 | ready_to_wait_for_devices = 1; | |
934 | wait_for_devices(NULL); | |
935 | return 0; | |
936 | } | |
937 | ||
938 | late_initcall(boot_wait_for_devices); | |
939 | #endif |