]>
Commit | Line | Data |
---|---|---|
4bac07c9 JF |
1 | /****************************************************************************** |
2 | * Talks to Xen Store to figure out what devices we have. | |
3 | * | |
4 | * Copyright (C) 2005 Rusty Russell, IBM Corporation | |
5 | * Copyright (C) 2005 Mike Wray, Hewlett-Packard | |
6 | * Copyright (C) 2005, 2006 XenSource Ltd | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License version 2 | |
10 | * as published by the Free Software Foundation; or, when distributed | |
11 | * separately from the Linux kernel or incorporated into other | |
12 | * software packages, subject to the following license: | |
13 | * | |
14 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
15 | * of this source file (the "Software"), to deal in the Software without | |
16 | * restriction, including without limitation the rights to use, copy, modify, | |
17 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
18 | * and to permit persons to whom the Software is furnished to do so, subject to | |
19 | * the following conditions: | |
20 | * | |
21 | * The above copyright notice and this permission notice shall be included in | |
22 | * all copies or substantial portions of the Software. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
29 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
30 | * IN THE SOFTWARE. | |
31 | */ | |
32 | ||
33 | #define DPRINTK(fmt, args...) \ | |
34 | pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \ | |
35 | __func__, __LINE__, ##args) | |
36 | ||
37 | #include <linux/kernel.h> | |
38 | #include <linux/err.h> | |
39 | #include <linux/string.h> | |
40 | #include <linux/ctype.h> | |
41 | #include <linux/fcntl.h> | |
42 | #include <linux/mm.h> | |
1107ba88 | 43 | #include <linux/proc_fs.h> |
4bac07c9 JF |
44 | #include <linux/notifier.h> |
45 | #include <linux/kthread.h> | |
46 | #include <linux/mutex.h> | |
47 | #include <linux/io.h> | |
5a0e3ad6 | 48 | #include <linux/slab.h> |
4bac07c9 JF |
49 | |
50 | #include <asm/page.h> | |
51 | #include <asm/pgtable.h> | |
52 | #include <asm/xen/hypervisor.h> | |
1ccbf534 JF |
53 | |
54 | #include <xen/xen.h> | |
4bac07c9 JF |
55 | #include <xen/xenbus.h> |
56 | #include <xen/events.h> | |
57 | #include <xen/page.h> | |
58 | ||
bee6ab53 SY |
59 | #include <xen/hvm.h> |
60 | ||
4bac07c9 JF |
61 | #include "xenbus_comms.h" |
62 | #include "xenbus_probe.h" | |
63 | ||
1107ba88 | 64 | |
4bac07c9 | 65 | int xen_store_evtchn; |
8e3e9991 | 66 | EXPORT_SYMBOL_GPL(xen_store_evtchn); |
1107ba88 | 67 | |
4bac07c9 | 68 | struct xenstore_domain_interface *xen_store_interface; |
8e3e9991 JF |
69 | EXPORT_SYMBOL_GPL(xen_store_interface); |
70 | ||
4bac07c9 JF |
71 | static unsigned long xen_store_mfn; |
72 | ||
73 | static BLOCKING_NOTIFIER_HEAD(xenstore_chain); | |
74 | ||
4bac07c9 JF |
75 | /* If something in array of ids matches this device, return it. */ |
76 | static const struct xenbus_device_id * | |
77 | match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev) | |
78 | { | |
79 | for (; *arr->devicetype != '\0'; arr++) { | |
80 | if (!strcmp(arr->devicetype, dev->devicetype)) | |
81 | return arr; | |
82 | } | |
83 | return NULL; | |
84 | } | |
85 | ||
86 | int xenbus_match(struct device *_dev, struct device_driver *_drv) | |
87 | { | |
88 | struct xenbus_driver *drv = to_xenbus_driver(_drv); | |
89 | ||
90 | if (!drv->ids) | |
91 | return 0; | |
92 | ||
93 | return match_device(drv->ids, to_xenbus_device(_dev)) != NULL; | |
94 | } | |
2de06cc1 IC |
95 | EXPORT_SYMBOL_GPL(xenbus_match); |
96 | ||
4bac07c9 | 97 | |
4bac07c9 JF |
98 | static void free_otherend_details(struct xenbus_device *dev) |
99 | { | |
100 | kfree(dev->otherend); | |
101 | dev->otherend = NULL; | |
102 | } | |
103 | ||
104 | ||
105 | static void free_otherend_watch(struct xenbus_device *dev) | |
106 | { | |
107 | if (dev->otherend_watch.node) { | |
108 | unregister_xenbus_watch(&dev->otherend_watch); | |
109 | kfree(dev->otherend_watch.node); | |
110 | dev->otherend_watch.node = NULL; | |
111 | } | |
112 | } | |
113 | ||
114 | ||
2de06cc1 IC |
115 | static int talk_to_otherend(struct xenbus_device *dev) |
116 | { | |
117 | struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver); | |
118 | ||
119 | free_otherend_watch(dev); | |
120 | free_otherend_details(dev); | |
121 | ||
122 | return drv->read_otherend_details(dev); | |
123 | } | |
124 | ||
125 | ||
126 | ||
127 | static int watch_otherend(struct xenbus_device *dev) | |
128 | { | |
129 | struct xen_bus_type *bus = container_of(dev->dev.bus, struct xen_bus_type, bus); | |
130 | ||
131 | return xenbus_watch_pathfmt(dev, &dev->otherend_watch, bus->otherend_changed, | |
132 | "%s/%s", dev->otherend, "state"); | |
133 | } | |
134 | ||
135 | ||
136 | int xenbus_read_otherend_details(struct xenbus_device *xendev, | |
4bac07c9 JF |
137 | char *id_node, char *path_node) |
138 | { | |
139 | int err = xenbus_gather(XBT_NIL, xendev->nodename, | |
140 | id_node, "%i", &xendev->otherend_id, | |
141 | path_node, NULL, &xendev->otherend, | |
142 | NULL); | |
143 | if (err) { | |
144 | xenbus_dev_fatal(xendev, err, | |
145 | "reading other end details from %s", | |
146 | xendev->nodename); | |
147 | return err; | |
148 | } | |
149 | if (strlen(xendev->otherend) == 0 || | |
150 | !xenbus_exists(XBT_NIL, xendev->otherend, "")) { | |
151 | xenbus_dev_fatal(xendev, -ENOENT, | |
152 | "unable to read other end from %s. " | |
153 | "missing or inaccessible.", | |
154 | xendev->nodename); | |
155 | free_otherend_details(xendev); | |
156 | return -ENOENT; | |
157 | } | |
158 | ||
159 | return 0; | |
160 | } | |
2de06cc1 | 161 | EXPORT_SYMBOL_GPL(xenbus_read_otherend_details); |
4bac07c9 | 162 | |
2de06cc1 IC |
163 | void xenbus_otherend_changed(struct xenbus_watch *watch, |
164 | const char **vec, unsigned int len, | |
165 | int ignore_on_shutdown) | |
4bac07c9 JF |
166 | { |
167 | struct xenbus_device *dev = | |
168 | container_of(watch, struct xenbus_device, otherend_watch); | |
169 | struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver); | |
170 | enum xenbus_state state; | |
171 | ||
172 | /* Protect us against watches firing on old details when the otherend | |
173 | details change, say immediately after a resume. */ | |
174 | if (!dev->otherend || | |
175 | strncmp(dev->otherend, vec[XS_WATCH_PATH], | |
176 | strlen(dev->otherend))) { | |
898eb71c JP |
177 | dev_dbg(&dev->dev, "Ignoring watch at %s\n", |
178 | vec[XS_WATCH_PATH]); | |
4bac07c9 JF |
179 | return; |
180 | } | |
181 | ||
182 | state = xenbus_read_driver_state(dev->otherend); | |
183 | ||
898eb71c | 184 | dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n", |
4bac07c9 JF |
185 | state, xenbus_strstate(state), dev->otherend_watch.node, |
186 | vec[XS_WATCH_PATH]); | |
187 | ||
188 | /* | |
189 | * Ignore xenbus transitions during shutdown. This prevents us doing | |
190 | * work that can fail e.g., when the rootfs is gone. | |
191 | */ | |
192 | if (system_state > SYSTEM_RUNNING) { | |
2de06cc1 | 193 | if (ignore_on_shutdown && (state == XenbusStateClosing)) |
4bac07c9 JF |
194 | xenbus_frontend_closed(dev); |
195 | return; | |
196 | } | |
197 | ||
198 | if (drv->otherend_changed) | |
199 | drv->otherend_changed(dev, state); | |
200 | } | |
2de06cc1 | 201 | EXPORT_SYMBOL_GPL(xenbus_otherend_changed); |
4bac07c9 JF |
202 | |
203 | int xenbus_dev_probe(struct device *_dev) | |
204 | { | |
205 | struct xenbus_device *dev = to_xenbus_device(_dev); | |
206 | struct xenbus_driver *drv = to_xenbus_driver(_dev->driver); | |
207 | const struct xenbus_device_id *id; | |
208 | int err; | |
209 | ||
210 | DPRINTK("%s", dev->nodename); | |
211 | ||
212 | if (!drv->probe) { | |
213 | err = -ENODEV; | |
214 | goto fail; | |
215 | } | |
216 | ||
217 | id = match_device(drv->ids, dev); | |
218 | if (!id) { | |
219 | err = -ENODEV; | |
220 | goto fail; | |
221 | } | |
222 | ||
223 | err = talk_to_otherend(dev); | |
224 | if (err) { | |
225 | dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n", | |
226 | dev->nodename); | |
227 | return err; | |
228 | } | |
229 | ||
230 | err = drv->probe(dev, id); | |
231 | if (err) | |
232 | goto fail; | |
233 | ||
234 | err = watch_otherend(dev); | |
235 | if (err) { | |
236 | dev_warn(&dev->dev, "watch_otherend on %s failed.\n", | |
237 | dev->nodename); | |
238 | return err; | |
239 | } | |
240 | ||
241 | return 0; | |
242 | fail: | |
243 | xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename); | |
244 | xenbus_switch_state(dev, XenbusStateClosed); | |
7432e4bd | 245 | return err; |
4bac07c9 | 246 | } |
2de06cc1 | 247 | EXPORT_SYMBOL_GPL(xenbus_dev_probe); |
4bac07c9 JF |
248 | |
249 | int xenbus_dev_remove(struct device *_dev) | |
250 | { | |
251 | struct xenbus_device *dev = to_xenbus_device(_dev); | |
252 | struct xenbus_driver *drv = to_xenbus_driver(_dev->driver); | |
253 | ||
254 | DPRINTK("%s", dev->nodename); | |
255 | ||
256 | free_otherend_watch(dev); | |
257 | free_otherend_details(dev); | |
258 | ||
259 | if (drv->remove) | |
260 | drv->remove(dev); | |
261 | ||
262 | xenbus_switch_state(dev, XenbusStateClosed); | |
263 | return 0; | |
264 | } | |
2de06cc1 | 265 | EXPORT_SYMBOL_GPL(xenbus_dev_remove); |
4bac07c9 | 266 | |
2de06cc1 | 267 | void xenbus_dev_shutdown(struct device *_dev) |
4bac07c9 JF |
268 | { |
269 | struct xenbus_device *dev = to_xenbus_device(_dev); | |
270 | unsigned long timeout = 5*HZ; | |
271 | ||
272 | DPRINTK("%s", dev->nodename); | |
273 | ||
274 | get_device(&dev->dev); | |
275 | if (dev->state != XenbusStateConnected) { | |
276 | printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__, | |
277 | dev->nodename, xenbus_strstate(dev->state)); | |
278 | goto out; | |
279 | } | |
280 | xenbus_switch_state(dev, XenbusStateClosing); | |
281 | timeout = wait_for_completion_timeout(&dev->down, timeout); | |
282 | if (!timeout) | |
283 | printk(KERN_INFO "%s: %s timeout closing device\n", | |
284 | __func__, dev->nodename); | |
285 | out: | |
286 | put_device(&dev->dev); | |
287 | } | |
2de06cc1 | 288 | EXPORT_SYMBOL_GPL(xenbus_dev_shutdown); |
4bac07c9 JF |
289 | |
290 | int xenbus_register_driver_common(struct xenbus_driver *drv, | |
291 | struct xen_bus_type *bus, | |
292 | struct module *owner, | |
293 | const char *mod_name) | |
294 | { | |
295 | drv->driver.name = drv->name; | |
296 | drv->driver.bus = &bus->bus; | |
297 | drv->driver.owner = owner; | |
298 | drv->driver.mod_name = mod_name; | |
299 | ||
300 | return driver_register(&drv->driver); | |
301 | } | |
2de06cc1 | 302 | EXPORT_SYMBOL_GPL(xenbus_register_driver_common); |
4bac07c9 JF |
303 | |
304 | void xenbus_unregister_driver(struct xenbus_driver *drv) | |
305 | { | |
306 | driver_unregister(&drv->driver); | |
307 | } | |
308 | EXPORT_SYMBOL_GPL(xenbus_unregister_driver); | |
309 | ||
310 | struct xb_find_info | |
311 | { | |
312 | struct xenbus_device *dev; | |
313 | const char *nodename; | |
314 | }; | |
315 | ||
316 | static int cmp_dev(struct device *dev, void *data) | |
317 | { | |
318 | struct xenbus_device *xendev = to_xenbus_device(dev); | |
319 | struct xb_find_info *info = data; | |
320 | ||
321 | if (!strcmp(xendev->nodename, info->nodename)) { | |
322 | info->dev = xendev; | |
323 | get_device(dev); | |
324 | return 1; | |
325 | } | |
326 | return 0; | |
327 | } | |
328 | ||
329 | struct xenbus_device *xenbus_device_find(const char *nodename, | |
330 | struct bus_type *bus) | |
331 | { | |
332 | struct xb_find_info info = { .dev = NULL, .nodename = nodename }; | |
333 | ||
334 | bus_for_each_dev(bus, NULL, &info, cmp_dev); | |
335 | return info.dev; | |
336 | } | |
337 | ||
338 | static int cleanup_dev(struct device *dev, void *data) | |
339 | { | |
340 | struct xenbus_device *xendev = to_xenbus_device(dev); | |
341 | struct xb_find_info *info = data; | |
342 | int len = strlen(info->nodename); | |
343 | ||
344 | DPRINTK("%s", info->nodename); | |
345 | ||
346 | /* Match the info->nodename path, or any subdirectory of that path. */ | |
347 | if (strncmp(xendev->nodename, info->nodename, len)) | |
348 | return 0; | |
349 | ||
350 | /* If the node name is longer, ensure it really is a subdirectory. */ | |
351 | if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/')) | |
352 | return 0; | |
353 | ||
354 | info->dev = xendev; | |
355 | get_device(dev); | |
356 | return 1; | |
357 | } | |
358 | ||
359 | static void xenbus_cleanup_devices(const char *path, struct bus_type *bus) | |
360 | { | |
361 | struct xb_find_info info = { .nodename = path }; | |
362 | ||
363 | do { | |
364 | info.dev = NULL; | |
365 | bus_for_each_dev(bus, NULL, &info, cleanup_dev); | |
366 | if (info.dev) { | |
367 | device_unregister(&info.dev->dev); | |
368 | put_device(&info.dev->dev); | |
369 | } | |
370 | } while (info.dev); | |
371 | } | |
372 | ||
373 | static void xenbus_dev_release(struct device *dev) | |
374 | { | |
375 | if (dev) | |
376 | kfree(to_xenbus_device(dev)); | |
377 | } | |
378 | ||
379 | static ssize_t xendev_show_nodename(struct device *dev, | |
380 | struct device_attribute *attr, char *buf) | |
381 | { | |
382 | return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename); | |
383 | } | |
db05fed0 | 384 | static DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL); |
4bac07c9 JF |
385 | |
386 | static ssize_t xendev_show_devtype(struct device *dev, | |
387 | struct device_attribute *attr, char *buf) | |
388 | { | |
389 | return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype); | |
390 | } | |
db05fed0 | 391 | static DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL); |
4bac07c9 | 392 | |
d2f0c52b MM |
393 | static ssize_t xendev_show_modalias(struct device *dev, |
394 | struct device_attribute *attr, char *buf) | |
395 | { | |
396 | return sprintf(buf, "xen:%s\n", to_xenbus_device(dev)->devicetype); | |
397 | } | |
db05fed0 | 398 | static DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL); |
4bac07c9 JF |
399 | |
400 | int xenbus_probe_node(struct xen_bus_type *bus, | |
401 | const char *type, | |
402 | const char *nodename) | |
403 | { | |
2a678cc5 | 404 | char devname[XEN_BUS_ID_SIZE]; |
4bac07c9 JF |
405 | int err; |
406 | struct xenbus_device *xendev; | |
407 | size_t stringlen; | |
408 | char *tmpstring; | |
409 | ||
410 | enum xenbus_state state = xenbus_read_driver_state(nodename); | |
411 | ||
412 | if (state != XenbusStateInitialising) { | |
413 | /* Device is not new, so ignore it. This can happen if a | |
414 | device is going away after switching to Closed. */ | |
415 | return 0; | |
416 | } | |
417 | ||
418 | stringlen = strlen(nodename) + 1 + strlen(type) + 1; | |
419 | xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL); | |
420 | if (!xendev) | |
421 | return -ENOMEM; | |
422 | ||
423 | xendev->state = XenbusStateInitialising; | |
424 | ||
425 | /* Copy the strings into the extra space. */ | |
426 | ||
427 | tmpstring = (char *)(xendev + 1); | |
428 | strcpy(tmpstring, nodename); | |
429 | xendev->nodename = tmpstring; | |
430 | ||
431 | tmpstring += strlen(tmpstring) + 1; | |
432 | strcpy(tmpstring, type); | |
433 | xendev->devicetype = tmpstring; | |
434 | init_completion(&xendev->down); | |
435 | ||
436 | xendev->dev.bus = &bus->bus; | |
437 | xendev->dev.release = xenbus_dev_release; | |
438 | ||
2a678cc5 | 439 | err = bus->get_bus_id(devname, xendev->nodename); |
4bac07c9 JF |
440 | if (err) |
441 | goto fail; | |
442 | ||
2a678cc5 KS |
443 | dev_set_name(&xendev->dev, devname); |
444 | ||
4bac07c9 JF |
445 | /* Register with generic device framework. */ |
446 | err = device_register(&xendev->dev); | |
447 | if (err) | |
448 | goto fail; | |
449 | ||
450 | err = device_create_file(&xendev->dev, &dev_attr_nodename); | |
451 | if (err) | |
452 | goto fail_unregister; | |
453 | ||
454 | err = device_create_file(&xendev->dev, &dev_attr_devtype); | |
455 | if (err) | |
d2f0c52b MM |
456 | goto fail_remove_nodename; |
457 | ||
458 | err = device_create_file(&xendev->dev, &dev_attr_modalias); | |
459 | if (err) | |
460 | goto fail_remove_devtype; | |
4bac07c9 JF |
461 | |
462 | return 0; | |
d2f0c52b MM |
463 | fail_remove_devtype: |
464 | device_remove_file(&xendev->dev, &dev_attr_devtype); | |
465 | fail_remove_nodename: | |
4bac07c9 JF |
466 | device_remove_file(&xendev->dev, &dev_attr_nodename); |
467 | fail_unregister: | |
468 | device_unregister(&xendev->dev); | |
469 | fail: | |
470 | kfree(xendev); | |
471 | return err; | |
472 | } | |
2de06cc1 | 473 | EXPORT_SYMBOL_GPL(xenbus_probe_node); |
4bac07c9 JF |
474 | |
475 | static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type) | |
476 | { | |
477 | int err = 0; | |
478 | char **dir; | |
479 | unsigned int dir_n = 0; | |
480 | int i; | |
481 | ||
482 | dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n); | |
a39bda27 | 483 | if (IS_ERR(dir)) |
4bac07c9 JF |
484 | return PTR_ERR(dir); |
485 | ||
486 | for (i = 0; i < dir_n; i++) { | |
2de06cc1 | 487 | err = bus->probe(bus, type, dir[i]); |
a39bda27 | 488 | if (err) |
4bac07c9 JF |
489 | break; |
490 | } | |
a39bda27 | 491 | |
4bac07c9 JF |
492 | kfree(dir); |
493 | return err; | |
494 | } | |
495 | ||
496 | int xenbus_probe_devices(struct xen_bus_type *bus) | |
497 | { | |
498 | int err = 0; | |
499 | char **dir; | |
500 | unsigned int i, dir_n; | |
501 | ||
502 | dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n); | |
a39bda27 | 503 | if (IS_ERR(dir)) |
4bac07c9 JF |
504 | return PTR_ERR(dir); |
505 | ||
506 | for (i = 0; i < dir_n; i++) { | |
507 | err = xenbus_probe_device_type(bus, dir[i]); | |
a39bda27 | 508 | if (err) |
4bac07c9 JF |
509 | break; |
510 | } | |
a39bda27 | 511 | |
4bac07c9 JF |
512 | kfree(dir); |
513 | return err; | |
514 | } | |
2de06cc1 | 515 | EXPORT_SYMBOL_GPL(xenbus_probe_devices); |
4bac07c9 JF |
516 | |
517 | static unsigned int char_count(const char *str, char c) | |
518 | { | |
519 | unsigned int i, ret = 0; | |
520 | ||
521 | for (i = 0; str[i]; i++) | |
522 | if (str[i] == c) | |
523 | ret++; | |
524 | return ret; | |
525 | } | |
526 | ||
527 | static int strsep_len(const char *str, char c, unsigned int len) | |
528 | { | |
529 | unsigned int i; | |
530 | ||
531 | for (i = 0; str[i]; i++) | |
532 | if (str[i] == c) { | |
533 | if (len == 0) | |
534 | return i; | |
535 | len--; | |
536 | } | |
537 | return (len == 0) ? i : -ERANGE; | |
538 | } | |
539 | ||
540 | void xenbus_dev_changed(const char *node, struct xen_bus_type *bus) | |
541 | { | |
542 | int exists, rootlen; | |
543 | struct xenbus_device *dev; | |
2a678cc5 | 544 | char type[XEN_BUS_ID_SIZE]; |
4bac07c9 JF |
545 | const char *p, *root; |
546 | ||
547 | if (char_count(node, '/') < 2) | |
548 | return; | |
549 | ||
550 | exists = xenbus_exists(XBT_NIL, node, ""); | |
551 | if (!exists) { | |
552 | xenbus_cleanup_devices(node, &bus->bus); | |
553 | return; | |
554 | } | |
555 | ||
556 | /* backend/<type>/... or device/<type>/... */ | |
557 | p = strchr(node, '/') + 1; | |
2a678cc5 KS |
558 | snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p); |
559 | type[XEN_BUS_ID_SIZE-1] = '\0'; | |
4bac07c9 JF |
560 | |
561 | rootlen = strsep_len(node, '/', bus->levels); | |
562 | if (rootlen < 0) | |
563 | return; | |
564 | root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node); | |
565 | if (!root) | |
566 | return; | |
567 | ||
568 | dev = xenbus_device_find(root, &bus->bus); | |
569 | if (!dev) | |
570 | xenbus_probe_node(bus, type, root); | |
571 | else | |
572 | put_device(&dev->dev); | |
573 | ||
574 | kfree(root); | |
575 | } | |
c6a960ce | 576 | EXPORT_SYMBOL_GPL(xenbus_dev_changed); |
4bac07c9 | 577 | |
2de06cc1 | 578 | int xenbus_dev_suspend(struct device *dev, pm_message_t state) |
4bac07c9 JF |
579 | { |
580 | int err = 0; | |
581 | struct xenbus_driver *drv; | |
2de06cc1 | 582 | struct xenbus_device *xdev = container_of(dev, struct xenbus_device, dev); |
4bac07c9 | 583 | |
2de06cc1 | 584 | DPRINTK("%s", xdev->nodename); |
4bac07c9 JF |
585 | |
586 | if (dev->driver == NULL) | |
587 | return 0; | |
588 | drv = to_xenbus_driver(dev->driver); | |
4bac07c9 | 589 | if (drv->suspend) |
de5b31bd | 590 | err = drv->suspend(xdev, state); |
4bac07c9 JF |
591 | if (err) |
592 | printk(KERN_WARNING | |
2a678cc5 | 593 | "xenbus: suspend %s failed: %i\n", dev_name(dev), err); |
4bac07c9 JF |
594 | return 0; |
595 | } | |
2de06cc1 | 596 | EXPORT_SYMBOL_GPL(xenbus_dev_suspend); |
4bac07c9 | 597 | |
2de06cc1 | 598 | int xenbus_dev_resume(struct device *dev) |
4bac07c9 JF |
599 | { |
600 | int err; | |
601 | struct xenbus_driver *drv; | |
2de06cc1 | 602 | struct xenbus_device *xdev = container_of(dev, struct xenbus_device, dev); |
4bac07c9 | 603 | |
2de06cc1 | 604 | DPRINTK("%s", xdev->nodename); |
4bac07c9 JF |
605 | |
606 | if (dev->driver == NULL) | |
607 | return 0; | |
4bac07c9 | 608 | drv = to_xenbus_driver(dev->driver); |
4bac07c9 JF |
609 | err = talk_to_otherend(xdev); |
610 | if (err) { | |
611 | printk(KERN_WARNING | |
612 | "xenbus: resume (talk_to_otherend) %s failed: %i\n", | |
2a678cc5 | 613 | dev_name(dev), err); |
4bac07c9 JF |
614 | return err; |
615 | } | |
616 | ||
617 | xdev->state = XenbusStateInitialising; | |
618 | ||
619 | if (drv->resume) { | |
620 | err = drv->resume(xdev); | |
621 | if (err) { | |
622 | printk(KERN_WARNING | |
623 | "xenbus: resume %s failed: %i\n", | |
2a678cc5 | 624 | dev_name(dev), err); |
4bac07c9 JF |
625 | return err; |
626 | } | |
627 | } | |
628 | ||
629 | err = watch_otherend(xdev); | |
630 | if (err) { | |
631 | printk(KERN_WARNING | |
632 | "xenbus_probe: resume (watch_otherend) %s failed: " | |
2a678cc5 | 633 | "%d.\n", dev_name(dev), err); |
4bac07c9 JF |
634 | return err; |
635 | } | |
636 | ||
637 | return 0; | |
638 | } | |
2de06cc1 | 639 | EXPORT_SYMBOL_GPL(xenbus_dev_resume); |
4bac07c9 | 640 | |
4bac07c9 JF |
641 | /* A flag to determine if xenstored is 'ready' (i.e. has started) */ |
642 | int xenstored_ready = 0; | |
643 | ||
644 | ||
645 | int register_xenstore_notifier(struct notifier_block *nb) | |
646 | { | |
647 | int ret = 0; | |
648 | ||
a947f0f8 SS |
649 | if (xenstored_ready > 0) |
650 | ret = nb->notifier_call(nb, 0, NULL); | |
651 | else | |
652 | blocking_notifier_chain_register(&xenstore_chain, nb); | |
4bac07c9 JF |
653 | |
654 | return ret; | |
655 | } | |
656 | EXPORT_SYMBOL_GPL(register_xenstore_notifier); | |
657 | ||
658 | void unregister_xenstore_notifier(struct notifier_block *nb) | |
659 | { | |
660 | blocking_notifier_chain_unregister(&xenstore_chain, nb); | |
661 | } | |
662 | EXPORT_SYMBOL_GPL(unregister_xenstore_notifier); | |
663 | ||
664 | void xenbus_probe(struct work_struct *unused) | |
665 | { | |
a947f0f8 | 666 | xenstored_ready = 1; |
4bac07c9 | 667 | |
4bac07c9 JF |
668 | /* Notify others that xenstore is up */ |
669 | blocking_notifier_call_chain(&xenstore_chain, 0, NULL); | |
670 | } | |
183d03cc | 671 | EXPORT_SYMBOL_GPL(xenbus_probe); |
4bac07c9 | 672 | |
183d03cc SS |
673 | static int __init xenbus_probe_initcall(void) |
674 | { | |
675 | if (!xen_domain()) | |
676 | return -ENODEV; | |
677 | ||
678 | if (xen_initial_domain() || xen_hvm_domain()) | |
679 | return 0; | |
680 | ||
681 | xenbus_probe(NULL); | |
682 | return 0; | |
683 | } | |
684 | ||
685 | device_initcall(xenbus_probe_initcall); | |
686 | ||
687 | static int __init xenbus_init(void) | |
4bac07c9 JF |
688 | { |
689 | int err = 0; | |
b37a56d6 | 690 | unsigned long page = 0; |
4bac07c9 JF |
691 | |
692 | DPRINTK(""); | |
693 | ||
694 | err = -ENODEV; | |
6e833587 | 695 | if (!xen_domain()) |
7432e4bd | 696 | return err; |
4bac07c9 | 697 | |
4bac07c9 JF |
698 | /* |
699 | * Domain0 doesn't have a store_evtchn or store_mfn yet. | |
700 | */ | |
6e833587 | 701 | if (xen_initial_domain()) { |
b37a56d6 JQ |
702 | struct evtchn_alloc_unbound alloc_unbound; |
703 | ||
704 | /* Allocate Xenstore page */ | |
705 | page = get_zeroed_page(GFP_KERNEL); | |
706 | if (!page) | |
707 | goto out_error; | |
708 | ||
709 | xen_store_mfn = xen_start_info->store_mfn = | |
710 | pfn_to_mfn(virt_to_phys((void *)page) >> | |
711 | PAGE_SHIFT); | |
712 | ||
713 | /* Next allocate a local port which xenstored can bind to */ | |
714 | alloc_unbound.dom = DOMID_SELF; | |
715 | alloc_unbound.remote_dom = 0; | |
716 | ||
717 | err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, | |
718 | &alloc_unbound); | |
719 | if (err == -ENOSYS) | |
720 | goto out_error; | |
721 | ||
722 | BUG_ON(err); | |
723 | xen_store_evtchn = xen_start_info->store_evtchn = | |
724 | alloc_unbound.port; | |
725 | ||
726 | xen_store_interface = mfn_to_virt(xen_store_mfn); | |
4bac07c9 | 727 | } else { |
bee6ab53 SY |
728 | if (xen_hvm_domain()) { |
729 | uint64_t v = 0; | |
730 | err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v); | |
731 | if (err) | |
732 | goto out_error; | |
733 | xen_store_evtchn = (int)v; | |
734 | err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v); | |
735 | if (err) | |
736 | goto out_error; | |
737 | xen_store_mfn = (unsigned long)v; | |
738 | xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT, PAGE_SIZE); | |
739 | } else { | |
740 | xen_store_evtchn = xen_start_info->store_evtchn; | |
741 | xen_store_mfn = xen_start_info->store_mfn; | |
742 | xen_store_interface = mfn_to_virt(xen_store_mfn); | |
a947f0f8 | 743 | xenstored_ready = 1; |
bee6ab53 | 744 | } |
4bac07c9 | 745 | } |
4bac07c9 JF |
746 | |
747 | /* Initialize the interface to xenstore. */ | |
748 | err = xs_init(); | |
749 | if (err) { | |
750 | printk(KERN_WARNING | |
751 | "XENBUS: Error initializing xenstore comms: %i\n", err); | |
2de06cc1 | 752 | goto out_error; |
4bac07c9 JF |
753 | } |
754 | ||
1107ba88 AZ |
755 | #ifdef CONFIG_XEN_COMPAT_XENFS |
756 | /* | |
757 | * Create xenfs mountpoint in /proc for compatibility with | |
758 | * utilities that expect to find "xenbus" under "/proc/xen". | |
759 | */ | |
760 | proc_mkdir("xen", NULL); | |
761 | #endif | |
762 | ||
4bac07c9 JF |
763 | return 0; |
764 | ||
4bac07c9 | 765 | out_error: |
b37a56d6 JQ |
766 | if (page != 0) |
767 | free_page(page); | |
2de06cc1 | 768 | |
4bac07c9 JF |
769 | return err; |
770 | } | |
771 | ||
183d03cc | 772 | postcore_initcall(xenbus_init); |
4bac07c9 JF |
773 | |
774 | MODULE_LICENSE("GPL"); |