xenbus_probe.c revision 1.43 1 1.43 jdolecek /* $NetBSD: xenbus_probe.c,v 1.43 2020/04/07 13:36:22 jdolecek Exp $ */
2 1.1 bouyer /******************************************************************************
3 1.1 bouyer * Talks to Xen Store to figure out what devices we have.
4 1.1 bouyer *
5 1.1 bouyer * Copyright (C) 2005 Rusty Russell, IBM Corporation
6 1.1 bouyer * Copyright (C) 2005 Mike Wray, Hewlett-Packard
7 1.1 bouyer * Copyright (C) 2005 XenSource Ltd
8 1.1 bouyer *
9 1.1 bouyer * This file may be distributed separately from the Linux kernel, or
10 1.1 bouyer * incorporated into other software packages, subject to the following license:
11 1.1 bouyer *
12 1.1 bouyer * Permission is hereby granted, free of charge, to any person obtaining a copy
13 1.1 bouyer * of this source file (the "Software"), to deal in the Software without
14 1.1 bouyer * restriction, including without limitation the rights to use, copy, modify,
15 1.1 bouyer * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 1.1 bouyer * and to permit persons to whom the Software is furnished to do so, subject to
17 1.1 bouyer * the following conditions:
18 1.1 bouyer *
19 1.1 bouyer * The above copyright notice and this permission notice shall be included in
20 1.1 bouyer * all copies or substantial portions of the Software.
21 1.1 bouyer *
22 1.1 bouyer * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 1.1 bouyer * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 1.1 bouyer * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 1.1 bouyer * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 1.1 bouyer * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 1.1 bouyer * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 1.1 bouyer * IN THE SOFTWARE.
29 1.1 bouyer */
30 1.1 bouyer
31 1.2 bouyer #include <sys/cdefs.h>
32 1.43 jdolecek __KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.43 2020/04/07 13:36:22 jdolecek Exp $");
33 1.2 bouyer
34 1.1 bouyer #if 0
35 1.1 bouyer #define DPRINTK(fmt, args...) \
36 1.18 perry printf("xenbus_probe (%s:%d) " fmt ".\n", __func__, __LINE__, ##args)
37 1.1 bouyer #else
38 1.1 bouyer #define DPRINTK(fmt, args...) ((void)0)
39 1.1 bouyer #endif
40 1.1 bouyer
41 1.2 bouyer #include <sys/types.h>
42 1.2 bouyer #include <sys/null.h>
43 1.2 bouyer #include <sys/errno.h>
44 1.2 bouyer #include <sys/malloc.h>
45 1.2 bouyer #include <sys/systm.h>
46 1.2 bouyer #include <sys/param.h>
47 1.2 bouyer #include <sys/kthread.h>
48 1.7 bouyer #include <uvm/uvm.h>
49 1.7 bouyer
50 1.26 cegger #include <xen/xen.h> /* for xendomain_is_dom0() */
51 1.17 bouyer #include <xen/hypervisor.h>
52 1.17 bouyer #include <xen/xenbus.h>
53 1.17 bouyer #include <xen/evtchn.h>
54 1.17 bouyer #include <xen/shutdown_xenbus.h>
55 1.1 bouyer
56 1.1 bouyer #include "xenbus_comms.h"
57 1.1 bouyer
58 1.1 bouyer extern struct semaphore xenwatch_mutex;
59 1.1 bouyer
60 1.1 bouyer #define streq(a, b) (strcmp((a), (b)) == 0)
61 1.1 bouyer
62 1.20 cegger static int xenbus_match(device_t, cfdata_t, void *);
63 1.20 cegger static void xenbus_attach(device_t, device_t, void *);
64 1.2 bouyer static int xenbus_print(void *, const char *);
65 1.2 bouyer
66 1.34 jym /* power management, for save/restore */
67 1.34 jym static bool xenbus_suspend(device_t, const pmf_qual_t *);
68 1.34 jym static bool xenbus_resume(device_t, const pmf_qual_t *);
69 1.34 jym
70 1.29 jym /* routines gathering device information from XenStore */
71 1.29 jym static int read_otherend_details(struct xenbus_device *,
72 1.29 jym const char *, const char *);
73 1.29 jym static int read_backend_details (struct xenbus_device *);
74 1.29 jym static int read_frontend_details(struct xenbus_device *);
75 1.29 jym static void free_otherend_details(struct xenbus_device *);
76 1.29 jym
77 1.29 jym static int watch_otherend (struct xenbus_device *);
78 1.29 jym static void free_otherend_watch(struct xenbus_device *);
79 1.29 jym
80 1.2 bouyer static void xenbus_probe_init(void *);
81 1.2 bouyer
82 1.5 bouyer static struct xenbus_device *xenbus_lookup_device_path(const char *);
83 1.5 bouyer
84 1.20 cegger CFATTACH_DECL_NEW(xenbus, 0, xenbus_match, xenbus_attach,
85 1.2 bouyer NULL, NULL);
86 1.2 bouyer
87 1.24 cegger device_t xenbus_dev;
88 1.5 bouyer
89 1.5 bouyer SLIST_HEAD(, xenbus_device) xenbus_device_list;
90 1.11 bouyer SLIST_HEAD(, xenbus_backend_driver) xenbus_backend_driver_list =
91 1.11 bouyer SLIST_HEAD_INITIALIZER(xenbus_backend_driver);
92 1.2 bouyer
93 1.2 bouyer int
94 1.20 cegger xenbus_match(device_t parent, cfdata_t match, void *aux)
95 1.2 bouyer {
96 1.2 bouyer struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
97 1.2 bouyer
98 1.2 bouyer if (strcmp(xa->xa_device, "xenbus") == 0)
99 1.2 bouyer return 1;
100 1.2 bouyer return 0;
101 1.2 bouyer }
102 1.2 bouyer
103 1.2 bouyer static void
104 1.20 cegger xenbus_attach(device_t parent, device_t self, void *aux)
105 1.2 bouyer {
106 1.15 ad int err;
107 1.15 ad
108 1.2 bouyer aprint_normal(": Xen Virtual Bus Interface\n");
109 1.24 cegger xenbus_dev = self;
110 1.38 riz config_pending_incr(self);
111 1.15 ad
112 1.15 ad err = kthread_create(PRI_NONE, 0, NULL, xenbus_probe_init, NULL,
113 1.15 ad NULL, "xenbus_probe");
114 1.15 ad if (err)
115 1.24 cegger aprint_error_dev(xenbus_dev,
116 1.23 cegger "kthread_create(xenbus_probe): %d\n", err);
117 1.34 jym
118 1.34 jym if (!pmf_device_register(self, xenbus_suspend, xenbus_resume))
119 1.34 jym aprint_error_dev(self, "couldn't establish power handler\n");
120 1.34 jym }
121 1.34 jym
122 1.34 jym static bool
123 1.34 jym xenbus_suspend(device_t dev, const pmf_qual_t *qual)
124 1.34 jym {
125 1.34 jym xs_suspend();
126 1.34 jym xb_suspend_comms(dev);
127 1.34 jym
128 1.34 jym return true;
129 1.34 jym }
130 1.34 jym
131 1.34 jym static bool
132 1.34 jym xenbus_resume(device_t dev, const pmf_qual_t *qual)
133 1.34 jym {
134 1.34 jym xb_init_comms(dev);
135 1.34 jym xs_resume();
136 1.34 jym
137 1.34 jym return true;
138 1.34 jym }
139 1.34 jym
140 1.34 jym /*
141 1.34 jym * Suspend a xenbus device
142 1.34 jym */
143 1.34 jym bool
144 1.34 jym xenbus_device_suspend(struct xenbus_device *dev) {
145 1.34 jym
146 1.34 jym free_otherend_details(dev);
147 1.34 jym return true;
148 1.34 jym }
149 1.34 jym
150 1.34 jym /*
151 1.34 jym * Resume a xenbus device
152 1.34 jym */
153 1.34 jym bool
154 1.34 jym xenbus_device_resume(struct xenbus_device *dev) {
155 1.34 jym
156 1.34 jym if (dev->xbusd_type == XENBUS_FRONTEND_DEVICE) {
157 1.34 jym read_backend_details(dev);
158 1.34 jym }
159 1.34 jym
160 1.34 jym return true;
161 1.2 bouyer }
162 1.2 bouyer
163 1.2 bouyer void
164 1.11 bouyer xenbus_backend_register(struct xenbus_backend_driver *xbakd)
165 1.11 bouyer {
166 1.11 bouyer SLIST_INSERT_HEAD(&xenbus_backend_driver_list, xbakd, xbakd_entries);
167 1.11 bouyer }
168 1.11 bouyer
169 1.2 bouyer static int
170 1.2 bouyer read_otherend_details(struct xenbus_device *xendev,
171 1.2 bouyer const char *id_node, const char *path_node)
172 1.2 bouyer {
173 1.2 bouyer int err;
174 1.42 jdolecek unsigned long id;
175 1.1 bouyer
176 1.42 jdolecek err = xenbus_read_ul(NULL, xendev->xbusd_path, id_node, &id, 10);
177 1.2 bouyer if (err) {
178 1.2 bouyer printf("reading other end details %s from %s\n",
179 1.2 bouyer id_node, xendev->xbusd_path);
180 1.2 bouyer xenbus_dev_fatal(xendev, err,
181 1.2 bouyer "reading other end details %s from %s",
182 1.2 bouyer id_node, xendev->xbusd_path);
183 1.2 bouyer return err;
184 1.2 bouyer }
185 1.42 jdolecek xendev->xbusd_otherend_id = (int)id;
186 1.42 jdolecek
187 1.42 jdolecek err = xenbus_read(NULL, xendev->xbusd_path, path_node,
188 1.42 jdolecek xendev->xbusd_otherend, sizeof(xendev->xbusd_otherend));
189 1.1 bouyer if (err) {
190 1.2 bouyer printf("reading other end details %s from %s (%d)\n",
191 1.2 bouyer path_node, xendev->xbusd_path, err);
192 1.1 bouyer xenbus_dev_fatal(xendev, err,
193 1.2 bouyer "reading other end details %s from %s",
194 1.2 bouyer path_node, xendev->xbusd_path);
195 1.1 bouyer return err;
196 1.1 bouyer }
197 1.3 bouyer DPRINTK("read_otherend_details: read %s/%s returned %s\n",
198 1.3 bouyer xendev->xbusd_path, path_node, val);
199 1.2 bouyer
200 1.2 bouyer if (strlen(xendev->xbusd_otherend) == 0 ||
201 1.2 bouyer !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
202 1.2 bouyer printf("missing other end from %s\n", xendev->xbusd_path);
203 1.1 bouyer xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
204 1.2 bouyer xendev->xbusd_path);
205 1.29 jym free_otherend_details(xendev);
206 1.2 bouyer return ENOENT;
207 1.1 bouyer }
208 1.1 bouyer
209 1.1 bouyer return 0;
210 1.1 bouyer }
211 1.1 bouyer
212 1.2 bouyer static int
213 1.2 bouyer read_backend_details(struct xenbus_device *xendev)
214 1.1 bouyer {
215 1.1 bouyer return read_otherend_details(xendev, "backend-id", "backend");
216 1.1 bouyer }
217 1.1 bouyer
218 1.1 bouyer
219 1.2 bouyer static int
220 1.2 bouyer read_frontend_details(struct xenbus_device *xendev)
221 1.1 bouyer {
222 1.1 bouyer return read_otherend_details(xendev, "frontend-id", "frontend");
223 1.1 bouyer }
224 1.1 bouyer
225 1.2 bouyer static void
226 1.2 bouyer free_otherend_details(struct xenbus_device *dev)
227 1.1 bouyer {
228 1.42 jdolecek /* Nothing to free */
229 1.42 jdolecek dev->xbusd_otherend[0] = '\0';
230 1.1 bouyer }
231 1.1 bouyer
232 1.2 bouyer static void
233 1.2 bouyer free_otherend_watch(struct xenbus_device *dev)
234 1.1 bouyer {
235 1.2 bouyer if (dev->xbusd_otherend_watch.node) {
236 1.2 bouyer unregister_xenbus_watch(&dev->xbusd_otherend_watch);
237 1.43 jdolecek free(dev->xbusd_otherend_watch.node, M_DEVBUF);
238 1.2 bouyer dev->xbusd_otherend_watch.node = NULL;
239 1.1 bouyer }
240 1.1 bouyer }
241 1.1 bouyer
242 1.2 bouyer static void
243 1.2 bouyer otherend_changed(struct xenbus_watch *watch,
244 1.1 bouyer const char **vec, unsigned int len)
245 1.1 bouyer {
246 1.3 bouyer struct xenbus_device *xdev = watch->xbw_dev;
247 1.1 bouyer XenbusState state;
248 1.1 bouyer
249 1.1 bouyer /* Protect us against watches firing on old details when the otherend
250 1.1 bouyer details change, say immediately after a resume. */
251 1.3 bouyer if (!xdev->xbusd_otherend ||
252 1.3 bouyer strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
253 1.3 bouyer strlen(xdev->xbusd_otherend))) {
254 1.1 bouyer DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
255 1.1 bouyer return;
256 1.1 bouyer }
257 1.1 bouyer
258 1.3 bouyer state = xenbus_read_driver_state(xdev->xbusd_otherend);
259 1.1 bouyer
260 1.1 bouyer DPRINTK("state is %d, %s, %s",
261 1.3 bouyer state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
262 1.5 bouyer if (state == XenbusStateClosed) {
263 1.5 bouyer int error;
264 1.11 bouyer if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
265 1.11 bouyer error = xdev->xbusd_u.b.b_detach(
266 1.11 bouyer xdev->xbusd_u.b.b_cookie);
267 1.11 bouyer if (error) {
268 1.11 bouyer printf("could not detach %s: %d\n",
269 1.11 bouyer xdev->xbusd_path, error);
270 1.11 bouyer return;
271 1.11 bouyer }
272 1.11 bouyer } else {
273 1.11 bouyer error = config_detach(xdev->xbusd_u.f.f_dev,
274 1.11 bouyer DETACH_FORCE);
275 1.11 bouyer if (error) {
276 1.11 bouyer printf("could not detach %s: %d\n",
277 1.19 cegger device_xname(xdev->xbusd_u.f.f_dev), error);
278 1.11 bouyer return;
279 1.11 bouyer }
280 1.5 bouyer }
281 1.5 bouyer xenbus_free_device(xdev);
282 1.5 bouyer return;
283 1.5 bouyer }
284 1.3 bouyer if (xdev->xbusd_otherend_changed)
285 1.11 bouyer xdev->xbusd_otherend_changed(
286 1.11 bouyer (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
287 1.11 bouyer xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
288 1.1 bouyer }
289 1.1 bouyer
290 1.2 bouyer static int
291 1.29 jym watch_otherend(struct xenbus_device *dev)
292 1.1 bouyer {
293 1.1 bouyer free_otherend_watch(dev);
294 1.1 bouyer
295 1.2 bouyer return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
296 1.3 bouyer &dev->xbusd_otherend_watch,
297 1.3 bouyer otherend_changed);
298 1.1 bouyer }
299 1.1 bouyer
300 1.5 bouyer static struct xenbus_device *
301 1.5 bouyer xenbus_lookup_device_path(const char *path)
302 1.5 bouyer {
303 1.5 bouyer struct xenbus_device *xbusd;
304 1.5 bouyer
305 1.5 bouyer SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
306 1.5 bouyer if (strcmp(xbusd->xbusd_path, path) == 0)
307 1.5 bouyer return xbusd;
308 1.5 bouyer }
309 1.5 bouyer return NULL;
310 1.5 bouyer }
311 1.5 bouyer
312 1.2 bouyer static int
313 1.11 bouyer xenbus_probe_device_type(const char *path, const char *type,
314 1.11 bouyer int (*create)(struct xenbus_device *))
315 1.1 bouyer {
316 1.36 sborrill int err, i, pos, msize;
317 1.36 sborrill int *lookup = NULL;
318 1.11 bouyer unsigned long state;
319 1.1 bouyer char **dir;
320 1.1 bouyer unsigned int dir_n = 0;
321 1.2 bouyer struct xenbus_device *xbusd;
322 1.2 bouyer struct xenbusdev_attach_args xa;
323 1.2 bouyer char *ep;
324 1.2 bouyer
325 1.11 bouyer DPRINTK("probe %s type %s", path, type);
326 1.11 bouyer err = xenbus_directory(NULL, path, "", &dir_n, &dir);
327 1.2 bouyer DPRINTK("directory err %d dir_n %d", err, dir_n);
328 1.2 bouyer if (err)
329 1.2 bouyer return err;
330 1.1 bouyer
331 1.36 sborrill /* Only sort frontend devices i.e. create == NULL*/
332 1.36 sborrill if (dir_n > 1 && create == NULL) {
333 1.36 sborrill int minp;
334 1.36 sborrill unsigned long minv;
335 1.36 sborrill unsigned long *id;
336 1.36 sborrill
337 1.36 sborrill lookup = malloc(sizeof(int) * dir_n, M_DEVBUF,
338 1.36 sborrill M_WAITOK | M_ZERO);
339 1.36 sborrill if (lookup == NULL)
340 1.36 sborrill panic("can't malloc lookup");
341 1.36 sborrill
342 1.36 sborrill id = malloc(sizeof(unsigned long) * dir_n, M_DEVBUF,
343 1.36 sborrill M_WAITOK | M_ZERO);
344 1.36 sborrill if (id == NULL)
345 1.36 sborrill panic("can't malloc id");
346 1.36 sborrill
347 1.36 sborrill /* Convert string values to numeric; skip invalid */
348 1.36 sborrill for (i = 0; i < dir_n; i++) {
349 1.37 sborrill /*
350 1.37 sborrill * Add one to differentiate numerical zero from invalid
351 1.37 sborrill * string. Has no effect on sort order.
352 1.37 sborrill */
353 1.37 sborrill id[i] = strtoul(dir[i], &ep, 10) + 1;
354 1.36 sborrill if (dir[i][0] == '\0' || *ep != '\0')
355 1.36 sborrill id[i] = 0;
356 1.36 sborrill }
357 1.36 sborrill
358 1.36 sborrill /* Build lookup table in ascending order */
359 1.36 sborrill for (pos = 0; pos < dir_n; ) {
360 1.36 sborrill minv = UINT32_MAX;
361 1.36 sborrill minp = -1;
362 1.36 sborrill for (i = 0; i < dir_n; i++) {
363 1.36 sborrill if (id[i] < minv && id[i] > 0) {
364 1.36 sborrill minv = id[i];
365 1.36 sborrill minp = i;
366 1.36 sborrill }
367 1.36 sborrill }
368 1.36 sborrill if (minp >= 0) {
369 1.36 sborrill lookup[pos++] = minp;
370 1.36 sborrill id[minp] = 0;
371 1.36 sborrill }
372 1.36 sborrill else
373 1.36 sborrill break;
374 1.36 sborrill }
375 1.36 sborrill
376 1.36 sborrill free(id, M_DEVBUF);
377 1.36 sborrill /* Adjust in case we had to skip non-numeric entries */
378 1.36 sborrill dir_n = pos;
379 1.36 sborrill }
380 1.36 sborrill
381 1.36 sborrill for (pos = 0; pos < dir_n; pos++) {
382 1.30 cegger err = 0;
383 1.36 sborrill if (lookup)
384 1.36 sborrill i = lookup[pos];
385 1.36 sborrill else
386 1.36 sborrill i = pos;
387 1.5 bouyer /*
388 1.5 bouyer * add size of path to size of xenbus_device. xenbus_device
389 1.5 bouyer * already has room for one char in xbusd_path.
390 1.5 bouyer */
391 1.11 bouyer msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
392 1.5 bouyer xbusd = malloc(msize, M_DEVBUF, M_WAITOK | M_ZERO);
393 1.2 bouyer if (xbusd == NULL)
394 1.2 bouyer panic("can't malloc xbusd");
395 1.2 bouyer
396 1.2 bouyer snprintf(__UNCONST(xbusd->xbusd_path),
397 1.11 bouyer msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
398 1.5 bouyer if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
399 1.5 bouyer /* device already registered */
400 1.5 bouyer free(xbusd, M_DEVBUF);
401 1.5 bouyer continue;
402 1.5 bouyer }
403 1.11 bouyer err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
404 1.12 bouyer &state, 10);
405 1.11 bouyer if (err) {
406 1.11 bouyer printf("xenbus: can't get state "
407 1.11 bouyer "for %s (%d)\n", xbusd->xbusd_path, err);
408 1.11 bouyer free(xbusd, M_DEVBUF);
409 1.31 cegger err = 0;
410 1.11 bouyer continue;
411 1.11 bouyer }
412 1.11 bouyer if (state != XenbusStateInitialising) {
413 1.11 bouyer /* device is not new */
414 1.11 bouyer free(xbusd, M_DEVBUF);
415 1.11 bouyer continue;
416 1.11 bouyer }
417 1.5 bouyer
418 1.3 bouyer xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
419 1.2 bouyer DPRINTK("xenbus_probe_device_type probe %s\n",
420 1.2 bouyer xbusd->xbusd_path);
421 1.11 bouyer if (create != NULL) {
422 1.11 bouyer xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
423 1.11 bouyer err = read_frontend_details(xbusd);
424 1.11 bouyer if (err != 0) {
425 1.11 bouyer printf("xenbus: can't get frontend details "
426 1.11 bouyer "for %s (%d)\n", xbusd->xbusd_path, err);
427 1.11 bouyer break;
428 1.11 bouyer }
429 1.11 bouyer if (create(xbusd)) {
430 1.11 bouyer free(xbusd, M_DEVBUF);
431 1.11 bouyer continue;
432 1.11 bouyer }
433 1.11 bouyer } else {
434 1.11 bouyer xbusd->xbusd_type = XENBUS_FRONTEND_DEVICE;
435 1.11 bouyer xa.xa_xbusd = xbusd;
436 1.11 bouyer xa.xa_type = type;
437 1.11 bouyer xa.xa_id = strtoul(dir[i], &ep, 0);
438 1.11 bouyer if (dir[i][0] == '\0' || *ep != '\0') {
439 1.11 bouyer printf("xenbus device type %s: id %s is not a"
440 1.11 bouyer " number\n", type, dir[i]);
441 1.11 bouyer err = EFTYPE;
442 1.11 bouyer free(xbusd, M_DEVBUF);
443 1.11 bouyer break;
444 1.11 bouyer }
445 1.11 bouyer err = read_backend_details(xbusd);
446 1.11 bouyer if (err != 0) {
447 1.11 bouyer printf("xenbus: can't get backend details "
448 1.11 bouyer "for %s (%d)\n", xbusd->xbusd_path, err);
449 1.11 bouyer break;
450 1.11 bouyer }
451 1.24 cegger xbusd->xbusd_u.f.f_dev = config_found_ia(xenbus_dev,
452 1.11 bouyer "xenbus", &xa, xenbus_print);
453 1.11 bouyer if (xbusd->xbusd_u.f.f_dev == NULL) {
454 1.11 bouyer free(xbusd, M_DEVBUF);
455 1.11 bouyer continue;
456 1.11 bouyer }
457 1.3 bouyer }
458 1.11 bouyer SLIST_INSERT_HEAD(&xenbus_device_list,
459 1.11 bouyer xbusd, xbusd_entries);
460 1.29 jym watch_otherend(xbusd);
461 1.1 bouyer }
462 1.2 bouyer free(dir, M_DEVBUF);
463 1.36 sborrill if (lookup)
464 1.36 sborrill free(lookup, M_DEVBUF);
465 1.36 sborrill
466 1.1 bouyer return err;
467 1.1 bouyer }
468 1.1 bouyer
469 1.2 bouyer static int
470 1.2 bouyer xenbus_print(void *aux, const char *pnp)
471 1.2 bouyer {
472 1.2 bouyer struct xenbusdev_attach_args *xa = aux;
473 1.2 bouyer
474 1.2 bouyer if (pnp) {
475 1.2 bouyer if (strcmp(xa->xa_type, "vbd") == 0)
476 1.2 bouyer aprint_normal("xbd");
477 1.2 bouyer else if (strcmp(xa->xa_type, "vif") == 0)
478 1.2 bouyer aprint_normal("xennet");
479 1.32 jym else if (strcmp(xa->xa_type, "balloon") == 0)
480 1.32 jym aprint_normal("balloon");
481 1.2 bouyer else
482 1.2 bouyer aprint_normal("unknown type %s", xa->xa_type);
483 1.2 bouyer aprint_normal(" at %s", pnp);
484 1.2 bouyer }
485 1.2 bouyer aprint_normal(" id %d", xa->xa_id);
486 1.2 bouyer return(UNCONF);
487 1.2 bouyer }
488 1.2 bouyer
489 1.2 bouyer static int
490 1.11 bouyer xenbus_probe_frontends(void)
491 1.1 bouyer {
492 1.2 bouyer int err;
493 1.1 bouyer char **dir;
494 1.1 bouyer unsigned int i, dir_n;
495 1.11 bouyer char path[30];
496 1.1 bouyer
497 1.11 bouyer DPRINTK("probe device");
498 1.11 bouyer err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
499 1.2 bouyer DPRINTK("directory err %d dir_n %d", err, dir_n);
500 1.2 bouyer if (err)
501 1.2 bouyer return err;
502 1.1 bouyer
503 1.1 bouyer for (i = 0; i < dir_n; i++) {
504 1.27 jym /*
505 1.27 jym * console is configured through xen_start_info when
506 1.27 jym * xencons is attaching to hypervisor, so avoid console
507 1.27 jym * probing when configuring xenbus devices
508 1.27 jym */
509 1.27 jym if (strcmp(dir[i], "console") == 0)
510 1.27 jym continue;
511 1.27 jym
512 1.11 bouyer snprintf(path, sizeof(path), "device/%s", dir[i]);
513 1.11 bouyer err = xenbus_probe_device_type(path, dir[i], NULL);
514 1.1 bouyer if (err)
515 1.1 bouyer break;
516 1.1 bouyer }
517 1.2 bouyer free(dir, M_DEVBUF);
518 1.1 bouyer return err;
519 1.1 bouyer }
520 1.1 bouyer
521 1.11 bouyer static int
522 1.11 bouyer xenbus_probe_backends(void)
523 1.11 bouyer {
524 1.11 bouyer int err;
525 1.11 bouyer char **dirt, **dirid;
526 1.11 bouyer unsigned int type, id, dirt_n, dirid_n;
527 1.11 bouyer char path[30];
528 1.11 bouyer struct xenbus_backend_driver *xbakd;
529 1.11 bouyer
530 1.11 bouyer DPRINTK("probe backend");
531 1.11 bouyer err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
532 1.11 bouyer DPRINTK("directory err %d dirt_n %d", err, dirt_n);
533 1.11 bouyer if (err)
534 1.11 bouyer return err;
535 1.11 bouyer
536 1.11 bouyer for (type = 0; type < dirt_n; type++) {
537 1.11 bouyer SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
538 1.11 bouyer xbakd_entries) {
539 1.11 bouyer if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
540 1.11 bouyer break;
541 1.11 bouyer }
542 1.11 bouyer if (xbakd == NULL)
543 1.11 bouyer continue;
544 1.11 bouyer err = xenbus_directory(NULL, "backend", dirt[type],
545 1.11 bouyer &dirid_n, &dirid);
546 1.11 bouyer DPRINTK("directory backend/%s err %d dirid_n %d",
547 1.11 bouyer dirt[type], err, dirid_n);
548 1.14 christos if (err) {
549 1.14 christos free(dirt, M_DEVBUF); /* to be checked */
550 1.11 bouyer return err;
551 1.14 christos }
552 1.11 bouyer for (id = 0; id < dirid_n; id++) {
553 1.11 bouyer snprintf(path, sizeof(path), "backend/%s/%s",
554 1.11 bouyer dirt[type], dirid[id]);
555 1.11 bouyer err = xenbus_probe_device_type(path, dirt[type],
556 1.11 bouyer xbakd->xbakd_create);
557 1.11 bouyer if (err)
558 1.11 bouyer break;
559 1.11 bouyer }
560 1.11 bouyer free(dirid, M_DEVBUF);
561 1.11 bouyer }
562 1.11 bouyer free(dirt, M_DEVBUF);
563 1.11 bouyer return err;
564 1.11 bouyer }
565 1.11 bouyer
566 1.5 bouyer int
567 1.5 bouyer xenbus_free_device(struct xenbus_device *xbusd)
568 1.5 bouyer {
569 1.5 bouyer KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
570 1.5 bouyer SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
571 1.5 bouyer free_otherend_watch(xbusd);
572 1.29 jym free_otherend_details(xbusd);
573 1.5 bouyer xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
574 1.5 bouyer free(xbusd, M_DEVBUF);
575 1.5 bouyer return 0;
576 1.5 bouyer }
577 1.5 bouyer
578 1.2 bouyer static void
579 1.2 bouyer frontend_changed(struct xenbus_watch *watch,
580 1.1 bouyer const char **vec, unsigned int len)
581 1.1 bouyer {
582 1.11 bouyer DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
583 1.11 bouyer xenbus_probe_frontends();
584 1.1 bouyer }
585 1.1 bouyer
586 1.2 bouyer static void
587 1.2 bouyer backend_changed(struct xenbus_watch *watch,
588 1.1 bouyer const char **vec, unsigned int len)
589 1.1 bouyer {
590 1.11 bouyer DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
591 1.11 bouyer xenbus_probe_backends();
592 1.1 bouyer }
593 1.1 bouyer
594 1.2 bouyer
595 1.1 bouyer /* We watch for devices appearing and vanishing. */
596 1.3 bouyer static struct xenbus_watch fe_watch;
597 1.1 bouyer
598 1.3 bouyer static struct xenbus_watch be_watch;
599 1.1 bouyer
600 1.1 bouyer /* A flag to determine if xenstored is 'ready' (i.e. has started) */
601 1.39 msaitoh int xenstored_ready = 0;
602 1.1 bouyer
603 1.2 bouyer void
604 1.2 bouyer xenbus_probe(void *unused)
605 1.1 bouyer {
606 1.32 jym struct xenbusdev_attach_args balloon_xa = {
607 1.32 jym .xa_id = 0,
608 1.32 jym .xa_type = "balloon"
609 1.32 jym };
610 1.32 jym
611 1.39 msaitoh KASSERT((xenstored_ready > 0));
612 1.1 bouyer
613 1.1 bouyer /* Enumerate devices in xenstore. */
614 1.11 bouyer xenbus_probe_frontends();
615 1.11 bouyer xenbus_probe_backends();
616 1.1 bouyer
617 1.1 bouyer /* Watch for changes. */
618 1.43 jdolecek fe_watch.node = malloc(strlen("device") + 1, M_DEVBUF, M_NOWAIT);
619 1.43 jdolecek strcpy(fe_watch.node, "device");
620 1.3 bouyer fe_watch.xbw_callback = frontend_changed;
621 1.1 bouyer register_xenbus_watch(&fe_watch);
622 1.43 jdolecek be_watch.node = malloc(strlen("backend") + 1, M_DEVBUF, M_NOWAIT);
623 1.43 jdolecek strcpy(be_watch.node, "backend");
624 1.3 bouyer be_watch.xbw_callback = backend_changed;
625 1.1 bouyer register_xenbus_watch(&be_watch);
626 1.32 jym
627 1.32 jym /* attach balloon. */
628 1.32 jym config_found_ia(xenbus_dev, "xenbus", &balloon_xa, xenbus_print);
629 1.32 jym
630 1.13 yamt shutdown_xenbus_setup();
631 1.1 bouyer
632 1.1 bouyer /* Notify others that xenstore is up */
633 1.2 bouyer //notifier_call_chain(&xenstore_chain, 0, NULL);
634 1.1 bouyer }
635 1.1 bouyer
636 1.2 bouyer static void
637 1.2 bouyer xenbus_probe_init(void *unused)
638 1.1 bouyer {
639 1.23 cegger int err = 0;
640 1.23 cegger bool dom0;
641 1.23 cegger vaddr_t page = 0;
642 1.1 bouyer
643 1.1 bouyer DPRINTK("");
644 1.1 bouyer
645 1.5 bouyer SLIST_INIT(&xenbus_device_list);
646 1.5 bouyer
647 1.1 bouyer /*
648 1.1 bouyer ** Domain0 doesn't have a store_evtchn or store_mfn yet.
649 1.1 bouyer */
650 1.23 cegger dom0 = xendomain_is_dom0();
651 1.1 bouyer if (dom0) {
652 1.7 bouyer #if defined(DOM0OPS)
653 1.7 bouyer paddr_t ma;
654 1.22 tron evtchn_op_t op = { .cmd = 0 };
655 1.1 bouyer
656 1.1 bouyer /* Allocate page. */
657 1.7 bouyer page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
658 1.7 bouyer UVM_KMF_ZERO | UVM_KMF_WIRED);
659 1.1 bouyer if (!page)
660 1.7 bouyer panic("can't get xenstore page");
661 1.1 bouyer
662 1.7 bouyer (void)pmap_extract_ma(pmap_kernel(), page, &ma);
663 1.7 bouyer xen_start_info.store_mfn = ma >> PAGE_SHIFT;
664 1.7 bouyer xenstore_interface = (void *)page;
665 1.1 bouyer
666 1.1 bouyer /* Next allocate a local port which xenstored can bind to */
667 1.1 bouyer op.cmd = EVTCHNOP_alloc_unbound;
668 1.1 bouyer op.u.alloc_unbound.dom = DOMID_SELF;
669 1.39 msaitoh op.u.alloc_unbound.remote_dom = 0;
670 1.1 bouyer
671 1.23 cegger err = HYPERVISOR_event_channel_op(&op);
672 1.23 cegger if (err) {
673 1.24 cegger aprint_error_dev(xenbus_dev,
674 1.23 cegger "can't register xenstore event\n");
675 1.23 cegger goto err0;
676 1.23 cegger }
677 1.23 cegger
678 1.7 bouyer xen_start_info.store_evtchn = op.u.alloc_unbound.port;
679 1.1 bouyer
680 1.21 cegger DELAY(1000);
681 1.7 bouyer #else /* DOM0OPS */
682 1.23 cegger kthread_exit(0); /* can't get a working xenstore in this case */
683 1.7 bouyer #endif /* DOM0OPS */
684 1.1 bouyer }
685 1.1 bouyer
686 1.35 jym /* Publish xenbus and Xenstore info in /kern/xen */
687 1.35 jym xenbus_kernfs_init();
688 1.35 jym
689 1.10 bouyer /* register event handler */
690 1.24 cegger xb_init_comms(xenbus_dev);
691 1.10 bouyer
692 1.1 bouyer /* Initialize the interface to xenstore. */
693 1.39 msaitoh err = xs_init(xenbus_dev);
694 1.1 bouyer if (err) {
695 1.24 cegger aprint_error_dev(xenbus_dev,
696 1.39 msaitoh "Error initializing xenstore comms: %i\n", err);
697 1.23 cegger goto err0;
698 1.1 bouyer }
699 1.1 bouyer
700 1.1 bouyer if (!dom0) {
701 1.1 bouyer xenstored_ready = 1;
702 1.1 bouyer xenbus_probe(NULL);
703 1.1 bouyer }
704 1.1 bouyer
705 1.2 bouyer DPRINTK("done");
706 1.38 riz config_pending_decr(xenbus_dev);
707 1.11 bouyer #ifdef DOM0OPS
708 1.11 bouyer if (dom0) {
709 1.11 bouyer int s;
710 1.11 bouyer s = spltty();
711 1.11 bouyer while (xenstored_ready == 0) {
712 1.11 bouyer tsleep(&xenstored_ready, PRIBIO, "xsready", 0);
713 1.11 bouyer xenbus_probe(NULL);
714 1.11 bouyer }
715 1.11 bouyer splx(s);
716 1.11 bouyer }
717 1.11 bouyer #endif
718 1.2 bouyer kthread_exit(0);
719 1.23 cegger
720 1.23 cegger err0:
721 1.23 cegger if (page)
722 1.23 cegger uvm_km_free(kernel_map, page, PAGE_SIZE,
723 1.23 cegger UVM_KMF_ZERO | UVM_KMF_WIRED);
724 1.23 cegger kthread_exit(err);
725 1.1 bouyer }
726 1.1 bouyer
727 1.1 bouyer /*
728 1.1 bouyer * Local variables:
729 1.1 bouyer * c-file-style: "linux"
730 1.1 bouyer * indent-tabs-mode: t
731 1.1 bouyer * c-indent-level: 8
732 1.1 bouyer * c-basic-offset: 8
733 1.1 bouyer * tab-width: 8
734 1.1 bouyer * End:
735 1.1 bouyer */
736