xref: /illumos-gate/usr/src/uts/common/xen/sys/xenbus_impl.h (revision 46b592853d0f4f11781b6b0a7533f267c6aee132)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  *
29  * xenbus.h (renamed to xenbus_impl.h)
30  *
31  * Talks to Xen Store to figure out what devices we have.
32  *
33  * Copyright (C) 2005 Rusty Russell, IBM Corporation
34  *
35  * This file may be distributed separately from the Linux kernel, or
36  * incorporated into other software packages, subject to the following license:
37  *
38  * Permission is hereby granted, free of charge, to any person obtaining a copy
39  * of this source file (the "Software"), to deal in the Software without
40  * restriction, including without limitation the rights to use, copy, modify,
41  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
42  * and to permit persons to whom the Software is furnished to do so, subject to
43  * the following conditions:
44  *
45  * The above copyright notice and this permission notice shall be included in
46  * all copies or substantial portions of the Software.
47  *
48  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
51  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
54  * IN THE SOFTWARE.
55  */
56 
57 #ifndef _SYS_XENBUS_H
58 #define	_SYS_XENBUS_H
59 
60 #include <sys/mutex.h>
61 #include <sys/list.h>
62 
63 #ifdef	__cplusplus
64 extern "C" {
65 #endif
66 
67 #define	XBT_NULL 0
68 
69 typedef uint32_t xenbus_transaction_t;
70 
71 /* Register callback to watch this node. */
72 struct xenbus_watch;
73 typedef void (*xenbus_watch_cb_t)(struct xenbus_watch *,
74     const char **vec, unsigned int len);
75 struct xenbus_watch {
76 	list_node_t 		list;
77 	const char		*node;	/* path being watched */
78 	xenbus_watch_cb_t	callback;
79 	struct xenbus_device	*dev;
80 };
81 
82 /*
83  * Call this function when xenstore is available, i.e. the daemon is
84  * connected to the xenbus device.
85  */
86 struct xenbus_notify {
87 	list_node_t list;
88 	void (*notify_func) (int);
89 };
90 
91 /* A xenbus device. */
92 struct xenbus_device {
93 	const char *devicetype;
94 	const char *nodename;
95 	const char *otherend;
96 	int otherend_id;
97 	int otherend_state;
98 	struct xenbus_watch otherend_watch;
99 	int has_error;
100 	int frontend;
101 	void (*otherend_changed)(struct xenbus_device *, XenbusState);
102 	void *data;
103 };
104 
105 typedef void (*xvdi_xb_watch_cb_t)(dev_info_t *dip, const char *path,
106     void *arg);
107 
108 typedef struct xd_xb_watches {
109 	list_node_t		xxw_list;
110 	int			xxw_ref;
111 	struct xenbus_watch	xxw_watch;
112 	struct xendev_ppd	*xxw_xppd;
113 	xvdi_xb_watch_cb_t	xxw_cb;
114 	void			*xxw_arg;
115 } xd_xb_watches_t;
116 
117 extern char **xenbus_directory(xenbus_transaction_t t, const char *dir,
118 	    const char *node, unsigned int *num);
119 extern int xenbus_read(xenbus_transaction_t t, const char *dir,
120 	    const char *node, void **rstr, unsigned int *len);
121 extern int xenbus_read_str(const char *dir, const char *node, char **rstr);
122 extern int xenbus_write(xenbus_transaction_t t, const char *dir,
123 	    const char *node, const char *string);
124 extern int xenbus_mkdir(xenbus_transaction_t t, const char *dir,
125 	    const char *node);
126 extern boolean_t xenbus_exists(const char *dir, const char *node);
127 extern boolean_t xenbus_exists_dir(const char *dir, const char *node);
128 extern int xenbus_rm(xenbus_transaction_t t, const char *dir,
129 	    const char *node);
130 extern int xenbus_transaction_start(xenbus_transaction_t *t);
131 extern int xenbus_transaction_end(xenbus_transaction_t t, int abort);
132 
133 /* Single read and scanf: returns errno or num scanned if > 0. */
134 extern int xenbus_scanf(xenbus_transaction_t t, const char *dir,
135 	    const char *node, const char *fmt, ...);
136 
137 /* Single printf and write: returns errno or 0. */
138 extern int xenbus_printf(xenbus_transaction_t t, const char *dir,
139 	    const char *node, const char *fmt, ...);
140 
141 /*
142  * Generic read function: NULL-terminated triples of name,
143  * sprintf-style type string, and pointer. Returns 0 or errno.
144  */
145 extern int xenbus_gather(xenbus_transaction_t t, const char *dir, ...);
146 
147 extern int register_xenbus_watch(struct xenbus_watch *watch);
148 extern void unregister_xenbus_watch(struct xenbus_watch *watch);
149 extern void reregister_xenbus_watches(void);
150 
151 /* Called from xen core code. */
152 extern void xenbus_suspend(void);
153 extern void xenbus_resume(void);
154 
155 #define	XENBUS_EXIST_ERR(err) ((err) == ENOENT || (err) == ERANGE)
156 
157 /*
158  * Register a watch on the given path, using the given xenbus_watch structure
159  * for storage, and the given callback function as the callback.  Return 0 on
160  * success, or errno on error.  On success, the given path will be saved as
161  * watch->node, and remains the caller's to free.  On error, watch->node will
162  * be NULL, the device will switch to XenbusStateClosing, and the error will
163  * be saved in the store.
164  */
165 extern int xenbus_watch_path(struct xenbus_device *dev, const char *path,
166 			struct xenbus_watch *watch,
167 			void (*callback)(struct xenbus_watch *,
168 			const char **, unsigned int));
169 
170 
171 /*
172  * Register a watch on the given path/path2, using the given xenbus_watch
173  * structure for storage, and the given callback function as the callback.
174  * Return 0 on success, or errno on error.  On success, the watched path
175  * (path/path2) will be saved as watch->node, and becomes the caller's to
176  * kfree().  On error, watch->node will be NULL, so the caller has nothing to
177  * free, the device will switch to XenbusStateClosing, and the error will be
178  * saved in the store.
179  */
180 extern int xenbus_watch_path2(struct xenbus_device *dev, const char *path,
181 			const char *path2, struct xenbus_watch *watch,
182 			void (*callback)(struct xenbus_watch *,
183 			const char **, unsigned int));
184 
185 
186 /*
187  * Advertise in the store a change of the given driver to the given new_state.
188  * Perform the change inside the given transaction xbt.  xbt may be NULL, in
189  * which case this is performed inside its own transaction.  Return 0 on
190  * success, or errno on error.  On error, the device will switch to
191  * XenbusStateClosing, and the error will be saved in the store.
192  */
193 extern int xenbus_switch_state(struct xenbus_device *dev,
194 			xenbus_transaction_t xbt,
195 			XenbusState new_state);
196 
197 
198 /*
199  * Grant access to the given ring_mfn to the peer of the given device.  Return
200  * 0 on success, or errno on error.  On error, the device will switch to
201  * XenbusStateClosing, and the error will be saved in the store.
202  */
203 extern int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn);
204 
205 
206 /*
207  * Allocate an event channel for the given xenbus_device, assigning the newly
208  * created local port to *port.  Return 0 on success, or errno on error.  On
209  * error, the device will switch to XenbusStateClosing, and the error will be
210  * saved in the store.
211  */
212 extern int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port);
213 
214 
215 /*
216  * Return the state of the driver rooted at the given store path, or
217  * XenbusStateClosed if no state can be read.
218  */
219 extern XenbusState xenbus_read_driver_state(const char *path);
220 
221 
222 /*
223  * Report the given negative errno into the store, along with the given
224  * formatted message.
225  */
226 extern void xenbus_dev_error(struct xenbus_device *dev, int err,
227 	const char *fmt, ...);
228 
229 
230 /*
231  * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
232  * xenbus_switch_state(dev, NULL, XenbusStateClosing) to schedule an orderly
233  * closedown of this driver and its peer.
234  */
235 extern void xenbus_dev_fatal(struct xenbus_device *dev,
236 	int err, const char *fmt, ...);
237 
238 /* Clear any error. */
239 extern void xenbus_dev_ok(struct xenbus_device *dev);
240 
241 /*
242  * Set up watches on other end of split device.
243  */
244 extern int talk_to_otherend(struct xenbus_device *dev);
245 
246 #define	XENSTORE_DOWN	0	/* xenstore is down */
247 #define	XENSTORE_UP	1	/* xenstore is up */
248 
249 /*
250  * Register a notify callback function.
251  */
252 extern int xs_register_xenbus_callback(void (*callback)(int));
253 
254 /*
255  * Notify clients that xenstore is up
256  */
257 extern void xs_notify_xenstore_up(void);
258 
259 /*
260  * Notify clients that xenstore is down
261  */
262 extern void xs_notify_xenstore_down(void);
263 
264 struct xsd_sockmsg;
265 
266 extern int xenbus_dev_request_and_reply(struct xsd_sockmsg *, void **);
267 
268 #ifdef	__cplusplus
269 }
270 #endif
271 
272 #endif /* _SYS_XENBUS_H */
273