Back to home page

OSCL-LXR

 
 

    


0001 /******************************************************************************
0002  * Talks to Xen Store to figure out what devices we have.
0003  *
0004  * Copyright (C) 2005 Rusty Russell, IBM Corporation
0005  * Copyright (C) 2005 Mike Wray, Hewlett-Packard
0006  * Copyright (C) 2005, 2006 XenSource Ltd
0007  *
0008  * This program is free software; you can redistribute it and/or
0009  * modify it under the terms of the GNU General Public License version 2
0010  * as published by the Free Software Foundation; or, when distributed
0011  * separately from the Linux kernel or incorporated into other
0012  * software packages, subject to the following license:
0013  *
0014  * Permission is hereby granted, free of charge, to any person obtaining a copy
0015  * of this source file (the "Software"), to deal in the Software without
0016  * restriction, including without limitation the rights to use, copy, modify,
0017  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
0018  * and to permit persons to whom the Software is furnished to do so, subject to
0019  * the following conditions:
0020  *
0021  * The above copyright notice and this permission notice shall be included in
0022  * all copies or substantial portions of the Software.
0023  *
0024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0025  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0026  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0027  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0028  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0029  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
0030  * IN THE SOFTWARE.
0031  */
0032 
0033 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0034 #define dev_fmt pr_fmt
0035 
0036 #define DPRINTK(fmt, args...)               \
0037     pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
0038          __func__, __LINE__, ##args)
0039 
0040 #include <linux/kernel.h>
0041 #include <linux/err.h>
0042 #include <linux/string.h>
0043 #include <linux/ctype.h>
0044 #include <linux/fcntl.h>
0045 #include <linux/mm.h>
0046 #include <linux/proc_fs.h>
0047 #include <linux/notifier.h>
0048 #include <linux/kthread.h>
0049 #include <linux/mutex.h>
0050 #include <linux/io.h>
0051 #include <linux/slab.h>
0052 #include <linux/module.h>
0053 
0054 #include <asm/page.h>
0055 #include <asm/xen/hypervisor.h>
0056 
0057 #include <xen/xen.h>
0058 #include <xen/xenbus.h>
0059 #include <xen/events.h>
0060 #include <xen/xen-ops.h>
0061 #include <xen/page.h>
0062 
0063 #include <xen/hvm.h>
0064 
0065 #include "xenbus.h"
0066 
0067 
0068 static int xs_init_irq;
0069 int xen_store_evtchn;
0070 EXPORT_SYMBOL_GPL(xen_store_evtchn);
0071 
0072 struct xenstore_domain_interface *xen_store_interface;
0073 EXPORT_SYMBOL_GPL(xen_store_interface);
0074 
0075 enum xenstore_init xen_store_domain_type;
0076 EXPORT_SYMBOL_GPL(xen_store_domain_type);
0077 
0078 static unsigned long xen_store_gfn;
0079 
0080 static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
0081 
0082 /* If something in array of ids matches this device, return it. */
0083 static const struct xenbus_device_id *
0084 match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
0085 {
0086     for (; *arr->devicetype != '\0'; arr++) {
0087         if (!strcmp(arr->devicetype, dev->devicetype))
0088             return arr;
0089     }
0090     return NULL;
0091 }
0092 
0093 int xenbus_match(struct device *_dev, struct device_driver *_drv)
0094 {
0095     struct xenbus_driver *drv = to_xenbus_driver(_drv);
0096 
0097     if (!drv->ids)
0098         return 0;
0099 
0100     return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
0101 }
0102 EXPORT_SYMBOL_GPL(xenbus_match);
0103 
0104 
0105 static void free_otherend_details(struct xenbus_device *dev)
0106 {
0107     kfree(dev->otherend);
0108     dev->otherend = NULL;
0109 }
0110 
0111 
0112 static void free_otherend_watch(struct xenbus_device *dev)
0113 {
0114     if (dev->otherend_watch.node) {
0115         unregister_xenbus_watch(&dev->otherend_watch);
0116         kfree(dev->otherend_watch.node);
0117         dev->otherend_watch.node = NULL;
0118     }
0119 }
0120 
0121 
0122 static int talk_to_otherend(struct xenbus_device *dev)
0123 {
0124     struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
0125 
0126     free_otherend_watch(dev);
0127     free_otherend_details(dev);
0128 
0129     return drv->read_otherend_details(dev);
0130 }
0131 
0132 
0133 
0134 static int watch_otherend(struct xenbus_device *dev)
0135 {
0136     struct xen_bus_type *bus =
0137         container_of(dev->dev.bus, struct xen_bus_type, bus);
0138 
0139     return xenbus_watch_pathfmt(dev, &dev->otherend_watch,
0140                     bus->otherend_will_handle,
0141                     bus->otherend_changed,
0142                     "%s/%s", dev->otherend, "state");
0143 }
0144 
0145 
0146 int xenbus_read_otherend_details(struct xenbus_device *xendev,
0147                  char *id_node, char *path_node)
0148 {
0149     int err = xenbus_gather(XBT_NIL, xendev->nodename,
0150                 id_node, "%i", &xendev->otherend_id,
0151                 path_node, NULL, &xendev->otherend,
0152                 NULL);
0153     if (err) {
0154         xenbus_dev_fatal(xendev, err,
0155                  "reading other end details from %s",
0156                  xendev->nodename);
0157         return err;
0158     }
0159     if (strlen(xendev->otherend) == 0 ||
0160         !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
0161         xenbus_dev_fatal(xendev, -ENOENT,
0162                  "unable to read other end from %s.  "
0163                  "missing or inaccessible.",
0164                  xendev->nodename);
0165         free_otherend_details(xendev);
0166         return -ENOENT;
0167     }
0168 
0169     return 0;
0170 }
0171 EXPORT_SYMBOL_GPL(xenbus_read_otherend_details);
0172 
0173 void xenbus_otherend_changed(struct xenbus_watch *watch,
0174                  const char *path, const char *token,
0175                  int ignore_on_shutdown)
0176 {
0177     struct xenbus_device *dev =
0178         container_of(watch, struct xenbus_device, otherend_watch);
0179     struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
0180     enum xenbus_state state;
0181 
0182     /* Protect us against watches firing on old details when the otherend
0183        details change, say immediately after a resume. */
0184     if (!dev->otherend ||
0185         strncmp(dev->otherend, path, strlen(dev->otherend))) {
0186         dev_dbg(&dev->dev, "Ignoring watch at %s\n", path);
0187         return;
0188     }
0189 
0190     state = xenbus_read_driver_state(dev->otherend);
0191 
0192     dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
0193         state, xenbus_strstate(state), dev->otherend_watch.node, path);
0194 
0195     /*
0196      * Ignore xenbus transitions during shutdown. This prevents us doing
0197      * work that can fail e.g., when the rootfs is gone.
0198      */
0199     if (system_state > SYSTEM_RUNNING) {
0200         if (ignore_on_shutdown && (state == XenbusStateClosing))
0201             xenbus_frontend_closed(dev);
0202         return;
0203     }
0204 
0205     if (drv->otherend_changed)
0206         drv->otherend_changed(dev, state);
0207 }
0208 EXPORT_SYMBOL_GPL(xenbus_otherend_changed);
0209 
0210 #define XENBUS_SHOW_STAT(name)                      \
0211 static ssize_t name##_show(struct device *_dev,             \
0212                struct device_attribute *attr,       \
0213                char *buf)                   \
0214 {                                   \
0215     struct xenbus_device *dev = to_xenbus_device(_dev);     \
0216                                     \
0217     return sprintf(buf, "%d\n", atomic_read(&dev->name));       \
0218 }                                   \
0219 static DEVICE_ATTR_RO(name)
0220 
0221 XENBUS_SHOW_STAT(event_channels);
0222 XENBUS_SHOW_STAT(events);
0223 XENBUS_SHOW_STAT(spurious_events);
0224 XENBUS_SHOW_STAT(jiffies_eoi_delayed);
0225 
0226 static ssize_t spurious_threshold_show(struct device *_dev,
0227                        struct device_attribute *attr,
0228                        char *buf)
0229 {
0230     struct xenbus_device *dev = to_xenbus_device(_dev);
0231 
0232     return sprintf(buf, "%d\n", dev->spurious_threshold);
0233 }
0234 
0235 static ssize_t spurious_threshold_store(struct device *_dev,
0236                     struct device_attribute *attr,
0237                     const char *buf, size_t count)
0238 {
0239     struct xenbus_device *dev = to_xenbus_device(_dev);
0240     unsigned int val;
0241     ssize_t ret;
0242 
0243     ret = kstrtouint(buf, 0, &val);
0244     if (ret)
0245         return ret;
0246 
0247     dev->spurious_threshold = val;
0248 
0249     return count;
0250 }
0251 
0252 static DEVICE_ATTR_RW(spurious_threshold);
0253 
0254 static struct attribute *xenbus_attrs[] = {
0255     &dev_attr_event_channels.attr,
0256     &dev_attr_events.attr,
0257     &dev_attr_spurious_events.attr,
0258     &dev_attr_jiffies_eoi_delayed.attr,
0259     &dev_attr_spurious_threshold.attr,
0260     NULL
0261 };
0262 
0263 static const struct attribute_group xenbus_group = {
0264     .name = "xenbus",
0265     .attrs = xenbus_attrs,
0266 };
0267 
0268 int xenbus_dev_probe(struct device *_dev)
0269 {
0270     struct xenbus_device *dev = to_xenbus_device(_dev);
0271     struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
0272     const struct xenbus_device_id *id;
0273     int err;
0274 
0275     DPRINTK("%s", dev->nodename);
0276 
0277     if (!drv->probe) {
0278         err = -ENODEV;
0279         goto fail;
0280     }
0281 
0282     id = match_device(drv->ids, dev);
0283     if (!id) {
0284         err = -ENODEV;
0285         goto fail;
0286     }
0287 
0288     err = talk_to_otherend(dev);
0289     if (err) {
0290         dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
0291              dev->nodename);
0292         return err;
0293     }
0294 
0295     if (!try_module_get(drv->driver.owner)) {
0296         dev_warn(&dev->dev, "failed to acquire module reference on '%s'\n",
0297              drv->driver.name);
0298         err = -ESRCH;
0299         goto fail;
0300     }
0301 
0302     down(&dev->reclaim_sem);
0303     err = drv->probe(dev, id);
0304     up(&dev->reclaim_sem);
0305     if (err)
0306         goto fail_put;
0307 
0308     err = watch_otherend(dev);
0309     if (err) {
0310         dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
0311                dev->nodename);
0312         return err;
0313     }
0314 
0315     dev->spurious_threshold = 1;
0316     if (sysfs_create_group(&dev->dev.kobj, &xenbus_group))
0317         dev_warn(&dev->dev, "sysfs_create_group on %s failed.\n",
0318              dev->nodename);
0319 
0320     return 0;
0321 fail_put:
0322     module_put(drv->driver.owner);
0323 fail:
0324     xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
0325     return err;
0326 }
0327 EXPORT_SYMBOL_GPL(xenbus_dev_probe);
0328 
0329 void xenbus_dev_remove(struct device *_dev)
0330 {
0331     struct xenbus_device *dev = to_xenbus_device(_dev);
0332     struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
0333 
0334     DPRINTK("%s", dev->nodename);
0335 
0336     sysfs_remove_group(&dev->dev.kobj, &xenbus_group);
0337 
0338     free_otherend_watch(dev);
0339 
0340     if (drv->remove) {
0341         down(&dev->reclaim_sem);
0342         drv->remove(dev);
0343         up(&dev->reclaim_sem);
0344     }
0345 
0346     module_put(drv->driver.owner);
0347 
0348     free_otherend_details(dev);
0349 
0350     /*
0351      * If the toolstack has forced the device state to closing then set
0352      * the state to closed now to allow it to be cleaned up.
0353      * Similarly, if the driver does not support re-bind, set the
0354      * closed.
0355      */
0356     if (!drv->allow_rebind ||
0357         xenbus_read_driver_state(dev->nodename) == XenbusStateClosing)
0358         xenbus_switch_state(dev, XenbusStateClosed);
0359 }
0360 EXPORT_SYMBOL_GPL(xenbus_dev_remove);
0361 
0362 int xenbus_register_driver_common(struct xenbus_driver *drv,
0363                   struct xen_bus_type *bus,
0364                   struct module *owner, const char *mod_name)
0365 {
0366     drv->driver.name = drv->name ? drv->name : drv->ids[0].devicetype;
0367     drv->driver.bus = &bus->bus;
0368     drv->driver.owner = owner;
0369     drv->driver.mod_name = mod_name;
0370 
0371     return driver_register(&drv->driver);
0372 }
0373 EXPORT_SYMBOL_GPL(xenbus_register_driver_common);
0374 
0375 void xenbus_unregister_driver(struct xenbus_driver *drv)
0376 {
0377     driver_unregister(&drv->driver);
0378 }
0379 EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
0380 
0381 struct xb_find_info {
0382     struct xenbus_device *dev;
0383     const char *nodename;
0384 };
0385 
0386 static int cmp_dev(struct device *dev, void *data)
0387 {
0388     struct xenbus_device *xendev = to_xenbus_device(dev);
0389     struct xb_find_info *info = data;
0390 
0391     if (!strcmp(xendev->nodename, info->nodename)) {
0392         info->dev = xendev;
0393         get_device(dev);
0394         return 1;
0395     }
0396     return 0;
0397 }
0398 
0399 static struct xenbus_device *xenbus_device_find(const char *nodename,
0400                         struct bus_type *bus)
0401 {
0402     struct xb_find_info info = { .dev = NULL, .nodename = nodename };
0403 
0404     bus_for_each_dev(bus, NULL, &info, cmp_dev);
0405     return info.dev;
0406 }
0407 
0408 static int cleanup_dev(struct device *dev, void *data)
0409 {
0410     struct xenbus_device *xendev = to_xenbus_device(dev);
0411     struct xb_find_info *info = data;
0412     int len = strlen(info->nodename);
0413 
0414     DPRINTK("%s", info->nodename);
0415 
0416     /* Match the info->nodename path, or any subdirectory of that path. */
0417     if (strncmp(xendev->nodename, info->nodename, len))
0418         return 0;
0419 
0420     /* If the node name is longer, ensure it really is a subdirectory. */
0421     if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
0422         return 0;
0423 
0424     info->dev = xendev;
0425     get_device(dev);
0426     return 1;
0427 }
0428 
0429 static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
0430 {
0431     struct xb_find_info info = { .nodename = path };
0432 
0433     do {
0434         info.dev = NULL;
0435         bus_for_each_dev(bus, NULL, &info, cleanup_dev);
0436         if (info.dev) {
0437             device_unregister(&info.dev->dev);
0438             put_device(&info.dev->dev);
0439         }
0440     } while (info.dev);
0441 }
0442 
0443 static void xenbus_dev_release(struct device *dev)
0444 {
0445     if (dev)
0446         kfree(to_xenbus_device(dev));
0447 }
0448 
0449 static ssize_t nodename_show(struct device *dev,
0450                  struct device_attribute *attr, char *buf)
0451 {
0452     return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
0453 }
0454 static DEVICE_ATTR_RO(nodename);
0455 
0456 static ssize_t devtype_show(struct device *dev,
0457                 struct device_attribute *attr, char *buf)
0458 {
0459     return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
0460 }
0461 static DEVICE_ATTR_RO(devtype);
0462 
0463 static ssize_t modalias_show(struct device *dev,
0464                  struct device_attribute *attr, char *buf)
0465 {
0466     return sprintf(buf, "%s:%s\n", dev->bus->name,
0467                to_xenbus_device(dev)->devicetype);
0468 }
0469 static DEVICE_ATTR_RO(modalias);
0470 
0471 static ssize_t state_show(struct device *dev,
0472                 struct device_attribute *attr, char *buf)
0473 {
0474     return sprintf(buf, "%s\n",
0475             xenbus_strstate(to_xenbus_device(dev)->state));
0476 }
0477 static DEVICE_ATTR_RO(state);
0478 
0479 static struct attribute *xenbus_dev_attrs[] = {
0480     &dev_attr_nodename.attr,
0481     &dev_attr_devtype.attr,
0482     &dev_attr_modalias.attr,
0483     &dev_attr_state.attr,
0484     NULL,
0485 };
0486 
0487 static const struct attribute_group xenbus_dev_group = {
0488     .attrs = xenbus_dev_attrs,
0489 };
0490 
0491 const struct attribute_group *xenbus_dev_groups[] = {
0492     &xenbus_dev_group,
0493     NULL,
0494 };
0495 EXPORT_SYMBOL_GPL(xenbus_dev_groups);
0496 
0497 int xenbus_probe_node(struct xen_bus_type *bus,
0498               const char *type,
0499               const char *nodename)
0500 {
0501     char devname[XEN_BUS_ID_SIZE];
0502     int err;
0503     struct xenbus_device *xendev;
0504     size_t stringlen;
0505     char *tmpstring;
0506 
0507     enum xenbus_state state = xenbus_read_driver_state(nodename);
0508 
0509     if (state != XenbusStateInitialising) {
0510         /* Device is not new, so ignore it.  This can happen if a
0511            device is going away after switching to Closed.  */
0512         return 0;
0513     }
0514 
0515     stringlen = strlen(nodename) + 1 + strlen(type) + 1;
0516     xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
0517     if (!xendev)
0518         return -ENOMEM;
0519 
0520     xendev->state = XenbusStateInitialising;
0521 
0522     /* Copy the strings into the extra space. */
0523 
0524     tmpstring = (char *)(xendev + 1);
0525     strcpy(tmpstring, nodename);
0526     xendev->nodename = tmpstring;
0527 
0528     tmpstring += strlen(tmpstring) + 1;
0529     strcpy(tmpstring, type);
0530     xendev->devicetype = tmpstring;
0531     init_completion(&xendev->down);
0532 
0533     xendev->dev.bus = &bus->bus;
0534     xendev->dev.release = xenbus_dev_release;
0535 
0536     err = bus->get_bus_id(devname, xendev->nodename);
0537     if (err)
0538         goto fail;
0539 
0540     dev_set_name(&xendev->dev, "%s", devname);
0541     sema_init(&xendev->reclaim_sem, 1);
0542 
0543     /* Register with generic device framework. */
0544     err = device_register(&xendev->dev);
0545     if (err) {
0546         put_device(&xendev->dev);
0547         xendev = NULL;
0548         goto fail;
0549     }
0550 
0551     return 0;
0552 fail:
0553     kfree(xendev);
0554     return err;
0555 }
0556 EXPORT_SYMBOL_GPL(xenbus_probe_node);
0557 
0558 static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
0559 {
0560     int err = 0;
0561     char **dir;
0562     unsigned int dir_n = 0;
0563     int i;
0564 
0565     dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
0566     if (IS_ERR(dir))
0567         return PTR_ERR(dir);
0568 
0569     for (i = 0; i < dir_n; i++) {
0570         err = bus->probe(bus, type, dir[i]);
0571         if (err)
0572             break;
0573     }
0574 
0575     kfree(dir);
0576     return err;
0577 }
0578 
0579 int xenbus_probe_devices(struct xen_bus_type *bus)
0580 {
0581     int err = 0;
0582     char **dir;
0583     unsigned int i, dir_n;
0584 
0585     dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
0586     if (IS_ERR(dir))
0587         return PTR_ERR(dir);
0588 
0589     for (i = 0; i < dir_n; i++) {
0590         err = xenbus_probe_device_type(bus, dir[i]);
0591         if (err)
0592             break;
0593     }
0594 
0595     kfree(dir);
0596     return err;
0597 }
0598 EXPORT_SYMBOL_GPL(xenbus_probe_devices);
0599 
0600 static unsigned int char_count(const char *str, char c)
0601 {
0602     unsigned int i, ret = 0;
0603 
0604     for (i = 0; str[i]; i++)
0605         if (str[i] == c)
0606             ret++;
0607     return ret;
0608 }
0609 
0610 static int strsep_len(const char *str, char c, unsigned int len)
0611 {
0612     unsigned int i;
0613 
0614     for (i = 0; str[i]; i++)
0615         if (str[i] == c) {
0616             if (len == 0)
0617                 return i;
0618             len--;
0619         }
0620     return (len == 0) ? i : -ERANGE;
0621 }
0622 
0623 void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
0624 {
0625     int exists, rootlen;
0626     struct xenbus_device *dev;
0627     char type[XEN_BUS_ID_SIZE];
0628     const char *p, *root;
0629 
0630     if (char_count(node, '/') < 2)
0631         return;
0632 
0633     exists = xenbus_exists(XBT_NIL, node, "");
0634     if (!exists) {
0635         xenbus_cleanup_devices(node, &bus->bus);
0636         return;
0637     }
0638 
0639     /* backend/<type>/... or device/<type>/... */
0640     p = strchr(node, '/') + 1;
0641     snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
0642     type[XEN_BUS_ID_SIZE-1] = '\0';
0643 
0644     rootlen = strsep_len(node, '/', bus->levels);
0645     if (rootlen < 0)
0646         return;
0647     root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
0648     if (!root)
0649         return;
0650 
0651     dev = xenbus_device_find(root, &bus->bus);
0652     if (!dev)
0653         xenbus_probe_node(bus, type, root);
0654     else
0655         put_device(&dev->dev);
0656 
0657     kfree(root);
0658 }
0659 EXPORT_SYMBOL_GPL(xenbus_dev_changed);
0660 
0661 int xenbus_dev_suspend(struct device *dev)
0662 {
0663     int err = 0;
0664     struct xenbus_driver *drv;
0665     struct xenbus_device *xdev
0666         = container_of(dev, struct xenbus_device, dev);
0667 
0668     DPRINTK("%s", xdev->nodename);
0669 
0670     if (dev->driver == NULL)
0671         return 0;
0672     drv = to_xenbus_driver(dev->driver);
0673     if (drv->suspend)
0674         err = drv->suspend(xdev);
0675     if (err)
0676         dev_warn(dev, "suspend failed: %i\n", err);
0677     return 0;
0678 }
0679 EXPORT_SYMBOL_GPL(xenbus_dev_suspend);
0680 
0681 int xenbus_dev_resume(struct device *dev)
0682 {
0683     int err;
0684     struct xenbus_driver *drv;
0685     struct xenbus_device *xdev
0686         = container_of(dev, struct xenbus_device, dev);
0687 
0688     DPRINTK("%s", xdev->nodename);
0689 
0690     if (dev->driver == NULL)
0691         return 0;
0692     drv = to_xenbus_driver(dev->driver);
0693     err = talk_to_otherend(xdev);
0694     if (err) {
0695         dev_warn(dev, "resume (talk_to_otherend) failed: %i\n", err);
0696         return err;
0697     }
0698 
0699     xdev->state = XenbusStateInitialising;
0700 
0701     if (drv->resume) {
0702         err = drv->resume(xdev);
0703         if (err) {
0704             dev_warn(dev, "resume failed: %i\n", err);
0705             return err;
0706         }
0707     }
0708 
0709     err = watch_otherend(xdev);
0710     if (err) {
0711         dev_warn(dev, "resume (watch_otherend) failed: %d\n", err);
0712         return err;
0713     }
0714 
0715     return 0;
0716 }
0717 EXPORT_SYMBOL_GPL(xenbus_dev_resume);
0718 
0719 int xenbus_dev_cancel(struct device *dev)
0720 {
0721     /* Do nothing */
0722     DPRINTK("cancel");
0723     return 0;
0724 }
0725 EXPORT_SYMBOL_GPL(xenbus_dev_cancel);
0726 
0727 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
0728 int xenstored_ready;
0729 
0730 
0731 int register_xenstore_notifier(struct notifier_block *nb)
0732 {
0733     int ret = 0;
0734 
0735     if (xenstored_ready > 0)
0736         ret = nb->notifier_call(nb, 0, NULL);
0737     else
0738         blocking_notifier_chain_register(&xenstore_chain, nb);
0739 
0740     return ret;
0741 }
0742 EXPORT_SYMBOL_GPL(register_xenstore_notifier);
0743 
0744 void unregister_xenstore_notifier(struct notifier_block *nb)
0745 {
0746     blocking_notifier_chain_unregister(&xenstore_chain, nb);
0747 }
0748 EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
0749 
0750 static void xenbus_probe(void)
0751 {
0752     xenstored_ready = 1;
0753 
0754     if (!xen_store_interface) {
0755         xen_store_interface = memremap(xen_store_gfn << XEN_PAGE_SHIFT,
0756                            XEN_PAGE_SIZE, MEMREMAP_WB);
0757         /*
0758          * Now it is safe to free the IRQ used for xenstore late
0759          * initialization. No need to unbind: it is about to be
0760          * bound again from xb_init_comms. Note that calling
0761          * unbind_from_irqhandler now would result in xen_evtchn_close()
0762          * being called and the event channel not being enabled again
0763          * afterwards, resulting in missed event notifications.
0764          */
0765         free_irq(xs_init_irq, &xb_waitq);
0766     }
0767 
0768     /*
0769      * In the HVM case, xenbus_init() deferred its call to
0770      * xs_init() in case callbacks were not operational yet.
0771      * So do it now.
0772      */
0773     if (xen_store_domain_type == XS_HVM)
0774         xs_init();
0775 
0776     /* Notify others that xenstore is up */
0777     blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
0778 }
0779 
0780 /*
0781  * Returns true when XenStore init must be deferred in order to
0782  * allow the PCI platform device to be initialised, before we
0783  * can actually have event channel interrupts working.
0784  */
0785 static bool xs_hvm_defer_init_for_callback(void)
0786 {
0787 #ifdef CONFIG_XEN_PVHVM
0788     return xen_store_domain_type == XS_HVM &&
0789         !xen_have_vector_callback;
0790 #else
0791     return false;
0792 #endif
0793 }
0794 
0795 static int xenbus_probe_thread(void *unused)
0796 {
0797     DEFINE_WAIT(w);
0798 
0799     /*
0800      * We actually just want to wait for *any* trigger of xb_waitq,
0801      * and run xenbus_probe() the moment it occurs.
0802      */
0803     prepare_to_wait(&xb_waitq, &w, TASK_INTERRUPTIBLE);
0804     schedule();
0805     finish_wait(&xb_waitq, &w);
0806 
0807     DPRINTK("probing");
0808     xenbus_probe();
0809     return 0;
0810 }
0811 
0812 static int __init xenbus_probe_initcall(void)
0813 {
0814     /*
0815      * Probe XenBus here in the XS_PV case, and also XS_HVM unless we
0816      * need to wait for the platform PCI device to come up or
0817      * xen_store_interface is not ready.
0818      */
0819     if (xen_store_domain_type == XS_PV ||
0820         (xen_store_domain_type == XS_HVM &&
0821          !xs_hvm_defer_init_for_callback() &&
0822          xen_store_interface != NULL))
0823         xenbus_probe();
0824 
0825     /*
0826      * For XS_LOCAL or when xen_store_interface is not ready, spawn a
0827      * thread which will wait for xenstored or a xenstore-stubdom to be
0828      * started, then probe.  It will be triggered when communication
0829      * starts happening, by waiting on xb_waitq.
0830      */
0831     if (xen_store_domain_type == XS_LOCAL || xen_store_interface == NULL) {
0832         struct task_struct *probe_task;
0833 
0834         probe_task = kthread_run(xenbus_probe_thread, NULL,
0835                      "xenbus_probe");
0836         if (IS_ERR(probe_task))
0837             return PTR_ERR(probe_task);
0838     }
0839     return 0;
0840 }
0841 device_initcall(xenbus_probe_initcall);
0842 
0843 int xen_set_callback_via(uint64_t via)
0844 {
0845     struct xen_hvm_param a;
0846     int ret;
0847 
0848     a.domid = DOMID_SELF;
0849     a.index = HVM_PARAM_CALLBACK_IRQ;
0850     a.value = via;
0851 
0852     ret = HYPERVISOR_hvm_op(HVMOP_set_param, &a);
0853     if (ret)
0854         return ret;
0855 
0856     /*
0857      * If xenbus_probe_initcall() deferred the xenbus_probe()
0858      * due to the callback not functioning yet, we can do it now.
0859      */
0860     if (!xenstored_ready && xs_hvm_defer_init_for_callback())
0861         xenbus_probe();
0862 
0863     return ret;
0864 }
0865 EXPORT_SYMBOL_GPL(xen_set_callback_via);
0866 
0867 /* Set up event channel for xenstored which is run as a local process
0868  * (this is normally used only in dom0)
0869  */
0870 static int __init xenstored_local_init(void)
0871 {
0872     int err = -ENOMEM;
0873     unsigned long page = 0;
0874     struct evtchn_alloc_unbound alloc_unbound;
0875 
0876     /* Allocate Xenstore page */
0877     page = get_zeroed_page(GFP_KERNEL);
0878     if (!page)
0879         goto out_err;
0880 
0881     xen_store_gfn = virt_to_gfn((void *)page);
0882 
0883     /* Next allocate a local port which xenstored can bind to */
0884     alloc_unbound.dom        = DOMID_SELF;
0885     alloc_unbound.remote_dom = DOMID_SELF;
0886 
0887     err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
0888                       &alloc_unbound);
0889     if (err == -ENOSYS)
0890         goto out_err;
0891 
0892     BUG_ON(err);
0893     xen_store_evtchn = alloc_unbound.port;
0894 
0895     return 0;
0896 
0897  out_err:
0898     if (page != 0)
0899         free_page(page);
0900     return err;
0901 }
0902 
0903 static int xenbus_resume_cb(struct notifier_block *nb,
0904                 unsigned long action, void *data)
0905 {
0906     int err = 0;
0907 
0908     if (xen_hvm_domain()) {
0909         uint64_t v = 0;
0910 
0911         err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
0912         if (!err && v)
0913             xen_store_evtchn = v;
0914         else
0915             pr_warn("Cannot update xenstore event channel: %d\n",
0916                 err);
0917     } else
0918         xen_store_evtchn = xen_start_info->store_evtchn;
0919 
0920     return err;
0921 }
0922 
0923 static struct notifier_block xenbus_resume_nb = {
0924     .notifier_call = xenbus_resume_cb,
0925 };
0926 
0927 static irqreturn_t xenbus_late_init(int irq, void *unused)
0928 {
0929     int err;
0930     uint64_t v = 0;
0931 
0932     err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
0933     if (err || !v || !~v)
0934         return IRQ_HANDLED;
0935     xen_store_gfn = (unsigned long)v;
0936 
0937     wake_up(&xb_waitq);
0938     return IRQ_HANDLED;
0939 }
0940 
0941 static int __init xenbus_init(void)
0942 {
0943     int err;
0944     uint64_t v = 0;
0945     bool wait = false;
0946     xen_store_domain_type = XS_UNKNOWN;
0947 
0948     if (!xen_domain())
0949         return -ENODEV;
0950 
0951     xenbus_ring_ops_init();
0952 
0953     if (xen_pv_domain())
0954         xen_store_domain_type = XS_PV;
0955     if (xen_hvm_domain())
0956         xen_store_domain_type = XS_HVM;
0957     if (xen_hvm_domain() && xen_initial_domain())
0958         xen_store_domain_type = XS_LOCAL;
0959     if (xen_pv_domain() && !xen_start_info->store_evtchn)
0960         xen_store_domain_type = XS_LOCAL;
0961     if (xen_pv_domain() && xen_start_info->store_evtchn)
0962         xenstored_ready = 1;
0963 
0964     switch (xen_store_domain_type) {
0965     case XS_LOCAL:
0966         err = xenstored_local_init();
0967         if (err)
0968             goto out_error;
0969         xen_store_interface = gfn_to_virt(xen_store_gfn);
0970         break;
0971     case XS_PV:
0972         xen_store_evtchn = xen_start_info->store_evtchn;
0973         xen_store_gfn = xen_start_info->store_mfn;
0974         xen_store_interface = gfn_to_virt(xen_store_gfn);
0975         break;
0976     case XS_HVM:
0977         err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
0978         if (err)
0979             goto out_error;
0980         xen_store_evtchn = (int)v;
0981         err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
0982         if (err)
0983             goto out_error;
0984         /*
0985          * Uninitialized hvm_params are zero and return no error.
0986          * Although it is theoretically possible to have
0987          * HVM_PARAM_STORE_PFN set to zero on purpose, in reality it is
0988          * not zero when valid. If zero, it means that Xenstore hasn't
0989          * been properly initialized. Instead of attempting to map a
0990          * wrong guest physical address return error.
0991          *
0992          * Also recognize all bits set as an invalid/uninitialized value.
0993          */
0994         if (!v) {
0995             err = -ENOENT;
0996             goto out_error;
0997         }
0998         if (v == ~0ULL) {
0999             wait = true;
1000         } else {
1001             /* Avoid truncation on 32-bit. */
1002 #if BITS_PER_LONG == 32
1003             if (v > ULONG_MAX) {
1004                 pr_err("%s: cannot handle HVM_PARAM_STORE_PFN=%llx > ULONG_MAX\n",
1005                        __func__, v);
1006                 err = -EINVAL;
1007                 goto out_error;
1008             }
1009 #endif
1010             xen_store_gfn = (unsigned long)v;
1011             xen_store_interface =
1012                 memremap(xen_store_gfn << XEN_PAGE_SHIFT,
1013                      XEN_PAGE_SIZE, MEMREMAP_WB);
1014             if (xen_store_interface->connection != XENSTORE_CONNECTED)
1015                 wait = true;
1016         }
1017         if (wait) {
1018             err = bind_evtchn_to_irqhandler(xen_store_evtchn,
1019                             xenbus_late_init,
1020                             0, "xenstore_late_init",
1021                             &xb_waitq);
1022             if (err < 0) {
1023                 pr_err("xenstore_late_init couldn't bind irq err=%d\n",
1024                        err);
1025                 return err;
1026             }
1027 
1028             xs_init_irq = err;
1029         }
1030         break;
1031     default:
1032         pr_warn("Xenstore state unknown\n");
1033         break;
1034     }
1035 
1036     /*
1037      * HVM domains may not have a functional callback yet. In that
1038      * case let xs_init() be called from xenbus_probe(), which will
1039      * get invoked at an appropriate time.
1040      */
1041     if (xen_store_domain_type != XS_HVM) {
1042         err = xs_init();
1043         if (err) {
1044             pr_warn("Error initializing xenstore comms: %i\n", err);
1045             goto out_error;
1046         }
1047     }
1048 
1049     if ((xen_store_domain_type != XS_LOCAL) &&
1050         (xen_store_domain_type != XS_UNKNOWN))
1051         xen_resume_notifier_register(&xenbus_resume_nb);
1052 
1053 #ifdef CONFIG_XEN_COMPAT_XENFS
1054     /*
1055      * Create xenfs mountpoint in /proc for compatibility with
1056      * utilities that expect to find "xenbus" under "/proc/xen".
1057      */
1058     proc_create_mount_point("xen");
1059 #endif
1060     return 0;
1061 
1062 out_error:
1063     xen_store_domain_type = XS_UNKNOWN;
1064     return err;
1065 }
1066 
1067 postcore_initcall(xenbus_init);
1068 
1069 MODULE_LICENSE("GPL");