blob: 132939f3602014abb1c9c62e3c97236fd7d45e02 [file] [log] [blame]
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -07001/******************************************************************************
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>
Alex Zeffertt1107ba82009-01-07 18:07:11 -080043#include <linux/proc_fs.h>
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070044#include <linux/notifier.h>
45#include <linux/kthread.h>
46#include <linux/mutex.h>
47#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070049
50#include <asm/page.h>
51#include <asm/pgtable.h>
52#include <asm/xen/hypervisor.h>
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070053
54#include <xen/xen.h>
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070055#include <xen/xenbus.h>
56#include <xen/events.h>
57#include <xen/page.h>
58
Stefano Stabellinic1c54132010-05-14 12:44:30 +010059#include <xen/platform_pci.h>
Sheng Yangbee6ab532010-05-14 12:39:33 +010060#include <xen/hvm.h>
61
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070062#include "xenbus_comms.h"
63#include "xenbus_probe.h"
64
Alex Zeffertt1107ba82009-01-07 18:07:11 -080065
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070066int xen_store_evtchn;
Jeremy Fitzhardinge8e3e9992009-03-21 23:51:26 -070067EXPORT_SYMBOL_GPL(xen_store_evtchn);
Alex Zeffertt1107ba82009-01-07 18:07:11 -080068
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070069struct xenstore_domain_interface *xen_store_interface;
Jeremy Fitzhardinge8e3e9992009-03-21 23:51:26 -070070EXPORT_SYMBOL_GPL(xen_store_interface);
71
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070072static unsigned long xen_store_mfn;
73
74static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
75
76static void wait_for_devices(struct xenbus_driver *xendrv);
77
78static int xenbus_probe_frontend(const char *type, const char *name);
79
80static void xenbus_dev_shutdown(struct device *_dev);
81
Ian Campbellde5b31b2009-02-09 12:05:50 -080082static int xenbus_dev_suspend(struct device *dev, pm_message_t state);
83static int xenbus_dev_resume(struct device *dev);
84
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -070085/* If something in array of ids matches this device, return it. */
86static const struct xenbus_device_id *
87match_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
96int 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
Mark McLoughlind2f0c522008-04-02 10:54:05 -0700106static 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
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700116/* device/<type>/<id> => <type>-<id> */
Kay Sievers2a678cc2009-01-06 10:44:34 -0800117static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700118{
119 nodename = strchr(nodename, '/');
Kay Sievers2a678cc2009-01-06 10:44:34 -0800120 if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700121 printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
122 return -EINVAL;
123 }
124
Kay Sievers2a678cc2009-01-06 10:44:34 -0800125 strlcpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700126 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
135static void free_otherend_details(struct xenbus_device *dev)
136{
137 kfree(dev->otherend);
138 dev->otherend = NULL;
139}
140
141
142static 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
152int 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
179static int read_backend_details(struct xenbus_device *xendev)
180{
181 return read_otherend_details(xendev, "backend-id", "backend");
182}
183
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800184static struct device_attribute xenbus_dev_attrs[] = {
185 __ATTR_NULL
186};
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700187
188/* Bus type for frontend drivers. */
189static 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 = {
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800195 .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,
Ian Campbellde5b31b2009-02-09 12:05:50 -0800202
203 .suspend = xenbus_dev_suspend,
204 .resume = xenbus_dev_resume,
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700205 },
206};
207
208static 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))) {
Joe Perches898eb712007-10-18 03:06:30 -0700221 dev_dbg(&dev->dev, "Ignoring watch at %s\n",
222 vec[XS_WATCH_PATH]);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700223 return;
224 }
225
226 state = xenbus_read_driver_state(dev->otherend);
227
Joe Perches898eb712007-10-18 03:06:30 -0700228 dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700229 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
251static 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
262static 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
269int 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;
308fail:
309 xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
310 xenbus_switch_state(dev, XenbusStateClosed);
311 return -ENODEV;
312}
313
314int 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
331static 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
353int 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
366int __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}
383EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
384
385void xenbus_unregister_driver(struct xenbus_driver *drv)
386{
387 driver_unregister(&drv->driver);
388}
389EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
390
391struct xb_find_info
392{
393 struct xenbus_device *dev;
394 const char *nodename;
395};
396
397static 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
410struct 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
419static 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
440static 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
454static void xenbus_dev_release(struct device *dev)
455{
456 if (dev)
457 kfree(to_xenbus_device(dev));
458}
459
460static 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}
Jeremy Fitzhardingedb05fed2009-11-24 16:41:47 -0800465static DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700466
467static 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}
Jeremy Fitzhardingedb05fed2009-11-24 16:41:47 -0800472static DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700473
Mark McLoughlind2f0c522008-04-02 10:54:05 -0700474static 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}
Jeremy Fitzhardingedb05fed2009-11-24 16:41:47 -0800479static DEVICE_ATTR(modalias, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_modalias, NULL);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700480
481int xenbus_probe_node(struct xen_bus_type *bus,
482 const char *type,
483 const char *nodename)
484{
Kay Sievers2a678cc2009-01-06 10:44:34 -0800485 char devname[XEN_BUS_ID_SIZE];
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700486 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
Kay Sievers2a678cc2009-01-06 10:44:34 -0800520 err = bus->get_bus_id(devname, xendev->nodename);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700521 if (err)
522 goto fail;
523
Kay Sievers2a678cc2009-01-06 10:44:34 -0800524 dev_set_name(&xendev->dev, devname);
525
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700526 /* 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)
Mark McLoughlind2f0c522008-04-02 10:54:05 -0700537 goto fail_remove_nodename;
538
539 err = device_create_file(&xendev->dev, &dev_attr_modalias);
540 if (err)
541 goto fail_remove_devtype;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700542
543 return 0;
Mark McLoughlind2f0c522008-04-02 10:54:05 -0700544fail_remove_devtype:
545 device_remove_file(&xendev->dev, &dev_attr_devtype);
546fail_remove_nodename:
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700547 device_remove_file(&xendev->dev, &dev_attr_nodename);
548fail_unregister:
549 device_unregister(&xendev->dev);
550fail:
551 kfree(xendev);
552 return err;
553}
554
555/* device/<typename>/<name> */
556static 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
573static 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
593int 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
612static 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
622static 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
635void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
636{
637 int exists, rootlen;
638 struct xenbus_device *dev;
Kay Sievers2a678cc2009-01-06 10:44:34 -0800639 char type[XEN_BUS_ID_SIZE];
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700640 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;
Kay Sievers2a678cc2009-01-06 10:44:34 -0800653 snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
654 type[XEN_BUS_ID_SIZE-1] = '\0';
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700655
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}
Jeremy Fitzhardingec6a960c2009-02-09 12:05:53 -0800671EXPORT_SYMBOL_GPL(xenbus_dev_changed);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700672
673static 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. */
682static struct xenbus_watch fe_watch = {
683 .node = "device",
684 .callback = frontend_changed,
685};
686
Ian Campbellde5b31b2009-02-09 12:05:50 -0800687static int xenbus_dev_suspend(struct device *dev, pm_message_t state)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700688{
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)
Ian Campbellde5b31b2009-02-09 12:05:50 -0800700 err = drv->suspend(xdev, state);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700701 if (err)
702 printk(KERN_WARNING
Kay Sievers2a678cc2009-01-06 10:44:34 -0800703 "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700704 return 0;
705}
706
Ian Campbellde5b31b2009-02-09 12:05:50 -0800707static int xenbus_dev_resume(struct device *dev)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700708{
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",
Kay Sievers2a678cc2009-01-06 10:44:34 -0800725 dev_name(dev), err);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700726 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",
Kay Sievers2a678cc2009-01-06 10:44:34 -0800736 dev_name(dev), err);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700737 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: "
Kay Sievers2a678cc2009-01-06 10:44:34 -0800745 "%d.\n", dev_name(dev), err);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700746 return err;
747 }
748
749 return 0;
750}
751
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700752/* A flag to determine if xenstored is 'ready' (i.e. has started) */
753int xenstored_ready = 0;
754
755
756int register_xenstore_notifier(struct notifier_block *nb)
757{
758 int ret = 0;
759
Stefano Stabellinia947f0f2010-10-04 16:10:06 +0100760 if (xenstored_ready > 0)
761 ret = nb->notifier_call(nb, 0, NULL);
762 else
763 blocking_notifier_chain_register(&xenstore_chain, nb);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700764
765 return ret;
766}
767EXPORT_SYMBOL_GPL(register_xenstore_notifier);
768
769void unregister_xenstore_notifier(struct notifier_block *nb)
770{
771 blocking_notifier_chain_unregister(&xenstore_chain, nb);
772}
773EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
774
775void xenbus_probe(struct work_struct *unused)
776{
Stefano Stabellinia947f0f2010-10-04 16:10:06 +0100777 xenstored_ready = 1;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700778
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}
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100787EXPORT_SYMBOL_GPL(xenbus_probe);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700788
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100789static 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
801device_initcall(xenbus_probe_initcall);
802
803static int __init xenbus_init(void)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700804{
805 int err = 0;
806
807 DPRINTK("");
808
809 err = -ENODEV;
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -0700810 if (!xen_domain())
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700811 goto out_error;
812
813 /* Register ourselves with the kernel bus subsystem */
814 err = bus_register(&xenbus_frontend.bus);
815 if (err)
816 goto out_error;
817
818 err = xenbus_backend_bus_register();
819 if (err)
820 goto out_unreg_front;
821
822 /*
823 * Domain0 doesn't have a store_evtchn or store_mfn yet.
824 */
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -0700825 if (xen_initial_domain()) {
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700826 /* dom0 not yet supported */
827 } else {
Sheng Yangbee6ab532010-05-14 12:39:33 +0100828 if (xen_hvm_domain()) {
829 uint64_t v = 0;
830 err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
831 if (err)
832 goto out_error;
833 xen_store_evtchn = (int)v;
834 err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
835 if (err)
836 goto out_error;
837 xen_store_mfn = (unsigned long)v;
838 xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT, PAGE_SIZE);
839 } else {
840 xen_store_evtchn = xen_start_info->store_evtchn;
841 xen_store_mfn = xen_start_info->store_mfn;
842 xen_store_interface = mfn_to_virt(xen_store_mfn);
Stefano Stabellinia947f0f2010-10-04 16:10:06 +0100843 xenstored_ready = 1;
Sheng Yangbee6ab532010-05-14 12:39:33 +0100844 }
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700845 }
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700846
847 /* Initialize the interface to xenstore. */
848 err = xs_init();
849 if (err) {
850 printk(KERN_WARNING
851 "XENBUS: Error initializing xenstore comms: %i\n", err);
852 goto out_unreg_back;
853 }
854
Alex Zeffertt1107ba82009-01-07 18:07:11 -0800855#ifdef CONFIG_XEN_COMPAT_XENFS
856 /*
857 * Create xenfs mountpoint in /proc for compatibility with
858 * utilities that expect to find "xenbus" under "/proc/xen".
859 */
860 proc_mkdir("xen", NULL);
861#endif
862
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700863 return 0;
864
865 out_unreg_back:
866 xenbus_backend_bus_unregister();
867
868 out_unreg_front:
869 bus_unregister(&xenbus_frontend.bus);
870
871 out_error:
872 return err;
873}
874
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100875postcore_initcall(xenbus_init);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700876
877MODULE_LICENSE("GPL");
878
Paolo Bonzinic6e19712009-07-08 12:27:37 +0200879static int is_device_connecting(struct device *dev, void *data)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700880{
881 struct xenbus_device *xendev = to_xenbus_device(dev);
882 struct device_driver *drv = data;
Christian Limpach1d78d702008-04-02 10:54:04 -0700883 struct xenbus_driver *xendrv;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700884
885 /*
886 * A device with no driver will never connect. We care only about
887 * devices which should currently be in the process of connecting.
888 */
889 if (!dev->driver)
890 return 0;
891
892 /* Is this search limited to a particular driver? */
893 if (drv && (dev->driver != drv))
894 return 0;
895
Christian Limpach1d78d702008-04-02 10:54:04 -0700896 xendrv = to_xenbus_driver(dev->driver);
Paolo Bonzinic6e19712009-07-08 12:27:37 +0200897 return (xendev->state < XenbusStateConnected ||
898 (xendev->state == XenbusStateConnected &&
899 xendrv->is_ready && !xendrv->is_ready(xendev)));
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700900}
901
Paolo Bonzinic6e19712009-07-08 12:27:37 +0200902static int exists_connecting_device(struct device_driver *drv)
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700903{
904 return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
Paolo Bonzinic6e19712009-07-08 12:27:37 +0200905 is_device_connecting);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700906}
907
908static int print_device_status(struct device *dev, void *data)
909{
910 struct xenbus_device *xendev = to_xenbus_device(dev);
911 struct device_driver *drv = data;
912
913 /* Is this operation limited to a particular driver? */
914 if (drv && (dev->driver != drv))
915 return 0;
916
917 if (!dev->driver) {
918 /* Information only: is this too noisy? */
919 printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
920 xendev->nodename);
Paolo Bonzinif8dc3302009-07-08 12:27:38 +0200921 } else if (xendev->state < XenbusStateConnected) {
922 enum xenbus_state rstate = XenbusStateUnknown;
923 if (xendev->otherend)
924 rstate = xenbus_read_driver_state(xendev->otherend);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700925 printk(KERN_WARNING "XENBUS: Timeout connecting "
Paolo Bonzinif8dc3302009-07-08 12:27:38 +0200926 "to device: %s (local state %d, remote state %d)\n",
927 xendev->nodename, xendev->state, rstate);
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700928 }
929
930 return 0;
931}
932
933/* We only wait for device setup after most initcalls have run. */
934static int ready_to_wait_for_devices;
935
936/*
Paolo Bonziniae788802009-07-08 12:27:39 +0200937 * On a 5-minute timeout, wait for all devices currently configured. We need
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700938 * to do this to guarantee that the filesystems and / or network devices
939 * needed for boot are available, before we can allow the boot to proceed.
940 *
941 * This needs to be on a late_initcall, to happen after the frontend device
942 * drivers have been initialised, but before the root fs is mounted.
943 *
944 * A possible improvement here would be to have the tools add a per-device
945 * flag to the store entry, indicating whether it is needed at boot time.
946 * This would allow people who knew what they were doing to accelerate their
947 * boot slightly, but of course needs tools or manual intervention to set up
948 * those flags correctly.
949 */
950static void wait_for_devices(struct xenbus_driver *xendrv)
951{
Paolo Bonziniae788802009-07-08 12:27:39 +0200952 unsigned long start = jiffies;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700953 struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
Paolo Bonziniae788802009-07-08 12:27:39 +0200954 unsigned int seconds_waited = 0;
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700955
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -0700956 if (!ready_to_wait_for_devices || !xen_domain())
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700957 return;
958
Paolo Bonzinic6e19712009-07-08 12:27:37 +0200959 while (exists_connecting_device(drv)) {
Paolo Bonziniae788802009-07-08 12:27:39 +0200960 if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
961 if (!seconds_waited)
962 printk(KERN_WARNING "XENBUS: Waiting for "
963 "devices to initialise: ");
964 seconds_waited += 5;
965 printk("%us...", 300 - seconds_waited);
966 if (seconds_waited == 300)
967 break;
968 }
969
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700970 schedule_timeout_interruptible(HZ/10);
971 }
972
Paolo Bonziniae788802009-07-08 12:27:39 +0200973 if (seconds_waited)
974 printk("\n");
975
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700976 bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
977 print_device_status);
978}
979
980#ifndef MODULE
981static int __init boot_wait_for_devices(void)
982{
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100983 if (xen_hvm_domain() && !xen_platform_pci_unplug)
984 return -ENODEV;
985
Jeremy Fitzhardinge4bac07c2007-07-17 18:37:06 -0700986 ready_to_wait_for_devices = 1;
987 wait_for_devices(NULL);
988 return 0;
989}
990
991late_initcall(boot_wait_for_devices);
992#endif