xenstorevar.h revision 185605
1/******************************************************************************
2 * xenbus.h
3 *
4 * Talks to Xen Store to figure out what devices we have.
5 *
6 * Copyright (C) 2005 Rusty Russell, IBM Corporation
7 * Copyright (C) 2005 XenSource Ltd.
8 *
9 * This file may be distributed separately from the Linux kernel, or
10 * incorporated into other software packages, subject to the following license:
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 * IN THE SOFTWARE.
29 *
30 * $FreeBSD: head/sys/xen/xenbus/xenbusvar.h 185605 2008-12-04 07:59:05Z kmacy $
31 */
32
33#ifndef _XEN_XENBUS_XENBUSVAR_H
34#define _XEN_XENBUS_XENBUSVAR_H
35
36#include <sys/queue.h>
37#include <sys/bus.h>
38#include <sys/eventhandler.h>
39#include <machine/xen/xen-os.h>
40#include <xen/interface/io/xenbus.h>
41#include <xen/interface/io/xs_wire.h>
42
43#include "xenbus_if.h"
44
45enum {
46	/*
47	 * Path of this device node.
48	 */
49	XENBUS_IVAR_NODE,
50
51	/*
52	 * The device type (e.g. vif, vbd).
53	 */
54	XENBUS_IVAR_TYPE,
55
56	/*
57	 * The state of this device (not the otherend's state).
58	 */
59	XENBUS_IVAR_STATE,
60
61	/*
62	 * Domain ID of the other end device.
63	 */
64	XENBUS_IVAR_OTHEREND_ID,
65
66	/*
67	 * Path of the other end device.
68	 */
69	XENBUS_IVAR_OTHEREND_PATH
70};
71
72/*
73 * Simplified accessors for xenbus devices
74 */
75#define	XENBUS_ACCESSOR(var, ivar, type) \
76	__BUS_ACCESSOR(xenbus, var, XENBUS, ivar, type)
77
78XENBUS_ACCESSOR(node,		NODE,			const char *)
79XENBUS_ACCESSOR(type,		TYPE,			const char *)
80XENBUS_ACCESSOR(state,		STATE,			enum xenbus_state)
81XENBUS_ACCESSOR(otherend_id,	OTHEREND_ID,		int)
82XENBUS_ACCESSOR(otherend_path,	OTHEREND_PATH,		const char *)
83
84/* Register callback to watch this node. */
85struct xenbus_watch
86{
87	LIST_ENTRY(xenbus_watch) list;
88
89	/* Path being watched. */
90	char *node;
91
92	/* Callback (executed in a process context with no locks held). */
93	void (*callback)(struct xenbus_watch *,
94			 const char **vec, unsigned int len);
95};
96
97typedef int (*xenstore_event_handler_t)(void *);
98
99struct xenbus_transaction
100{
101		uint32_t id;
102};
103
104#define XBT_NIL ((struct xenbus_transaction) { 0 })
105
106char **xenbus_directory(struct xenbus_transaction t,
107			const char *dir, const char *node, unsigned int *num);
108void *xenbus_read(struct xenbus_transaction t,
109		  const char *dir, const char *node, unsigned int *len);
110int xenbus_write(struct xenbus_transaction t,
111		 const char *dir, const char *node, const char *string);
112int xenbus_mkdir(struct xenbus_transaction t,
113		 const char *dir, const char *node);
114int xenbus_exists(struct xenbus_transaction t,
115		  const char *dir, const char *node);
116int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node);
117int xenbus_transaction_start(struct xenbus_transaction *t);
118int xenbus_transaction_end(struct xenbus_transaction t, int abort);
119
120/* Single read and scanf: returns -errno or num scanned if > 0. */
121int xenbus_scanf(struct xenbus_transaction t,
122		 const char *dir, const char *node, const char *fmt, ...)
123	__attribute__((format(scanf, 4, 5)));
124
125/* Single printf and write: returns -errno or 0. */
126int xenbus_printf(struct xenbus_transaction t,
127		  const char *dir, const char *node, const char *fmt, ...)
128	__attribute__((format(printf, 4, 5)));
129
130/* Generic read function: NULL-terminated triples of name,
131 * sprintf-style type string, and pointer. Returns 0 or errno.*/
132int xenbus_gather(struct xenbus_transaction t, const char *dir, ...);
133
134/* notifer routines for when the xenstore comes up */
135int register_xenstore_notifier(xenstore_event_handler_t func, void *arg, int priority);
136#if 0
137void unregister_xenstore_notifier();
138#endif
139int register_xenbus_watch(struct xenbus_watch *watch);
140void unregister_xenbus_watch(struct xenbus_watch *watch);
141void xs_suspend(void);
142void xs_resume(void);
143
144/* Used by xenbus_dev to borrow kernel's store connection. */
145void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg);
146
147#define XENBUS_IS_ERR_READ(str) ({			\
148	if (!IS_ERR(str) && strlen(str) == 0) {		\
149		free(str, M_DEVBUF);				\
150		str = ERR_PTR(-ERANGE);			\
151	}						\
152	IS_ERR(str);					\
153})
154
155#define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE)
156
157/**
158 * Register a watch on the given path, using the given xenbus_watch structure
159 * for storage, and the given callback function as the callback.  Return 0 on
160 * success, or -errno on error.  On success, the given path will be saved as
161 * watch->node, and remains the caller's to free.  On error, watch->node will
162 * be NULL, the device will switch to XenbusStateClosing, and the error will
163 * be saved in the store.
164 */
165int xenbus_watch_path(device_t dev, char *path,
166		      struct xenbus_watch *watch,
167		      void (*callback)(struct xenbus_watch *,
168				       const char **, unsigned int));
169
170
171/**
172 * Register a watch on the given path/path2, using the given xenbus_watch
173 * structure for storage, and the given callback function as the callback.
174 * Return 0 on success, or -errno on error.  On success, the watched path
175 * (path/path2) will be saved as watch->node, and becomes the caller's to
176 * kfree().  On error, watch->node will be NULL, so the caller has nothing to
177 * free, the device will switch to XenbusStateClosing, and the error will be
178 * saved in the store.
179 */
180int xenbus_watch_path2(device_t dev, const char *path,
181		       const char *path2, struct xenbus_watch *watch,
182		       void (*callback)(struct xenbus_watch *,
183					const char **, unsigned int));
184
185
186/**
187 * Advertise in the store a change of the given driver to the given new_state.
188 * which case this is performed inside its own transaction.  Return 0 on
189 * success, or -errno on error.  On error, the device will switch to
190 * XenbusStateClosing, and the error will be saved in the store.
191 */
192int xenbus_switch_state(device_t dev,
193			XenbusState new_state);
194
195
196/**
197 * Grant access to the given ring_mfn to the peer of the given device.  Return
198 * 0 on success, or -errno on error.  On error, the device will switch to
199 * XenbusStateClosing, and the error will be saved in the store.
200 */
201int xenbus_grant_ring(device_t dev, unsigned long ring_mfn);
202
203
204/**
205 * Allocate an event channel for the given xenbus_device, assigning the newly
206 * created local port to *port.  Return 0 on success, or -errno on error.  On
207 * error, the device will switch to XenbusStateClosing, and the error will be
208 * saved in the store.
209 */
210int xenbus_alloc_evtchn(device_t dev, int *port);
211
212
213/**
214 * Free an existing event channel. Returns 0 on success or -errno on error.
215 */
216int xenbus_free_evtchn(device_t dev, int port);
217
218
219/**
220 * Return the state of the driver rooted at the given store path, or
221 * XenbusStateClosed if no state can be read.
222 */
223XenbusState xenbus_read_driver_state(const char *path);
224
225
226/***
227 * Report the given negative errno into the store, along with the given
228 * formatted message.
229 */
230void xenbus_dev_error(device_t dev, int err, const char *fmt,
231		      ...);
232
233
234/***
235 * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
236 * xenbus_switch_state(dev, NULL, XenbusStateClosing) to schedule an orderly
237 * closedown of this driver and its peer.
238 */
239void xenbus_dev_fatal(device_t dev, int err, const char *fmt,
240		      ...);
241
242int xenbus_dev_init(void);
243
244const char *xenbus_strstate(enum xenbus_state state);
245int xenbus_dev_is_online(device_t dev);
246int xenbus_frontend_closed(device_t dev);
247
248#endif /* _XEN_XENBUS_XENBUSVAR_H */
249