xenbusvar.h revision 231743
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 231743 2012-02-15 06:45:49Z 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>
46185605Skmacy#include <machine/xen/xen-os.h>
47214077Sgibbs
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 * Allocate an event channel for the given XenBus device.
199214077Sgibbs *
200214077Sgibbs * \param dev        The device for which to allocate the event channel.
201214077Sgibbs * \param port[out]  The port identifier for the allocated event channel.
202214077Sgibbs *
203214077Sgibbs * \return  On success, 0. Otherwise an errno value indicating the
204214077Sgibbs *          type of failure.
205214077Sgibbs *
206214077Sgibbs * A successfully allocated event channel should be free'd using
207214077Sgibbs * xenbus_free_evtchn().
208214077Sgibbs *
209214077Sgibbs * \note  On error, \a dev will be switched to the XenbusStateClosing
210214077Sgibbs *        state and the returned error is saved in the per-device error node
211214077Sgibbs *        for \a dev in the XenStore.
212185605Skmacy */
213214077Sgibbsint xenbus_alloc_evtchn(device_t dev, evtchn_port_t *port);
214185605Skmacy
215214077Sgibbs/**
216214077Sgibbs * Free an existing event channel.
217214077Sgibbs *
218214077Sgibbs * \param dev   The device which allocated this event channel.
219214077Sgibbs * \param port  The port identifier for the event channel to free.
220214077Sgibbs *
221214077Sgibbs * \return  On success, 0. Otherwise an errno value indicating the
222214077Sgibbs *          type of failure.
223214077Sgibbs *
224214077Sgibbs * \note  On error, \a dev will be switched to the XenbusStateClosing
225214077Sgibbs *        state and the returned error is saved in the per-device error node
226214077Sgibbs *        for \a dev in the XenStore.
227214077Sgibbs */
228214077Sgibbsint xenbus_free_evtchn(device_t dev, evtchn_port_t port);
229185605Skmacy
230185605Skmacy/**
231214077Sgibbs * Record the given errno, along with the given, printf-style, formatted
232214077Sgibbs * message in dev's device specific error node in the XenStore.
233214077Sgibbs *
234214077Sgibbs * \param dev  The device which encountered the error.
235214077Sgibbs * \param err  The errno value corresponding to the error.
236214077Sgibbs * \param fmt  Printf format string followed by a variable number of
237214077Sgibbs *             printf arguments.
238185605Skmacy */
239214077Sgibbsvoid xenbus_dev_error(device_t dev, int err, const char *fmt, ...)
240214077Sgibbs	__attribute__((format(printf, 3, 4)));
241185605Skmacy
242185605Skmacy/**
243214077Sgibbs * va_list version of xenbus_dev_error().
244214077Sgibbs *
245214077Sgibbs * \param dev  The device which encountered the error.
246214077Sgibbs * \param err  The errno value corresponding to the error.
247214077Sgibbs * \param fmt  Printf format string.
248214077Sgibbs * \param ap   Va_list of printf arguments.
249185605Skmacy */
250214077Sgibbsvoid xenbus_dev_verror(device_t dev, int err, const char *fmt, va_list ap)
251214077Sgibbs	__attribute__((format(printf, 3, 0)));
252185605Skmacy
253185605Skmacy/**
254214077Sgibbs * Equivalent to xenbus_dev_error(), followed by
255214077Sgibbs * xenbus_set_state(dev, XenbusStateClosing).
256214077Sgibbs *
257214077Sgibbs * \param dev  The device which encountered the error.
258214077Sgibbs * \param err  The errno value corresponding to the error.
259214077Sgibbs * \param fmt  Printf format string followed by a variable number of
260214077Sgibbs *             printf arguments.
261185605Skmacy */
262214077Sgibbsvoid xenbus_dev_fatal(device_t dev, int err, const char *fmt, ...)
263214077Sgibbs	__attribute__((format(printf, 3, 4)));
264185605Skmacy
265185605Skmacy/**
266214077Sgibbs * va_list version of xenbus_dev_fatal().
267214077Sgibbs *
268214077Sgibbs * \param dev  The device which encountered the error.
269214077Sgibbs * \param err  The errno value corresponding to the error.
270214077Sgibbs * \param fmt  Printf format string.
271214077Sgibbs * \param ap   Va_list of printf arguments.
272185605Skmacy */
273214077Sgibbsvoid xenbus_dev_vfatal(device_t dev, int err, const char *fmt, va_list)
274214077Sgibbs	__attribute__((format(printf, 3, 0)));
275185605Skmacy
276214077Sgibbs/**
277214077Sgibbs * Convert a member of the xenbus_state enum into an ASCII string.
278214077Sgibbs *
279214077Sgibbs * /param state  The XenBus state to lookup.
280214077Sgibbs *
281214077Sgibbs * /return  A string representing state or, for unrecognized states,
282214077Sgibbs *	    the string "Unknown".
283185605Skmacy */
284214077Sgibbsconst char *xenbus_strstate(enum xenbus_state state);
285185605Skmacy
286214077Sgibbs/**
287214077Sgibbs * Return the value of a XenBus device's "online" node within the XenStore.
288214077Sgibbs *
289214077Sgibbs * \param dev  The XenBus device to query.
290214077Sgibbs *
291214077Sgibbs * \return  The value of the "online" node for the device.  If the node
292214077Sgibbs *          does not exist, 0 (offline) is returned.
293185605Skmacy */
294185605Skmacyint xenbus_dev_is_online(device_t dev);
295185605Skmacy
296222975Sgibbs/**
297222975Sgibbs * Default callback invoked when a change to the local XenStore sub-tree
298222975Sgibbs * for a device is modified.
299222975Sgibbs *
300222975Sgibbs * \param dev   The XenBus device whose tree was modified.
301222975Sgibbs * \param path  The tree relative sub-path to the modified node.  The empty
302222975Sgibbs *              string indicates the root of the tree was destroyed.
303222975Sgibbs */
304222975Sgibbsvoid xenbus_localend_changed(device_t dev, const char *path);
305222975Sgibbs
306222975Sgibbs#include "xenbus_if.h"
307222975Sgibbs
308185605Skmacy#endif /* _XEN_XENBUS_XENBUSVAR_H */
309