2007-03-15 01:26:02 +03:00
|
|
|
/* $NetBSD: xenbus_client.c,v 1.6 2007/03/14 22:26:02 dogcow Exp $ */
|
2006-03-06 23:16:33 +03: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 file may be 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.
|
|
|
|
*/
|
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
#include <sys/cdefs.h>
|
2007-03-15 01:26:02 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: xenbus_client.c,v 1.6 2007/03/14 22:26:02 dogcow Exp $");
|
2006-03-06 23:16:33 +03:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
#define DPRINTK(fmt, args...) \
|
|
|
|
printk("xenbus_client (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args)
|
|
|
|
#else
|
|
|
|
#define DPRINTK(fmt, args...) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/null.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/systm.h>
|
2006-03-06 23:16:33 +03:00
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
#include <machine/stdarg.h>
|
2006-03-06 23:16:33 +03:00
|
|
|
|
2007-03-15 01:26:02 +03:00
|
|
|
#include <machine/xen.h>
|
|
|
|
#include <machine/hypervisor.h>
|
2006-03-06 23:21:35 +03:00
|
|
|
#include <machine/evtchn.h>
|
|
|
|
#include <machine/xenbus.h>
|
|
|
|
#include <machine/granttables.h>
|
2006-03-06 23:16:33 +03:00
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
xenbus_watch_path(struct xenbus_device *dev, char *path,
|
2006-03-06 23:16:33 +03:00
|
|
|
struct xenbus_watch *watch,
|
|
|
|
void (*callback)(struct xenbus_watch *,
|
|
|
|
const char **, unsigned int))
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
watch->node = path;
|
2006-03-16 01:20:06 +03:00
|
|
|
watch->xbw_callback = callback;
|
2006-03-06 23:16:33 +03:00
|
|
|
|
|
|
|
err = register_xenbus_watch(watch);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
watch->node = NULL;
|
2006-03-16 01:20:06 +03:00
|
|
|
watch->xbw_callback = NULL;
|
2006-03-06 23:16:33 +03:00
|
|
|
xenbus_dev_fatal(dev, err, "adding watch on %s", path);
|
|
|
|
}
|
2006-03-16 01:20:06 +03:00
|
|
|
err = 0;
|
2006-03-06 23:16:33 +03:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
int
|
|
|
|
xenbus_watch_path2(struct xenbus_device *dev, const char *path,
|
2006-03-06 23:16:33 +03:00
|
|
|
const char *path2, struct xenbus_watch *watch,
|
|
|
|
void (*callback)(struct xenbus_watch *,
|
|
|
|
const char **, unsigned int))
|
|
|
|
{
|
|
|
|
int err;
|
2006-03-16 01:20:06 +03:00
|
|
|
char *state;
|
|
|
|
|
|
|
|
DPRINTK("xenbus_watch_path2 path %s path2 %s\n", path, path2);
|
|
|
|
state =
|
2006-03-06 23:21:35 +03:00
|
|
|
malloc(strlen(path) + 1 + strlen(path2) + 1, M_DEVBUF,
|
|
|
|
M_NOWAIT);
|
2006-03-06 23:16:33 +03:00
|
|
|
if (!state) {
|
2006-03-06 23:21:35 +03:00
|
|
|
xenbus_dev_fatal(dev, ENOMEM, "allocating path for watch");
|
|
|
|
return ENOMEM;
|
2006-03-06 23:16:33 +03:00
|
|
|
}
|
|
|
|
strcpy(state, path);
|
|
|
|
strcat(state, "/");
|
|
|
|
strcat(state, path2);
|
|
|
|
|
|
|
|
err = xenbus_watch_path(dev, state, watch, callback);
|
|
|
|
|
|
|
|
if (err) {
|
2006-03-06 23:21:35 +03:00
|
|
|
free(state, M_DEVBUF);
|
2006-03-06 23:16:33 +03:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
int
|
|
|
|
xenbus_switch_state(struct xenbus_device *dev,
|
2006-03-06 23:16:33 +03:00
|
|
|
struct xenbus_transaction *xbt,
|
|
|
|
XenbusState state)
|
|
|
|
{
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2006-03-17 02:08:08 +03:00
|
|
|
u_long current_state;
|
2006-03-06 23:16:33 +03:00
|
|
|
|
2006-06-25 20:46:59 +04:00
|
|
|
int err = xenbus_read_ul(xbt, dev->xbusd_path, "state",
|
|
|
|
¤t_state, 10);
|
2006-03-17 02:08:08 +03:00
|
|
|
if (err)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((XenbusState)current_state == state)
|
2006-03-06 23:16:33 +03:00
|
|
|
return 0;
|
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
err = xenbus_printf(xbt, dev->xbusd_path, "state", "%d", state);
|
2006-03-06 23:16:33 +03:00
|
|
|
if (err) {
|
|
|
|
xenbus_dev_fatal(dev, err, "writing new state");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2006-03-06 23:21:35 +03:00
|
|
|
static char *
|
|
|
|
error_path(struct xenbus_device *dev)
|
2006-03-06 23:16:33 +03:00
|
|
|
{
|
2006-03-06 23:21:35 +03:00
|
|
|
char *path_buffer = malloc(strlen("error/") + strlen(dev->xbusd_path) +
|
|
|
|
1, M_DEVBUF, M_NOWAIT);
|
2006-03-06 23:16:33 +03:00
|
|
|
if (path_buffer == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(path_buffer, "error/");
|
2006-03-06 23:21:35 +03:00
|
|
|
strcpy(path_buffer + strlen("error/"), dev->xbusd_path);
|
2006-03-06 23:16:33 +03:00
|
|
|
|
|
|
|
return path_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
static void
|
|
|
|
_dev_error(struct xenbus_device *dev, int err, const char *fmt,
|
2006-03-06 23:16:33 +03:00
|
|
|
va_list ap)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned int len;
|
|
|
|
char *printf_buffer = NULL, *path_buffer = NULL;
|
|
|
|
|
|
|
|
#define PRINTF_BUFFER_SIZE 4096
|
2006-03-06 23:21:35 +03:00
|
|
|
printf_buffer = malloc(PRINTF_BUFFER_SIZE, M_DEVBUF, M_NOWAIT);
|
2006-03-06 23:16:33 +03:00
|
|
|
if (printf_buffer == NULL)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
len = sprintf(printf_buffer, "%i ", -err);
|
|
|
|
ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
|
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
KASSERT(len + ret < PRINTF_BUFFER_SIZE);
|
|
|
|
dev->xbusd_has_error = 1;
|
2006-03-06 23:16:33 +03:00
|
|
|
|
|
|
|
path_buffer = error_path(dev);
|
|
|
|
|
|
|
|
if (path_buffer == NULL) {
|
|
|
|
printk("xenbus: failed to write error node for %s (%s)\n",
|
2006-03-06 23:21:35 +03:00
|
|
|
dev->xbusd_path, printf_buffer);
|
2006-03-06 23:16:33 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xenbus_write(NULL, path_buffer, "error", printf_buffer) != 0) {
|
|
|
|
printk("xenbus: failed to write error node for %s (%s)\n",
|
2006-03-06 23:21:35 +03:00
|
|
|
dev->xbusd_path, printf_buffer);
|
2006-03-06 23:16:33 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (printf_buffer)
|
2006-03-06 23:21:35 +03:00
|
|
|
free(printf_buffer, M_DEVBUF);
|
2006-03-06 23:16:33 +03:00
|
|
|
if (path_buffer)
|
2006-03-06 23:21:35 +03:00
|
|
|
free(path_buffer, M_DEVBUF);
|
2006-03-06 23:16:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
void
|
|
|
|
xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
|
2006-03-06 23:16:33 +03:00
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
_dev_error(dev, err, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
void
|
|
|
|
xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
|
2006-03-06 23:16:33 +03:00
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
_dev_error(dev, err, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
xenbus_switch_state(dev, NULL, XenbusStateClosing);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
int
|
|
|
|
xenbus_grant_ring(struct xenbus_device *dev, paddr_t ring_pa,
|
|
|
|
grant_ref_t *entryp)
|
2006-03-06 23:16:33 +03:00
|
|
|
{
|
2006-03-06 23:21:35 +03:00
|
|
|
int err = xengnt_grant_access(dev->xbusd_otherend_id, ring_pa,
|
|
|
|
0, entryp);
|
|
|
|
if (err != 0)
|
2006-03-06 23:16:33 +03:00
|
|
|
xenbus_dev_fatal(dev, err, "granting access to ring page");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
int
|
|
|
|
xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
|
2006-03-06 23:16:33 +03:00
|
|
|
{
|
|
|
|
evtchn_op_t op = {
|
|
|
|
.cmd = EVTCHNOP_alloc_unbound,
|
|
|
|
.u.alloc_unbound.dom = DOMID_SELF,
|
2006-03-06 23:21:35 +03:00
|
|
|
.u.alloc_unbound.remote_dom = dev->xbusd_otherend_id };
|
2006-03-06 23:16:33 +03:00
|
|
|
|
|
|
|
int err = HYPERVISOR_event_channel_op(&op);
|
|
|
|
if (err)
|
|
|
|
xenbus_dev_fatal(dev, err, "allocating event channel");
|
|
|
|
else
|
|
|
|
*port = op.u.alloc_unbound.port;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-06 23:21:35 +03:00
|
|
|
XenbusState
|
|
|
|
xenbus_read_driver_state(const char *path)
|
2006-03-06 23:16:33 +03:00
|
|
|
{
|
2006-03-17 02:08:08 +03:00
|
|
|
u_long result;
|
2006-03-06 23:16:33 +03:00
|
|
|
|
2006-06-25 20:46:59 +04:00
|
|
|
int err = xenbus_read_ul(NULL, path, "state", &result, 10);
|
2006-03-06 23:16:33 +03:00
|
|
|
if (err)
|
|
|
|
result = XenbusStateClosed;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-file-style: "linux"
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* tab-width: 8
|
|
|
|
* End:
|
|
|
|
*/
|