xref: /netbsd-src/sys/arch/xen/include/xenbus.h (revision c24c993fe4cf289234b8ce9b47d92eb1278cfbda)
1 /* $NetBSD: xenbus.h,v 1.25 2020/04/25 15:26:17 bouyer Exp $ */
2 /******************************************************************************
3  * xenbus.h
4  *
5  * Talks to Xen Store to figure out what devices we have.
6  *
7  * Copyright (C) 2005 Rusty Russell, IBM Corporation
8  * Copyright (C) 2005 XenSource Ltd.
9  *
10  * This file may be distributed separately from the Linux kernel, or
11  * incorporated into other software packages, subject to the following license:
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a copy
14  * of this source file (the "Software"), to deal in the Software without
15  * restriction, including without limitation the rights to use, copy, modify,
16  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17  * and to permit persons to whom the Software is furnished to do so, subject to
18  * the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  * IN THE SOFTWARE.
30  */
31 
32 #ifndef _ASM_XEN_XENBUS_H
33 #define _ASM_XEN_XENBUS_H
34 
35 #include <sys/device.h>
36 #include <sys/bus.h>
37 #include <sys/queue.h>
38 #include <xen/include/public/xen.h>
39 #include <xen/include/public/io/xenbus.h>
40 #include <xen/include/public/io/xs_wire.h>
41 #include <xen/include/public/grant_table.h>	/* for grant_ref_t */
42 
43 /* xenbus to hypervisor attach */
44 struct xenbus_attach_args {
45 	const char 		*xa_device;
46 	int			xa_handle;
47 	bus_dma_tag_t		xa_dmat;
48 };
49 
50 /* devices to xenbus attach */
51 struct xenbusdev_attach_args {
52 	const char		*xa_type;
53 	int 			xa_id;
54 	struct xenbus_device	*xa_xbusd;
55 };
56 
57 /* Register callback to watch this node. */
58 struct xenbus_watch {
59 	SLIST_ENTRY(xenbus_watch) watch_next;
60 
61 	/* Path being watched. */
62 	char *node;
63 	size_t node_sz;
64 
65 	/* Callback (executed in a process context with no locks held). */
66 	void (*xbw_callback)(struct xenbus_watch *,
67 			 const char **vec, unsigned int len);
68 	struct xenbus_device *xbw_dev;
69 };
70 
71 
72 /*
73  * A xenbus device. Note that the malloced memory will be larger than
74  * sizeof(xenbus_device) to have the storage for xbusd_path, so xbusd_path
75  * has to be the last entry.
76  */
77 typedef enum {
78 	XENBUS_FRONTEND_DEVICE,
79 	XENBUS_BACKEND_DEVICE
80 } xenbusdev_type_t;
81 
82 struct xenbus_device {
83 	SLIST_ENTRY(xenbus_device) xbusd_entries;
84 	char xbusd_otherend[64]; /* the otherend path (size arbitrary) */
85 	int xbusd_otherend_id; /* the otherend's id */
86 	/* callback for otherend change */
87 	void (*xbusd_otherend_changed)(void *, XenbusState);
88 	xenbusdev_type_t xbusd_type;
89 	union {
90 		struct {
91 			device_t f_dev;
92 		} f;
93 		struct {
94 			void *b_cookie; /* private to backend driver */
95 			int (*b_detach)(void *);
96 		} b;
97 	} xbusd_u;
98 	int xbusd_has_error;
99 	/* for xenbus internal use */
100 	struct xenbus_watch xbusd_otherend_watch;
101 	size_t xbusd_sz;		/* size of allocated structure */
102 	bus_dma_tag_t xbusd_dmat;
103 	const char xbusd_path[1]; /* our path */
104 };
105 
106 /*
107  * frontend devices use the normal autoconf(9) framework to attach.
108  * backend drivers need something more clever because we want the
109  * domain's name or uid in the device's name. Each backend driver registers
110  * to xenbus.
111  */
112 
113 struct xenbus_backend_driver {
114 	SLIST_ENTRY(xenbus_backend_driver) xbakd_entries;
115 	int (*xbakd_create) (struct xenbus_device *); /* called for new devs */
116 	const char *xbakd_type; /* device type we register for */
117 };
118 
119 void xenbus_backend_register(struct xenbus_backend_driver *);
120 
121 struct xenbus_transaction;
122 
123 int xenbus_directory(struct xenbus_transaction *t,
124 			const char *dir, const char *node, unsigned int *num,
125 			char ***);
126 void xenbus_directory_free(unsigned int, char **);
127 int xenbus_read(struct xenbus_transaction *,
128 		  const char *, const char *, char *, size_t);
129 int xenbus_read_ul(struct xenbus_transaction *,
130 		  const char *, const char *, unsigned long *, int);
131 int xenbus_read_ull(struct xenbus_transaction *,
132 		  const char *, const char *, unsigned long long *, int);
133 int xenbus_write(struct xenbus_transaction *t,
134 		 const char *dir, const char *node, const char *string);
135 int xenbus_mkdir(struct xenbus_transaction *t,
136 		 const char *dir, const char *node);
137 int xenbus_exists(struct xenbus_transaction *t,
138 		  const char *dir, const char *node);
139 int xenbus_rm(struct xenbus_transaction *t, const char *dir, const char *node);
140 struct xenbus_transaction *xenbus_transaction_start(void);
141 int xenbus_transaction_end(struct xenbus_transaction *t, int abort);
142 
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)));
147 
148 /* notifer routines for when the xenstore comes up */
149 // XXX int register_xenstore_notifier(struct notifier_block *nb);
150 // XXX void unregister_xenstore_notifier(struct notifier_block *nb);
151 
152 int register_xenbus_watch(struct xenbus_watch *watch);
153 void unregister_xenbus_watch(struct xenbus_watch *watch);
154 void xs_suspend(void);
155 void xs_resume(void);
156 
157 /* Used by xenbus_dev to borrow kernel's store connection. */
158 int xenbus_dev_request_and_reply(struct xsd_sockmsg *msg, void **);
159 void xenbus_dev_reply_free(struct xsd_sockmsg *msg, void *);
160 
161 void xenbus_probe(void *);
162 
163 int xenbus_free_device(struct xenbus_device *);
164 
165 #define XENBUS_IS_ERR_READ(str) ({			\
166 	if (!IS_ERR(str) && strlen(str) == 0) {		\
167 		kfree(str);				\
168 		str = ERR_PTR(-ERANGE);			\
169 	}						\
170 	IS_ERR(str);					\
171 })
172 
173 #define XENBUS_EXIST_ERR(err) ((err) == -ENOENT || (err) == -ERANGE)
174 
175 
176 /**
177  * Register a watch on the given path/path2, using the given xenbus_watch
178  * structure for storage, and the given callback function as the callback.
179  * Return 0 on success, or -errno on error.  On success, the watched path
180  * (path/path2) will be saved as watch->node, and becomes the caller's to
181  * kfree().  On error, watch->node will be NULL, so the caller has nothing to
182  * free, the device will switch to XenbusStateClosing, and the error will be
183  * saved in the store.
184  */
185 int xenbus_watch_path2(struct xenbus_device *dev, const char *path,
186 		       const char *path2, struct xenbus_watch *watch,
187 		       void (*callback)(struct xenbus_watch *,
188 					const char **, unsigned int));
189 
190 /* Unregister the watch, and free associated internal structures. */
191 void xenbus_unwatch_path(struct xenbus_watch *);
192 
193 /**
194  * Advertise in the store a change of the given driver to the given new_state.
195  * Perform the change inside the given transaction xbt.  xbt may be NULL, in
196  * which case this is performed inside its own transaction.  Return 0 on
197  * success, or -errno on error.  On error, the device will switch to
198  * XenbusStateClosing, and the error will be saved in the store.
199  */
200 int xenbus_switch_state(struct xenbus_device *dev,
201 			struct xenbus_transaction *xbt,
202 			XenbusState new_state);
203 
204 
205 /**
206  * Grant access to the given ring_mfn to the peer of the given device.  Return
207  * 0 on success, or -errno on error.  On error, the device will switch to
208  * XenbusStateClosing, and the error will be saved in the store.
209  */
210 int xenbus_grant_ring(struct xenbus_device *, paddr_t, grant_ref_t *);
211 
212 
213 /**
214  * Allocate an event channel for the given xenbus_device, assigning the newly
215  * created local port to *port.  Return 0 on success, or -errno on error.  On
216  * error, the device will switch to XenbusStateClosing, and the error will be
217  * saved in the store.
218  */
219 int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port);
220 
221 
222 /**
223  * Return the state of the driver rooted at the given store path, or
224  * XenbusStateClosed if no state can be read.
225  */
226 XenbusState xenbus_read_driver_state(const char *path);
227 
228 
229 /***
230  * Report the given negative errno into the store, along with the given
231  * formatted message.
232  */
233 void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt,
234 		      ...);
235 
236 
237 /***
238  * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
239  * xenbus_switch_state(dev, NULL, XenbusStateClosing) to schedule an orderly
240  * closedown of this driver and its peer.
241  */
242 void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt,
243 		      ...);
244 
245 bool xenbus_device_suspend(struct xenbus_device *);
246 bool xenbus_device_resume(struct xenbus_device *);
247 
248 #endif /* _ASM_XEN_XENBUS_H */
249 
250 /*
251  * Local variables:
252  *  c-file-style: "linux"
253  *  indent-tabs-mode: t
254  *  c-indent-level: 8
255  *  c-basic-offset: 8
256  *  tab-width: 8
257  * End:
258  */
259