xenbusvar.h revision 255040
1185605Skmacy/****************************************************************************** 2185605Skmacy * Copyright (C) 2005 Rusty Russell, IBM Corporation 3185605Skmacy * Copyright (C) 2005 XenSource Ltd. 4185605Skmacy * 5185605Skmacy * This file may be distributed separately from the Linux kernel, or 6185605Skmacy * incorporated into other software packages, subject to the following license: 7185605Skmacy * 8185605Skmacy * Permission is hereby granted, free of charge, to any person obtaining a copy 9185605Skmacy * of this source file (the "Software"), to deal in the Software without 10185605Skmacy * restriction, including without limitation the rights to use, copy, modify, 11185605Skmacy * merge, publish, distribute, sublicense, and/or sell copies of the Software, 12185605Skmacy * and to permit persons to whom the Software is furnished to do so, subject to 13185605Skmacy * the following conditions: 14185605Skmacy * 15185605Skmacy * The above copyright notice and this permission notice shall be included in 16185605Skmacy * all copies or substantial portions of the Software. 17185605Skmacy * 18185605Skmacy * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19185605Skmacy * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20185605Skmacy * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21185605Skmacy * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22185605Skmacy * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23185605Skmacy * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 24185605Skmacy * IN THE SOFTWARE. 25185605Skmacy * 26185605Skmacy * $FreeBSD: head/sys/xen/xenbus/xenbusvar.h 255040 2013-08-29 19:52:18Z gibbs $ 27185605Skmacy */ 28185605Skmacy 29214077Sgibbs/** 30214077Sgibbs * \file xenbusvar.h 31214077Sgibbs * 32214077Sgibbs * \brief Datastructures and function declarations for usedby device 33214077Sgibbs * drivers operating on the XenBus. 34214077Sgibbs */ 35214077Sgibbs 36185605Skmacy#ifndef _XEN_XENBUS_XENBUSVAR_H 37185605Skmacy#define _XEN_XENBUS_XENBUSVAR_H 38185605Skmacy 39185605Skmacy#include <sys/queue.h> 40185605Skmacy#include <sys/bus.h> 41185605Skmacy#include <sys/eventhandler.h> 42214077Sgibbs#include <sys/malloc.h> 43214077Sgibbs#include <sys/sbuf.h> 44214077Sgibbs 45214077Sgibbs#include <machine/stdarg.h> 46214077Sgibbs 47255040Sgibbs#include <xen/xen-os.h> 48214077Sgibbs#include <xen/interface/grant_table.h> 49185605Skmacy#include <xen/interface/io/xenbus.h> 50185605Skmacy#include <xen/interface/io/xs_wire.h> 51185605Skmacy 52214077Sgibbs#include <xen/xenstore/xenstorevar.h> 53214077Sgibbs 54214077Sgibbs/* XenBus allocations including XenStore data returned to clients. */ 55214077SgibbsMALLOC_DECLARE(M_XENBUS); 56214077Sgibbs 57185605Skmacyenum { 58214077Sgibbs /** 59185605Skmacy * Path of this device node. 60185605Skmacy */ 61185605Skmacy XENBUS_IVAR_NODE, 62185605Skmacy 63214077Sgibbs /** 64185605Skmacy * The device type (e.g. vif, vbd). 65185605Skmacy */ 66185605Skmacy XENBUS_IVAR_TYPE, 67185605Skmacy 68214077Sgibbs /** 69185605Skmacy * The state of this device (not the otherend's state). 70185605Skmacy */ 71185605Skmacy XENBUS_IVAR_STATE, 72185605Skmacy 73214077Sgibbs /** 74185605Skmacy * Domain ID of the other end device. 75185605Skmacy */ 76185605Skmacy XENBUS_IVAR_OTHEREND_ID, 77185605Skmacy 78214077Sgibbs /** 79185605Skmacy * Path of the other end device. 80185605Skmacy */ 81185605Skmacy XENBUS_IVAR_OTHEREND_PATH 82185605Skmacy}; 83185605Skmacy 84214077Sgibbs/** 85185605Skmacy * Simplified accessors for xenbus devices 86185605Skmacy */ 87185605Skmacy#define XENBUS_ACCESSOR(var, ivar, type) \ 88185605Skmacy __BUS_ACCESSOR(xenbus, var, XENBUS, ivar, type) 89185605Skmacy 90185605SkmacyXENBUS_ACCESSOR(node, NODE, const char *) 91185605SkmacyXENBUS_ACCESSOR(type, TYPE, const char *) 92185605SkmacyXENBUS_ACCESSOR(state, STATE, enum xenbus_state) 93185605SkmacyXENBUS_ACCESSOR(otherend_id, OTHEREND_ID, int) 94185605SkmacyXENBUS_ACCESSOR(otherend_path, OTHEREND_PATH, const char *) 95185605Skmacy 96214077Sgibbs/** 97214077Sgibbs * Return the state of a XenBus device. 98214077Sgibbs * 99214077Sgibbs * \param path The root XenStore path for the device. 100214077Sgibbs * 101214077Sgibbs * \return The current state of the device or XenbusStateClosed if no 102214077Sgibbs * state can be read. 103186557Skmacy */ 104214077SgibbsXenbusState xenbus_read_driver_state(const char *path); 105185605Skmacy 106185605Skmacy/** 107231743Sgibbs * Return the state of the "other end" (peer) of a XenBus device. 108231743Sgibbs * 109231743Sgibbs * \param dev The XenBus device whose peer to query. 110231743Sgibbs * 111231743Sgibbs * \return The current state of the peer device or XenbusStateClosed if no 112231743Sgibbs * state can be read. 113231743Sgibbs */ 114231743Sgibbsstatic inline XenbusState 115231743Sgibbsxenbus_get_otherend_state(device_t dev) 116231743Sgibbs{ 117231743Sgibbs return (xenbus_read_driver_state(xenbus_get_otherend_path(dev))); 118231743Sgibbs} 119231743Sgibbs 120231743Sgibbs/** 121214077Sgibbs * Initialize and register a watch on the given path (client suplied storage). 122214077Sgibbs * 123214077Sgibbs * \param dev The XenBus device requesting the watch service. 124214077Sgibbs * \param path The XenStore path of the object to be watched. The 125214077Sgibbs * storage for this string must be stable for the lifetime 126214077Sgibbs * of the watch. 127214077Sgibbs * \param watch The watch object to use for this request. This object 128214077Sgibbs * must be stable for the lifetime of the watch. 129214077Sgibbs * \param callback The function to call when XenStore objects at or below 130214077Sgibbs * path are modified. 131222975Sgibbs * \param cb_data Client data that can be retrieved from the watch object 132222975Sgibbs * during the callback. 133214077Sgibbs * 134214077Sgibbs * \return On success, 0. Otherwise an errno value indicating the 135214077Sgibbs * type of failure. 136214077Sgibbs * 137214077Sgibbs * \note On error, the device 'dev' will be switched to the XenbusStateClosing 138214077Sgibbs * state and the returned error is saved in the per-device error node 139214077Sgibbs * for dev in the XenStore. 140185605Skmacy */ 141185605Skmacyint xenbus_watch_path(device_t dev, char *path, 142214077Sgibbs struct xs_watch *watch, 143222975Sgibbs xs_watch_cb_t *callback, 144222975Sgibbs uintptr_t cb_data); 145185605Skmacy 146185605Skmacy/** 147214077Sgibbs * Initialize and register a watch at path/path2 in the XenStore. 148214077Sgibbs * 149214077Sgibbs * \param dev The XenBus device requesting the watch service. 150214077Sgibbs * \param path The base XenStore path of the object to be watched. 151214077Sgibbs * \param path2 The tail XenStore path of the object to be watched. 152214077Sgibbs * \param watch The watch object to use for this request. This object 153214077Sgibbs * must be stable for the lifetime of the watch. 154214077Sgibbs * \param callback The function to call when XenStore objects at or below 155214077Sgibbs * path are modified. 156222975Sgibbs * \param cb_data Client data that can be retrieved from the watch object 157222975Sgibbs * during the callback. 158214077Sgibbs * 159214077Sgibbs * \return On success, 0. Otherwise an errno value indicating the 160214077Sgibbs * type of failure. 161214077Sgibbs * 162214077Sgibbs * \note On error, \a dev will be switched to the XenbusStateClosing 163214077Sgibbs * state and the returned error is saved in the per-device error node 164214077Sgibbs * for \a dev in the XenStore. 165214077Sgibbs * 166214077Sgibbs * Similar to xenbus_watch_path, however the storage for the path to the 167214077Sgibbs * watched object is allocated from the heap and filled with "path '/' path2". 168214077Sgibbs * Should a call to this function succeed, it is the callers responsibility 169214077Sgibbs * to free watch->node using the M_XENBUS malloc type. 170185605Skmacy */ 171185605Skmacyint xenbus_watch_path2(device_t dev, const char *path, 172214077Sgibbs const char *path2, struct xs_watch *watch, 173222975Sgibbs xs_watch_cb_t *callback, 174222975Sgibbs uintptr_t cb_data); 175185605Skmacy 176214077Sgibbs/** 177214077Sgibbs * Grant access to the given ring_mfn to the peer of the given device. 178214077Sgibbs * 179214077Sgibbs * \param dev The device granting access to the ring page. 180214077Sgibbs * \param ring_mfn The guest machine page number of the page to grant 181214077Sgibbs * peer access rights. 182214077Sgibbs * \param refp[out] The grant reference for the page. 183214077Sgibbs * 184214077Sgibbs * \return On success, 0. Otherwise an errno value indicating the 185214077Sgibbs * type of failure. 186214077Sgibbs * 187214077Sgibbs * A successful call to xenbus_grant_ring should be paired with a call 188214077Sgibbs * to gnttab_end_foreign_access() when foregn access to this page is no 189214077Sgibbs * longer requried. 190214077Sgibbs * 191214077Sgibbs * \note On error, \a dev will be switched to the XenbusStateClosing 192214077Sgibbs * state and the returned error is saved in the per-device error node 193214077Sgibbs * for \a dev in the XenStore. 194214077Sgibbs */ 195214077Sgibbsint xenbus_grant_ring(device_t dev, unsigned long ring_mfn, grant_ref_t *refp); 196185605Skmacy 197185605Skmacy/** 198214077Sgibbs * Record the given errno, along with the given, printf-style, formatted 199214077Sgibbs * message in dev's device specific error node in the XenStore. 200214077Sgibbs * 201214077Sgibbs * \param dev The device which encountered the error. 202214077Sgibbs * \param err The errno value corresponding to the error. 203214077Sgibbs * \param fmt Printf format string followed by a variable number of 204214077Sgibbs * printf arguments. 205185605Skmacy */ 206214077Sgibbsvoid xenbus_dev_error(device_t dev, int err, const char *fmt, ...) 207214077Sgibbs __attribute__((format(printf, 3, 4))); 208185605Skmacy 209185605Skmacy/** 210214077Sgibbs * va_list version of xenbus_dev_error(). 211214077Sgibbs * 212214077Sgibbs * \param dev The device which encountered the error. 213214077Sgibbs * \param err The errno value corresponding to the error. 214214077Sgibbs * \param fmt Printf format string. 215214077Sgibbs * \param ap Va_list of printf arguments. 216185605Skmacy */ 217214077Sgibbsvoid xenbus_dev_verror(device_t dev, int err, const char *fmt, va_list ap) 218214077Sgibbs __attribute__((format(printf, 3, 0))); 219185605Skmacy 220185605Skmacy/** 221214077Sgibbs * Equivalent to xenbus_dev_error(), followed by 222214077Sgibbs * xenbus_set_state(dev, XenbusStateClosing). 223214077Sgibbs * 224214077Sgibbs * \param dev The device which encountered the error. 225214077Sgibbs * \param err The errno value corresponding to the error. 226214077Sgibbs * \param fmt Printf format string followed by a variable number of 227214077Sgibbs * printf arguments. 228185605Skmacy */ 229214077Sgibbsvoid xenbus_dev_fatal(device_t dev, int err, const char *fmt, ...) 230214077Sgibbs __attribute__((format(printf, 3, 4))); 231185605Skmacy 232185605Skmacy/** 233214077Sgibbs * va_list version of xenbus_dev_fatal(). 234214077Sgibbs * 235214077Sgibbs * \param dev The device which encountered the error. 236214077Sgibbs * \param err The errno value corresponding to the error. 237214077Sgibbs * \param fmt Printf format string. 238214077Sgibbs * \param ap Va_list of printf arguments. 239185605Skmacy */ 240214077Sgibbsvoid xenbus_dev_vfatal(device_t dev, int err, const char *fmt, va_list) 241214077Sgibbs __attribute__((format(printf, 3, 0))); 242185605Skmacy 243214077Sgibbs/** 244214077Sgibbs * Convert a member of the xenbus_state enum into an ASCII string. 245214077Sgibbs * 246214077Sgibbs * /param state The XenBus state to lookup. 247214077Sgibbs * 248214077Sgibbs * /return A string representing state or, for unrecognized states, 249214077Sgibbs * the string "Unknown". 250185605Skmacy */ 251214077Sgibbsconst char *xenbus_strstate(enum xenbus_state state); 252185605Skmacy 253214077Sgibbs/** 254214077Sgibbs * Return the value of a XenBus device's "online" node within the XenStore. 255214077Sgibbs * 256214077Sgibbs * \param dev The XenBus device to query. 257214077Sgibbs * 258214077Sgibbs * \return The value of the "online" node for the device. If the node 259214077Sgibbs * does not exist, 0 (offline) is returned. 260185605Skmacy */ 261185605Skmacyint xenbus_dev_is_online(device_t dev); 262185605Skmacy 263222975Sgibbs/** 264222975Sgibbs * Default callback invoked when a change to the local XenStore sub-tree 265222975Sgibbs * for a device is modified. 266222975Sgibbs * 267222975Sgibbs * \param dev The XenBus device whose tree was modified. 268222975Sgibbs * \param path The tree relative sub-path to the modified node. The empty 269222975Sgibbs * string indicates the root of the tree was destroyed. 270222975Sgibbs */ 271222975Sgibbsvoid xenbus_localend_changed(device_t dev, const char *path); 272222975Sgibbs 273222975Sgibbs#include "xenbus_if.h" 274222975Sgibbs 275185605Skmacy#endif /* _XEN_XENBUS_XENBUSVAR_H */ 276