2019-05-19 12:08:20 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2013-06-28 10:21:41 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
|
|
|
#define DPRINTK(fmt, ...) \
|
|
|
|
pr_debug("(%s:%d) " fmt "\n", \
|
|
|
|
__func__, __LINE__, ##__VA_ARGS__)
|
2009-02-09 20:05:51 +00:00
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <linux/fcntl.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/notifier.h>
|
|
|
|
#include <linux/kthread.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/io.h>
|
2011-07-03 20:20:57 +00:00
|
|
|
#include <linux/module.h>
|
2009-02-09 20:05:51 +00:00
|
|
|
|
|
|
|
#include <asm/page.h>
|
|
|
|
#include <asm/xen/hypervisor.h>
|
|
|
|
#include <xen/xenbus.h>
|
|
|
|
#include <xen/events.h>
|
|
|
|
#include <xen/page.h>
|
2012-08-06 14:27:09 +00:00
|
|
|
#include <xen/xen.h>
|
2009-02-09 20:05:51 +00:00
|
|
|
|
|
|
|
#include <xen/platform_pci.h>
|
|
|
|
|
2017-02-09 13:39:56 +00:00
|
|
|
#include "xenbus.h"
|
2009-02-09 20:05:51 +00:00
|
|
|
|
|
|
|
|
2013-05-28 17:09:56 +00:00
|
|
|
|
2009-02-09 20:05:51 +00:00
|
|
|
/* device/<type>/<id> => <type>-<id> */
|
|
|
|
static int frontend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
|
|
|
|
{
|
|
|
|
nodename = strchr(nodename, '/');
|
|
|
|
if (!nodename || strlen(nodename + 1) >= XEN_BUS_ID_SIZE) {
|
2013-06-28 10:21:41 +00:00
|
|
|
pr_warn("bad frontend %s\n", nodename);
|
2009-02-09 20:05:51 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-08-18 21:01:22 +00:00
|
|
|
strscpy(bus_id, nodename + 1, XEN_BUS_ID_SIZE);
|
2009-02-09 20:05:51 +00:00
|
|
|
if (!strchr(bus_id, '/')) {
|
2013-06-28 10:21:41 +00:00
|
|
|
pr_warn("bus_id %s no slash\n", bus_id);
|
2009-02-09 20:05:51 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
*strchr(bus_id, '/') = '-';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* device/<typename>/<name> */
|
2010-12-10 14:39:15 +00:00
|
|
|
static int xenbus_probe_frontend(struct xen_bus_type *bus, const char *type,
|
|
|
|
const char *name)
|
2009-02-09 20:05:51 +00:00
|
|
|
{
|
|
|
|
char *nodename;
|
|
|
|
int err;
|
|
|
|
|
2012-03-13 18:30:44 +00:00
|
|
|
/* ignore console/0 */
|
|
|
|
if (!strncmp(type, "console", 7) && !strncmp(name, "0", 1)) {
|
|
|
|
DPRINTK("Ignoring buggy device entry console/0");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-09 20:05:51 +00:00
|
|
|
nodename = kasprintf(GFP_KERNEL, "%s/%s/%s", bus->root, type, name);
|
|
|
|
if (!nodename)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
DPRINTK("%s", nodename);
|
|
|
|
|
|
|
|
err = xenbus_probe_node(bus, type, nodename);
|
|
|
|
kfree(nodename);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-01-11 11:30:17 +00:00
|
|
|
static int xenbus_uevent_frontend(const struct device *_dev,
|
2010-12-10 14:39:15 +00:00
|
|
|
struct kobj_uevent_env *env)
|
2009-02-09 20:05:51 +00:00
|
|
|
{
|
2023-01-11 11:30:17 +00:00
|
|
|
const struct xenbus_device *dev = to_xenbus_device(_dev);
|
2009-02-09 20:05:51 +00:00
|
|
|
|
|
|
|
if (add_uevent_var(env, "MODALIAS=xen:%s", dev->devicetype))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-09 20:05:51 +00:00
|
|
|
static void backend_changed(struct xenbus_watch *watch,
|
2017-02-09 13:39:57 +00:00
|
|
|
const char *path, const char *token)
|
2009-02-09 20:05:51 +00:00
|
|
|
{
|
2017-02-09 13:39:57 +00:00
|
|
|
xenbus_otherend_changed(watch, path, token, 1);
|
2009-02-09 20:05:51 +00:00
|
|
|
}
|
|
|
|
|
2013-05-28 17:09:56 +00:00
|
|
|
static void xenbus_frontend_delayed_resume(struct work_struct *w)
|
|
|
|
{
|
|
|
|
struct xenbus_device *xdev = container_of(w, struct xenbus_device, work);
|
|
|
|
|
|
|
|
xenbus_dev_resume(&xdev->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xenbus_frontend_dev_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If xenstored is running in this domain, we cannot access the backend
|
|
|
|
* state at the moment, so we need to defer xenbus_dev_resume
|
|
|
|
*/
|
|
|
|
if (xen_store_domain_type == XS_LOCAL) {
|
|
|
|
struct xenbus_device *xdev = to_xenbus_device(dev);
|
|
|
|
|
2016-05-31 16:56:30 +00:00
|
|
|
schedule_work(&xdev->work);
|
2013-05-28 17:09:56 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return xenbus_dev_resume(dev);
|
|
|
|
}
|
|
|
|
|
2013-07-09 13:29:35 +00:00
|
|
|
static int xenbus_frontend_dev_probe(struct device *dev)
|
|
|
|
{
|
|
|
|
if (xen_store_domain_type == XS_LOCAL) {
|
|
|
|
struct xenbus_device *xdev = to_xenbus_device(dev);
|
|
|
|
INIT_WORK(&xdev->work, xenbus_frontend_delayed_resume);
|
|
|
|
}
|
|
|
|
|
|
|
|
return xenbus_dev_probe(dev);
|
|
|
|
}
|
|
|
|
|
2019-12-11 15:29:53 +00:00
|
|
|
static void xenbus_frontend_dev_shutdown(struct device *_dev)
|
|
|
|
{
|
|
|
|
struct xenbus_device *dev = to_xenbus_device(_dev);
|
|
|
|
unsigned long timeout = 5*HZ;
|
|
|
|
|
|
|
|
DPRINTK("%s", dev->nodename);
|
|
|
|
|
|
|
|
get_device(&dev->dev);
|
|
|
|
if (dev->state != XenbusStateConnected) {
|
|
|
|
pr_info("%s: %s: %s != Connected, skipping\n",
|
|
|
|
__func__, dev->nodename, xenbus_strstate(dev->state));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
xenbus_switch_state(dev, XenbusStateClosing);
|
|
|
|
timeout = wait_for_completion_timeout(&dev->down, timeout);
|
|
|
|
if (!timeout)
|
|
|
|
pr_info("%s: %s timeout closing device\n",
|
|
|
|
__func__, dev->nodename);
|
|
|
|
out:
|
|
|
|
put_device(&dev->dev);
|
|
|
|
}
|
|
|
|
|
2011-02-18 22:43:07 +00:00
|
|
|
static const struct dev_pm_ops xenbus_pm_ops = {
|
2011-02-22 22:59:06 +00:00
|
|
|
.suspend = xenbus_dev_suspend,
|
2013-05-28 17:09:56 +00:00
|
|
|
.resume = xenbus_frontend_dev_resume,
|
2011-02-22 22:59:06 +00:00
|
|
|
.freeze = xenbus_dev_suspend,
|
|
|
|
.thaw = xenbus_dev_cancel,
|
|
|
|
.restore = xenbus_dev_resume,
|
2011-02-18 22:43:07 +00:00
|
|
|
};
|
|
|
|
|
2009-02-09 20:05:51 +00:00
|
|
|
static struct xen_bus_type xenbus_frontend = {
|
|
|
|
.root = "device",
|
2010-12-10 14:39:15 +00:00
|
|
|
.levels = 2, /* device/type/<id> */
|
2009-02-09 20:05:51 +00:00
|
|
|
.get_bus_id = frontend_bus_id,
|
|
|
|
.probe = xenbus_probe_frontend,
|
|
|
|
.otherend_changed = backend_changed,
|
|
|
|
.bus = {
|
2010-12-10 14:39:15 +00:00
|
|
|
.name = "xen",
|
|
|
|
.match = xenbus_match,
|
|
|
|
.uevent = xenbus_uevent_frontend,
|
2013-07-09 13:29:35 +00:00
|
|
|
.probe = xenbus_frontend_dev_probe,
|
2010-12-10 14:39:15 +00:00
|
|
|
.remove = xenbus_dev_remove,
|
2019-12-11 15:29:53 +00:00
|
|
|
.shutdown = xenbus_frontend_dev_shutdown,
|
2013-10-07 06:55:49 +00:00
|
|
|
.dev_groups = xenbus_dev_groups,
|
2010-12-10 14:39:15 +00:00
|
|
|
|
2011-02-18 22:43:07 +00:00
|
|
|
.pm = &xenbus_pm_ops,
|
2009-02-09 20:05:51 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void frontend_changed(struct xenbus_watch *watch,
|
2017-02-09 13:39:57 +00:00
|
|
|
const char *path, const char *token)
|
2009-02-09 20:05:51 +00:00
|
|
|
{
|
|
|
|
DPRINTK("");
|
|
|
|
|
2017-02-09 13:39:57 +00:00
|
|
|
xenbus_dev_changed(path, &xenbus_frontend);
|
2009-02-09 20:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* We watch for devices appearing and vanishing. */
|
|
|
|
static struct xenbus_watch fe_watch = {
|
|
|
|
.node = "device",
|
|
|
|
.callback = frontend_changed,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int read_backend_details(struct xenbus_device *xendev)
|
|
|
|
{
|
|
|
|
return xenbus_read_otherend_details(xendev, "backend-id", "backend");
|
|
|
|
}
|
|
|
|
|
2012-04-18 02:21:38 +00:00
|
|
|
static int is_device_connecting(struct device *dev, void *data, bool ignore_nonessential)
|
2009-02-09 20:05:51 +00:00
|
|
|
{
|
|
|
|
struct xenbus_device *xendev = to_xenbus_device(dev);
|
|
|
|
struct device_driver *drv = data;
|
|
|
|
struct xenbus_driver *xendrv;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A device with no driver will never connect. We care only about
|
|
|
|
* devices which should currently be in the process of connecting.
|
|
|
|
*/
|
|
|
|
if (!dev->driver)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Is this search limited to a particular driver? */
|
|
|
|
if (drv && (dev->driver != drv))
|
|
|
|
return 0;
|
|
|
|
|
2021-10-22 06:47:56 +00:00
|
|
|
xendrv = to_xenbus_driver(dev->driver);
|
2012-04-18 02:21:38 +00:00
|
|
|
|
2021-10-22 06:47:56 +00:00
|
|
|
if (ignore_nonessential && xendrv->not_essential)
|
|
|
|
return 0;
|
2012-04-18 02:21:38 +00:00
|
|
|
|
2009-02-09 20:05:51 +00:00
|
|
|
return (xendev->state < XenbusStateConnected ||
|
|
|
|
(xendev->state == XenbusStateConnected &&
|
|
|
|
xendrv->is_ready && !xendrv->is_ready(xendev)));
|
|
|
|
}
|
2012-04-18 02:21:38 +00:00
|
|
|
static int essential_device_connecting(struct device *dev, void *data)
|
|
|
|
{
|
|
|
|
return is_device_connecting(dev, data, true /* ignore PV[KBB+FB] */);
|
|
|
|
}
|
|
|
|
static int non_essential_device_connecting(struct device *dev, void *data)
|
|
|
|
{
|
|
|
|
return is_device_connecting(dev, data, false);
|
|
|
|
}
|
2009-02-09 20:05:51 +00:00
|
|
|
|
2012-04-18 02:21:38 +00:00
|
|
|
static int exists_essential_connecting_device(struct device_driver *drv)
|
2009-02-09 20:05:51 +00:00
|
|
|
{
|
|
|
|
return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
|
2012-04-18 02:21:38 +00:00
|
|
|
essential_device_connecting);
|
|
|
|
}
|
|
|
|
static int exists_non_essential_connecting_device(struct device_driver *drv)
|
|
|
|
{
|
|
|
|
return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
|
|
|
|
non_essential_device_connecting);
|
2009-02-09 20:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int print_device_status(struct device *dev, void *data)
|
|
|
|
{
|
|
|
|
struct xenbus_device *xendev = to_xenbus_device(dev);
|
|
|
|
struct device_driver *drv = data;
|
|
|
|
|
|
|
|
/* Is this operation limited to a particular driver? */
|
|
|
|
if (drv && (dev->driver != drv))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!dev->driver) {
|
|
|
|
/* Information only: is this too noisy? */
|
2013-06-28 10:21:41 +00:00
|
|
|
pr_info("Device with no driver: %s\n", xendev->nodename);
|
2009-02-09 20:05:51 +00:00
|
|
|
} else if (xendev->state < XenbusStateConnected) {
|
|
|
|
enum xenbus_state rstate = XenbusStateUnknown;
|
|
|
|
if (xendev->otherend)
|
|
|
|
rstate = xenbus_read_driver_state(xendev->otherend);
|
2013-06-28 10:21:41 +00:00
|
|
|
pr_warn("Timeout connecting to device: %s (local state %d, remote state %d)\n",
|
|
|
|
xendev->nodename, xendev->state, rstate);
|
2009-02-09 20:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We only wait for device setup after most initcalls have run. */
|
|
|
|
static int ready_to_wait_for_devices;
|
|
|
|
|
2012-04-18 02:21:38 +00:00
|
|
|
static bool wait_loop(unsigned long start, unsigned int max_delay,
|
|
|
|
unsigned int *seconds_waited)
|
|
|
|
{
|
|
|
|
if (time_after(jiffies, start + (*seconds_waited+5)*HZ)) {
|
|
|
|
if (!*seconds_waited)
|
2013-06-28 10:21:41 +00:00
|
|
|
pr_warn("Waiting for devices to initialise: ");
|
2012-04-18 02:21:38 +00:00
|
|
|
*seconds_waited += 5;
|
2013-06-28 10:21:41 +00:00
|
|
|
pr_cont("%us...", max_delay - *seconds_waited);
|
|
|
|
if (*seconds_waited == max_delay) {
|
|
|
|
pr_cont("\n");
|
2012-04-18 02:21:38 +00:00
|
|
|
return true;
|
2013-06-28 10:21:41 +00:00
|
|
|
}
|
2012-04-18 02:21:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
schedule_timeout_interruptible(HZ/10);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-09 20:05:51 +00:00
|
|
|
/*
|
|
|
|
* On a 5-minute timeout, wait for all devices currently configured. We need
|
|
|
|
* to do this to guarantee that the filesystems and / or network devices
|
|
|
|
* needed for boot are available, before we can allow the boot to proceed.
|
|
|
|
*
|
|
|
|
* This needs to be on a late_initcall, to happen after the frontend device
|
|
|
|
* drivers have been initialised, but before the root fs is mounted.
|
|
|
|
*
|
|
|
|
* A possible improvement here would be to have the tools add a per-device
|
|
|
|
* flag to the store entry, indicating whether it is needed at boot time.
|
|
|
|
* This would allow people who knew what they were doing to accelerate their
|
|
|
|
* boot slightly, but of course needs tools or manual intervention to set up
|
|
|
|
* those flags correctly.
|
|
|
|
*/
|
|
|
|
static void wait_for_devices(struct xenbus_driver *xendrv)
|
|
|
|
{
|
|
|
|
unsigned long start = jiffies;
|
|
|
|
struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
|
|
|
|
unsigned int seconds_waited = 0;
|
|
|
|
|
|
|
|
if (!ready_to_wait_for_devices || !xen_domain())
|
|
|
|
return;
|
|
|
|
|
2012-04-18 02:21:38 +00:00
|
|
|
while (exists_non_essential_connecting_device(drv))
|
|
|
|
if (wait_loop(start, 30, &seconds_waited))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Skips PVKB and PVFB check.*/
|
|
|
|
while (exists_essential_connecting_device(drv))
|
|
|
|
if (wait_loop(start, 270, &seconds_waited))
|
|
|
|
break;
|
2009-02-09 20:05:51 +00:00
|
|
|
|
|
|
|
if (seconds_waited)
|
|
|
|
printk("\n");
|
|
|
|
|
|
|
|
bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
|
|
|
|
print_device_status);
|
|
|
|
}
|
|
|
|
|
2014-09-08 16:30:41 +00:00
|
|
|
int __xenbus_register_frontend(struct xenbus_driver *drv, struct module *owner,
|
|
|
|
const char *mod_name)
|
2009-02-09 20:05:51 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
drv->read_otherend_details = read_backend_details;
|
|
|
|
|
2014-09-08 16:30:41 +00:00
|
|
|
ret = xenbus_register_driver_common(drv, &xenbus_frontend,
|
|
|
|
owner, mod_name);
|
2009-02-09 20:05:51 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* If this driver is loaded as a module wait for devices to attach. */
|
|
|
|
wait_for_devices(drv);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-09-08 16:30:41 +00:00
|
|
|
EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
|
2009-02-09 20:05:51 +00:00
|
|
|
|
2011-08-25 16:34:45 +00:00
|
|
|
static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
|
|
|
|
static int backend_state;
|
|
|
|
|
|
|
|
static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
|
2017-02-09 13:39:57 +00:00
|
|
|
const char *path, const char *token)
|
2011-08-25 16:34:45 +00:00
|
|
|
{
|
2017-02-09 13:39:57 +00:00
|
|
|
if (xenbus_scanf(XBT_NIL, path, "", "%i",
|
2016-10-24 15:05:18 +00:00
|
|
|
&backend_state) != 1)
|
|
|
|
backend_state = XenbusStateUnknown;
|
2011-08-25 16:34:45 +00:00
|
|
|
printk(KERN_DEBUG "XENBUS: backend %s %s\n",
|
2017-02-09 13:39:57 +00:00
|
|
|
path, xenbus_strstate(backend_state));
|
2011-08-25 16:34:45 +00:00
|
|
|
wake_up(&backend_state_wq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xenbus_reset_wait_for_backend(char *be, int expected)
|
|
|
|
{
|
|
|
|
long timeout;
|
|
|
|
timeout = wait_event_interruptible_timeout(backend_state_wq,
|
|
|
|
backend_state == expected, 5 * HZ);
|
|
|
|
if (timeout <= 0)
|
2013-06-28 10:21:41 +00:00
|
|
|
pr_info("backend %s timed out\n", be);
|
2011-08-25 16:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset frontend if it is in Connected or Closed state.
|
|
|
|
* Wait for backend to catch up.
|
|
|
|
* State Connected happens during kdump, Closed after kexec.
|
|
|
|
*/
|
|
|
|
static void xenbus_reset_frontend(char *fe, char *be, int be_state)
|
|
|
|
{
|
|
|
|
struct xenbus_watch be_watch;
|
|
|
|
|
|
|
|
printk(KERN_DEBUG "XENBUS: backend %s %s\n",
|
|
|
|
be, xenbus_strstate(be_state));
|
|
|
|
|
|
|
|
memset(&be_watch, 0, sizeof(be_watch));
|
|
|
|
be_watch.node = kasprintf(GFP_NOIO | __GFP_HIGH, "%s/state", be);
|
|
|
|
if (!be_watch.node)
|
|
|
|
return;
|
|
|
|
|
|
|
|
be_watch.callback = xenbus_reset_backend_state_changed;
|
|
|
|
backend_state = XenbusStateUnknown;
|
|
|
|
|
2013-06-28 10:21:41 +00:00
|
|
|
pr_info("triggering reconnect on %s\n", be);
|
2011-08-25 16:34:45 +00:00
|
|
|
register_xenbus_watch(&be_watch);
|
|
|
|
|
|
|
|
/* fall through to forward backend to state XenbusStateInitialising */
|
|
|
|
switch (be_state) {
|
|
|
|
case XenbusStateConnected:
|
|
|
|
xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosing);
|
|
|
|
xenbus_reset_wait_for_backend(be, XenbusStateClosing);
|
2020-08-23 22:36:59 +00:00
|
|
|
fallthrough;
|
2011-08-25 16:34:45 +00:00
|
|
|
|
|
|
|
case XenbusStateClosing:
|
|
|
|
xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateClosed);
|
|
|
|
xenbus_reset_wait_for_backend(be, XenbusStateClosed);
|
2020-08-23 22:36:59 +00:00
|
|
|
fallthrough;
|
2011-08-25 16:34:45 +00:00
|
|
|
|
|
|
|
case XenbusStateClosed:
|
|
|
|
xenbus_printf(XBT_NIL, fe, "state", "%d", XenbusStateInitialising);
|
|
|
|
xenbus_reset_wait_for_backend(be, XenbusStateInitWait);
|
|
|
|
}
|
|
|
|
|
|
|
|
unregister_xenbus_watch(&be_watch);
|
2013-06-28 10:21:41 +00:00
|
|
|
pr_info("reconnect done on %s\n", be);
|
2011-08-25 16:34:45 +00:00
|
|
|
kfree(be_watch.node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xenbus_check_frontend(char *class, char *dev)
|
|
|
|
{
|
|
|
|
int be_state, fe_state, err;
|
|
|
|
char *backend, *frontend;
|
|
|
|
|
|
|
|
frontend = kasprintf(GFP_NOIO | __GFP_HIGH, "device/%s/%s", class, dev);
|
|
|
|
if (!frontend)
|
|
|
|
return;
|
|
|
|
|
|
|
|
err = xenbus_scanf(XBT_NIL, frontend, "state", "%i", &fe_state);
|
|
|
|
if (err != 1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
switch (fe_state) {
|
|
|
|
case XenbusStateConnected:
|
|
|
|
case XenbusStateClosed:
|
|
|
|
printk(KERN_DEBUG "XENBUS: frontend %s %s\n",
|
|
|
|
frontend, xenbus_strstate(fe_state));
|
|
|
|
backend = xenbus_read(XBT_NIL, frontend, "backend", NULL);
|
|
|
|
if (!backend || IS_ERR(backend))
|
|
|
|
goto out;
|
|
|
|
err = xenbus_scanf(XBT_NIL, backend, "state", "%i", &be_state);
|
|
|
|
if (err == 1)
|
|
|
|
xenbus_reset_frontend(frontend, backend, be_state);
|
|
|
|
kfree(backend);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
kfree(frontend);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xenbus_reset_state(void)
|
|
|
|
{
|
|
|
|
char **devclass, **dev;
|
|
|
|
int devclass_n, dev_n;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
devclass = xenbus_directory(XBT_NIL, "device", "", &devclass_n);
|
|
|
|
if (IS_ERR(devclass))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < devclass_n; i++) {
|
|
|
|
dev = xenbus_directory(XBT_NIL, "device", devclass[i], &dev_n);
|
|
|
|
if (IS_ERR(dev))
|
|
|
|
continue;
|
|
|
|
for (j = 0; j < dev_n; j++)
|
|
|
|
xenbus_check_frontend(devclass[i], dev[j]);
|
|
|
|
kfree(dev);
|
|
|
|
}
|
|
|
|
kfree(devclass);
|
|
|
|
}
|
|
|
|
|
2009-02-09 20:05:51 +00:00
|
|
|
static int frontend_probe_and_watch(struct notifier_block *notifier,
|
|
|
|
unsigned long event,
|
|
|
|
void *data)
|
|
|
|
{
|
2011-08-25 16:34:45 +00:00
|
|
|
/* reset devices in Connected or Closed state */
|
|
|
|
if (xen_hvm_domain())
|
|
|
|
xenbus_reset_state();
|
2009-02-09 20:05:51 +00:00
|
|
|
/* Enumerate devices in xenstore and watch for changes. */
|
|
|
|
xenbus_probe_devices(&xenbus_frontend);
|
|
|
|
register_xenbus_watch(&fe_watch);
|
2010-03-29 21:38:54 +00:00
|
|
|
|
2009-02-09 20:05:51 +00:00
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-09 20:05:51 +00:00
|
|
|
static int __init xenbus_probe_frontend_init(void)
|
|
|
|
{
|
2009-02-09 20:05:51 +00:00
|
|
|
static struct notifier_block xenstore_notifier = {
|
|
|
|
.notifier_call = frontend_probe_and_watch
|
|
|
|
};
|
2009-02-09 20:05:51 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
DPRINTK("");
|
|
|
|
|
|
|
|
/* Register ourselves with the kernel bus subsystem */
|
|
|
|
err = bus_register(&xenbus_frontend.bus);
|
2010-03-29 21:38:54 +00:00
|
|
|
if (err)
|
2009-02-09 20:05:51 +00:00
|
|
|
return err;
|
|
|
|
|
2009-02-09 20:05:51 +00:00
|
|
|
register_xenstore_notifier(&xenstore_notifier);
|
2009-02-09 20:05:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-03-05 06:31:45 +00:00
|
|
|
subsys_initcall(xenbus_probe_frontend_init);
|
2009-02-09 20:05:51 +00:00
|
|
|
|
|
|
|
#ifndef MODULE
|
|
|
|
static int __init boot_wait_for_devices(void)
|
|
|
|
{
|
xen/pvhvm: If xen_platform_pci=0 is set don't blow up (v4).
The user has the option of disabling the platform driver:
00:02.0 Unassigned class [ff80]: XenSource, Inc. Xen Platform Device (rev 01)
which is used to unplug the emulated drivers (IDE, Realtek 8169, etc)
and allow the PV drivers to take over. If the user wishes
to disable that they can set:
xen_platform_pci=0
(in the guest config file)
or
xen_emul_unplug=never
(on the Linux command line)
except it does not work properly. The PV drivers still try to
load and since the Xen platform driver is not run - and it
has not initialized the grant tables, most of the PV drivers
stumble upon:
input: Xen Virtual Keyboard as /devices/virtual/input/input5
input: Xen Virtual Pointer as /devices/virtual/input/input6M
------------[ cut here ]------------
kernel BUG at /home/konrad/ssd/konrad/linux/drivers/xen/grant-table.c:1206!
invalid opcode: 0000 [#1] SMP
Modules linked in: xen_kbdfront(+) xenfs xen_privcmd
CPU: 6 PID: 1389 Comm: modprobe Not tainted 3.13.0-rc1upstream-00021-ga6c892b-dirty #1
Hardware name: Xen HVM domU, BIOS 4.4-unstable 11/26/2013
RIP: 0010:[<ffffffff813ddc40>] [<ffffffff813ddc40>] get_free_entries+0x2e0/0x300
Call Trace:
[<ffffffff8150d9a3>] ? evdev_connect+0x1e3/0x240
[<ffffffff813ddd0e>] gnttab_grant_foreign_access+0x2e/0x70
[<ffffffffa0010081>] xenkbd_connect_backend+0x41/0x290 [xen_kbdfront]
[<ffffffffa0010a12>] xenkbd_probe+0x2f2/0x324 [xen_kbdfront]
[<ffffffff813e5757>] xenbus_dev_probe+0x77/0x130
[<ffffffff813e7217>] xenbus_frontend_dev_probe+0x47/0x50
[<ffffffff8145e9a9>] driver_probe_device+0x89/0x230
[<ffffffff8145ebeb>] __driver_attach+0x9b/0xa0
[<ffffffff8145eb50>] ? driver_probe_device+0x230/0x230
[<ffffffff8145eb50>] ? driver_probe_device+0x230/0x230
[<ffffffff8145cf1c>] bus_for_each_dev+0x8c/0xb0
[<ffffffff8145e7d9>] driver_attach+0x19/0x20
[<ffffffff8145e260>] bus_add_driver+0x1a0/0x220
[<ffffffff8145f1ff>] driver_register+0x5f/0xf0
[<ffffffff813e55c5>] xenbus_register_driver_common+0x15/0x20
[<ffffffff813e76b3>] xenbus_register_frontend+0x23/0x40
[<ffffffffa0015000>] ? 0xffffffffa0014fff
[<ffffffffa001502b>] xenkbd_init+0x2b/0x1000 [xen_kbdfront]
[<ffffffff81002049>] do_one_initcall+0x49/0x170
.. snip..
which is hardly nice. This patch fixes this by having each
PV driver check for:
- if running in PV, then it is fine to execute (as that is their
native environment).
- if running in HVM, check if user wanted 'xen_emul_unplug=never',
in which case bail out and don't load any PV drivers.
- if running in HVM, and if PCI device 5853:0001 (xen_platform_pci)
does not exist, then bail out and not load PV drivers.
- (v2) if running in HVM, and if the user wanted 'xen_emul_unplug=ide-disks',
then bail out for all PV devices _except_ the block one.
Ditto for the network one ('nics').
- (v2) if running in HVM, and if the user wanted 'xen_emul_unplug=unnecessary'
then load block PV driver, and also setup the legacy IDE paths.
In (v3) make it actually load PV drivers.
Reported-by: Sander Eikelenboom <linux@eikelenboom.it
Reported-by: Anthony PERARD <anthony.perard@citrix.com>
Reported-and-Tested-by: Fabio Fantoni <fabio.fantoni@m2r.biz>
Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
[v2: Add extra logic to handle the myrid ways 'xen_emul_unplug'
can be used per Ian and Stefano suggestion]
[v3: Make the unnecessary case work properly]
[v4: s/disks/ide-disks/ spotted by Fabio]
Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Acked-by: Bjorn Helgaas <bhelgaas@google.com> [for PCI parts]
CC: stable@vger.kernel.org
2013-11-26 20:05:40 +00:00
|
|
|
if (!xen_has_pv_devices())
|
2009-02-09 20:05:51 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
ready_to_wait_for_devices = 1;
|
|
|
|
wait_for_devices(NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
late_initcall(boot_wait_for_devices);
|
|
|
|
#endif
|
2009-03-27 23:29:44 +00:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|