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 <xen/interface/xen.h>
41 #include <xen/interface/grant_table.h>
42 #include <xen/interface/io/xenbus.h>
43 #include <xen/interface/io/xs_wire.h>
45 /* Register callback to watch this node. */
48 struct list_head list;
50 /* Path being watched. */
53 /* Callback (executed in a process context with no locks held). */
54 void (*callback)(struct xenbus_watch *,
55 const char **vec, unsigned int len);
57 /* See XBWF_ definitions below. */
62 * Execute callback in its own kthread. Useful if the callback is long
63 * running or heavily serialised, to avoid taking out the main xenwatch thread
64 * for a long period of time (or even unwittingly causing a deadlock).
66 #define XBWF_new_thread 1
68 /* A xenbus device. */
69 struct xenbus_device {
70 const char *devicetype;
74 struct xenbus_watch otherend_watch;
76 enum xenbus_state state;
79 static inline struct xenbus_device *to_xenbus_device(struct device *dev)
81 return container_of(dev, struct xenbus_device, dev);
84 struct xenbus_device_id
86 /* .../device/<device_type>/<identifier> */
87 char devicetype[32]; /* General class of device. */
90 /* A xenbus driver. */
91 struct xenbus_driver {
94 const struct xenbus_device_id *ids;
95 int (*probe)(struct xenbus_device *dev,
96 const struct xenbus_device_id *id);
97 void (*otherend_changed)(struct xenbus_device *dev,
98 enum xenbus_state backend_state);
99 int (*remove)(struct xenbus_device *dev);
100 int (*suspend)(struct xenbus_device *dev);
101 int (*resume)(struct xenbus_device *dev);
102 int (*uevent)(struct xenbus_device *, char **, int, char *, int);
103 struct device_driver driver;
104 int (*read_otherend_details)(struct xenbus_device *dev);
107 static inline struct xenbus_driver *to_xenbus_driver(struct device_driver *drv)
109 return container_of(drv, struct xenbus_driver, driver);
112 int xenbus_register_frontend(struct xenbus_driver *drv);
113 int xenbus_register_backend(struct xenbus_driver *drv);
114 void xenbus_unregister_driver(struct xenbus_driver *drv);
116 struct xenbus_transaction
121 /* Nil transaction ID. */
122 #define XBT_NIL ((struct xenbus_transaction) { 0 })
124 char **xenbus_directory(struct xenbus_transaction t,
125 const char *dir, const char *node, unsigned int *num);
126 void *xenbus_read(struct xenbus_transaction t,
127 const char *dir, const char *node, unsigned int *len);
128 int xenbus_write(struct xenbus_transaction t,
129 const char *dir, const char *node, const char *string);
130 int xenbus_mkdir(struct xenbus_transaction t,
131 const char *dir, const char *node);
132 int xenbus_exists(struct xenbus_transaction t,
133 const char *dir, const char *node);
134 int xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node);
135 int xenbus_transaction_start(struct xenbus_transaction *t);
136 int xenbus_transaction_end(struct xenbus_transaction t, int abort);
138 /* Single read and scanf: returns -errno or num scanned if > 0. */
139 int xenbus_scanf(struct xenbus_transaction t,
140 const char *dir, const char *node, const char *fmt, ...)
141 __attribute__((format(scanf, 4, 5)));
143 /* Single printf and write: returns -errno or 0. */
144 int xenbus_printf(struct xenbus_transaction t,
145 const char *dir, const char *node, const char *fmt, ...)
146 __attribute__((format(printf, 4, 5)));
148 /* Generic read function: NULL-terminated triples of name,
149 * sprintf-style type string, and pointer. Returns 0 or errno.*/
150 int xenbus_gather(struct xenbus_transaction t, const char *dir, ...);
152 /* notifer routines for when the xenstore comes up */
153 int register_xenstore_notifier(struct notifier_block *nb);
154 void unregister_xenstore_notifier(struct notifier_block *nb);
156 int register_xenbus_watch(struct xenbus_watch *watch);
157 void unregister_xenbus_watch(struct xenbus_watch *watch);
158 void xs_suspend(void);
159 void xs_resume(void);
161 /* Used by xenbus_dev to borrow kernel's store connection. */
162 void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg);
164 /* Called from xen core code. */
165 void xenbus_suspend(void);
166 void xenbus_resume(void);
168 #define XENBUS_IS_ERR_READ(str) ({ \
169 if (!IS_ERR(str) && strlen(str) == 0) { \
171 str = ERR_PTR(-ERANGE); \
176 #define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE)
180 * Register a watch on the given path, using the given xenbus_watch structure
181 * for storage, and the given callback function as the callback. Return 0 on
182 * success, or -errno on error. On success, the given path will be saved as
183 * watch->node, and remains the caller's to free. On error, watch->node will
184 * be NULL, the device will switch to XenbusStateClosing, and the error will
185 * be saved in the store.
187 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
188 struct xenbus_watch *watch,
189 void (*callback)(struct xenbus_watch *,
190 const char **, unsigned int));
194 * Register a watch on the given path/path2, using the given xenbus_watch
195 * structure for storage, and the given callback function as the callback.
196 * Return 0 on success, or -errno on error. On success, the watched path
197 * (path/path2) will be saved as watch->node, and becomes the caller's to
198 * kfree(). On error, watch->node will be NULL, so the caller has nothing to
199 * free, the device will switch to XenbusStateClosing, and the error will be
200 * saved in the store.
202 int xenbus_watch_path2(struct xenbus_device *dev, const char *path,
203 const char *path2, struct xenbus_watch *watch,
204 void (*callback)(struct xenbus_watch *,
205 const char **, unsigned int));
209 * Advertise in the store a change of the given driver to the given new_state.
210 * Return 0 on success, or -errno on error. On error, the device will switch
211 * to XenbusStateClosing, and the error will be saved in the store.
213 int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state new_state);
217 * Grant access to the given ring_mfn to the peer of the given device. Return
218 * 0 on 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_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn);
225 * Map a page of memory into this domain from another domain's grant table.
226 * xenbus_map_ring_valloc allocates a page of virtual address space, maps the
227 * page to that address, and sets *vaddr to that address.
228 * xenbus_map_ring does not allocate the virtual address space (you must do
229 * this yourself!). It only maps in the page to the specified address.
230 * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
231 * or -ENOMEM on error. If an error is returned, device will switch to
232 * XenbusStateClosing and the error message will be saved in XenStore.
234 struct vm_struct *xenbus_map_ring_valloc(struct xenbus_device *dev,
236 int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
237 grant_handle_t *handle, void *vaddr);
241 * Unmap a page of memory in this domain that was imported from another domain.
242 * Use xenbus_unmap_ring_vfree if you mapped in your memory with
243 * xenbus_map_ring_valloc (it will free the virtual address space).
244 * Returns 0 on success and returns GNTST_* on error
245 * (see xen/include/interface/grant_table.h).
247 int xenbus_unmap_ring_vfree(struct xenbus_device *dev, struct vm_struct *);
248 int xenbus_unmap_ring(struct xenbus_device *dev,
249 grant_handle_t handle, void *vaddr);
253 * Allocate an event channel for the given xenbus_device, assigning the newly
254 * created local port to *port. Return 0 on success, or -errno on error. On
255 * error, the device will switch to XenbusStateClosing, and the error will be
256 * saved in the store.
258 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port);
262 * Bind to an existing interdomain event channel in another domain. Returns 0
263 * on success and stores the local port in *port. On error, returns -errno,
264 * switches the device to XenbusStateClosing, and saves the error in XenStore.
266 int xenbus_bind_evtchn(struct xenbus_device *dev, int remote_port, int *port);
270 * Free an existing event channel. Returns 0 on success or -errno on error.
272 int xenbus_free_evtchn(struct xenbus_device *dev, int port);
276 * Return the state of the driver rooted at the given store path, or
277 * XenbusStateClosed if no state can be read.
279 enum xenbus_state xenbus_read_driver_state(const char *path);
283 * Report the given negative errno into the store, along with the given
286 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
291 * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
292 * xenbus_switch_state(dev, NULL, XenbusStateClosing) to schedule an orderly
293 * closedown of this driver and its peer.
295 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
299 #endif /* _XEN_XENBUS_H */