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
33 #ifndef _ASM_XEN_XENBUS_H
34 #define _ASM_XEN_XENBUS_H
36 #include <sys/queue.h>
38 #include <sys/eventhandler.h>
39 #include <xen/interface/io/xenbus.h>
40 #include <xen/interface/io/xs_wire.h>
42 LIST_HEAD(xendev_list_head, xenbus_device);
44 /* Register callback to watch this node. */
47 LIST_ENTRY(xenbus_watch) list;
49 /* Path being watched. */
52 /* Callback (executed in a process context with no locks held). */
53 void (*callback)(struct xenbus_watch *,
54 const char **vec, unsigned int len);
58 /* A xenbus device. */
59 struct xenbus_device {
60 struct xenbus_watch otherend_watch; /* must be first */
61 const char *devicetype;
65 struct xendev_list_head *bus;
66 struct xenbus_driver *driver;
68 enum xenbus_state state;
69 void *dev_driver_data;
70 LIST_ENTRY(xenbus_device) list;
73 static inline struct xenbus_device *to_xenbus_device(device_t dev)
75 return device_get_softc(dev);
78 struct xenbus_device_id
80 /* .../device/<device_type>/<identifier> */
81 char devicetype[32]; /* General class of device. */
84 /* A xenbus driver. */
85 struct xenbus_driver {
88 const struct xenbus_device_id *ids;
89 int (*probe)(struct xenbus_device *dev,
90 const struct xenbus_device_id *id);
91 void (*otherend_changed)(struct xenbus_device *dev,
92 XenbusState backend_state);
93 int (*remove)(struct xenbus_device *dev);
94 int (*suspend)(struct xenbus_device *dev);
95 int (*resume)(struct xenbus_device *dev);
96 int (*hotplug)(struct xenbus_device *, char **, int, char *, int);
98 struct device_driver driver;
101 int (*read_otherend_details)(struct xenbus_device *dev);
102 int (*watch_otherend)(struct xenbus_device *dev);
103 int (*cleanup_device)(struct xenbus_device *dev);
104 int (*is_ready)(struct xenbus_device *dev);
105 LIST_ENTRY(xenbus_driver) list;
108 static inline struct xenbus_driver *to_xenbus_driver(driver_t *drv)
111 return container_of(drv, struct xenbus_driver, driver);
115 typedef int (*xenstore_event_handler_t)(void *);
117 int xenbus_register_frontend(struct xenbus_driver *drv);
118 int xenbus_register_backend(struct xenbus_driver *drv);
119 void xenbus_unregister_driver(struct xenbus_driver *drv);
121 int xenbus_remove_device(struct xenbus_device *dev);
123 struct xenbus_transaction
128 #define XBT_NIL ((struct xenbus_transaction) { 0 })
130 char **xenbus_directory(struct xenbus_transaction t,
131 const char *dir, const char *node, unsigned int *num);
132 void *xenbus_read(struct xenbus_transaction t,
133 const char *dir, const char *node, unsigned int *len);
134 int xenbus_write(struct xenbus_transaction t,
135 const char *dir, const char *node, const char *string);
136 int xenbus_mkdir(struct xenbus_transaction t,
137 const char *dir, const char *node);
138 int xenbus_exists(struct xenbus_transaction t,
139 const char *dir, const char *node);
140 int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node);
141 int xenbus_transaction_start(struct xenbus_transaction *t);
142 int xenbus_transaction_end(struct xenbus_transaction t, int abort);
144 /* Single read and scanf: returns -errno or num scanned if > 0. */
145 int xenbus_scanf(struct xenbus_transaction t,
146 const char *dir, const char *node, const char *fmt, ...)
147 __attribute__((format(scanf, 4, 5)));
149 /* Single printf and write: returns -errno or 0. */
150 int xenbus_printf(struct xenbus_transaction t,
151 const char *dir, const char *node, const char *fmt, ...)
152 __attribute__((format(printf, 4, 5)));
154 /* Generic read function: NULL-terminated triples of name,
155 * sprintf-style type string, and pointer. Returns 0 or errno.*/
156 int xenbus_gather(struct xenbus_transaction t, const char *dir, ...);
158 /* notifer routines for when the xenstore comes up */
159 int register_xenstore_notifier(xenstore_event_handler_t func, void *arg, int priority);
161 void unregister_xenstore_notifier();
163 int register_xenbus_watch(struct xenbus_watch *watch);
164 void unregister_xenbus_watch(struct xenbus_watch *watch);
165 void xs_suspend(void);
166 void xs_resume(void);
168 /* Used by xenbus_dev to borrow kernel's store connection. */
169 void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg);
171 /* Called from xen core code. */
172 void xenbus_suspend(void);
173 void xenbus_resume(void);
175 #define XENBUS_IS_ERR_READ(str) ({ \
176 if (!IS_ERR(str) && strlen(str) == 0) { \
177 free(str, M_DEVBUF); \
178 str = ERR_PTR(-ERANGE); \
183 #define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE)
187 * Register a watch on the given path, using the given xenbus_watch structure
188 * for storage, and the given callback function as the callback. Return 0 on
189 * success, or -errno on error. On success, the given path will be saved as
190 * watch->node, and remains the caller's to free. On error, watch->node will
191 * be NULL, the device will switch to XenbusStateClosing, and the error will
192 * be saved in the store.
194 int xenbus_watch_path(struct xenbus_device *dev, char *path,
195 struct xenbus_watch *watch,
196 void (*callback)(struct xenbus_watch *,
197 const char **, unsigned int));
201 * Register a watch on the given path/path2, using the given xenbus_watch
202 * structure for storage, and the given callback function as the callback.
203 * Return 0 on success, or -errno on error. On success, the watched path
204 * (path/path2) will be saved as watch->node, and becomes the caller's to
205 * kfree(). On error, watch->node will be NULL, so the caller has nothing to
206 * free, the device will switch to XenbusStateClosing, and the error will be
207 * saved in the store.
209 int xenbus_watch_path2(struct xenbus_device *dev, const char *path,
210 const char *path2, struct xenbus_watch *watch,
211 void (*callback)(struct xenbus_watch *,
212 const char **, unsigned int));
216 * Advertise in the store a change of the given driver to the given new_state.
217 * which case this is performed inside its own transaction. Return 0 on
218 * success, or -errno on error. On error, the device will switch to
219 * XenbusStateClosing, and the error will be saved in the store.
221 int xenbus_switch_state(struct xenbus_device *dev,
222 XenbusState new_state);
226 * Grant access to the given ring_mfn to the peer of the given device. Return
227 * 0 on success, or -errno on error. On error, the device will switch to
228 * XenbusStateClosing, and the error will be saved in the store.
230 int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn);
234 * Allocate an event channel for the given xenbus_device, assigning the newly
235 * created local port to *port. Return 0 on success, or -errno on error. On
236 * error, the device will switch to XenbusStateClosing, and the error will be
237 * saved in the store.
239 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port);
243 * Free an existing event channel. Returns 0 on success or -errno on error.
245 int xenbus_free_evtchn(struct xenbus_device *dev, int port);
249 * Return the state of the driver rooted at the given store path, or
250 * XenbusStateClosed if no state can be read.
252 XenbusState xenbus_read_driver_state(const char *path);
256 * Report the given negative errno into the store, along with the given
259 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
264 * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
265 * xenbus_switch_state(dev, NULL, XenbusStateClosing) to schedule an orderly
266 * closedown of this driver and its peer.
268 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
271 int xenbus_dev_init(void);
273 const char *xenbus_strstate(enum xenbus_state state);
274 int xenbus_dev_is_online(struct xenbus_device *dev);
275 int xenbus_frontend_closed(struct xenbus_device *dev);
277 #endif /* _ASM_XEN_XENBUS_H */
281 * c-file-style: "bsd"
282 * indent-tabs-mode: t