xref: /freebsd-src/sys/xen/xenbus/xenbusvar.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
123dc5621SKip Macy /******************************************************************************
223dc5621SKip Macy  * Copyright (C) 2005 Rusty Russell, IBM Corporation
323dc5621SKip Macy  * Copyright (C) 2005 XenSource Ltd.
423dc5621SKip Macy  *
523dc5621SKip Macy  * This file may be distributed separately from the Linux kernel, or
623dc5621SKip Macy  * incorporated into other software packages, subject to the following license:
723dc5621SKip Macy  *
823dc5621SKip Macy  * Permission is hereby granted, free of charge, to any person obtaining a copy
923dc5621SKip Macy  * of this source file (the "Software"), to deal in the Software without
1023dc5621SKip Macy  * restriction, including without limitation the rights to use, copy, modify,
1123dc5621SKip Macy  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
1223dc5621SKip Macy  * and to permit persons to whom the Software is furnished to do so, subject to
1323dc5621SKip Macy  * the following conditions:
1423dc5621SKip Macy  *
1523dc5621SKip Macy  * The above copyright notice and this permission notice shall be included in
1623dc5621SKip Macy  * all copies or substantial portions of the Software.
1723dc5621SKip Macy  *
1823dc5621SKip Macy  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1923dc5621SKip Macy  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2023dc5621SKip Macy  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2123dc5621SKip Macy  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2223dc5621SKip Macy  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2323dc5621SKip Macy  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2423dc5621SKip Macy  * IN THE SOFTWARE.
2523dc5621SKip Macy  */
2623dc5621SKip Macy 
27ff662b5cSJustin T. Gibbs /**
28ff662b5cSJustin T. Gibbs  * \file xenbusvar.h
29ff662b5cSJustin T. Gibbs  *
30ff662b5cSJustin T. Gibbs  * \brief Datastructures and function declarations for usedby device
31ff662b5cSJustin T. Gibbs  *        drivers operating on the XenBus.
32ff662b5cSJustin T. Gibbs  */
33ff662b5cSJustin T. Gibbs 
3423dc5621SKip Macy #ifndef _XEN_XENBUS_XENBUSVAR_H
3523dc5621SKip Macy #define _XEN_XENBUS_XENBUSVAR_H
3623dc5621SKip Macy 
3723dc5621SKip Macy #include <sys/queue.h>
3823dc5621SKip Macy #include <sys/bus.h>
3923dc5621SKip Macy #include <sys/eventhandler.h>
40ff662b5cSJustin T. Gibbs #include <sys/malloc.h>
41ff662b5cSJustin T. Gibbs #include <sys/sbuf.h>
42ff662b5cSJustin T. Gibbs 
43ff662b5cSJustin T. Gibbs #include <machine/stdarg.h>
44ff662b5cSJustin T. Gibbs 
4576acc41fSJustin T. Gibbs #include <xen/xen-os.h>
46*ad7dd514SElliott Mitchell #include <contrib/xen/grant_table.h>
47*ad7dd514SElliott Mitchell #include <contrib/xen/io/xenbus.h>
48*ad7dd514SElliott Mitchell #include <contrib/xen/io/xs_wire.h>
4923dc5621SKip Macy 
50ff662b5cSJustin T. Gibbs #include <xen/xenstore/xenstorevar.h>
51ff662b5cSJustin T. Gibbs 
52ff662b5cSJustin T. Gibbs /* XenBus allocations including XenStore data returned to clients. */
53ff662b5cSJustin T. Gibbs MALLOC_DECLARE(M_XENBUS);
54ff662b5cSJustin T. Gibbs 
5523dc5621SKip Macy enum {
56ff662b5cSJustin T. Gibbs 	/**
5723dc5621SKip Macy 	 * Path of this device node.
5823dc5621SKip Macy 	 */
5923dc5621SKip Macy 	XENBUS_IVAR_NODE,
6023dc5621SKip Macy 
61ff662b5cSJustin T. Gibbs 	/**
6223dc5621SKip Macy 	 * The device type (e.g. vif, vbd).
6323dc5621SKip Macy 	 */
6423dc5621SKip Macy 	XENBUS_IVAR_TYPE,
6523dc5621SKip Macy 
66ff662b5cSJustin T. Gibbs 	/**
6723dc5621SKip Macy 	 * The state of this device (not the otherend's state).
6823dc5621SKip Macy 	 */
6923dc5621SKip Macy 	XENBUS_IVAR_STATE,
7023dc5621SKip Macy 
71ff662b5cSJustin T. Gibbs 	/**
7223dc5621SKip Macy 	 * Domain ID of the other end device.
7323dc5621SKip Macy 	 */
7423dc5621SKip Macy 	XENBUS_IVAR_OTHEREND_ID,
7523dc5621SKip Macy 
76ff662b5cSJustin T. Gibbs 	/**
7723dc5621SKip Macy 	 * Path of the other end device.
7823dc5621SKip Macy 	 */
7923dc5621SKip Macy 	XENBUS_IVAR_OTHEREND_PATH
8023dc5621SKip Macy };
8123dc5621SKip Macy 
82ff662b5cSJustin T. Gibbs /**
83137c69eaSRoger Pau Monné  * Simplified accessors for xenbus devices:
84137c69eaSRoger Pau Monné  *
85137c69eaSRoger Pau Monné  * xenbus_get_node
86137c69eaSRoger Pau Monné  * xenbus_get_type
87137c69eaSRoger Pau Monné  * xenbus_get_state
88137c69eaSRoger Pau Monné  * xenbus_get_otherend_id
89137c69eaSRoger Pau Monné  * xenbus_get_otherend_path
9023dc5621SKip Macy  */
9123dc5621SKip Macy #define	XENBUS_ACCESSOR(var, ivar, type) \
9223dc5621SKip Macy 	__BUS_ACCESSOR(xenbus, var, XENBUS, ivar, type)
9323dc5621SKip Macy 
9423dc5621SKip Macy XENBUS_ACCESSOR(node,		NODE,			const char *)
9523dc5621SKip Macy XENBUS_ACCESSOR(type,		TYPE,			const char *)
9623dc5621SKip Macy XENBUS_ACCESSOR(state,		STATE,			enum xenbus_state)
9723dc5621SKip Macy XENBUS_ACCESSOR(otherend_id,	OTHEREND_ID,		int)
9823dc5621SKip Macy XENBUS_ACCESSOR(otherend_path,	OTHEREND_PATH,		const char *)
9923dc5621SKip Macy 
10023dc5621SKip Macy /**
101ff662b5cSJustin T. Gibbs  * Return the state of a XenBus device.
102ff662b5cSJustin T. Gibbs  *
103ff662b5cSJustin T. Gibbs  * \param path  The root XenStore path for the device.
104ff662b5cSJustin T. Gibbs  *
105ff662b5cSJustin T. Gibbs  * \return  The current state of the device or XenbusStateClosed if no
106ff662b5cSJustin T. Gibbs  *	    state can be read.
10723dc5621SKip Macy  */
10823dc5621SKip Macy XenbusState xenbus_read_driver_state(const char *path);
10923dc5621SKip Macy 
110ff662b5cSJustin T. Gibbs /**
1118b8bfa35SJustin T. Gibbs  * Return the state of the "other end" (peer) of a XenBus device.
1128b8bfa35SJustin T. Gibbs  *
1138b8bfa35SJustin T. Gibbs  * \param dev   The XenBus device whose peer to query.
1148b8bfa35SJustin T. Gibbs  *
1158b8bfa35SJustin T. Gibbs  * \return  The current state of the peer device or XenbusStateClosed if no
1168b8bfa35SJustin T. Gibbs  *          state can be read.
1178b8bfa35SJustin T. Gibbs  */
1188b8bfa35SJustin T. Gibbs static inline XenbusState
xenbus_get_otherend_state(device_t dev)1198b8bfa35SJustin T. Gibbs xenbus_get_otherend_state(device_t dev)
1208b8bfa35SJustin T. Gibbs {
1218b8bfa35SJustin T. Gibbs 	return (xenbus_read_driver_state(xenbus_get_otherend_path(dev)));
1228b8bfa35SJustin T. Gibbs }
1238b8bfa35SJustin T. Gibbs 
1248b8bfa35SJustin T. Gibbs /**
125ff662b5cSJustin T. Gibbs  * Grant access to the given ring_mfn to the peer of the given device.
126ff662b5cSJustin T. Gibbs  *
127ff662b5cSJustin T. Gibbs  * \param dev        The device granting access to the ring page.
128ff662b5cSJustin T. Gibbs  * \param ring_mfn   The guest machine page number of the page to grant
129ff662b5cSJustin T. Gibbs  *                   peer access rights.
130ff662b5cSJustin T. Gibbs  * \param refp[out]  The grant reference for the page.
131ff662b5cSJustin T. Gibbs  *
132ff662b5cSJustin T. Gibbs  * \return  On success, 0. Otherwise an errno value indicating the
133ff662b5cSJustin T. Gibbs  *          type of failure.
134ff662b5cSJustin T. Gibbs  *
135ff662b5cSJustin T. Gibbs  * A successful call to xenbus_grant_ring should be paired with a call
136ff662b5cSJustin T. Gibbs  * to gnttab_end_foreign_access() when foregn access to this page is no
137ff662b5cSJustin T. Gibbs  * longer requried.
138ff662b5cSJustin T. Gibbs  *
139ff662b5cSJustin T. Gibbs  * \note  On error, \a dev will be switched to the XenbusStateClosing
140ff662b5cSJustin T. Gibbs  *        state and the returned error is saved in the per-device error node
141ff662b5cSJustin T. Gibbs  *        for \a dev in the XenStore.
142ff662b5cSJustin T. Gibbs  */
143ff662b5cSJustin T. Gibbs int xenbus_grant_ring(device_t dev, unsigned long ring_mfn, grant_ref_t *refp);
14423dc5621SKip Macy 
145ff662b5cSJustin T. Gibbs /**
146ff662b5cSJustin T. Gibbs  * Record the given errno, along with the given, printf-style, formatted
147ff662b5cSJustin T. Gibbs  * message in dev's device specific error node in the XenStore.
148ff662b5cSJustin T. Gibbs  *
149ff662b5cSJustin T. Gibbs  * \param dev  The device which encountered the error.
150ff662b5cSJustin T. Gibbs  * \param err  The errno value corresponding to the error.
151ff662b5cSJustin T. Gibbs  * \param fmt  Printf format string followed by a variable number of
152ff662b5cSJustin T. Gibbs  *             printf arguments.
153ff662b5cSJustin T. Gibbs  */
154ff662b5cSJustin T. Gibbs void xenbus_dev_error(device_t dev, int err, const char *fmt, ...)
155ff662b5cSJustin T. Gibbs 	__attribute__((format(printf, 3, 4)));
156ff662b5cSJustin T. Gibbs 
157ff662b5cSJustin T. Gibbs /**
158ff662b5cSJustin T. Gibbs  * va_list version of xenbus_dev_error().
159ff662b5cSJustin T. Gibbs  *
160ff662b5cSJustin T. Gibbs  * \param dev  The device which encountered the error.
161ff662b5cSJustin T. Gibbs  * \param err  The errno value corresponding to the error.
162ff662b5cSJustin T. Gibbs  * \param fmt  Printf format string.
163ff662b5cSJustin T. Gibbs  * \param ap   Va_list of printf arguments.
164ff662b5cSJustin T. Gibbs  */
165ff662b5cSJustin T. Gibbs void xenbus_dev_verror(device_t dev, int err, const char *fmt, va_list ap)
166ff662b5cSJustin T. Gibbs 	__attribute__((format(printf, 3, 0)));
167ff662b5cSJustin T. Gibbs 
168ff662b5cSJustin T. Gibbs /**
169ff662b5cSJustin T. Gibbs  * Equivalent to xenbus_dev_error(), followed by
170ff662b5cSJustin T. Gibbs  * xenbus_set_state(dev, XenbusStateClosing).
171ff662b5cSJustin T. Gibbs  *
172ff662b5cSJustin T. Gibbs  * \param dev  The device which encountered the error.
173ff662b5cSJustin T. Gibbs  * \param err  The errno value corresponding to the error.
174ff662b5cSJustin T. Gibbs  * \param fmt  Printf format string followed by a variable number of
175ff662b5cSJustin T. Gibbs  *             printf arguments.
176ff662b5cSJustin T. Gibbs  */
177ff662b5cSJustin T. Gibbs void xenbus_dev_fatal(device_t dev, int err, const char *fmt, ...)
178ff662b5cSJustin T. Gibbs 	__attribute__((format(printf, 3, 4)));
179ff662b5cSJustin T. Gibbs 
180ff662b5cSJustin T. Gibbs /**
181ff662b5cSJustin T. Gibbs  * va_list version of xenbus_dev_fatal().
182ff662b5cSJustin T. Gibbs  *
183ff662b5cSJustin T. Gibbs  * \param dev  The device which encountered the error.
184ff662b5cSJustin T. Gibbs  * \param err  The errno value corresponding to the error.
185ff662b5cSJustin T. Gibbs  * \param fmt  Printf format string.
186ff662b5cSJustin T. Gibbs  * \param ap   Va_list of printf arguments.
187ff662b5cSJustin T. Gibbs  */
188ff662b5cSJustin T. Gibbs void xenbus_dev_vfatal(device_t dev, int err, const char *fmt, va_list)
189ff662b5cSJustin T. Gibbs 	__attribute__((format(printf, 3, 0)));
190ff662b5cSJustin T. Gibbs 
191ff662b5cSJustin T. Gibbs /**
192ff662b5cSJustin T. Gibbs  * Convert a member of the xenbus_state enum into an ASCII string.
193ff662b5cSJustin T. Gibbs  *
194ff662b5cSJustin T. Gibbs  * /param state  The XenBus state to lookup.
195ff662b5cSJustin T. Gibbs  *
196ff662b5cSJustin T. Gibbs  * /return  A string representing state or, for unrecognized states,
197ff662b5cSJustin T. Gibbs  *	    the string "Unknown".
198ff662b5cSJustin T. Gibbs  */
19923dc5621SKip Macy const char *xenbus_strstate(enum xenbus_state state);
200ff662b5cSJustin T. Gibbs 
201ff662b5cSJustin T. Gibbs /**
202ff662b5cSJustin T. Gibbs  * Return the value of a XenBus device's "online" node within the XenStore.
203ff662b5cSJustin T. Gibbs  *
204ff662b5cSJustin T. Gibbs  * \param dev  The XenBus device to query.
205ff662b5cSJustin T. Gibbs  *
206ff662b5cSJustin T. Gibbs  * \return  The value of the "online" node for the device.  If the node
207ff662b5cSJustin T. Gibbs  *          does not exist, 0 (offline) is returned.
208ff662b5cSJustin T. Gibbs  */
20923dc5621SKip Macy int xenbus_dev_is_online(device_t dev);
21023dc5621SKip Macy 
211283d6f72SJustin T. Gibbs /**
212283d6f72SJustin T. Gibbs  * Default callback invoked when a change to the local XenStore sub-tree
213283d6f72SJustin T. Gibbs  * for a device is modified.
214283d6f72SJustin T. Gibbs  *
215283d6f72SJustin T. Gibbs  * \param dev   The XenBus device whose tree was modified.
216283d6f72SJustin T. Gibbs  * \param path  The tree relative sub-path to the modified node.  The empty
217283d6f72SJustin T. Gibbs  *              string indicates the root of the tree was destroyed.
218283d6f72SJustin T. Gibbs  */
219283d6f72SJustin T. Gibbs void xenbus_localend_changed(device_t dev, const char *path);
220283d6f72SJustin T. Gibbs 
221283d6f72SJustin T. Gibbs #include "xenbus_if.h"
222283d6f72SJustin T. Gibbs 
22323dc5621SKip Macy #endif /* _XEN_XENBUS_XENBUSVAR_H */
224