]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/include/xen/xenbus.h
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / sys / i386 / 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 file may be distributed separately from the Linux kernel, or
10  * incorporated into other software packages, subject to the following license:
11  * 
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:
18  * 
19  * The above copyright notice and this permission notice shall be included in
20  * all copies or substantial portions of the Software.
21  * 
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
28  * IN THE SOFTWARE.
29  */
30
31 #ifndef _ASM_XEN_XENBUS_H
32 #define _ASM_XEN_XENBUS_H
33
34 #include <sys/queue.h>
35 #include <sys/bus.h>
36 #include <sys/eventhandler.h>
37 #include <xen/interface/io/xenbus.h>
38 #include <xen/interface/io/xs_wire.h>
39
40 LIST_HEAD(xendev_list_head, xenbus_device); 
41
42 /* Register callback to watch this node. */
43 struct xenbus_watch
44 {
45         LIST_ENTRY(xenbus_watch) list;
46
47         /* Path being watched. */
48         char *node;
49
50         /* Callback (executed in a process context with no locks held). */
51         void (*callback)(struct xenbus_watch *,
52                          const char **vec, unsigned int len);
53 };
54
55
56 /* A xenbus device. */
57 struct xenbus_device {
58                 struct xenbus_watch otherend_watch; /* must be first */
59                 const char *devicetype;
60                 const char *nodename;
61                 const char *otherend;
62                 int otherend_id;
63                 struct xendev_list_head *bus;
64                 struct xenbus_driver *driver;
65                 int has_error;
66                 enum xenbus_state state;
67                 void *dev_driver_data;
68                 LIST_ENTRY(xenbus_device) list;
69 };
70
71 static inline struct xenbus_device *to_xenbus_device(device_t dev)
72 {       
73                 return device_get_softc(dev);
74 }
75
76 struct xenbus_device_id
77 {
78         /* .../device/<device_type>/<identifier> */
79         char devicetype[32];    /* General class of device. */
80 };
81
82 /* A xenbus driver. */
83 struct xenbus_driver {
84                 char *name;
85                 struct module *owner;
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);
95 #if 0
96                 struct device_driver driver;
97 #endif
98                 driver_t 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;
104 };
105
106 static inline struct xenbus_driver *to_xenbus_driver(driver_t *drv)
107 {
108 #if 0
109         return container_of(drv, struct xenbus_driver, driver);
110 #endif
111         return NULL;
112 }
113 typedef int (*xenstore_event_handler_t)(void *);
114
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);
118
119 int xenbus_remove_device(struct xenbus_device *dev);
120
121 struct xenbus_transaction
122 {
123                 uint32_t id;
124 };
125
126 #define XBT_NIL ((struct xenbus_transaction) { 0 })
127
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);
141
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)));
146
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)));
151
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, ...);
155
156 /* notifer routines for when the xenstore comes up */
157 int register_xenstore_notifier(xenstore_event_handler_t func, void *arg, int priority);
158 #if 0
159 void unregister_xenstore_notifier();
160 #endif
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);
165
166 /* Used by xenbus_dev to borrow kernel's store connection. */
167 void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg);
168
169 /* Called from xen core code. */
170 void xenbus_suspend(void);
171 void xenbus_resume(void);
172
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);                 \
177         }                                               \
178         IS_ERR(str);                                    \
179 })
180
181 #define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE)
182
183
184 /**
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.
191  */
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));
196
197
198 /**
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.
206  */
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));
211
212
213 /**
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.
218  */
219 int xenbus_switch_state(struct xenbus_device *dev,
220                         XenbusState new_state);
221
222
223 /**
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.
227  */
228 int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn);
229
230
231 /**
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.
236  */
237 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port);
238
239
240 /**
241  * Free an existing event channel. Returns 0 on success or -errno on error.
242  */
243 int xenbus_free_evtchn(struct xenbus_device *dev, int port);
244
245
246 /**
247  * Return the state of the driver rooted at the given store path, or
248  * XenbusStateClosed if no state can be read.
249  */
250 XenbusState xenbus_read_driver_state(const char *path);
251
252
253 /***
254  * Report the given negative errno into the store, along with the given
255  * formatted message.
256  */
257 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
258                       ...);
259
260
261 /***
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.
265  */
266 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
267                       ...);
268
269 int xenbus_dev_init(void);
270
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);
274
275 #endif /* _ASM_XEN_XENBUS_H */
276
277 /*
278  * Local variables:
279  *  c-file-style: "bsd"
280  *  indent-tabs-mode: t
281  *  c-indent-level: 4
282  *  c-basic-offset: 8
283  *  tab-width: 4
284  * End:
285  */