]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/xen/xenbus/xenbusvar.h
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / xen / xenbus / xenbusvar.h
1 /******************************************************************************
2  * Copyright (C) 2005 Rusty Russell, IBM Corporation
3  * Copyright (C) 2005 XenSource Ltd.
4  * 
5  * This file may be distributed separately from the Linux kernel, or
6  * incorporated into other software packages, subject to the following license:
7  * 
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this source file (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
12  * and to permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  * 
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24  * IN THE SOFTWARE.
25  *
26  * $FreeBSD$
27  */
28
29 /**
30  * \file xenbusvar.h
31  *
32  * \brief Datastructures and function declarations for usedby device
33  *        drivers operating on the XenBus.
34  */
35
36 #ifndef _XEN_XENBUS_XENBUSVAR_H
37 #define _XEN_XENBUS_XENBUSVAR_H
38
39 #include <sys/queue.h>
40 #include <sys/bus.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
43 #include <sys/sbuf.h>
44
45 #include <machine/stdarg.h>
46 #include <machine/xen/xen-os.h>
47
48 #include <xen/interface/grant_table.h>
49 #include <xen/interface/io/xenbus.h>
50 #include <xen/interface/io/xs_wire.h>
51
52 #include <xen/xenstore/xenstorevar.h>
53
54 /* XenBus allocations including XenStore data returned to clients. */
55 MALLOC_DECLARE(M_XENBUS);
56
57 enum {
58         /**
59          * Path of this device node.
60          */
61         XENBUS_IVAR_NODE,
62
63         /**
64          * The device type (e.g. vif, vbd).
65          */
66         XENBUS_IVAR_TYPE,
67
68         /**
69          * The state of this device (not the otherend's state).
70          */
71         XENBUS_IVAR_STATE,
72
73         /**
74          * Domain ID of the other end device.
75          */
76         XENBUS_IVAR_OTHEREND_ID,
77
78         /**
79          * Path of the other end device.
80          */
81         XENBUS_IVAR_OTHEREND_PATH
82 };
83
84 /**
85  * Simplified accessors for xenbus devices
86  */
87 #define XENBUS_ACCESSOR(var, ivar, type) \
88         __BUS_ACCESSOR(xenbus, var, XENBUS, ivar, type)
89
90 XENBUS_ACCESSOR(node,           NODE,                   const char *)
91 XENBUS_ACCESSOR(type,           TYPE,                   const char *)
92 XENBUS_ACCESSOR(state,          STATE,                  enum xenbus_state)
93 XENBUS_ACCESSOR(otherend_id,    OTHEREND_ID,            int)
94 XENBUS_ACCESSOR(otherend_path,  OTHEREND_PATH,          const char *)
95
96 /**
97  * Return the state of a XenBus device.
98  *
99  * \param path  The root XenStore path for the device.
100  *
101  * \return  The current state of the device or XenbusStateClosed if no
102  *          state can be read.
103  */
104 XenbusState xenbus_read_driver_state(const char *path);
105
106 /**
107  * Initialize and register a watch on the given path (client suplied storage).
108  *
109  * \param dev       The XenBus device requesting the watch service.
110  * \param path      The XenStore path of the object to be watched.  The
111  *                  storage for this string must be stable for the lifetime
112  *                  of the watch.
113  * \param watch     The watch object to use for this request.  This object
114  *                  must be stable for the lifetime of the watch.
115  * \param callback  The function to call when XenStore objects at or below
116  *                  path are modified.
117  * \param cb_data   Client data that can be retrieved from the watch object
118  *                  during the callback.
119  *
120  * \return  On success, 0. Otherwise an errno value indicating the
121  *          type of failure.
122  *
123  * \note  On error, the device 'dev' will be switched to the XenbusStateClosing
124  *        state and the returned error is saved in the per-device error node
125  *        for dev in the XenStore.
126  */
127 int xenbus_watch_path(device_t dev, char *path,
128                       struct xs_watch *watch, 
129                       xs_watch_cb_t *callback,
130                       uintptr_t cb_data);
131
132 /**
133  * Initialize and register a watch at path/path2 in the XenStore.
134  *
135  * \param dev       The XenBus device requesting the watch service.
136  * \param path      The base XenStore path of the object to be watched.
137  * \param path2     The tail XenStore path of the object to be watched.
138  * \param watch     The watch object to use for this request.  This object
139  *                  must be stable for the lifetime of the watch.
140  * \param callback  The function to call when XenStore objects at or below
141  *                  path are modified.
142  * \param cb_data   Client data that can be retrieved from the watch object
143  *                  during the callback.
144  *
145  * \return  On success, 0. Otherwise an errno value indicating the
146  *          type of failure.
147  *
148  * \note  On error, \a dev will be switched to the XenbusStateClosing
149  *        state and the returned error is saved in the per-device error node
150  *        for \a dev in the XenStore.
151  *
152  * Similar to xenbus_watch_path, however the storage for the path to the
153  * watched object is allocated from the heap and filled with "path '/' path2".
154  * Should a call to this function succeed, it is the callers responsibility
155  * to free watch->node using the M_XENBUS malloc type.
156  */
157 int xenbus_watch_path2(device_t dev, const char *path,
158                        const char *path2, struct xs_watch *watch, 
159                        xs_watch_cb_t *callback,
160                        uintptr_t cb_data);
161
162 /**
163  * Grant access to the given ring_mfn to the peer of the given device.
164  *
165  * \param dev        The device granting access to the ring page.
166  * \param ring_mfn   The guest machine page number of the page to grant
167  *                   peer access rights.
168  * \param refp[out]  The grant reference for the page.
169  *
170  * \return  On success, 0. Otherwise an errno value indicating the
171  *          type of failure.
172  *
173  * A successful call to xenbus_grant_ring should be paired with a call
174  * to gnttab_end_foreign_access() when foregn access to this page is no
175  * longer requried.
176  * 
177  * \note  On error, \a dev will be switched to the XenbusStateClosing
178  *        state and the returned error is saved in the per-device error node
179  *        for \a dev in the XenStore.
180  */
181 int xenbus_grant_ring(device_t dev, unsigned long ring_mfn, grant_ref_t *refp);
182
183 /**
184  * Allocate an event channel for the given XenBus device.
185  *
186  * \param dev        The device for which to allocate the event channel.
187  * \param port[out]  The port identifier for the allocated event channel.
188  *
189  * \return  On success, 0. Otherwise an errno value indicating the
190  *          type of failure.
191  *
192  * A successfully allocated event channel should be free'd using
193  * xenbus_free_evtchn().
194  *
195  * \note  On error, \a dev will be switched to the XenbusStateClosing
196  *        state and the returned error is saved in the per-device error node
197  *        for \a dev in the XenStore.
198  */
199 int xenbus_alloc_evtchn(device_t dev, evtchn_port_t *port);
200
201 /**
202  * Free an existing event channel.
203  *
204  * \param dev   The device which allocated this event channel.
205  * \param port  The port identifier for the event channel to free.
206  *
207  * \return  On success, 0. Otherwise an errno value indicating the
208  *          type of failure.
209  *
210  * \note  On error, \a dev will be switched to the XenbusStateClosing
211  *        state and the returned error is saved in the per-device error node
212  *        for \a dev in the XenStore.
213  */
214 int xenbus_free_evtchn(device_t dev, evtchn_port_t port);
215
216 /**
217  * Record the given errno, along with the given, printf-style, formatted
218  * message in dev's device specific error node in the XenStore.
219  *
220  * \param dev  The device which encountered the error.
221  * \param err  The errno value corresponding to the error.
222  * \param fmt  Printf format string followed by a variable number of
223  *             printf arguments.
224  */
225 void xenbus_dev_error(device_t dev, int err, const char *fmt, ...)
226         __attribute__((format(printf, 3, 4)));
227
228 /**
229  * va_list version of xenbus_dev_error().
230  *
231  * \param dev  The device which encountered the error.
232  * \param err  The errno value corresponding to the error.
233  * \param fmt  Printf format string.
234  * \param ap   Va_list of printf arguments.
235  */
236 void xenbus_dev_verror(device_t dev, int err, const char *fmt, va_list ap)
237         __attribute__((format(printf, 3, 0)));
238
239 /**
240  * Equivalent to xenbus_dev_error(), followed by
241  * xenbus_set_state(dev, XenbusStateClosing).
242  *
243  * \param dev  The device which encountered the error.
244  * \param err  The errno value corresponding to the error.
245  * \param fmt  Printf format string followed by a variable number of
246  *             printf arguments.
247  */
248 void xenbus_dev_fatal(device_t dev, int err, const char *fmt, ...)
249         __attribute__((format(printf, 3, 4)));
250
251 /**
252  * va_list version of xenbus_dev_fatal().
253  *
254  * \param dev  The device which encountered the error.
255  * \param err  The errno value corresponding to the error.
256  * \param fmt  Printf format string.
257  * \param ap   Va_list of printf arguments.
258  */
259 void xenbus_dev_vfatal(device_t dev, int err, const char *fmt, va_list)
260         __attribute__((format(printf, 3, 0)));
261
262 /**
263  * Convert a member of the xenbus_state enum into an ASCII string.
264  *
265  * /param state  The XenBus state to lookup.
266  *
267  * /return  A string representing state or, for unrecognized states,
268  *          the string "Unknown".
269  */
270 const char *xenbus_strstate(enum xenbus_state state);
271
272 /**
273  * Return the value of a XenBus device's "online" node within the XenStore.
274  *
275  * \param dev  The XenBus device to query.
276  *
277  * \return  The value of the "online" node for the device.  If the node
278  *          does not exist, 0 (offline) is returned.
279  */
280 int xenbus_dev_is_online(device_t dev);
281
282 /**
283  * Default callback invoked when a change to the local XenStore sub-tree
284  * for a device is modified.
285  * 
286  * \param dev   The XenBus device whose tree was modified.
287  * \param path  The tree relative sub-path to the modified node.  The empty
288  *              string indicates the root of the tree was destroyed.
289  */
290 void xenbus_localend_changed(device_t dev, const char *path);
291
292 #include "xenbus_if.h"
293
294 #endif /* _XEN_XENBUS_XENBUSVAR_H */