2007-07-18 09:37:06 +08:00
|
|
|
/******************************************************************************
|
|
|
|
* Client-facing interface for the Xenbus driver. In other words, the
|
|
|
|
* interface between the Xenbus and the device-specific code, be it the
|
|
|
|
* frontend or the backend of that driver.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 XenSource Ltd
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation; or, when distributed
|
|
|
|
* separately from the Linux kernel or incorporated into other
|
|
|
|
* software packages, subject to the following license:
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this source file (the "Software"), to deal in the Software without
|
|
|
|
* restriction, including without limitation the rights to use, copy, modify,
|
|
|
|
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-03-01 21:14:59 +08:00
|
|
|
#include <linux/mm.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2007-07-18 09:37:06 +08:00
|
|
|
#include <linux/types.h>
|
2011-12-20 03:55:14 +08:00
|
|
|
#include <linux/spinlock.h>
|
2007-07-18 09:37:06 +08:00
|
|
|
#include <linux/vmalloc.h>
|
2011-07-11 01:22:07 +08:00
|
|
|
#include <linux/export.h>
|
2007-07-18 09:37:06 +08:00
|
|
|
#include <asm/xen/hypervisor.h>
|
2011-09-29 23:53:32 +08:00
|
|
|
#include <asm/xen/page.h>
|
2007-07-18 09:37:06 +08:00
|
|
|
#include <xen/interface/xen.h>
|
|
|
|
#include <xen/interface/event_channel.h>
|
2011-12-20 03:55:14 +08:00
|
|
|
#include <xen/balloon.h>
|
2007-07-18 09:37:06 +08:00
|
|
|
#include <xen/events.h>
|
|
|
|
#include <xen/grant_table.h>
|
|
|
|
#include <xen/xenbus.h>
|
2011-12-20 03:55:14 +08:00
|
|
|
#include <xen/xen.h>
|
2014-01-01 02:57:35 +08:00
|
|
|
#include <xen/features.h>
|
2011-12-20 03:55:14 +08:00
|
|
|
|
|
|
|
#include "xenbus_probe.h"
|
|
|
|
|
|
|
|
struct xenbus_map_node {
|
|
|
|
struct list_head next;
|
|
|
|
union {
|
|
|
|
struct vm_struct *area; /* PV */
|
|
|
|
struct page *page; /* HVM */
|
|
|
|
};
|
|
|
|
grant_handle_t handle;
|
|
|
|
};
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(xenbus_valloc_lock);
|
|
|
|
static LIST_HEAD(xenbus_valloc_pages);
|
|
|
|
|
|
|
|
struct xenbus_ring_ops {
|
|
|
|
int (*map)(struct xenbus_device *dev, int gnt, void **vaddr);
|
|
|
|
int (*unmap)(struct xenbus_device *dev, void *vaddr);
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct xenbus_ring_ops *ring_ops __read_mostly;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
const char *xenbus_strstate(enum xenbus_state state)
|
|
|
|
{
|
|
|
|
static const char *const name[] = {
|
|
|
|
[ XenbusStateUnknown ] = "Unknown",
|
|
|
|
[ XenbusStateInitialising ] = "Initialising",
|
|
|
|
[ XenbusStateInitWait ] = "InitWait",
|
|
|
|
[ XenbusStateInitialised ] = "Initialised",
|
|
|
|
[ XenbusStateConnected ] = "Connected",
|
|
|
|
[ XenbusStateClosing ] = "Closing",
|
|
|
|
[ XenbusStateClosed ] = "Closed",
|
2009-10-14 05:22:27 +08:00
|
|
|
[XenbusStateReconfiguring] = "Reconfiguring",
|
|
|
|
[XenbusStateReconfigured] = "Reconfigured",
|
2007-07-18 09:37:06 +08:00
|
|
|
};
|
|
|
|
return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_strstate);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* xenbus_watch_path - register a watch
|
|
|
|
* @dev: xenbus device
|
|
|
|
* @path: path to watch
|
|
|
|
* @watch: watch to register
|
|
|
|
* @callback: callback to register
|
|
|
|
*
|
|
|
|
* Register a @watch on the given path, using the given xenbus_watch structure
|
|
|
|
* for storage, and the given @callback function as the callback. Return 0 on
|
|
|
|
* success, or -errno on error. On success, the given @path will be saved as
|
|
|
|
* @watch->node, and remains the caller's to free. On error, @watch->node will
|
|
|
|
* be NULL, the device will switch to %XenbusStateClosing, and the error will
|
|
|
|
* be saved in the store.
|
|
|
|
*/
|
|
|
|
int xenbus_watch_path(struct xenbus_device *dev, const char *path,
|
|
|
|
struct xenbus_watch *watch,
|
|
|
|
void (*callback)(struct xenbus_watch *,
|
|
|
|
const char **, unsigned int))
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
watch->node = path;
|
|
|
|
watch->callback = callback;
|
|
|
|
|
|
|
|
err = register_xenbus_watch(watch);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
watch->node = NULL;
|
|
|
|
watch->callback = NULL;
|
|
|
|
xenbus_dev_fatal(dev, err, "adding watch on %s", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_watch_path);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* xenbus_watch_pathfmt - register a watch on a sprintf-formatted path
|
|
|
|
* @dev: xenbus device
|
|
|
|
* @watch: watch to register
|
|
|
|
* @callback: callback to register
|
|
|
|
* @pathfmt: format of path to watch
|
|
|
|
*
|
|
|
|
* Register a watch on the given @path, using the given xenbus_watch
|
|
|
|
* structure for storage, and the given @callback function as the callback.
|
|
|
|
* Return 0 on success, or -errno on error. On success, the watched path
|
|
|
|
* (@path/@path2) will be saved as @watch->node, and becomes the caller's to
|
|
|
|
* kfree(). On error, watch->node will be NULL, so the caller has nothing to
|
|
|
|
* free, the device will switch to %XenbusStateClosing, and the error will be
|
|
|
|
* saved in the store.
|
|
|
|
*/
|
|
|
|
int xenbus_watch_pathfmt(struct xenbus_device *dev,
|
|
|
|
struct xenbus_watch *watch,
|
|
|
|
void (*callback)(struct xenbus_watch *,
|
|
|
|
const char **, unsigned int),
|
|
|
|
const char *pathfmt, ...)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
va_list ap;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
va_start(ap, pathfmt);
|
2008-06-17 16:47:08 +08:00
|
|
|
path = kvasprintf(GFP_NOIO | __GFP_HIGH, pathfmt, ap);
|
2007-07-18 09:37:06 +08:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (!path) {
|
|
|
|
xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
err = xenbus_watch_path(dev, path, watch, callback);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
kfree(path);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt);
|
|
|
|
|
2010-05-01 06:01:15 +08:00
|
|
|
static void xenbus_switch_fatal(struct xenbus_device *, int, int,
|
|
|
|
const char *, ...);
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2010-05-01 06:01:15 +08:00
|
|
|
static int
|
|
|
|
__xenbus_switch_state(struct xenbus_device *dev,
|
|
|
|
enum xenbus_state state, int depth)
|
2007-07-18 09:37:06 +08:00
|
|
|
{
|
|
|
|
/* We check whether the state is currently set to the given value, and
|
|
|
|
if not, then the state is set. We don't want to unconditionally
|
|
|
|
write the given state, because we don't want to fire watches
|
|
|
|
unnecessarily. Furthermore, if the node has gone, we don't write
|
|
|
|
to it, as the device will be tearing down, and we don't want to
|
|
|
|
resurrect that directory.
|
|
|
|
|
2010-05-01 06:01:15 +08:00
|
|
|
Note that, because of this cached value of our state, this
|
|
|
|
function will not take a caller's Xenstore transaction
|
|
|
|
(something it was trying to in the past) because dev->state
|
|
|
|
would not get reset if the transaction was aborted.
|
2007-07-18 09:37:06 +08:00
|
|
|
*/
|
|
|
|
|
2010-05-01 06:01:15 +08:00
|
|
|
struct xenbus_transaction xbt;
|
2007-07-18 09:37:06 +08:00
|
|
|
int current_state;
|
2010-05-01 06:01:15 +08:00
|
|
|
int err, abort;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
if (state == dev->state)
|
|
|
|
return 0;
|
|
|
|
|
2010-05-01 06:01:15 +08:00
|
|
|
again:
|
|
|
|
abort = 1;
|
|
|
|
|
|
|
|
err = xenbus_transaction_start(&xbt);
|
|
|
|
if (err) {
|
|
|
|
xenbus_switch_fatal(dev, depth, err, "starting transaction");
|
2007-07-18 09:37:06 +08:00
|
|
|
return 0;
|
2010-05-01 06:01:15 +08:00
|
|
|
}
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2010-05-01 06:01:15 +08:00
|
|
|
err = xenbus_scanf(xbt, dev->nodename, "state", "%d", ¤t_state);
|
|
|
|
if (err != 1)
|
|
|
|
goto abort;
|
|
|
|
|
|
|
|
err = xenbus_printf(xbt, dev->nodename, "state", "%d", state);
|
2007-07-18 09:37:06 +08:00
|
|
|
if (err) {
|
2010-05-01 06:01:15 +08:00
|
|
|
xenbus_switch_fatal(dev, depth, err, "writing new state");
|
|
|
|
goto abort;
|
2007-07-18 09:37:06 +08:00
|
|
|
}
|
|
|
|
|
2010-05-01 06:01:15 +08:00
|
|
|
abort = 0;
|
|
|
|
abort:
|
|
|
|
err = xenbus_transaction_end(xbt, abort);
|
|
|
|
if (err) {
|
|
|
|
if (err == -EAGAIN && !abort)
|
|
|
|
goto again;
|
|
|
|
xenbus_switch_fatal(dev, depth, err, "ending transaction");
|
|
|
|
} else
|
|
|
|
dev->state = state;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-01 06:01:15 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* xenbus_switch_state
|
|
|
|
* @dev: xenbus device
|
|
|
|
* @state: new state
|
|
|
|
*
|
|
|
|
* Advertise in the store a change of the given driver to the given new_state.
|
|
|
|
* Return 0 on success, or -errno on error. On error, the device will switch
|
|
|
|
* to XenbusStateClosing, and the error will be saved in the store.
|
|
|
|
*/
|
|
|
|
int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
|
|
|
|
{
|
|
|
|
return __xenbus_switch_state(dev, state, 0);
|
|
|
|
}
|
|
|
|
|
2007-07-18 09:37:06 +08:00
|
|
|
EXPORT_SYMBOL_GPL(xenbus_switch_state);
|
|
|
|
|
|
|
|
int xenbus_frontend_closed(struct xenbus_device *dev)
|
|
|
|
{
|
|
|
|
xenbus_switch_state(dev, XenbusStateClosed);
|
|
|
|
complete(&dev->down);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_frontend_closed);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the path to the error node for the given device, or NULL on failure.
|
|
|
|
* If the value returned is non-NULL, then it is the caller's to kfree.
|
|
|
|
*/
|
|
|
|
static char *error_path(struct xenbus_device *dev)
|
|
|
|
{
|
|
|
|
return kasprintf(GFP_KERNEL, "error/%s", dev->nodename);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void xenbus_va_dev_error(struct xenbus_device *dev, int err,
|
|
|
|
const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned int len;
|
|
|
|
char *printf_buffer = NULL;
|
|
|
|
char *path_buffer = NULL;
|
|
|
|
|
|
|
|
#define PRINTF_BUFFER_SIZE 4096
|
|
|
|
printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
|
|
|
|
if (printf_buffer == NULL)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
len = sprintf(printf_buffer, "%i ", -err);
|
|
|
|
ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
|
|
|
|
|
|
|
|
BUG_ON(len + ret > PRINTF_BUFFER_SIZE-1);
|
|
|
|
|
|
|
|
dev_err(&dev->dev, "%s\n", printf_buffer);
|
|
|
|
|
|
|
|
path_buffer = error_path(dev);
|
|
|
|
|
|
|
|
if (path_buffer == NULL) {
|
|
|
|
dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
|
|
|
|
dev->nodename, printf_buffer);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
|
|
|
|
dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
|
|
|
|
dev->nodename, printf_buffer);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
kfree(printf_buffer);
|
|
|
|
kfree(path_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* xenbus_dev_error
|
|
|
|
* @dev: xenbus device
|
|
|
|
* @err: error to report
|
|
|
|
* @fmt: error message format
|
|
|
|
*
|
|
|
|
* Report the given negative errno into the store, along with the given
|
|
|
|
* formatted message.
|
|
|
|
*/
|
|
|
|
void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xenbus_va_dev_error(dev, err, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_dev_error);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* xenbus_dev_fatal
|
|
|
|
* @dev: xenbus device
|
|
|
|
* @err: error to report
|
|
|
|
* @fmt: error message format
|
|
|
|
*
|
|
|
|
* Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
|
2009-01-08 10:07:10 +08:00
|
|
|
* xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly
|
2007-07-18 09:37:06 +08:00
|
|
|
* closedown of this driver and its peer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xenbus_va_dev_error(dev, err, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
xenbus_switch_state(dev, XenbusStateClosing);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
|
|
|
|
|
2010-05-01 06:01:15 +08:00
|
|
|
/**
|
|
|
|
* Equivalent to xenbus_dev_fatal(dev, err, fmt, args), but helps
|
|
|
|
* avoiding recursion within xenbus_switch_state.
|
|
|
|
*/
|
|
|
|
static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xenbus_va_dev_error(dev, err, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (!depth)
|
|
|
|
__xenbus_switch_state(dev, XenbusStateClosing, 1);
|
|
|
|
}
|
|
|
|
|
2007-07-18 09:37:06 +08:00
|
|
|
/**
|
|
|
|
* xenbus_grant_ring
|
|
|
|
* @dev: xenbus device
|
|
|
|
* @ring_mfn: mfn of ring to grant
|
|
|
|
|
|
|
|
* Grant access to the given @ring_mfn to the peer of the given device. Return
|
|
|
|
* 0 on success, or -errno on error. On error, the device will switch to
|
|
|
|
* XenbusStateClosing, and the error will be saved in the store.
|
|
|
|
*/
|
|
|
|
int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn)
|
|
|
|
{
|
|
|
|
int err = gnttab_grant_foreign_access(dev->otherend_id, ring_mfn, 0);
|
|
|
|
if (err < 0)
|
|
|
|
xenbus_dev_fatal(dev, err, "granting access to ring page");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_grant_ring);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate an event channel for the given xenbus_device, assigning the newly
|
|
|
|
* created local port to *port. Return 0 on success, or -errno on error. On
|
|
|
|
* error, the device will switch to XenbusStateClosing, and the error will be
|
|
|
|
* saved in the store.
|
|
|
|
*/
|
|
|
|
int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
|
|
|
|
{
|
|
|
|
struct evtchn_alloc_unbound alloc_unbound;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
alloc_unbound.dom = DOMID_SELF;
|
|
|
|
alloc_unbound.remote_dom = dev->otherend_id;
|
|
|
|
|
|
|
|
err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
|
|
|
|
&alloc_unbound);
|
|
|
|
if (err)
|
|
|
|
xenbus_dev_fatal(dev, err, "allocating event channel");
|
|
|
|
else
|
|
|
|
*port = alloc_unbound.port;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free an existing event channel. Returns 0 on success or -errno on error.
|
|
|
|
*/
|
|
|
|
int xenbus_free_evtchn(struct xenbus_device *dev, int port)
|
|
|
|
{
|
|
|
|
struct evtchn_close close;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
close.port = port;
|
|
|
|
|
|
|
|
err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
|
|
|
|
if (err)
|
|
|
|
xenbus_dev_error(dev, err, "freeing event channel %d", port);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* xenbus_map_ring_valloc
|
|
|
|
* @dev: xenbus device
|
|
|
|
* @gnt_ref: grant reference
|
|
|
|
* @vaddr: pointer to address to be filled out by mapping
|
|
|
|
*
|
|
|
|
* Based on Rusty Russell's skeleton driver's map_page.
|
|
|
|
* Map a page of memory into this domain from another domain's grant table.
|
|
|
|
* xenbus_map_ring_valloc allocates a page of virtual address space, maps the
|
|
|
|
* page to that address, and sets *vaddr to that address.
|
|
|
|
* Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
|
|
|
|
* or -ENOMEM on error. If an error is returned, device will switch to
|
|
|
|
* XenbusStateClosing and the error message will be saved in XenStore.
|
|
|
|
*/
|
|
|
|
int xenbus_map_ring_valloc(struct xenbus_device *dev, int gnt_ref, void **vaddr)
|
2011-12-20 03:55:14 +08:00
|
|
|
{
|
|
|
|
return ring_ops->map(dev, gnt_ref, vaddr);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
|
|
|
|
|
|
|
|
static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev,
|
|
|
|
int gnt_ref, void **vaddr)
|
2007-07-18 09:37:06 +08:00
|
|
|
{
|
|
|
|
struct gnttab_map_grant_ref op = {
|
2011-09-29 23:53:32 +08:00
|
|
|
.flags = GNTMAP_host_map | GNTMAP_contains_pte,
|
2007-07-18 09:37:06 +08:00
|
|
|
.ref = gnt_ref,
|
|
|
|
.dom = dev->otherend_id,
|
|
|
|
};
|
2011-12-20 03:55:14 +08:00
|
|
|
struct xenbus_map_node *node;
|
2007-07-18 09:37:06 +08:00
|
|
|
struct vm_struct *area;
|
2011-09-29 23:53:32 +08:00
|
|
|
pte_t *pte;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
*vaddr = NULL;
|
|
|
|
|
2011-12-20 03:55:14 +08:00
|
|
|
node = kzalloc(sizeof(*node), GFP_KERNEL);
|
|
|
|
if (!node)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-09-29 23:53:32 +08:00
|
|
|
area = alloc_vm_area(PAGE_SIZE, &pte);
|
2011-12-20 03:55:14 +08:00
|
|
|
if (!area) {
|
|
|
|
kfree(node);
|
2007-07-18 09:37:06 +08:00
|
|
|
return -ENOMEM;
|
2011-12-20 03:55:14 +08:00
|
|
|
}
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2011-09-29 23:53:32 +08:00
|
|
|
op.host_addr = arbitrary_virt_to_machine(pte).maddr;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2012-09-14 22:26:59 +08:00
|
|
|
gnttab_batch_map(&op, 1);
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
if (op.status != GNTST_okay) {
|
2011-09-29 23:53:29 +08:00
|
|
|
free_vm_area(area);
|
2011-12-20 03:55:14 +08:00
|
|
|
kfree(node);
|
2007-07-18 09:37:06 +08:00
|
|
|
xenbus_dev_fatal(dev, op.status,
|
|
|
|
"mapping in shared page %d from domain %d",
|
|
|
|
gnt_ref, dev->otherend_id);
|
|
|
|
return op.status;
|
|
|
|
}
|
|
|
|
|
2011-12-20 03:55:14 +08:00
|
|
|
node->handle = op.handle;
|
|
|
|
node->area = area;
|
|
|
|
|
|
|
|
spin_lock(&xenbus_valloc_lock);
|
|
|
|
list_add(&node->next, &xenbus_valloc_pages);
|
|
|
|
spin_unlock(&xenbus_valloc_lock);
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
*vaddr = area->addr;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-12-20 03:55:14 +08:00
|
|
|
|
|
|
|
static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev,
|
|
|
|
int gnt_ref, void **vaddr)
|
|
|
|
{
|
|
|
|
struct xenbus_map_node *node;
|
|
|
|
int err;
|
|
|
|
void *addr;
|
|
|
|
|
|
|
|
*vaddr = NULL;
|
|
|
|
|
|
|
|
node = kzalloc(sizeof(*node), GFP_KERNEL);
|
|
|
|
if (!node)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
err = alloc_xenballooned_pages(1, &node->page, false /* lowmem */);
|
|
|
|
if (err)
|
|
|
|
goto out_err;
|
|
|
|
|
|
|
|
addr = pfn_to_kaddr(page_to_pfn(node->page));
|
|
|
|
|
|
|
|
err = xenbus_map_ring(dev, gnt_ref, &node->handle, addr);
|
|
|
|
if (err)
|
2013-05-30 00:02:58 +08:00
|
|
|
goto out_err_free_ballooned_pages;
|
2011-12-20 03:55:14 +08:00
|
|
|
|
|
|
|
spin_lock(&xenbus_valloc_lock);
|
|
|
|
list_add(&node->next, &xenbus_valloc_pages);
|
|
|
|
spin_unlock(&xenbus_valloc_lock);
|
|
|
|
|
|
|
|
*vaddr = addr;
|
|
|
|
return 0;
|
|
|
|
|
2013-05-30 00:02:58 +08:00
|
|
|
out_err_free_ballooned_pages:
|
2011-12-20 03:55:14 +08:00
|
|
|
free_xenballooned_pages(1, &node->page);
|
2013-05-30 00:02:58 +08:00
|
|
|
out_err:
|
2011-12-20 03:55:14 +08:00
|
|
|
kfree(node);
|
|
|
|
return err;
|
|
|
|
}
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* xenbus_map_ring
|
|
|
|
* @dev: xenbus device
|
|
|
|
* @gnt_ref: grant reference
|
|
|
|
* @handle: pointer to grant handle to be filled
|
|
|
|
* @vaddr: address to be mapped to
|
|
|
|
*
|
|
|
|
* Map a page of memory into this domain from another domain's grant table.
|
|
|
|
* xenbus_map_ring does not allocate the virtual address space (you must do
|
|
|
|
* this yourself!). It only maps in the page to the specified address.
|
|
|
|
* Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
|
|
|
|
* or -ENOMEM on error. If an error is returned, device will switch to
|
|
|
|
* XenbusStateClosing and the error message will be saved in XenStore.
|
|
|
|
*/
|
|
|
|
int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
|
|
|
|
grant_handle_t *handle, void *vaddr)
|
|
|
|
{
|
2011-12-15 04:12:10 +08:00
|
|
|
struct gnttab_map_grant_ref op;
|
|
|
|
|
2012-02-24 19:46:32 +08:00
|
|
|
gnttab_set_map_op(&op, (unsigned long)vaddr, GNTMAP_host_map, gnt_ref,
|
2011-12-15 04:12:10 +08:00
|
|
|
dev->otherend_id);
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2012-09-14 22:26:59 +08:00
|
|
|
gnttab_batch_map(&op, 1);
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
if (op.status != GNTST_okay) {
|
|
|
|
xenbus_dev_fatal(dev, op.status,
|
|
|
|
"mapping in shared page %d from domain %d",
|
|
|
|
gnt_ref, dev->otherend_id);
|
|
|
|
} else
|
|
|
|
*handle = op.handle;
|
|
|
|
|
|
|
|
return op.status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_map_ring);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* xenbus_unmap_ring_vfree
|
|
|
|
* @dev: xenbus device
|
|
|
|
* @vaddr: addr to unmap
|
|
|
|
*
|
|
|
|
* Based on Rusty Russell's skeleton driver's unmap_page.
|
|
|
|
* Unmap a page of memory in this domain that was imported from another domain.
|
|
|
|
* Use xenbus_unmap_ring_vfree if you mapped in your memory with
|
|
|
|
* xenbus_map_ring_valloc (it will free the virtual address space).
|
|
|
|
* Returns 0 on success and returns GNTST_* on error
|
|
|
|
* (see xen/include/interface/grant_table.h).
|
|
|
|
*/
|
|
|
|
int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
|
|
|
|
{
|
2011-12-20 03:55:14 +08:00
|
|
|
return ring_ops->unmap(dev, vaddr);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
|
|
|
|
|
|
|
|
static int xenbus_unmap_ring_vfree_pv(struct xenbus_device *dev, void *vaddr)
|
|
|
|
{
|
|
|
|
struct xenbus_map_node *node;
|
2007-07-18 09:37:06 +08:00
|
|
|
struct gnttab_unmap_grant_ref op = {
|
|
|
|
.host_addr = (unsigned long)vaddr,
|
|
|
|
};
|
2011-09-29 23:53:32 +08:00
|
|
|
unsigned int level;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2011-12-20 03:55:14 +08:00
|
|
|
spin_lock(&xenbus_valloc_lock);
|
|
|
|
list_for_each_entry(node, &xenbus_valloc_pages, next) {
|
|
|
|
if (node->area->addr == vaddr) {
|
|
|
|
list_del(&node->next);
|
|
|
|
goto found;
|
|
|
|
}
|
2007-07-18 09:37:06 +08:00
|
|
|
}
|
2011-12-20 03:55:14 +08:00
|
|
|
node = NULL;
|
|
|
|
found:
|
|
|
|
spin_unlock(&xenbus_valloc_lock);
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2011-12-20 03:55:14 +08:00
|
|
|
if (!node) {
|
2007-07-18 09:37:06 +08:00
|
|
|
xenbus_dev_error(dev, -ENOENT,
|
|
|
|
"can't find mapped virtual address %p", vaddr);
|
|
|
|
return GNTST_bad_virt_addr;
|
|
|
|
}
|
|
|
|
|
2011-12-20 03:55:14 +08:00
|
|
|
op.handle = node->handle;
|
2011-09-29 23:53:32 +08:00
|
|
|
op.host_addr = arbitrary_virt_to_machine(
|
|
|
|
lookup_address((unsigned long)vaddr, &level)).maddr;
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
|
|
|
|
BUG();
|
|
|
|
|
|
|
|
if (op.status == GNTST_okay)
|
2011-12-20 03:55:14 +08:00
|
|
|
free_vm_area(node->area);
|
2007-07-18 09:37:06 +08:00
|
|
|
else
|
|
|
|
xenbus_dev_error(dev, op.status,
|
|
|
|
"unmapping page at handle %d error %d",
|
2011-12-20 03:55:14 +08:00
|
|
|
node->handle, op.status);
|
2007-07-18 09:37:06 +08:00
|
|
|
|
2011-12-20 03:55:14 +08:00
|
|
|
kfree(node);
|
2007-07-18 09:37:06 +08:00
|
|
|
return op.status;
|
|
|
|
}
|
|
|
|
|
2011-12-20 03:55:14 +08:00
|
|
|
static int xenbus_unmap_ring_vfree_hvm(struct xenbus_device *dev, void *vaddr)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
struct xenbus_map_node *node;
|
|
|
|
void *addr;
|
|
|
|
|
|
|
|
spin_lock(&xenbus_valloc_lock);
|
|
|
|
list_for_each_entry(node, &xenbus_valloc_pages, next) {
|
|
|
|
addr = pfn_to_kaddr(page_to_pfn(node->page));
|
|
|
|
if (addr == vaddr) {
|
|
|
|
list_del(&node->next);
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
2012-02-24 19:46:32 +08:00
|
|
|
node = addr = NULL;
|
2011-12-20 03:55:14 +08:00
|
|
|
found:
|
|
|
|
spin_unlock(&xenbus_valloc_lock);
|
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
xenbus_dev_error(dev, -ENOENT,
|
|
|
|
"can't find mapped virtual address %p", vaddr);
|
|
|
|
return GNTST_bad_virt_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = xenbus_unmap_ring(dev, node->handle, addr);
|
|
|
|
|
|
|
|
if (!rv)
|
|
|
|
free_xenballooned_pages(1, &node->page);
|
|
|
|
else
|
|
|
|
WARN(1, "Leaking %p\n", vaddr);
|
|
|
|
|
|
|
|
kfree(node);
|
|
|
|
return rv;
|
|
|
|
}
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* xenbus_unmap_ring
|
|
|
|
* @dev: xenbus device
|
|
|
|
* @handle: grant handle
|
|
|
|
* @vaddr: addr to unmap
|
|
|
|
*
|
|
|
|
* Unmap a page of memory in this domain that was imported from another domain.
|
|
|
|
* Returns 0 on success and returns GNTST_* on error
|
|
|
|
* (see xen/include/interface/grant_table.h).
|
|
|
|
*/
|
|
|
|
int xenbus_unmap_ring(struct xenbus_device *dev,
|
|
|
|
grant_handle_t handle, void *vaddr)
|
|
|
|
{
|
2011-12-15 04:12:10 +08:00
|
|
|
struct gnttab_unmap_grant_ref op;
|
|
|
|
|
2012-02-24 19:46:32 +08:00
|
|
|
gnttab_set_unmap_op(&op, (unsigned long)vaddr, GNTMAP_host_map, handle);
|
2007-07-18 09:37:06 +08:00
|
|
|
|
|
|
|
if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
|
|
|
|
BUG();
|
|
|
|
|
|
|
|
if (op.status != GNTST_okay)
|
|
|
|
xenbus_dev_error(dev, op.status,
|
|
|
|
"unmapping page at handle %d error %d",
|
|
|
|
handle, op.status);
|
|
|
|
|
|
|
|
return op.status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_unmap_ring);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* xenbus_read_driver_state
|
|
|
|
* @path: path for driver
|
|
|
|
*
|
|
|
|
* Return the state of the driver rooted at the given store path, or
|
|
|
|
* XenbusStateUnknown if no state can be read.
|
|
|
|
*/
|
|
|
|
enum xenbus_state xenbus_read_driver_state(const char *path)
|
|
|
|
{
|
|
|
|
enum xenbus_state result;
|
|
|
|
int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
|
|
|
|
if (err)
|
|
|
|
result = XenbusStateUnknown;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(xenbus_read_driver_state);
|
2011-12-20 03:55:14 +08:00
|
|
|
|
|
|
|
static const struct xenbus_ring_ops ring_ops_pv = {
|
|
|
|
.map = xenbus_map_ring_valloc_pv,
|
|
|
|
.unmap = xenbus_unmap_ring_vfree_pv,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct xenbus_ring_ops ring_ops_hvm = {
|
|
|
|
.map = xenbus_map_ring_valloc_hvm,
|
|
|
|
.unmap = xenbus_unmap_ring_vfree_hvm,
|
|
|
|
};
|
|
|
|
|
|
|
|
void __init xenbus_ring_ops_init(void)
|
|
|
|
{
|
2014-01-01 02:57:35 +08:00
|
|
|
if (!xen_feature(XENFEAT_auto_translated_physmap))
|
2011-12-20 03:55:14 +08:00
|
|
|
ring_ops = &ring_ops_pv;
|
|
|
|
else
|
|
|
|
ring_ops = &ring_ops_hvm;
|
|
|
|
}
|