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 | ||
2de06cc1 IC |
482 | printk(KERN_CRIT "%s type %s\n", __func__, type); |
483 | ||
4bac07c9 | 484 | dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n); |
2de06cc1 IC |
485 | if (IS_ERR(dir)) { |
486 | printk(KERN_CRIT "%s failed xenbus_directory\n", __func__); | |
4bac07c9 | 487 | return PTR_ERR(dir); |
2de06cc1 | 488 | } |
4bac07c9 JF |
489 | |
490 | for (i = 0; i < dir_n; i++) { | |
2de06cc1 IC |
491 | printk(KERN_CRIT "%s %d/%d %s\n", __func__, i+1,dir_n, dir[i]); |
492 | err = bus->probe(bus, type, dir[i]); | |
493 | if (err) { | |
494 | printk(KERN_CRIT "%s failed\n", __func__); | |
4bac07c9 | 495 | break; |
2de06cc1 | 496 | } |
4bac07c9 | 497 | } |
2de06cc1 | 498 | printk("%s done\n", __func__); |
4bac07c9 JF |
499 | kfree(dir); |
500 | return err; | |
501 | } | |
502 | ||
503 | int xenbus_probe_devices(struct xen_bus_type *bus) | |
504 | { | |
505 | int err = 0; | |
506 | char **dir; | |
507 | unsigned int i, dir_n; | |
508 | ||
2de06cc1 IC |
509 | printk(KERN_CRIT "%s %s\n", __func__, bus->root); |
510 | ||
4bac07c9 | 511 | dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n); |
2de06cc1 IC |
512 | if (IS_ERR(dir)) { |
513 | printk(KERN_CRIT "%s failed xenbus_directory\n", __func__); | |
4bac07c9 | 514 | return PTR_ERR(dir); |
2de06cc1 | 515 | } |
4bac07c9 JF |
516 | |
517 | for (i = 0; i < dir_n; i++) { | |
2de06cc1 | 518 | printk(KERN_CRIT "%s %d/%d %s\n", __func__, i+1,dir_n, dir[i]); |
4bac07c9 | 519 | err = xenbus_probe_device_type(bus, dir[i]); |
2de06cc1 IC |
520 | if (err) { |
521 | printk(KERN_CRIT "%s failed\n", __func__); | |
4bac07c9 | 522 | break; |
2de06cc1 | 523 | } |
4bac07c9 | 524 | } |
2de06cc1 | 525 | printk("%s done\n", __func__); |
4bac07c9 JF |
526 | kfree(dir); |
527 | return err; | |
528 | } | |
2de06cc1 | 529 | EXPORT_SYMBOL_GPL(xenbus_probe_devices); |
4bac07c9 JF |
530 | |
531 | static unsigned int char_count(const char *str, char c) | |
532 | { | |
533 | unsigned int i, ret = 0; | |
534 | ||
535 | for (i = 0; str[i]; i++) | |
536 | if (str[i] == c) | |
537 | ret++; | |
538 | return ret; | |
539 | } | |
540 | ||
541 | static int strsep_len(const char *str, char c, unsigned int len) | |
542 | { | |
543 | unsigned int i; | |
544 | ||
545 | for (i = 0; str[i]; i++) | |
546 | if (str[i] == c) { | |
547 | if (len == 0) | |
548 | return i; | |
549 | len--; | |
550 | } | |
551 | return (len == 0) ? i : -ERANGE; | |
552 | } | |
553 | ||
554 | void xenbus_dev_changed(const char *node, struct xen_bus_type *bus) | |
555 | { | |
556 | int exists, rootlen; | |
557 | struct xenbus_device *dev; | |
2a678cc5 | 558 | char type[XEN_BUS_ID_SIZE]; |
4bac07c9 JF |
559 | const char *p, *root; |
560 | ||
561 | if (char_count(node, '/') < 2) | |
562 | return; | |
563 | ||
564 | exists = xenbus_exists(XBT_NIL, node, ""); | |
565 | if (!exists) { | |
566 | xenbus_cleanup_devices(node, &bus->bus); | |
567 | return; | |
568 | } | |
569 | ||
570 | /* backend/<type>/... or device/<type>/... */ | |
571 | p = strchr(node, '/') + 1; | |
2a678cc5 KS |
572 | snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p); |
573 | type[XEN_BUS_ID_SIZE-1] = '\0'; | |
4bac07c9 JF |
574 | |
575 | rootlen = strsep_len(node, '/', bus->levels); | |
576 | if (rootlen < 0) | |
577 | return; | |
578 | root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node); | |
579 | if (!root) | |
580 | return; | |
581 | ||
582 | dev = xenbus_device_find(root, &bus->bus); | |
583 | if (!dev) | |
584 | xenbus_probe_node(bus, type, root); | |
585 | else | |
586 | put_device(&dev->dev); | |
587 | ||
588 | kfree(root); | |
589 | } | |
c6a960ce | 590 | EXPORT_SYMBOL_GPL(xenbus_dev_changed); |
4bac07c9 | 591 | |
2de06cc1 | 592 | int xenbus_dev_suspend(struct device *dev, pm_message_t state) |
4bac07c9 JF |
593 | { |
594 | int err = 0; | |
595 | struct xenbus_driver *drv; | |
2de06cc1 | 596 | struct xenbus_device *xdev = container_of(dev, struct xenbus_device, dev); |
4bac07c9 | 597 | |
2de06cc1 | 598 | DPRINTK("%s", xdev->nodename); |
4bac07c9 JF |
599 | |
600 | if (dev->driver == NULL) | |
601 | return 0; | |
602 | drv = to_xenbus_driver(dev->driver); | |
4bac07c9 | 603 | if (drv->suspend) |
de5b31bd | 604 | err = drv->suspend(xdev, state); |
4bac07c9 JF |
605 | if (err) |
606 | printk(KERN_WARNING | |
2a678cc5 | 607 | "xenbus: suspend %s failed: %i\n", dev_name(dev), err); |
4bac07c9 JF |
608 | return 0; |
609 | } | |
2de06cc1 | 610 | EXPORT_SYMBOL_GPL(xenbus_dev_suspend); |
4bac07c9 | 611 | |
2de06cc1 | 612 | int xenbus_dev_resume(struct device *dev) |
4bac07c9 JF |
613 | { |
614 | int err; | |
615 | struct xenbus_driver *drv; | |
2de06cc1 | 616 | struct xenbus_device *xdev = container_of(dev, struct xenbus_device, dev); |
4bac07c9 | 617 | |
2de06cc1 | 618 | DPRINTK("%s", xdev->nodename); |
4bac07c9 JF |
619 | |
620 | if (dev->driver == NULL) | |
621 | return 0; | |
4bac07c9 | 622 | drv = to_xenbus_driver(dev->driver); |
4bac07c9 JF |
623 | err = talk_to_otherend(xdev); |
624 | if (err) { | |
625 | printk(KERN_WARNING | |
626 | "xenbus: resume (talk_to_otherend) %s failed: %i\n", | |
2a678cc5 | 627 | dev_name(dev), err); |
4bac07c9 JF |
628 | return err; |
629 | } | |
630 | ||
631 | xdev->state = XenbusStateInitialising; | |
632 | ||
633 | if (drv->resume) { | |
634 | err = drv->resume(xdev); | |
635 | if (err) { | |
636 | printk(KERN_WARNING | |
637 | "xenbus: resume %s failed: %i\n", | |
2a678cc5 | 638 | dev_name(dev), err); |
4bac07c9 JF |
639 | return err; |
640 | } | |
641 | } | |
642 | ||
643 | err = watch_otherend(xdev); | |
644 | if (err) { | |
645 | printk(KERN_WARNING | |
646 | "xenbus_probe: resume (watch_otherend) %s failed: " | |
2a678cc5 | 647 | "%d.\n", dev_name(dev), err); |
4bac07c9 JF |
648 | return err; |
649 | } | |
650 | ||
651 | return 0; | |
652 | } | |
2de06cc1 | 653 | EXPORT_SYMBOL_GPL(xenbus_dev_resume); |
4bac07c9 | 654 | |
4bac07c9 JF |
655 | /* A flag to determine if xenstored is 'ready' (i.e. has started) */ |
656 | int xenstored_ready = 0; | |
657 | ||
658 | ||
659 | int register_xenstore_notifier(struct notifier_block *nb) | |
660 | { | |
661 | int ret = 0; | |
662 | ||
a947f0f8 SS |
663 | if (xenstored_ready > 0) |
664 | ret = nb->notifier_call(nb, 0, NULL); | |
665 | else | |
666 | blocking_notifier_chain_register(&xenstore_chain, nb); | |
4bac07c9 JF |
667 | |
668 | return ret; | |
669 | } | |
670 | EXPORT_SYMBOL_GPL(register_xenstore_notifier); | |
671 | ||
672 | void unregister_xenstore_notifier(struct notifier_block *nb) | |
673 | { | |
674 | blocking_notifier_chain_unregister(&xenstore_chain, nb); | |
675 | } | |
676 | EXPORT_SYMBOL_GPL(unregister_xenstore_notifier); | |
677 | ||
678 | void xenbus_probe(struct work_struct *unused) | |
679 | { | |
a947f0f8 | 680 | xenstored_ready = 1; |
4bac07c9 | 681 | |
df660251 IC |
682 | printk(KERN_CRIT "xenbus_probe wake_waiting\n"); |
683 | ||
4bac07c9 JF |
684 | /* Notify others that xenstore is up */ |
685 | blocking_notifier_call_chain(&xenstore_chain, 0, NULL); | |
686 | } | |
183d03cc | 687 | EXPORT_SYMBOL_GPL(xenbus_probe); |
4bac07c9 | 688 | |
183d03cc SS |
689 | static int __init xenbus_probe_initcall(void) |
690 | { | |
691 | if (!xen_domain()) | |
692 | return -ENODEV; | |
693 | ||
694 | if (xen_initial_domain() || xen_hvm_domain()) | |
695 | return 0; | |
696 | ||
697 | xenbus_probe(NULL); | |
698 | return 0; | |
699 | } | |
700 | ||
701 | device_initcall(xenbus_probe_initcall); | |
702 | ||
703 | static int __init xenbus_init(void) | |
4bac07c9 JF |
704 | { |
705 | int err = 0; | |
b37a56d6 | 706 | unsigned long page = 0; |
4bac07c9 JF |
707 | |
708 | DPRINTK(""); | |
709 | ||
710 | err = -ENODEV; | |
6e833587 | 711 | if (!xen_domain()) |
7432e4bd | 712 | return err; |
4bac07c9 | 713 | |
4bac07c9 JF |
714 | /* |
715 | * Domain0 doesn't have a store_evtchn or store_mfn yet. | |
716 | */ | |
6e833587 | 717 | if (xen_initial_domain()) { |
b37a56d6 JQ |
718 | struct evtchn_alloc_unbound alloc_unbound; |
719 | ||
720 | /* Allocate Xenstore page */ | |
721 | page = get_zeroed_page(GFP_KERNEL); | |
722 | if (!page) | |
723 | goto out_error; | |
724 | ||
725 | xen_store_mfn = xen_start_info->store_mfn = | |
726 | pfn_to_mfn(virt_to_phys((void *)page) >> | |
727 | PAGE_SHIFT); | |
728 | ||
729 | /* Next allocate a local port which xenstored can bind to */ | |
730 | alloc_unbound.dom = DOMID_SELF; | |
731 | alloc_unbound.remote_dom = 0; | |
732 | ||
733 | err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, | |
734 | &alloc_unbound); | |
735 | if (err == -ENOSYS) | |
736 | goto out_error; | |
737 | ||
738 | BUG_ON(err); | |
739 | xen_store_evtchn = xen_start_info->store_evtchn = | |
740 | alloc_unbound.port; | |
741 | ||
742 | xen_store_interface = mfn_to_virt(xen_store_mfn); | |
4bac07c9 | 743 | } else { |
bee6ab53 SY |
744 | if (xen_hvm_domain()) { |
745 | uint64_t v = 0; | |
746 | err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v); | |
747 | if (err) | |
748 | goto out_error; | |
749 | xen_store_evtchn = (int)v; | |
750 | err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v); | |
751 | if (err) | |
752 | goto out_error; | |
753 | xen_store_mfn = (unsigned long)v; | |
754 | xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT, PAGE_SIZE); | |
755 | } else { | |
756 | xen_store_evtchn = xen_start_info->store_evtchn; | |
757 | xen_store_mfn = xen_start_info->store_mfn; | |
758 | xen_store_interface = mfn_to_virt(xen_store_mfn); | |
a947f0f8 | 759 | xenstored_ready = 1; |
bee6ab53 | 760 | } |
4bac07c9 | 761 | } |
4bac07c9 JF |
762 | |
763 | /* Initialize the interface to xenstore. */ | |
764 | err = xs_init(); | |
765 | if (err) { | |
766 | printk(KERN_WARNING | |
767 | "XENBUS: Error initializing xenstore comms: %i\n", err); | |
2de06cc1 | 768 | goto out_error; |
4bac07c9 JF |
769 | } |
770 | ||
1107ba88 AZ |
771 | #ifdef CONFIG_XEN_COMPAT_XENFS |
772 | /* | |
773 | * Create xenfs mountpoint in /proc for compatibility with | |
774 | * utilities that expect to find "xenbus" under "/proc/xen". | |
775 | */ | |
776 | proc_mkdir("xen", NULL); | |
777 | #endif | |
778 | ||
2de06cc1 | 779 | printk(KERN_CRIT "%s ok\n", __func__); |
4bac07c9 JF |
780 | return 0; |
781 | ||
4bac07c9 | 782 | out_error: |
b37a56d6 JQ |
783 | if (page != 0) |
784 | free_page(page); | |
2de06cc1 | 785 | |
4bac07c9 JF |
786 | return err; |
787 | } | |
788 | ||
183d03cc | 789 | postcore_initcall(xenbus_init); |
4bac07c9 JF |
790 | |
791 | MODULE_LICENSE("GPL"); |