xenbusvar.h revision 185605
1226031Sstas/******************************************************************************
2226031Sstas * xenbus.h
3226031Sstas *
4226031Sstas * Talks to Xen Store to figure out what devices we have.
5226031Sstas *
6226031Sstas * Copyright (C) 2005 Rusty Russell, IBM Corporation
7226031Sstas * Copyright (C) 2005 XenSource Ltd.
8226031Sstas *
9226031Sstas * This file may be distributed separately from the Linux kernel, or
10226031Sstas * incorporated into other software packages, subject to the following license:
11226031Sstas *
12226031Sstas * Permission is hereby granted, free of charge, to any person obtaining a copy
13226031Sstas * of this source file (the "Software"), to deal in the Software without
14226031Sstas * restriction, including without limitation the rights to use, copy, modify,
15226031Sstas * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16226031Sstas * and to permit persons to whom the Software is furnished to do so, subject to
17226031Sstas * the following conditions:
18226031Sstas *
19226031Sstas * The above copyright notice and this permission notice shall be included in
20226031Sstas * all copies or substantial portions of the Software.
21226031Sstas *
22226031Sstas * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23226031Sstas * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24226031Sstas * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25226031Sstas * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26226031Sstas * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27226031Sstas * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28226031Sstas * IN THE SOFTWARE.
29226031Sstas *
30226031Sstas * $FreeBSD: head/sys/xen/xenbus/xenbusvar.h 185605 2008-12-04 07:59:05Z kmacy $
31226031Sstas */
32226031Sstas
33226031Sstas#ifndef _XEN_XENBUS_XENBUSVAR_H
34226031Sstas#define _XEN_XENBUS_XENBUSVAR_H
35226031Sstas
36226031Sstas#include <sys/queue.h>
37226031Sstas#include <sys/bus.h>
38226031Sstas#include <sys/eventhandler.h>
39226031Sstas#include <machine/xen/xen-os.h>
40226031Sstas#include <xen/interface/io/xenbus.h>
41226031Sstas#include <xen/interface/io/xs_wire.h>
42226031Sstas
43226031Sstas#include "xenbus_if.h"
44226031Sstas
45226031Sstasenum {
46226031Sstas	/*
47226031Sstas	 * Path of this device node.
48226031Sstas	 */
49226031Sstas	XENBUS_IVAR_NODE,
50226031Sstas
51226031Sstas	/*
52226031Sstas	 * The device type (e.g. vif, vbd).
53226031Sstas	 */
54226031Sstas	XENBUS_IVAR_TYPE,
55226031Sstas
56226031Sstas	/*
57226031Sstas	 * The state of this device (not the otherend's state).
58226031Sstas	 */
59226031Sstas	XENBUS_IVAR_STATE,
60226031Sstas
61226031Sstas	/*
62226031Sstas	 * Domain ID of the other end device.
63226031Sstas	 */
64226031Sstas	XENBUS_IVAR_OTHEREND_ID,
65226031Sstas
66226031Sstas	/*
67226031Sstas	 * Path of the other end device.
68226031Sstas	 */
69226031Sstas	XENBUS_IVAR_OTHEREND_PATH
70226031Sstas};
71226031Sstas
72226031Sstas/*
73226031Sstas * Simplified accessors for xenbus devices
74226031Sstas */
75226031Sstas#define	XENBUS_ACCESSOR(var, ivar, type) \
76226031Sstas	__BUS_ACCESSOR(xenbus, var, XENBUS, ivar, type)
77226031Sstas
78226031SstasXENBUS_ACCESSOR(node,		NODE,			const char *)
79226031SstasXENBUS_ACCESSOR(type,		TYPE,			const char *)
80226031SstasXENBUS_ACCESSOR(state,		STATE,			enum xenbus_state)
81226031SstasXENBUS_ACCESSOR(otherend_id,	OTHEREND_ID,		int)
82226031SstasXENBUS_ACCESSOR(otherend_path,	OTHEREND_PATH,		const char *)
83226031Sstas
84226031Sstas/* Register callback to watch this node. */
85226031Sstasstruct xenbus_watch
86226031Sstas{
87226031Sstas	LIST_ENTRY(xenbus_watch) list;
88226031Sstas
89226031Sstas	/* Path being watched. */
90226031Sstas	char *node;
91226031Sstas
92226031Sstas	/* Callback (executed in a process context with no locks held). */
93226031Sstas	void (*callback)(struct xenbus_watch *,
94226031Sstas			 const char **vec, unsigned int len);
95226031Sstas};
96226031Sstas
97226031Sstastypedef int (*xenstore_event_handler_t)(void *);
98226031Sstas
99226031Sstasstruct xenbus_transaction
100226031Sstas{
101226031Sstas		uint32_t id;
102226031Sstas};
103226031Sstas
104226031Sstas#define XBT_NIL ((struct xenbus_transaction) { 0 })
105226031Sstas
106226031Sstaschar **xenbus_directory(struct xenbus_transaction t,
107226031Sstas			const char *dir, const char *node, unsigned int *num);
108226031Sstasvoid *xenbus_read(struct xenbus_transaction t,
109226031Sstas		  const char *dir, const char *node, unsigned int *len);
110226031Sstasint xenbus_write(struct xenbus_transaction t,
111226031Sstas		 const char *dir, const char *node, const char *string);
112226031Sstasint xenbus_mkdir(struct xenbus_transaction t,
113226031Sstas		 const char *dir, const char *node);
114226031Sstasint xenbus_exists(struct xenbus_transaction t,
115226031Sstas		  const char *dir, const char *node);
116226031Sstasint xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node);
117226031Sstasint xenbus_transaction_start(struct xenbus_transaction *t);
118226031Sstasint xenbus_transaction_end(struct xenbus_transaction t, int abort);
119226031Sstas
120226031Sstas/* Single read and scanf: returns -errno or num scanned if > 0. */
121226031Sstasint xenbus_scanf(struct xenbus_transaction t,
122226031Sstas		 const char *dir, const char *node, const char *fmt, ...)
123226031Sstas	__attribute__((format(scanf, 4, 5)));
124226031Sstas
125226031Sstas/* Single printf and write: returns -errno or 0. */
126226031Sstasint xenbus_printf(struct xenbus_transaction t,
127226031Sstas		  const char *dir, const char *node, const char *fmt, ...)
128226031Sstas	__attribute__((format(printf, 4, 5)));
129226031Sstas
130226031Sstas/* Generic read function: NULL-terminated triples of name,
131226031Sstas * sprintf-style type string, and pointer. Returns 0 or errno.*/
132226031Sstasint xenbus_gather(struct xenbus_transaction t, const char *dir, ...);
133226031Sstas
134226031Sstas/* notifer routines for when the xenstore comes up */
135226031Sstasint register_xenstore_notifier(xenstore_event_handler_t func, void *arg, int priority);
136226031Sstas#if 0
137226031Sstasvoid unregister_xenstore_notifier();
138226031Sstas#endif
139226031Sstasint register_xenbus_watch(struct xenbus_watch *watch);
140226031Sstasvoid unregister_xenbus_watch(struct xenbus_watch *watch);
141226031Sstasvoid xs_suspend(void);
142226031Sstasvoid xs_resume(void);
143226031Sstas
144226031Sstas/* Used by xenbus_dev to borrow kernel's store connection. */
145226031Sstasvoid *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg);
146226031Sstas
147226031Sstas#define XENBUS_IS_ERR_READ(str) ({			\
148226031Sstas	if (!IS_ERR(str) && strlen(str) == 0) {		\
149226031Sstas		free(str, M_DEVBUF);				\
150226031Sstas		str = ERR_PTR(-ERANGE);			\
151226031Sstas	}						\
152226031Sstas	IS_ERR(str);					\
153226031Sstas})
154226031Sstas
155226031Sstas#define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE)
156226031Sstas
157226031Sstas/**
158226031Sstas * Register a watch on the given path, using the given xenbus_watch structure
159226031Sstas * for storage, and the given callback function as the callback.  Return 0 on
160226031Sstas * success, or -errno on error.  On success, the given path will be saved as
161226031Sstas * watch->node, and remains the caller's to free.  On error, watch->node will
162226031Sstas * be NULL, the device will switch to XenbusStateClosing, and the error will
163226031Sstas * be saved in the store.
164226031Sstas */
165226031Sstasint xenbus_watch_path(device_t dev, char *path,
166226031Sstas		      struct xenbus_watch *watch,
167226031Sstas		      void (*callback)(struct xenbus_watch *,
168226031Sstas				       const char **, unsigned int));
169226031Sstas
170226031Sstas
171234027Sstas/**
172226031Sstas * Register a watch on the given path/path2, using the given xenbus_watch
173226031Sstas * 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