xenbusvar.h revision 185605
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 185605 2008-12-04 07:59:05Z 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 106185605Skmacychar **xenbus_directory(struct xenbus_transaction t, 107185605Skmacy const char *dir, const char *node, unsigned int *num); 108185605Skmacyvoid *xenbus_read(struct xenbus_transaction t, 109185605Skmacy const char *dir, const char *node, unsigned int *len); 110185605Skmacyint xenbus_write(struct xenbus_transaction t, 111185605Skmacy const char *dir, const char *node, const char *string); 112185605Skmacyint xenbus_mkdir(struct xenbus_transaction t, 113185605Skmacy const char *dir, const char *node); 114185605Skmacyint xenbus_exists(struct xenbus_transaction t, 115185605Skmacy const char *dir, 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 120185605Skmacy/* Single read and scanf: returns -errno or num scanned if > 0. */ 121185605Skmacyint xenbus_scanf(struct xenbus_transaction t, 122185605Skmacy const char *dir, const char *node, const char *fmt, ...) 123185605Skmacy __attribute__((format(scanf, 4, 5))); 124185605Skmacy 125185605Skmacy/* Single printf and write: returns -errno or 0. */ 126185605Skmacyint xenbus_printf(struct xenbus_transaction t, 127185605Skmacy const char *dir, const char *node, const char *fmt, ...) 128185605Skmacy __attribute__((format(printf, 4, 5))); 129185605Skmacy 130185605Skmacy/* Generic read function: NULL-terminated triples of name, 131185605Skmacy * sprintf-style type string, and pointer. Returns 0 or errno.*/ 132185605Skmacyint xenbus_gather(struct xenbus_transaction t, const char *dir, ...); 133185605Skmacy 134185605Skmacy/* notifer routines for when the xenstore comes up */ 135185605Skmacyint register_xenstore_notifier(xenstore_event_handler_t func, void *arg, int priority); 136185605Skmacy#if 0 137185605Skmacyvoid unregister_xenstore_notifier(); 138185605Skmacy#endif 139185605Skmacyint register_xenbus_watch(struct xenbus_watch *watch); 140185605Skmacyvoid unregister_xenbus_watch(struct xenbus_watch *watch); 141185605Skmacyvoid xs_suspend(void); 142185605Skmacyvoid xs_resume(void); 143185605Skmacy 144185605Skmacy/* Used by xenbus_dev to borrow kernel's store connection. */ 145185605Skmacyvoid *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg); 146185605Skmacy 147185605Skmacy#define XENBUS_IS_ERR_READ(str) ({ \ 148185605Skmacy if (!IS_ERR(str) && strlen(str) == 0) { \ 149185605Skmacy free(str, M_DEVBUF); \ 150185605Skmacy str = ERR_PTR(-ERANGE); \ 151185605Skmacy } \ 152185605Skmacy IS_ERR(str); \ 153185605Skmacy}) 154185605Skmacy 155185605Skmacy#define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE) 156185605Skmacy 157185605Skmacy/** 158185605Skmacy * Register a watch on the given path, using the given xenbus_watch structure 159185605Skmacy * for storage, and the given callback function as the callback. Return 0 on 160185605Skmacy * success, or -errno on error. On success, the given path will be saved as 161185605Skmacy * watch->node, and remains the caller's to free. On error, watch->node will 162185605Skmacy * be NULL, the device will switch to XenbusStateClosing, and the error will 163185605Skmacy * be saved in the store. 164185605Skmacy */ 165185605Skmacyint xenbus_watch_path(device_t dev, char *path, 166185605Skmacy struct xenbus_watch *watch, 167185605Skmacy void (*callback)(struct xenbus_watch *, 168185605Skmacy const char **, unsigned int)); 169185605Skmacy 170185605Skmacy 171185605Skmacy/** 172185605Skmacy * Register a watch on the given path/path2, using the given xenbus_watch 173185605Skmacy * structure for storage, and the given callback function as the callback. 174185605Skmacy * Return 0 on success, or -errno on error. On success, the watched path 175185605Skmacy * (path/path2) will be saved as watch->node, and becomes the caller's to 176185605Skmacy * kfree(). On error, watch->node will be NULL, so the caller has nothing to 177185605Skmacy * free, the device will switch to XenbusStateClosing, and the error will be 178185605Skmacy * saved in the store. 179185605Skmacy */ 180185605Skmacyint xenbus_watch_path2(device_t dev, const char *path, 181185605Skmacy const char *path2, struct xenbus_watch *watch, 182185605Skmacy void (*callback)(struct xenbus_watch *, 183185605Skmacy const char **, unsigned int)); 184185605Skmacy 185185605Skmacy 186185605Skmacy/** 187185605Skmacy * Advertise in the store a change of the given driver to the given new_state. 188185605Skmacy * which case this is performed inside its own transaction. Return 0 on 189185605Skmacy * success, or -errno on error. On error, the device will switch to 190185605Skmacy * XenbusStateClosing, and the error will be saved in the store. 191185605Skmacy */ 192185605Skmacyint xenbus_switch_state(device_t dev, 193185605Skmacy XenbusState new_state); 194185605Skmacy 195185605Skmacy 196185605Skmacy/** 197185605Skmacy * Grant access to the given ring_mfn to the peer of the given device. Return 198185605Skmacy * 0 on success, or -errno on error. On error, the device will switch to 199185605Skmacy * XenbusStateClosing, and the error will be saved in the store. 200185605Skmacy */ 201185605Skmacyint xenbus_grant_ring(device_t dev, unsigned long ring_mfn); 202185605Skmacy 203185605Skmacy 204185605Skmacy/** 205185605Skmacy * Allocate an event channel for the given xenbus_device, assigning the newly 206185605Skmacy * created local port to *port. Return 0 on success, or -errno on error. On 207185605Skmacy * error, the device will switch to XenbusStateClosing, and the error will be 208185605Skmacy * saved in the store. 209185605Skmacy */ 210185605Skmacyint xenbus_alloc_evtchn(device_t dev, int *port); 211185605Skmacy 212185605Skmacy 213185605Skmacy/** 214185605Skmacy * Free an existing event channel. Returns 0 on success or -errno on error. 215185605Skmacy */ 216185605Skmacyint xenbus_free_evtchn(device_t dev, int port); 217185605Skmacy 218185605Skmacy 219185605Skmacy/** 220185605Skmacy * Return the state of the driver rooted at the given store path, or 221185605Skmacy * XenbusStateClosed if no state can be read. 222185605Skmacy */ 223185605SkmacyXenbusState xenbus_read_driver_state(const char *path); 224185605Skmacy 225185605Skmacy 226185605Skmacy/*** 227185605Skmacy * Report the given negative errno into the store, along with the given 228185605Skmacy * formatted message. 229185605Skmacy */ 230185605Skmacyvoid xenbus_dev_error(device_t dev, int err, const char *fmt, 231185605Skmacy ...); 232185605Skmacy 233185605Skmacy 234185605Skmacy/*** 235185605Skmacy * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by 236185605Skmacy * xenbus_switch_state(dev, NULL, XenbusStateClosing) to schedule an orderly 237185605Skmacy * closedown of this driver and its peer. 238185605Skmacy */ 239185605Skmacyvoid xenbus_dev_fatal(device_t dev, int err, const char *fmt, 240185605Skmacy ...); 241185605Skmacy 242185605Skmacyint xenbus_dev_init(void); 243185605Skmacy 244185605Skmacyconst char *xenbus_strstate(enum xenbus_state state); 245185605Skmacyint xenbus_dev_is_online(device_t dev); 246185605Skmacyint xenbus_frontend_closed(device_t dev); 247185605Skmacy 248185605Skmacy#endif /* _XEN_XENBUS_XENBUSVAR_H */ 249