xenbusvar.h revision 186557
1185605Skmacy/****************************************************************************** 2185605Skmacy * xenbus.h 3185605Skmacy * 4185605Skmacy * Talks to Xen Store to figure out what devices we have. 5185605Skmacy * 6185605Skmacy * Copyright (C) 2005 Rusty Russell, IBM Corporation 7185605Skmacy * Copyright (C) 2005 XenSource Ltd. 8185605Skmacy * 9185605Skmacy * This file may be distributed separately from the Linux kernel, or 10185605Skmacy * incorporated into other software packages, subject to the following license: 11185605Skmacy * 12185605Skmacy * Permission is hereby granted, free of charge, to any person obtaining a copy 13185605Skmacy * of this source file (the "Software"), to deal in the Software without 14185605Skmacy * restriction, including without limitation the rights to use, copy, modify, 15185605Skmacy * merge, publish, distribute, sublicense, and/or sell copies of the Software, 16185605Skmacy * and to permit persons to whom the Software is furnished to do so, subject to 17185605Skmacy * the following conditions: 18185605Skmacy * 19185605Skmacy * The above copyright notice and this permission notice shall be included in 20185605Skmacy * all copies or substantial portions of the Software. 21185605Skmacy * 22185605Skmacy * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23185605Skmacy * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24185605Skmacy * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25185605Skmacy * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26185605Skmacy * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27185605Skmacy * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 28185605Skmacy * IN THE SOFTWARE. 29185605Skmacy * 30185605Skmacy * $FreeBSD: head/sys/xen/xenbus/xenbusvar.h 186557 2008-12-29 06:31:03Z kmacy $ 31185605Skmacy */ 32185605Skmacy 33185605Skmacy#ifndef _XEN_XENBUS_XENBUSVAR_H 34185605Skmacy#define _XEN_XENBUS_XENBUSVAR_H 35185605Skmacy 36185605Skmacy#include <sys/queue.h> 37185605Skmacy#include <sys/bus.h> 38185605Skmacy#include <sys/eventhandler.h> 39185605Skmacy#include <machine/xen/xen-os.h> 40185605Skmacy#include <xen/interface/io/xenbus.h> 41185605Skmacy#include <xen/interface/io/xs_wire.h> 42185605Skmacy 43185605Skmacy#include "xenbus_if.h" 44185605Skmacy 45185605Skmacyenum { 46185605Skmacy /* 47185605Skmacy * Path of this device node. 48185605Skmacy */ 49185605Skmacy XENBUS_IVAR_NODE, 50185605Skmacy 51185605Skmacy /* 52185605Skmacy * The device type (e.g. vif, vbd). 53185605Skmacy */ 54185605Skmacy XENBUS_IVAR_TYPE, 55185605Skmacy 56185605Skmacy /* 57185605Skmacy * The state of this device (not the otherend's state). 58185605Skmacy */ 59185605Skmacy XENBUS_IVAR_STATE, 60185605Skmacy 61185605Skmacy /* 62185605Skmacy * Domain ID of the other end device. 63185605Skmacy */ 64185605Skmacy XENBUS_IVAR_OTHEREND_ID, 65185605Skmacy 66185605Skmacy /* 67185605Skmacy * Path of the other end device. 68185605Skmacy */ 69185605Skmacy XENBUS_IVAR_OTHEREND_PATH 70185605Skmacy}; 71185605Skmacy 72185605Skmacy/* 73185605Skmacy * Simplified accessors for xenbus devices 74185605Skmacy */ 75185605Skmacy#define XENBUS_ACCESSOR(var, ivar, type) \ 76185605Skmacy __BUS_ACCESSOR(xenbus, var, XENBUS, ivar, type) 77185605Skmacy 78185605SkmacyXENBUS_ACCESSOR(node, NODE, const char *) 79185605SkmacyXENBUS_ACCESSOR(type, TYPE, const char *) 80185605SkmacyXENBUS_ACCESSOR(state, STATE, enum xenbus_state) 81185605SkmacyXENBUS_ACCESSOR(otherend_id, OTHEREND_ID, int) 82185605SkmacyXENBUS_ACCESSOR(otherend_path, OTHEREND_PATH, const char *) 83185605Skmacy 84185605Skmacy/* Register callback to watch this node. */ 85185605Skmacystruct xenbus_watch 86185605Skmacy{ 87185605Skmacy LIST_ENTRY(xenbus_watch) list; 88185605Skmacy 89185605Skmacy /* Path being watched. */ 90185605Skmacy char *node; 91185605Skmacy 92185605Skmacy /* Callback (executed in a process context with no locks held). */ 93185605Skmacy void (*callback)(struct xenbus_watch *, 94185605Skmacy const char **vec, unsigned int len); 95185605Skmacy}; 96185605Skmacy 97185605Skmacytypedef int (*xenstore_event_handler_t)(void *); 98185605Skmacy 99185605Skmacystruct xenbus_transaction 100185605Skmacy{ 101185605Skmacy uint32_t id; 102185605Skmacy}; 103185605Skmacy 104185605Skmacy#define XBT_NIL ((struct xenbus_transaction) { 0 }) 105185605Skmacy 106186557Skmacyint xenbus_directory(struct xenbus_transaction t, const char *dir, 107186557Skmacy const char *node, unsigned int *num, char ***result); 108186557Skmacyint xenbus_read(struct xenbus_transaction t, const char *dir, 109186557Skmacy const char *node, unsigned int *len, void **result); 110186557Skmacyint xenbus_write(struct xenbus_transaction t, const char *dir, 111186557Skmacy const char *node, const char *string); 112186557Skmacyint xenbus_mkdir(struct xenbus_transaction t, const char *dir, 113186557Skmacy const char *node); 114186557Skmacyint xenbus_exists(struct xenbus_transaction t, const char *dir, 115186557Skmacy const char *node); 116185605Skmacyint xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node); 117185605Skmacyint xenbus_transaction_start(struct xenbus_transaction *t); 118185605Skmacyint xenbus_transaction_end(struct xenbus_transaction t, int abort); 119185605Skmacy 120186557Skmacy/* 121186557Skmacy * Single read and scanf: returns errno or zero. If scancountp is 122186557Skmacy * non-null, then number of items scanned is returned in *scanncountp. 123186557Skmacy */ 124185605Skmacyint xenbus_scanf(struct xenbus_transaction t, 125186557Skmacy const char *dir, const char *node, int *scancountp, const char *fmt, ...) 126186557Skmacy __attribute__((format(scanf, 5, 6))); 127185605Skmacy 128186557Skmacy/* Single printf and write: returns errno or 0. */ 129185605Skmacyint xenbus_printf(struct xenbus_transaction t, 130185605Skmacy const char *dir, const char *node, const char *fmt, ...) 131185605Skmacy __attribute__((format(printf, 4, 5))); 132185605Skmacy 133186557Skmacy/* 134186557Skmacy * Generic read function: NULL-terminated triples of name, 135186557Skmacy * sprintf-style type string, and pointer. Returns 0 or errno. 136186557Skmacy */ 137185605Skmacyint xenbus_gather(struct xenbus_transaction t, const char *dir, ...); 138185605Skmacy 139185605Skmacy/* notifer routines for when the xenstore comes up */ 140185605Skmacyint register_xenstore_notifier(xenstore_event_handler_t func, void *arg, int priority); 141185605Skmacy#if 0 142185605Skmacyvoid unregister_xenstore_notifier(); 143185605Skmacy#endif 144185605Skmacyint register_xenbus_watch(struct xenbus_watch *watch); 145185605Skmacyvoid unregister_xenbus_watch(struct xenbus_watch *watch); 146185605Skmacyvoid xs_suspend(void); 147185605Skmacyvoid xs_resume(void); 148185605Skmacy 149185605Skmacy/* Used by xenbus_dev to borrow kernel's store connection. */ 150186557Skmacyint xenbus_dev_request_and_reply(struct xsd_sockmsg *msg, void **result); 151185605Skmacy 152186557Skmacy#if 0 153186557Skmacy 154185605Skmacy#define XENBUS_IS_ERR_READ(str) ({ \ 155185605Skmacy if (!IS_ERR(str) && strlen(str) == 0) { \ 156185605Skmacy free(str, M_DEVBUF); \ 157185605Skmacy str = ERR_PTR(-ERANGE); \ 158185605Skmacy } \ 159185605Skmacy IS_ERR(str); \ 160185605Skmacy}) 161185605Skmacy 162186557Skmacy#endif 163185605Skmacy 164186557Skmacy#define XENBUS_EXIST_ERR(err) ((err) == ENOENT || (err) == ERANGE) 165186557Skmacy 166186557Skmacy 167185605Skmacy/** 168185605Skmacy * Register a watch on the given path, using the given xenbus_watch structure 169185605Skmacy * for storage, and the given callback function as the callback. Return 0 on 170186557Skmacy * success, or errno on error. On success, the given path will be saved as 171185605Skmacy * watch->node, and remains the caller's to free. On error, watch->node will 172185605Skmacy * be NULL, the device will switch to XenbusStateClosing, and the error will 173185605Skmacy * be saved in the store. 174185605Skmacy */ 175185605Skmacyint xenbus_watch_path(device_t dev, char *path, 176185605Skmacy struct xenbus_watch *watch, 177185605Skmacy void (*callback)(struct xenbus_watch *, 178185605Skmacy const char **, unsigned int)); 179185605Skmacy 180185605Skmacy 181185605Skmacy/** 182185605Skmacy * Register a watch on the given path/path2, using the given xenbus_watch 183185605Skmacy * structure for storage, and the given callback function as the callback. 184186557Skmacy * Return 0 on success, or errno on error. On success, the watched path 185185605Skmacy * (path/path2) will be saved as watch->node, and becomes the caller's to 186185605Skmacy * kfree(). On error, watch->node will be NULL, so the caller has nothing to 187185605Skmacy * free, the device will switch to XenbusStateClosing, and the error will be 188185605Skmacy * saved in the store. 189185605Skmacy */ 190185605Skmacyint xenbus_watch_path2(device_t dev, const char *path, 191185605Skmacy const char *path2, struct xenbus_watch *watch, 192185605Skmacy void (*callback)(struct xenbus_watch *, 193185605Skmacy const char **, unsigned int)); 194185605Skmacy 195185605Skmacy 196185605Skmacy/** 197185605Skmacy * Advertise in the store a change of the given driver to the given new_state. 198185605Skmacy * which case this is performed inside its own transaction. Return 0 on 199186557Skmacy * success, or errno on error. On error, the device will switch to 200185605Skmacy * XenbusStateClosing, and the error will be saved in the store. 201185605Skmacy */ 202185605Skmacyint xenbus_switch_state(device_t dev, 203185605Skmacy XenbusState new_state); 204185605Skmacy 205185605Skmacy 206185605Skmacy/** 207186557Skmacy * Grant access to the given ring_mfn to the peer of the given device. 208186557Skmacy * Return 0 on success, or errno on error. On error, the device will 209186557Skmacy * switch to XenbusStateClosing, and the error will be saved in the 210186557Skmacy * store. The grant ring reference is returned in *refp. 211185605Skmacy */ 212186557Skmacyint xenbus_grant_ring(device_t dev, unsigned long ring_mfn, int *refp); 213185605Skmacy 214185605Skmacy 215185605Skmacy/** 216185605Skmacy * Allocate an event channel for the given xenbus_device, assigning the newly 217186557Skmacy * created local port to *port. Return 0 on success, or errno on error. On 218185605Skmacy * error, the device will switch to XenbusStateClosing, and the error will be 219185605Skmacy * saved in the store. 220185605Skmacy */ 221185605Skmacyint xenbus_alloc_evtchn(device_t dev, int *port); 222185605Skmacy 223185605Skmacy 224185605Skmacy/** 225186557Skmacy * Free an existing event channel. Returns 0 on success or errno on error. 226185605Skmacy */ 227185605Skmacyint xenbus_free_evtchn(device_t dev, int port); 228185605Skmacy 229185605Skmacy 230185605Skmacy/** 231185605Skmacy * Return the state of the driver rooted at the given store path, or 232185605Skmacy * XenbusStateClosed if no state can be read. 233185605Skmacy */ 234185605SkmacyXenbusState xenbus_read_driver_state(const char *path); 235185605Skmacy 236185605Skmacy 237185605Skmacy/*** 238185605Skmacy * Report the given negative errno into the store, along with the given 239185605Skmacy * formatted message. 240185605Skmacy */ 241185605Skmacyvoid xenbus_dev_error(device_t dev, int err, const char *fmt, 242185605Skmacy ...); 243185605Skmacy 244185605Skmacy 245185605Skmacy/*** 246185605Skmacy * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by 247185605Skmacy * xenbus_switch_state(dev, NULL, XenbusStateClosing) to schedule an orderly 248185605Skmacy * closedown of this driver and its peer. 249185605Skmacy */ 250185605Skmacyvoid xenbus_dev_fatal(device_t dev, int err, const char *fmt, 251185605Skmacy ...); 252185605Skmacy 253185605Skmacyint xenbus_dev_init(void); 254185605Skmacy 255185605Skmacyconst char *xenbus_strstate(enum xenbus_state state); 256185605Skmacyint xenbus_dev_is_online(device_t dev); 257185605Skmacyint xenbus_frontend_closed(device_t dev); 258185605Skmacy 259185605Skmacy#endif /* _XEN_XENBUS_XENBUSVAR_H */ 260