xref: /freebsd/sys/xen/xenbus/xenbusvar.h (revision e453e498cbb88570a3ff7b3679de65c88707da95)
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 
27 /**
28  * \file xenbusvar.h
29  *
30  * \brief Datastructures and function declarations for usedby device
31  *        drivers operating on the XenBus.
32  */
33 
34 #ifndef _XEN_XENBUS_XENBUSVAR_H
35 #define _XEN_XENBUS_XENBUSVAR_H
36 
37 #include <sys/queue.h>
38 #include <sys/bus.h>
39 #include <sys/eventhandler.h>
40 #include <sys/malloc.h>
41 #include <sys/sbuf.h>
42 #include <sys/stdarg.h>
43 
44 #include <xen/xen-os.h>
45 #include <contrib/xen/grant_table.h>
46 #include <contrib/xen/io/xenbus.h>
47 #include <contrib/xen/io/xs_wire.h>
48 
49 #include <xen/xenstore/xenstorevar.h>
50 
51 /* XenBus allocations including XenStore data returned to clients. */
52 MALLOC_DECLARE(M_XENBUS);
53 
54 enum {
55 	/**
56 	 * Path of this device node.
57 	 */
58 	XENBUS_IVAR_NODE,
59 
60 	/**
61 	 * The device type (e.g. vif, vbd).
62 	 */
63 	XENBUS_IVAR_TYPE,
64 
65 	/**
66 	 * The state of this device (not the otherend's state).
67 	 */
68 	XENBUS_IVAR_STATE,
69 
70 	/**
71 	 * Domain ID of the other end device.
72 	 */
73 	XENBUS_IVAR_OTHEREND_ID,
74 
75 	/**
76 	 * Path of the other end device.
77 	 */
78 	XENBUS_IVAR_OTHEREND_PATH
79 };
80 
81 /**
82  * Simplified accessors for xenbus devices:
83  *
84  * xenbus_get_node
85  * xenbus_get_type
86  * xenbus_get_state
87  * xenbus_get_otherend_id
88  * xenbus_get_otherend_path
89  */
90 #define	XENBUS_ACCESSOR(var, ivar, type) \
91 	__BUS_ACCESSOR(xenbus, var, XENBUS, ivar, type)
92 
93 XENBUS_ACCESSOR(node,		NODE,			const char *)
94 XENBUS_ACCESSOR(type,		TYPE,			const char *)
95 XENBUS_ACCESSOR(state,		STATE,			enum xenbus_state)
96 XENBUS_ACCESSOR(otherend_id,	OTHEREND_ID,		int)
97 XENBUS_ACCESSOR(otherend_path,	OTHEREND_PATH,		const char *)
98 
99 /**
100  * Return the state of a XenBus device.
101  *
102  * \param path  The root XenStore path for the device.
103  *
104  * \return  The current state of the device or XenbusStateClosed if no
105  *	    state can be read.
106  */
107 XenbusState xenbus_read_driver_state(const char *path);
108 
109 /**
110  * Return the state of the "other end" (peer) of a XenBus device.
111  *
112  * \param dev   The XenBus device whose peer to query.
113  *
114  * \return  The current state of the peer device or XenbusStateClosed if no
115  *          state can be read.
116  */
117 static inline XenbusState
xenbus_get_otherend_state(device_t dev)118 xenbus_get_otherend_state(device_t dev)
119 {
120 	return (xenbus_read_driver_state(xenbus_get_otherend_path(dev)));
121 }
122 
123 /**
124  * Grant access to the given ring_mfn to the peer of the given device.
125  *
126  * \param dev        The device granting access to the ring page.
127  * \param ring_mfn   The guest machine page number of the page to grant
128  *                   peer access rights.
129  * \param refp[out]  The grant reference for the page.
130  *
131  * \return  On success, 0. Otherwise an errno value indicating the
132  *          type of failure.
133  *
134  * A successful call to xenbus_grant_ring should be paired with a call
135  * to gnttab_end_foreign_access() when foregn access to this page is no
136  * longer requried.
137  *
138  * \note  On error, \a dev will be switched to the XenbusStateClosing
139  *        state and the returned error is saved in the per-device error node
140  *        for \a dev in the XenStore.
141  */
142 int xenbus_grant_ring(device_t dev, unsigned long ring_mfn, grant_ref_t *refp);
143 
144 /**
145  * Record the given errno, along with the given, printf-style, formatted
146  * message in dev's device specific error node in the XenStore.
147  *
148  * \param dev  The device which encountered the error.
149  * \param err  The errno value corresponding to the error.
150  * \param fmt  Printf format string followed by a variable number of
151  *             printf arguments.
152  */
153 void xenbus_dev_error(device_t dev, int err, const char *fmt, ...)
154 	__attribute__((format(printf, 3, 4)));
155 
156 /**
157  * va_list version of xenbus_dev_error().
158  *
159  * \param dev  The device which encountered the error.
160  * \param err  The errno value corresponding to the error.
161  * \param fmt  Printf format string.
162  * \param ap   Va_list of printf arguments.
163  */
164 void xenbus_dev_verror(device_t dev, int err, const char *fmt, va_list ap)
165 	__attribute__((format(printf, 3, 0)));
166 
167 /**
168  * Equivalent to xenbus_dev_error(), followed by
169  * xenbus_set_state(dev, XenbusStateClosing).
170  *
171  * \param dev  The device which encountered the error.
172  * \param err  The errno value corresponding to the error.
173  * \param fmt  Printf format string followed by a variable number of
174  *             printf arguments.
175  */
176 void xenbus_dev_fatal(device_t dev, int err, const char *fmt, ...)
177 	__attribute__((format(printf, 3, 4)));
178 
179 /**
180  * va_list version of xenbus_dev_fatal().
181  *
182  * \param dev  The device which encountered the error.
183  * \param err  The errno value corresponding to the error.
184  * \param fmt  Printf format string.
185  * \param ap   Va_list of printf arguments.
186  */
187 void xenbus_dev_vfatal(device_t dev, int err, const char *fmt, va_list)
188 	__attribute__((format(printf, 3, 0)));
189 
190 /**
191  * Convert a member of the xenbus_state enum into an ASCII string.
192  *
193  * /param state  The XenBus state to lookup.
194  *
195  * /return  A string representing state or, for unrecognized states,
196  *	    the string "Unknown".
197  */
198 const char *xenbus_strstate(enum xenbus_state state);
199 
200 /**
201  * Return the value of a XenBus device's "online" node within the XenStore.
202  *
203  * \param dev  The XenBus device to query.
204  *
205  * \return  The value of the "online" node for the device.  If the node
206  *          does not exist, 0 (offline) is returned.
207  */
208 int xenbus_dev_is_online(device_t dev);
209 
210 /**
211  * Default callback invoked when a change to the local XenStore sub-tree
212  * for a device is modified.
213  *
214  * \param dev   The XenBus device whose tree was modified.
215  * \param path  The tree relative sub-path to the modified node.  The empty
216  *              string indicates the root of the tree was destroyed.
217  */
218 void xenbus_localend_changed(device_t dev, const char *path);
219 
220 #include "xenbus_if.h"
221 
222 #endif /* _XEN_XENBUS_XENBUSVAR_H */
223