summaryrefslogtreecommitdiffstats
path: root/sys/xen/xenbus/xenbusvar.h
blob: 55d7f29d4d650cc9c1a43c7b0e1a81f1acea3c35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/******************************************************************************
 * Copyright (C) 2005 Rusty Russell, IBM Corporation
 * Copyright (C) 2005 XenSource Ltd.
 * 
 * This file may be distributed separately from the Linux kernel, or
 * incorporated into other software packages, subject to the following license:
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this source file (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * $FreeBSD$
 */

/**
 * \file xenbusvar.h
 *
 * \brief Datastructures and function declarations for usedby device
 *        drivers operating on the XenBus.
 */

#ifndef _XEN_XENBUS_XENBUSVAR_H
#define _XEN_XENBUS_XENBUSVAR_H

#include <sys/queue.h>
#include <sys/bus.h>
#include <sys/eventhandler.h>
#include <sys/malloc.h>
#include <sys/sbuf.h>

#include <machine/stdarg.h>
#include <machine/xen/xen-os.h>

#include <xen/interface/grant_table.h>
#include <xen/interface/io/xenbus.h>
#include <xen/interface/io/xs_wire.h>

#include <xen/xenstore/xenstorevar.h>

#include "xenbus_if.h"

/* XenBus allocations including XenStore data returned to clients. */
MALLOC_DECLARE(M_XENBUS);

enum {
	/**
	 * Path of this device node.
	 */
	XENBUS_IVAR_NODE,

	/**
	 * The device type (e.g. vif, vbd).
	 */
	XENBUS_IVAR_TYPE,

	/**
	 * The state of this device (not the otherend's state).
	 */
	XENBUS_IVAR_STATE,

	/**
	 * Domain ID of the other end device.
	 */
	XENBUS_IVAR_OTHEREND_ID,

	/**
	 * Path of the other end device.
	 */
	XENBUS_IVAR_OTHEREND_PATH
};

/**
 * Simplified accessors for xenbus devices
 */
#define	XENBUS_ACCESSOR(var, ivar, type) \
	__BUS_ACCESSOR(xenbus, var, XENBUS, ivar, type)

XENBUS_ACCESSOR(node,		NODE,			const char *)
XENBUS_ACCESSOR(type,		TYPE,			const char *)
XENBUS_ACCESSOR(state,		STATE,			enum xenbus_state)
XENBUS_ACCESSOR(otherend_id,	OTHEREND_ID,		int)
XENBUS_ACCESSOR(otherend_path,	OTHEREND_PATH,		const char *)

/**
 * Return the state of a XenBus device.
 *
 * \param path  The root XenStore path for the device.
 *
 * \return  The current state of the device or XenbusStateClosed if no
 *	    state can be read.
 */
XenbusState xenbus_read_driver_state(const char *path);

/**
 * Initialize and register a watch on the given path (client suplied storage).
 *
 * \param dev       The XenBus device requesting the watch service.
 * \param path      The XenStore path of the object to be watched.  The
 *                  storage for this string must be stable for the lifetime
 *                  of the watch.
 * \param watch     The watch object to use for this request.  This object
 *                  must be stable for the lifetime of the watch.
 * \param callback  The function to call when XenStore objects at or below
 *                  path are modified.
 *
 * \return  On success, 0. Otherwise an errno value indicating the
 *          type of failure.
 *
 * \note  On error, the device 'dev' will be switched to the XenbusStateClosing
 *        state and the returned error is saved in the per-device error node
 *        for dev in the XenStore.
 */
int xenbus_watch_path(device_t dev, char *path,
		      struct xs_watch *watch, 
		      xs_watch_cb_t *callback);

/**
 * Initialize and register a watch at path/path2 in the XenStore.
 *
 * \param dev       The XenBus device requesting the watch service.
 * \param path      The base XenStore path of the object to be watched.
 * \param path2     The tail XenStore path of the object to be watched.
 * \param watch     The watch object to use for this request.  This object
 *                  must be stable for the lifetime of the watch.
 * \param callback  The function to call when XenStore objects at or below
 *                  path are modified.
 *
 * \return  On success, 0. Otherwise an errno value indicating the
 *          type of failure.
 *
 * \note  On error, \a dev will be switched to the XenbusStateClosing
 *        state and the returned error is saved in the per-device error node
 *        for \a dev in the XenStore.
 *
 * Similar to xenbus_watch_path, however the storage for the path to the
 * watched object is allocated from the heap and filled with "path '/' path2".
 * Should a call to this function succeed, it is the callers responsibility
 * to free watch->node using the M_XENBUS malloc type.
 */
