xref: /freebsd/sys/xen/xenbus/xenbusvar.h (revision 74d9553e43cfafc29448d0bb836916aa21dea0de)
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 
47 #include <xen/xen-os.h>
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  * xenbus_get_node
88  * xenbus_get_type
89  * xenbus_get_state
90  * xenbus_get_otherend_id
91  * xenbus_get_otherend_path
92  */
93 #define	XENBUS_ACCESSOR(var, ivar, type) \
94 	__BUS_ACCESSOR(xenbus, var, XENBUS, ivar, type)
95 
96 XENBUS_ACCESSOR(node,		NODE,			const char *)
97 XENBUS_ACCESSOR(type,		TYPE,			const char *)
98 XENBUS_ACCESSOR(state,		STATE,			enum xenbus_state)
99 XENBUS_ACCESSOR(otherend_id,	OTHEREND_ID,		int)
100 XENBUS_ACCESSOR(otherend_path,	OTHEREND_PATH,		const char *)
101 
102 /**
103  * Return the state of a XenBus device.
104  *
105  * \param path  The root XenStore path for the device.
106  *
107  * \return  The current state of the device or XenbusStateClosed if no
108  *	    state can be read.
109  */
110 XenbusState xenbus_read_driver_state(const char *path);
111 
112 /**
113  * Return the state of the "other end" (peer) of a XenBus device.
114  *
115  * \param dev   The XenBus device whose peer to query.
116  *
117  * \return  The current state of the peer device or XenbusStateClosed if no
118  *          state can be read.
119  */
120 static inline XenbusState
121 xenbus_get_otherend_state(device_t dev)
122 {
123 	return (xenbus_read_driver_state(xenbus_get_otherend_path(dev)));
124 }
125 
126 /**
127  * Initialize and register a watch on the given path (client suplied storage).
128  *
129  * \param dev       The XenBus device requesting the watch service.
130  * \param path      The XenStore path of the object to be watched.  The
131  *                  storage for this string must be stable for the lifetime
132  *                  of the watch.
133  * \param watch     The watch object to use for this request.  This object
134  *                  must be stable for the lifetime of the watch.
135  * \param callback  The function to call when XenStore objects at or below
136  *                  path are modified.
137  * \param cb_data   Client data that can be retrieved from the watch object
138  *                  during the callback.
139  *
140  * \return  On success, 0. Otherwise an errno value indicating the
141  *          type of failure.
142  *
143  * \note  On error, the device 'dev' will be switched to the XenbusStateClosing
144  *        state and the returned error is saved in the per-device error node
145  *        for dev in the XenStore.
146  */
147 int xenbus_watch_path(device_t dev, char *path,
148 		      struct xs_watch *watch,
149 		      xs_watch_cb_t *callback,
150 		      uintptr_t cb_data);
151 
152 /**
153  * Initialize and register a watch at path/path2 in the XenStore.
154  *
155  * \param dev       The XenBus device requesting the watch service.
156  * \param path      The base XenStore path of the object to be watched.
157  * \param path2     The tail XenStore path of the object to be watched.
158  * \param watch     The watch object to use for this request.  This object
159  *                  must be stable for the lifetime of the watch.
160  * \param callback  The function to call when XenStore objects at or below
161  *                  path are modified.
162  * \param cb_data   Client data that can be retrieved from the watch object
163  *                  during the callback.
164  *
165  * \return  On success, 0. Otherwise an errno value indicating the
166  *          type of failure.
167  *
168  * \note  On error, \a dev will be switched to the XenbusStateClosing
169  *        state and the returned error is saved in the per-device error node
170  *        for \a dev in the XenStore.
171  *
172  * Similar to xenbus_watch_path, however the storage for the path to the
173  * watched object is allocated from the heap and filled with "path '/' path2".
174  * Should a call to this function succeed, it is the callers responsibility
175  * to free watch->node using the M_XENBUS malloc type.
176  */
177 int xenbus_watch_path2(device_t dev, const char *path,
178 		       const char *path2, struct xs_watch *watch,
179 		       xs_watch_cb_t *callback,
180 		       uintptr_t cb_data);
181 
182 /**
183  * Grant access to the given ring_mfn to the peer of the given device.
184  *
185  * \param dev        The device granting access to the ring page.
186  * \param ring_mfn   The guest machine page number of the page to grant
187  *                   peer access rights.
188  * \param refp[out]  The grant reference for the page.
189  *
190  * \return  On success, 0. Otherwise an errno value indicating the
191  *          type of failure.
192  *
193  * A successful call to xenbus_grant_ring should be paired with a call
194  * to gnttab_end_foreign_access() when foregn access to this page is no
195  * longer requried.
196  *
197  * \note  On error, \a dev will be switched to the XenbusStateClosing
198  *        state and the returned error is saved in the per-device error node
199  *        for \a dev in the XenStore.
200  */
201 int xenbus_grant_ring(device_t dev, unsigned long ring_mfn, grant_ref_t *refp);
202 
203 /**
204  * Record the given errno, along with the given, printf-style, formatted
205  * message in dev's device specific error node in the XenStore.
206  *
207  * \param dev  The device which encountered the error.
208  * \param err  The errno value corresponding to the error.
209  * \param fmt  Printf format string followed by a variable number of
210  *             printf arguments.
211  */
212 void xenbus_dev_error(device_t dev, int err, const char *fmt, ...)
213 	__attribute__((format(printf, 3, 4)));
214 
215 /**
216  * va_list version of xenbus_dev_error().
217  *
218  * \param dev  The device which encountered the error.
219  * \param err  The errno value corresponding to the error.
220  * \param fmt  Printf format string.
221  * \param ap   Va_list of printf arguments.
222  */
223 void xenbus_dev_verror(device_t dev, int err, const char *fmt, va_list ap)
224 	__attribute__((format(printf, 3, 0)));
225 
226 /**
227  * Equivalent to xenbus_dev_error(), followed by
228  * xenbus_set_state(dev, XenbusStateClosing).
229  *
230  * \param dev  The device which encountered the error.
231  * \param err  The errno value corresponding to the error.
232  * \param fmt  Printf format string followed by a variable number of
233  *             printf arguments.
234  */
235 void xenbus_dev_fatal(device_t dev, int err, const char *fmt, ...)
236 	__attribute__((format(printf, 3, 4)));
237 
238 /**
239  * va_list version of xenbus_dev_fatal().
240  *
241  * \param dev  The device which encountered the error.
242  * \param err  The errno value corresponding to the error.
243  * \param fmt  Printf format string.
244  * \param ap   Va_list of printf arguments.
245  */
246 void xenbus_dev_vfatal(device_t dev, int err, const char *fmt, va_list)
247 	__attribute__((format(printf, 3, 0)));
248 
249 /**
250  * Convert a member of the xenbus_state enum into an ASCII string.
251  *
252  * /param state  The XenBus state to lookup.
253  *
254  * /return  A string representing state or, for unrecognized states,
255  *	    the string "Unknown".
256  */
257 const char *xenbus_strstate(enum xenbus_state state);
258 
259 /**
260  * Return the value of a XenBus device's "online" node within the XenStore.
261  *
262  * \param dev  The XenBus device to query.
263  *
264  * \return  The value of the "online" node for the device.  If the node
265  *          does not exist, 0 (offline) is returned.
266  */
267 int xenbus_dev_is_online(device_t dev);
268 
269 /**
270  * Default callback invoked when a change to the local XenStore sub-tree
271  * for a device is modified.
272  *
273  * \param dev   The XenBus device whose tree was modified.
274  * \param path  The tree relative sub-path to the modified node.  The empty
275  *              string indicates the root of the tree was destroyed.
276  */
277 void xenbus_localend_changed(device_t dev, const char *path);
278 
279 #include "xenbus_if.h"
280 
281 #endif /* _XEN_XENBUS_XENBUSVAR_H */
282