1 /******************************************************************************
4 * Talks to Xen Store to figure out what devices we have.
6 * Copyright (C) 2005 Rusty Russell, IBM Corporation
7 * Copyright (C) 2005 XenSource Ltd.
9 * This file may be distributed separately from the Linux kernel, or
10 * incorporated into other software packages, subject to the following license:
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:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
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
31 #ifndef _ASM_XEN_XENBUS_H
32 #define _ASM_XEN_XENBUS_H
34 #include <sys/queue.h>
36 #include <sys/eventhandler.h>
37 #include <xen/interface/io/xenbus.h>
38 #include <xen/interface/io/xs_wire.h>
40 LIST_HEAD(xendev_list_head, xenbus_device);
42 /* Register callback to watch this node. */
45 LIST_ENTRY(xenbus_watch) list;
47 /* Path being watched. */
50 /* Callback (executed in a process context with no locks held). */
51 void (*callback)(struct xenbus_watch *,
52 const char **vec, unsigned int len);
56 /* A xenbus device. */
57 struct xenbus_device {
58 struct xenbus_watch otherend_watch; /* must be first */
59 const char *devicetype;
63 struct xendev_list_head *bus;
64 struct xenbus_driver *driver;
66 enum xenbus_state state;
67 void *dev_driver_data;
68 LIST_ENTRY(xenbus_device) list;
71 static inline struct xenbus_device *to_xenbus_device(device_t dev)
73 return device_get_softc(dev);
76 struct xenbus_device_id
78 /* .../device/<device_type>/<identifier> */
79 char devicetype[32]; /* General class of device. */
82 /* A xenbus driver. */
83 struct xenbus_driver {
86 const struct xenbus_device_id *ids;
87 int (*probe)(struct xenbus_device *dev,
88 const struct xenbus_device_id *id);
89 void (*otherend_changed)(struct xenbus_device *dev,
90 XenbusState backend_state);
91 int (*remove)(struct xenbus_device *dev);
92 int (*suspend)(struct xenbus_device *dev);
93 int (*resume)(struct xenbus_device *dev);
94 int (*hotplug)(struct xenbus_device *, char **, int, char *, int);
96 struct device_driver driver;
99 int (*read_otherend_details)(struct xenbus_device *dev);
100 int (*watch_otherend)(struct xenbus_device *dev);
101 int (*cleanup_device)(struct xenbus_device *dev);
102 int (*is_ready)(struct xenbus_device *dev);
103 LIST_ENTRY(xenbus_driver) list;
106 static inline struct xenbus_driver *to_xenbus_driver(driver_t *drv)
109 return container_of(drv, struct xenbus_driver, driver);
113 typedef int (*xenstore_event_handler_t)(void *);
115 int xenbus_register_frontend(struct xenbus_driver *drv);
116 int xenbus_register_backend(struct xenbus_driver *drv);
117 void xenbus_unregister_driver(struct xenbus_driver *drv);
119 int xenbus_remove_device(struct xenbus_device *dev);
121 struct xenbus_transaction
126 #define XBT_NIL ((struct xenbus_transaction) { 0 })
128 char **xenbus_directory(struct xenbus_transaction t,
129 const char *dir, const char *node, unsigned int *num);
130 void *xenbus_read(struct xenbus_transaction t,
131 const char *dir, const char *node, unsigned int *len);
132 int xenbus_write(struct xenbus_transaction t,
133 const char *dir, const char *node, const char *string);
134 int xenbus_mkdir(struct xenbus_transaction t,
135 const char *dir, const char *node);
136 int xenbus_exists(struct xenbus_transaction t,
137 const char *dir, const char *node);
138 int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node);
139 int xenbus_transaction_start(struct xenbus_transaction *t);
140 int xenbus_transaction_end(struct xenbus_transaction t, int abort);
142 /* Single read and scanf: returns -errno or num scanned if > 0. */
143 int xenbus_scanf(struct xenbus_transaction t,
144 const char *dir, const char *node, const char *fmt, ...)
145 __attribute__((format(scanf, 4, 5)));
147 /* Single printf and write: returns -errno or 0. */
148 int xenbus_printf(struct xenbus_transaction t,
149 const char *dir, const char *node, const char *fmt, ...)
150 __attribute__((format(printf, 4, 5)));
152 /* Generic read function: NULL-terminated triples of name,
153 * sprintf-style type string, and pointer. Returns 0 or errno.*/
154 int xenbus_gather(struct xenbus_transaction t, const char *dir, ...);
156 /* notifer routines for when the xenstore comes up */
157 int register_xenstore_notifier(xenstore_event_handler_t func, void *arg, int priority);
159 void unregister_xenstore_notifier();
161 int register_xenbus_watch(struct xenbus_watch *watch);
162 void unregister_xenbus_watch(struct xenbus_watch *watch);
163 void xs_suspend(void);
164 void xs_resume(void);
166 /* Used by xenbus_dev to borrow kernel's store connection. */
167 void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg);
169 /* Called from xen core code. */
170 void xenbus_suspend(void);
171 void xenbus_resume(void);
173 #define XENBUS_IS_ERR_READ(str) ({ \
174 if (!IS_ERR(str) && strlen(str) == 0) { \
175 free(str, M_DEVBUF); \
176 str = ERR_PTR(-ERANGE); \
181 #define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE)
185 * Register a watch on the given path, using the given xenbus_watch structure
186 * for storage, and the given callback function as the callback. Return 0 on
187 * success, or -errno on error. On success, the given path will be saved as
188 * watch->node, and remains the caller's to free. On error, watch->node will
189 * be NULL, the device will switch to XenbusStateClosing, and the error will
190 * be saved in the store.
192 int xenbus_watch_path(struct xenbus_device *dev, char *path,
193 struct xenbus_watch *watch,
194 void (*callback)(struct xenbus_watch *,
195 const char **, unsigned int));
199 * Register a watch on the given path/path2, using the given xenbus_watch
200 * structure for storage, and the given callback function as the callback.
201 * Return 0 on success, or -errno on error. On success, the watched path
202 * (path/path2) will be saved as watch->node, and becomes the caller's to
203 * kfree(). On error, watch->node will be NULL, so the caller has nothing to
204 * free, the device will switch to XenbusStateClosing, and the error will be
205 * saved in the store.
207 int xenbus_watch_path2(struct xenbus_device *dev, const char *path,
208 const char *path2, struct xenbus_watch *watch,
209 void (*callback)(struct xenbus_watch *,
210 const char **, unsigned int));
214 * Advertise in the store a change of the given driver to the given new_state.
215 * which case this is performed inside its own transaction. Return 0 on
216 * success, or -errno on error. On error, the device will switch to
217 * XenbusStateClosing, and the error will be saved in the store.
219 int xenbus_switch_state(struct xenbus_device *dev,
220 XenbusState new_state);
224 * Grant access to the given ring_mfn to the peer of the given device. Return
225 * 0 on success, or -errno on error. On error, the device will switch to
226 * XenbusStateClosing, and the error will be saved in the store.
228 int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn);
232 * Allocate an event channel for the given xenbus_device, assigning the newly
233 * created local port to *port. Return 0 on success, or -errno on error. On
234 * error, the device will switch to XenbusStateClosing, and the error will be
235 * saved in the store.
237 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port);
241 * Free an existing event channel. Returns 0 on success or -errno on error.
243 int xenbus_free_evtchn(struct xenbus_device *dev, int port);
247 * Return the state of the driver rooted at the given store path, or
248 * XenbusStateClosed if no state can be read.
250 XenbusState xenbus_read_driver_state(const char *path);
254 * Report the given negative errno into the store, along with the given
257 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
262 * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
263 * xenbus_switch_state(dev, NULL, XenbusStateClosing) to schedule an orderly
264 * closedown of this driver and its peer.
266 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
269 int xenbus_dev_init(void);
271 const char *xenbus_strstate(enum xenbus_state state);
272 int xenbus_dev_is_online(struct xenbus_device *dev);
273 int xenbus_frontend_closed(struct xenbus_device *dev);
275 #endif /* _ASM_XEN_XENBUS_H */
279 * c-file-style: "bsd"
280 * indent-tabs-mode: t