Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / include / xen / xenbus.h
1 /******************************************************************************
2  * xenbus.h
3  *
4  * Talks to Xen Store to figure out what devices we have.
5  *
6  * Copyright (C) 2005 Rusty Russell, IBM Corporation
7  * Copyright (C) 2005 XenSource Ltd.
8  * 
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:
14  * 
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:
21  * 
22  * The above copyright notice and this permission notice shall be included in
23  * all copies or substantial portions of the Software.
24  * 
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
31  * IN THE SOFTWARE.
32  */
33
34 #ifndef _XEN_XENBUS_H
35 #define _XEN_XENBUS_H
36
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>
45
46 /* Register callback to watch this node. */
47 struct xenbus_watch
48 {
49         struct list_head list;
50
51         /* Path being watched. */
52         const char *node;
53
54         /* Callback (executed in a process context with no locks held). */
55         void (*callback)(struct xenbus_watch *,
56                          const char **vec, unsigned int len);
57
58         /* See XBWF_ definitions below. */
59         unsigned long flags;
60 };
61
62 /*
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).
66  */
67 #define XBWF_new_thread 1
68
69 /* A xenbus device. */
70 struct xenbus_device {
71         const char *devicetype;
72         const char *nodename;
73         const char *otherend;
74         int otherend_id;
75         struct xenbus_watch otherend_watch;
76         struct device dev;
77         enum xenbus_state state;
78         struct completion down;
79 };
80
81 static inline struct xenbus_device *to_xenbus_device(struct device *dev)
82 {
83         return container_of(dev, struct xenbus_device, dev);
84 }
85
86 struct xenbus_device_id
87 {
88         /* .../device/<device_type>/<identifier> */
89         char devicetype[32];    /* General class of device. */
90 };
91
92 /* A xenbus driver. */
93 struct xenbus_driver {
94         char *name;
95         struct module *owner;
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);
107 };
108
109 static inline struct xenbus_driver *to_xenbus_driver(struct device_driver *drv)
110 {
111         return container_of(drv, struct xenbus_driver, driver);
112 }
113
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);
117
118 struct xenbus_transaction
119 {
120         u32 id;
121 };
122
123 /* Nil transaction ID. */
124 #define XBT_NIL ((struct xenbus_transaction) { 0 })
125
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);
139
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)));
144
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)));
149
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, ...);
153
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);
157
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);
162
163 /* Used by xenbus_dev to borrow kernel's store connection. */
164 void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg);
165
166 /* Called from xen core code. */
167 void xenbus_suspend(void);
168 void xenbus_resume(void);
169
170 #define XENBUS_IS_ERR_READ(str) ({                      \
171         if (!IS_ERR(str) && strlen(str) == 0) {         \
172                 kfree(str);                             \
173                 str = ERR_PTR(-ERANGE);                 \
174         }                                               \
175         IS_ERR(str);                                    \
176 })
177
178 #define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE)
179
180
181 /**
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.
188  */
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));
193
194
195 /**
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.
203  */
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));
208
209
210 /**
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.
214  */
215 int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state new_state);
216
217
218 /**
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.
222  */
223 int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn);
224
225
226 /**
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.
235  */
236 struct vm_struct *xenbus_map_ring_valloc(struct xenbus_device *dev,
237                                          int gnt_ref);
238 int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
239                            grant_handle_t *handle, void *vaddr);
240
241
242 /**
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).
248  */
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);
252
253
254 /**
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.
259  */
260 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port);
261
262
263 /**
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.
267  */
268 int xenbus_bind_evtchn(struct xenbus_device *dev, int remote_port, int *port);
269
270
271 /**
272  * Free an existing event channel. Returns 0 on success or -errno on error.
273  */
274 int xenbus_free_evtchn(struct xenbus_device *dev, int port);
275
276
277 /**
278  * Return the state of the driver rooted at the given store path, or
279  * XenbusStateUnknown if no state can be read.
280  */
281 enum xenbus_state xenbus_read_driver_state(const char *path);
282
283
284 /***
285  * Report the given negative errno into the store, along with the given
286  * formatted message.
287  */
288 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
289                       ...);
290
291
292 /***
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.
296  */
297 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
298                       ...);
299
300 int __init xenbus_dev_init(void);
301
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);
305
306 #endif /* _XEN_XENBUS_H */