int xenbus_watch_path2(device_t dev, const char *path,
		       const char *path2, struct xs_watch *watch, 
		       xs_watch_cb_t *callback);

/**
 * Grant access to the given ring_mfn to the peer of the given device.
 *
 * \param dev        The device granting access to the ring page.
 * \param ring_mfn   The guest machine page number of the page to grant
 *                   peer access rights.
 * \param refp[out]  The grant reference for the page.
 *
 * \return  On success, 0. Otherwise an errno value indicating the
 *          type of failure.
 *
 * A successful call to xenbus_grant_ring should be paired with a call
 * to gnttab_end_foreign_access() when foregn access to this page is no
 * longer requried.
 * 
 * \note  On error, \a dev will be switched to the XenbusStateClosing
 *        state and the returned error is saved in the per-device error node
 *        for \a dev in the XenStore.
 */
int xenbus_grant_ring(device_t dev, unsigned long ring_mfn, grant_ref_t *refp);

/**
 * Allocate an event channel for the given XenBus device.
 *
 * \param dev        The device for which to allocate the event channel.
 * \param port[out]  The port identifier for the allocated event channel.
 *
 * \return  On success, 0. Otherwise an errno value indicating the
 *          type of failure.
 *
 * A successfully allocated event channel should be free'd using
 * xenbus_free_evtchn().
 *
 * \note  On error, \a dev will be switched to the XenbusStateClosing
 *        state and the returned error is saved in the per-device error node
 *        for \a dev in the XenStore.
 */
int xenbus_alloc_evtchn(device_t dev, evtchn_port_t *port);

/**
 * Free an existing event channel.
 *
 * \param dev   The device which allocated this event channel.
 * \param port  The port identifier for the event channel to free.
 *
 * \return  On success, 0. Otherwise an errno value indicating the
 *          type of failure.
 *
 * \note  On error, \a dev will be switched to the XenbusStateClosing
 *        state and the returned error is saved in the per-device error node
 *        for \a dev in the XenStore.
 */
int xenbus_free_evtchn(device_t dev, evtchn_port_t port);

/**
 * Record the given errno, along with the given, printf-style, formatted
 * message in dev's device specific error node in the XenStore.
 *
 * \param dev  The device which encountered the error.
 * \param err  The errno value corresponding to the error.
 * \param fmt  Printf format string followed by a variable number of
 *             printf arguments.
 */
void xenbus_dev_error(device_t dev, int err, const char *fmt, ...)
	__attribute__((format(printf, 3, 4)));

/**
 * va_list version of xenbus_dev_error().
 *
 * \param dev  The device which encountered the error.
 * \param err  The errno value corresponding to the error.
 * \param fmt  Printf format string.
 * \param ap   Va_list of printf arguments.
 */
void xenbus_dev_verror(device_t dev, int err, const char *fmt, va_list ap)
	__attribute__((format(printf, 3, 0)));

/**
 * Equivalent to xenbus_dev_error(), followed by
 * xenbus_set_state(dev, XenbusStateClosing).
 *
 * \param dev  The device which encountered the error.
 * \param err  The errno value corresponding to the error.
 * \param fmt  Printf format string followed by a variable number of
 *             printf arguments.
 */
void xenbus_dev_fatal(device_t dev, int err, const char *fmt, ...)
	__attribute__((format(printf, 3, 4)));

/**
 * va_list version of xenbus_dev_fatal().
 *
 * \param dev  The device which encountered the error.
 * \param err  The errno value corresponding to the error.
 * \param fmt  Printf format string.
 * \param ap   Va_list of printf arguments.
 */
void xenbus_dev_vfatal(device_t dev, int err, const char *fmt, va_list)
	__attribute__((format(printf, 3, 0)));

/**
 * Convert a member of the xenbus_state enum into an ASCII string.
 *
 * /param state  The XenBus state to lookup.
 *
 * /return  A string representing state or, for unrecognized states,
 *	    the string "Unknown".
 */
const char *xenbus_strstate(enum xenbus_state state);

/**
 * Return the value of a XenBus device's "online" node within the XenStore.
 *
 * \param dev  The XenBus device to query.
 *
 * \return  The value of the "online" node for the device.  If the node
 *          does not exist, 0 (offline) is returned.
 */
int xenbus_dev_is_online(device_t dev);

#endif /* _XEN_XENBUS_XENBUSVAR_H */
OpenPOWER on IntegriCloud