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 program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
37 #include <linux/device.h>
38 #include <linux/notifier.h>
39 #include <linux/mutex.h>
40 #include <linux/completion.h>
41 #include <xen/interface/xen.h>
42 #include <xen/interface/grant_table.h>
43 #include <xen/interface/io/xenbus.h>
44 #include <xen/interface/io/xs_wire.h>
46 /* Register callback to watch this node. */
49 struct list_head list;
51 /* Path being watched. */
54 /* Callback (executed in a process context with no locks held). */
55 void (*callback)(struct xenbus_watch *,
56 const char **vec, unsigned int len);
58 /* See XBWF_ definitions below. */
63 * Execute callback in its own kthread. Useful if the callback is long
64 * running or heavily serialised, to avoid taking out the main xenwatch thread
65 * for a long period of time (or even unwittingly causing a deadlock).
67 #define XBWF_new_thread 1
69 /* A xenbus device. */
70 struct xenbus_device {
71 const char *devicetype;
75 struct xenbus_watch otherend_watch;
77 enum xenbus_state state;
78 struct completion down;
81 static inline struct xenbus_device *to_xenbus_device(struct device *dev)
83 return container_of(dev, struct xenbus_device, dev);
86 struct xenbus_device_id
88 /* .../device/<device_type>/<identifier> */
89 char devicetype[32]; /* General class of device. */
92 /* A xenbus driver. */
93 struct xenbus_driver {
96 const struct xenbus_device_id *ids;
97 int (*probe)(struct xenbus_device *dev,
98 const struct xenbus_device_id *id);
99 void (*otherend_changed)(struct xenbus_device *dev,
100 enum xenbus_state backend_state);
101 int (*remove)(struct xenbus_device *dev);
102 int (*suspend)(struct xenbus_device *dev);
103 int (*resume)(struct xenbus_device *dev);
104 int (*uevent)(struct xenbus_device *, char **, int, char *, int);
105 struct device_driver driver;
106 int (*read_otherend_details)(struct xenbus_device *dev);
109 static inline struct xenbus_driver *to_xenbus_driver(struct device_driver *drv)
111 return container_of(drv, struct xenbus_driver, driver);
114 int xenbus_register_frontend(struct xenbus_driver *drv);
115 int xenbus_register_backend(struct xenbus_driver *drv);
116 void xenbus_unregister_driver(struct xenbus_driver *drv);
118 struct xenbus_transaction
123 /* Nil transaction ID. */
124 #define XBT_NIL ((struct xenbus_transaction) { 0 })
126 char **xenbus_directory(struct xenbus_transaction t,
127 const char *dir, const char *node, unsigned int *num);
128 void *xenbus_read(struct xenbus_transaction t,
129 const char *dir, const char *node, unsigned int *len);
130 int xenbus_write(struct xenbus_transaction t,
131 const char *dir, const char *node, const char *string);
132 int xenbus_mkdir(struct xenbus_transaction t,
133 const char *dir, const char *node);
134 int xenbus_exists(struct xenbus_transaction t,
135 const char *dir, const char *node);
136 int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node);
137 int xenbus_transaction_start(struct xenbus_transaction *t);
138 int xenbus_transaction_end(struct xenbus_transaction t, int abort);
140 /* Single read and scanf: returns -errno or num scanned if > 0. */
141 int xenbus_scanf(struct xenbus_transaction t,
142 const char *dir, const char *node, const char *fmt, ...)
143 __attribute__((format(scanf, 4, 5)));
145 /* Single printf and write: returns -errno or 0. */
146 int xenbus_printf(struct xenbus_transaction t,
147 const char *dir, const char *node, const char *fmt, ...)
148 __attribute__((format(printf, 4, 5)));
150 /* Generic read function: NULL-terminated triples of name,
151 * sprintf-style type string, and pointer. Returns 0 or errno.*/
152 int xenbus_gather(struct xenbus_transaction t, const char *dir, ...);
154 /* notifer routines for when the xenstore comes up */
155 int register_xenstore_notifier(struct notifier_block *nb);
156 void unregister_xenstore_notifier(struct notifier_block *nb);
158 int register_xenbus_watch(struct xenbus_watch *watch);
159 void unregister_xenbus_watch(struct xenbus_watch *watch);
160 void xs_suspend(void);
161 void xs_resume(void);
163 /* Used by xenbus_dev to borrow kernel's store connection. */
164 void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg);
166 /* Called from xen core code. */
167 void xenbus_suspend(void);
168 void xenbus_resume(void);
170 #define XENBUS_IS_ERR_READ(str) ({ \
171 if (!IS_ERR(str) && strlen(str) == 0) { \
173 str = ERR_PTR(-ERANGE); \
178 #define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE)
182 * Register a watch on the given path, using the given xenbus_watch structure
183 * for storage, and the given callback function as the callback. Return 0 on
184 * success, or -errno on error. On success, the given path will be saved as
185 * watch->node, and remains the caller's to free. On error, watch->node will
186 * be NULL, the device will switch to XenbusStateClosing, and the error will
187 * be saved in the store.
189 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
190 struct xenbus_watch *watch,
191 void (*callback)(struct xenbus_watch *,
192 const char **, unsigned int));
196 * Register a watch on the given path/path2, using the given xenbus_watch
197 * structure for storage, and the given callback function as the callback.
198 * Return 0 on success, or -errno on error. On success, the watched path
199 * (path/path2) will be saved as watch->node, and becomes the caller's to
200 * kfree(). On error, watch->node will be NULL, so the caller has nothing to
201 * free, the device will switch to XenbusStateClosing, and the error will be
202 * saved in the store.
204 int xenbus_watch_path2(struct xenbus_device *dev, const char *path,
205 const char *path2, struct xenbus_watch *watch,
206 void (*callback)(struct xenbus_watch *,
207 const char **, unsigned int));
211 * Advertise in the store a change of the given driver to the given new_state.
212 * Return 0 on success, or -errno on error. On error, the device will switch
213 * to XenbusStateClosing, and the error will be saved in the store.
215 int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state new_state);
219 * Grant access to the given ring_mfn to the peer of the given device. Return
220 * 0 on success, or -errno on error. On error, the device will switch to
221 * XenbusStateClosing, and the error will be saved in the store.
223 int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn);
227 * Map a page of memory into this domain from another domain's grant table.
228 * xenbus_map_ring_valloc allocates a page of virtual address space, maps the
229 * page to that address, and sets *vaddr to that address.
230 * xenbus_map_ring does not allocate the virtual address space (you must do
231 * this yourself!). It only maps in the page to the specified address.
232 * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
233 * or -ENOMEM on error. If an error is returned, device will switch to
234 * XenbusStateClosing and the error message will be saved in XenStore.
236 struct vm_struct *xenbus_map_ring_valloc(struct xenbus_device *dev,
238 int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
239 grant_handle_t *handle, void *vaddr);
243 * Unmap a page of memory in this domain that was imported from another domain.
244 * Use xenbus_unmap_ring_vfree if you mapped in your memory with
245 * xenbus_map_ring_valloc (it will free the virtual address space).
246 * Returns 0 on success and returns GNTST_* on error
247 * (see xen/include/interface/grant_table.h).
249 int xenbus_unmap_ring_vfree(struct xenbus_device *dev, struct vm_struct *);
250 int xenbus_unmap_ring(struct xenbus_device *dev,
251 grant_handle_t handle, void *vaddr);
255 * Allocate an event channel for the given xenbus_device, assigning the newly
256 * created local port to *port. Return 0 on success, or -errno on error. On
257 * error, the device will switch to XenbusStateClosing, and the error will be
258 * saved in the store.
260 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port);
264 * Bind to an existing interdomain event channel in another domain. Returns 0
265 * on success and stores the local port in *port. On error, returns -errno,
266 * switches the device to XenbusStateClosing, and saves the error in XenStore.
268 int xenbus_bind_evtchn(struct xenbus_device *dev, int remote_port, int *port);
272 * Free an existing event channel. Returns 0 on success or -errno on error.
274 int xenbus_free_evtchn(struct xenbus_device *dev, int port);
278 * Return the state of the driver rooted at the given store path, or
279 * XenbusStateUnknown if no state can be read.
281 enum xenbus_state xenbus_read_driver_state(const char *path);
285 * Report the given negative errno into the store, along with the given
288 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
293 * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
294 * xenbus_switch_state(dev, NULL, XenbusStateClosing) to schedule an orderly
295 * closedown of this driver and its peer.
297 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
300 int __init xenbus_dev_init(void);
302 char *xenbus_strstate(enum xenbus_state state);
303 int xenbus_dev_is_online(struct xenbus_device *dev);
304 int xenbus_frontend_closed(struct xenbus_device *dev);
306 #endif /* _XEN_XENBUS_H */