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: releng/11.0/sys/xen/xenbus/xenbusvar.h 294090 2016-01-15 14:34:31Z royger $
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/**
85294090Sroyger * Simplified accessors for xenbus devices:
86294090Sroyger *
87294090Sroyger * xenbus_get_node
88294090Sroyger * xenbus_get_type
89294090Sroyger * xenbus_get_state
90294090Sroyger * xenbus_get_otherend_id
91294090Sroyger * xenbus_get_otherend_path
92185605Skmacy */
93185605Skmacy#define	XENBUS_ACCESSOR(var, ivar, type) \
94185605Skmacy	__BUS_ACCESSOR(xenbus, var, XENBUS, ivar, type)
95185605Skmacy
96185605SkmacyXENBUS_ACCESSOR(node,		NODE,			const char *)
97185605SkmacyXENBUS_ACCESSOR(type,		TYPE,			const char *)
98185605SkmacyXENBUS_ACCESSOR(state,		STATE,			enum xenbus_state)
99185605SkmacyXENBUS_ACCESSOR(otherend_id,	OTHEREND_ID,		int)
100185605SkmacyXENBUS_ACCESSOR(otherend_path,	OTHEREND_PATH,		const char *)
101185605Skmacy
102214077Sgibbs/**
103214077Sgibbs * Return the state of a XenBus device.
104214077Sgibbs *
105214077Sgibbs * \param path  The root XenStore path for the device.
106214077Sgibbs *
107214077Sgibbs * \return  The current state of the device or XenbusStateClosed if no
108214077Sgibbs *	    state can be read.
109186557Skmacy */
110214077SgibbsXenbusState xenbus_read_driver_state(const char *path);
111185605Skmacy
112185605Skmacy/**
113231743Sgibbs * Return the state of the "other end" (peer) of a XenBus device.
114231743Sgibbs *
115231743Sgibbs * \param dev   The XenBus device whose peer to query.
116231743Sgibbs *
117231743Sgibbs * \return  The current state of the peer device or XenbusStateClosed if no
118231743Sgibbs *          state can be read.
119231743Sgibbs */
120231743Sgibbsstatic inline XenbusState
121231743Sgibbsxenbus_get_otherend_state(device_t dev)
122231743Sgibbs{
123231743Sgibbs	return (xenbus_read_driver_state(xenbus_get_otherend_path(dev)));
124231743Sgibbs}
125231743Sgibbs
126231743Sgibbs/**
127214077Sgibbs * Initialize and register a watch on the given path (client suplied storage).
128214077Sgibbs *
129214077Sgibbs * \param dev       The XenBus device requesting the watch service.
130214077Sgibbs * \param path      The XenStore path of the object to be watched.  The
131214077Sgibbs *                  storage for this string must be stable for the lifetime
132214077Sgibbs *                  of the watch.
133214077Sgibbs * \param watch     The watch object to use for this request.  This object
134214077Sgibbs *                  must be stable for the lifetime of the watch.
135214077Sgibbs * \param callback  The function to call when XenStore objects at or below
136214077Sgibbs *                  path are modified.
137222975Sgibbs * \param cb_data   Client data that can be retrieved from the watch object
138222975Sgibbs *                  during the callback.
139214077Sgibbs *
140214077Sgibbs * \return  On success, 0. Otherwise an errno value indicating the
141214077Sgibbs *          type of failure.
142214077Sgibbs *
143214077Sgibbs * \note  On error, the device 'dev' will be switched to the XenbusStateClosing
144214077Sgibbs *        state and the returned error is saved in the per-device error node
145214077Sgibbs *        for dev in the XenStore.
146185605Skmacy */
147185605Skmacyint xenbus_watch_path(device_t dev, char *path,
148214077Sgibbs		      struct xs_watch *watch,
149222975Sgibbs		      xs_watch_cb_t *callback,
150222975Sgibbs		      uintptr_t cb_data);
151185605Skmacy
152185605Skmacy/**
153214077Sgibbs * Initialize and register a watch at path/path2 in the XenStore.
154214077Sgibbs *
155214077Sgibbs * \param dev       The XenBus device requesting the watch service.
156214077Sgibbs * \param path      The base XenStore path of the object to be watched.
157214077Sgibbs * \param path2     The tail XenStore path of the object to be watched.
158214077Sgibbs * \param watch     The watch object to use for this request.  This object
159214077Sgibbs *                  must be stable for the lifetime of the watch.
160214077Sgibbs * \param callback  The function to call when XenStore objects at or below
161214077Sgibbs *                  path are modified.
162222975Sgibbs * \param cb_data   Client data that can be retrieved from the watch object
163222975Sgibbs *                  during the callback.
164214077Sgibbs *
165214077Sgibbs * \return  On success, 0. Otherwise an errno value indicating the
166214077Sgibbs *          type of failure.
167214077Sgibbs *
168214077Sgibbs * \note  On error, \a dev will be switched to the XenbusStateClosing
169214077Sgibbs *        state and the returned error is saved in the per-device error node
170214077Sgibbs *        for \a dev in the XenStore.
171214077Sgibbs *
172214077Sgibbs * Similar to xenbus_watch_path, however the storage for the path to the
173214077Sgibbs * watched object is allocated from the heap and filled with "path '/' path2".
174214077Sgibbs * Should a call to this function succeed, it is the callers responsibility
175214077Sgibbs * to free watch->node using the M_XENBUS malloc type.
176185605Skmacy */
177185605Skmacyint xenbus_watch_path2(device_t dev, const char *path,
178214077Sgibbs		       const char *path2, struct xs_watch *watch,
179222975Sgibbs		       xs_watch_cb_t *callback,
180222975Sgibbs		       uintptr_t cb_data);
181185605Skmacy
182214077Sgibbs/**
183214077Sgibbs * Grant access to the given ring_mfn to the peer of the given device.
184214077Sgibbs *
185214077Sgibbs * \param dev        The device granting access to the ring page.
186214077Sgibbs * \param ring_mfn   The guest machine page number of the page to grant
187214077Sgibbs *                   peer access rights.
188214077Sgibbs * \param refp[out]  The grant reference for the page.
189214077Sgibbs *
190214077Sgibbs * \return  On success, 0. Otherwise an errno value indicating the
191214077Sgibbs *          type of failure.
192214077Sgibbs *
193214077Sgibbs * A successful call to xenbus_grant_ring should be paired with a call
194214077Sgibbs * to gnttab_end_foreign_access() when foregn access to this page is no
195214077Sgibbs * longer requried.
196214077Sgibbs *
197214077Sgibbs * \note  On error, \a dev will be switched to the XenbusStateClosing
198214077Sgibbs *        state and the returned error is saved in the per-device error node
199214077Sgibbs *        for \a dev in the XenStore.
200214077Sgibbs */
201214077Sgibbsint xenbus_grant_ring(device_t dev, unsigned long ring_mfn, grant_ref_t *refp);
202185605Skmacy
203185605Skmacy/**
204214077Sgibbs * Record the given errno, along with the given, printf-style, formatted
205214077Sgibbs * message in dev's device specific error node in the XenStore.
206214077Sgibbs *
207214077Sgibbs * \param dev  The device which encountered the error.
208214077Sgibbs * \param err  The errno value corresponding to the error.
209214077Sgibbs * \param fmt  Printf format string followed by a variable number of
210214077Sgibbs *             printf arguments.
211185605Skmacy */
212214077Sgibbsvoid xenbus_dev_error(device_t dev, int err, const char *fmt, ...)
213214077Sgibbs	__attribute__((format(printf, 3, 4)));
214185605Skmacy
215185605Skmacy/**
216214077Sgibbs * va_list version of xenbus_dev_error().
217214077Sgibbs *
218214077Sgibbs * \param dev  The device which encountered the error.
219214077Sgibbs * \param err  The errno value corresponding to the error.
220214077Sgibbs * \param fmt  Printf format string.
221214077Sgibbs * \param ap   Va_list of printf arguments.
222185605Skmacy */
223214077Sgibbsvoid xenbus_dev_verror(device_t dev, int err, const char *fmt, va_list ap)
224214077Sgibbs	__attribute__((format(printf, 3, 0)));
225185605Skmacy
226185605Skmacy/**
227214077Sgibbs * Equivalent to xenbus_dev_error(), followed by
228214077Sgibbs * xenbus_set_state(dev, XenbusStateClosing).
229214077Sgibbs *
230214077Sgibbs * \param dev  The device which encountered the error.
231214077Sgibbs * \param err  The errno value corresponding to the error.
232214077Sgibbs * \param fmt  Printf format string followed by a variable number of
233214077Sgibbs *             printf arguments.
234185605Skmacy */
235214077Sgibbsvoid xenbus_dev_fatal(device_t dev, int err, const char *fmt, ...)
236214077Sgibbs	__attribute__((format(printf, 3, 4)));
237185605Skmacy
238185605Skmacy/**
239214077Sgibbs * va_list version of xenbus_dev_fatal().
240214077Sgibbs *
241214077Sgibbs * \param dev  The device which encountered the error.
242214077Sgibbs * \param err  The errno value corresponding to the error.
243214077Sgibbs * \param fmt  Printf format string.
244214077Sgibbs * \param ap   Va_list of printf arguments.
245185605Skmacy */
246214077Sgibbsvoid xenbus_dev_vfatal(device_t dev, int err, const char *fmt, va_list)
247214077Sgibbs	__attribute__((format(printf, 3, 0)));
248185605Skmacy
249214077Sgibbs/**
250214077Sgibbs * Convert a member of the xenbus_state enum into an ASCII string.
251214077Sgibbs *
252214077Sgibbs * /param state  The XenBus state to lookup.
253214077Sgibbs *
254214077Sgibbs * /return  A string representing state or, for unrecognized states,
255214077Sgibbs *	    the string "Unknown".
256185605Skmacy */
257214077Sgibbsconst char *xenbus_strstate(enum xenbus_state state);
258185605Skmacy
259214077Sgibbs/**
260214077Sgibbs * Return the value of a XenBus device's "online" node within the XenStore.
261214077Sgibbs *
262214077Sgibbs * \param dev  The XenBus device to query.
263214077Sgibbs *
264214077Sgibbs * \return  The value of the "online" node for the device.  If the node
265214077Sgibbs *          does not exist, 0 (offline) is returned.
266185605Skmacy */
267185605Skmacyint xenbus_dev_is_online(device_t dev);
268185605Skmacy
269222975Sgibbs/**
270222975Sgibbs * Default callback invoked when a change to the local XenStore sub-tree
271222975Sgibbs * for a device is modified.
272222975Sgibbs *
273222975Sgibbs * \param dev   The XenBus device whose tree was modified.
274222975Sgibbs * \param path  The tree relative sub-path to the modified node.  The empty
275222975Sgibbs *              string indicates the root of the tree was destroyed.
276222975Sgibbs */
277222975Sgibbsvoid xenbus_localend_changed(device_t dev, const char *path);
278222975Sgibbs
279222975Sgibbs#include "xenbus_if.h"
280222975Sgibbs
281185605Skmacy#endif /* _XEN_XENBUS_XENBUSVAR_H */
282