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