xenbus_probe.c revision 1.58.6.2 1 /* $NetBSD: xenbus_probe.c,v 1.58.6.2 2025/02/20 19:32:55 martin Exp $ */
2 /******************************************************************************
3 * Talks to Xen Store to figure out what devices we have.
4 *
5 * Copyright (C) 2005 Rusty Russell, IBM Corporation
6 * Copyright (C) 2005 Mike Wray, Hewlett-Packard
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 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.58.6.2 2025/02/20 19:32:55 martin Exp $");
33
34 #if 0
35 #define DPRINTK(fmt, args...) \
36 printf("xenbus_probe (%s:%d) " fmt ".\n", __func__, __LINE__, ##args)
37 #else
38 #define DPRINTK(fmt, args...) ((void)0)
39 #endif
40
41 #include <sys/types.h>
42 #include <sys/null.h>
43 #include <sys/errno.h>
44 #include <sys/kmem.h>
45 #include <sys/systm.h>
46 #include <sys/param.h>
47 #include <sys/kthread.h>
48 #include <sys/mutex.h>
49 #include <uvm/uvm.h>
50
51 #include <xen/xen.h> /* for xendomain_is_dom0() */
52 #include <xen/hypervisor.h>
53 #include <xen/xenbus.h>
54 #include <xen/evtchn.h>
55 #include <xen/shutdown_xenbus.h>
56
57 #include "xenbus_comms.h"
58
59 #include "kernfs.h"
60
61 static int xenbus_match(device_t, cfdata_t, void *);
62 static void xenbus_attach(device_t, device_t, void *);
63 static int xenbus_print(void *, const char *);
64
65 /* power management, for save/restore */
66 static bool xenbus_suspend(device_t, const pmf_qual_t *);
67 static bool xenbus_resume(device_t, const pmf_qual_t *);
68
69 /* routines gathering device information from XenStore */
70 static int read_otherend_details(struct xenbus_device *,
71 const char *, const char *);
72 static int read_backend_details (struct xenbus_device *);
73 static int read_frontend_details(struct xenbus_device *);
74 static void free_otherend_details(struct xenbus_device *);
75
76 static int watch_otherend (struct xenbus_device *);
77 static void free_otherend_watch(struct xenbus_device *);
78
79 static void xenbus_probe_init(void *);
80
81 static struct xenbus_device *xenbus_lookup_device_path(const char *);
82
83 CFATTACH_DECL_NEW(xenbus, 0, xenbus_match, xenbus_attach,
84 NULL, NULL);
85
86 device_t xenbus_dev;
87 bus_dma_tag_t xenbus_dmat;
88
89 SLIST_HEAD(, xenbus_device) xenbus_device_list;
90 SLIST_HEAD(, xenbus_backend_driver) xenbus_backend_driver_list =
91 SLIST_HEAD_INITIALIZER(xenbus_backend_driver);
92
93 int
94 xenbus_match(device_t parent, cfdata_t match, void *aux)
95 {
96 struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
97
98 if (strcmp(xa->xa_device, "xenbus") == 0)
99 return 1;
100 return 0;
101 }
102
103 static void
104 xenbus_attach(device_t parent, device_t self, void *aux)
105 {
106 struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
107 int err;
108
109 aprint_normal(": Xen Virtual Bus Interface\n");
110 xenbus_dev = self;
111 xenbus_dmat = xa->xa_dmat;
112 config_pending_incr(self);
113
114 err = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
115 xenbus_probe_init, NULL, NULL, "xenbus_probe");
116 if (err)
117 aprint_error_dev(xenbus_dev,
118 "kthread_create(xenbus_probe): %d\n", err);
119
120 if (!pmf_device_register(self, xenbus_suspend, xenbus_resume))
121 aprint_error_dev(self, "couldn't establish power handler\n");
122 }
123
124 static bool
125 xenbus_suspend(device_t dev, const pmf_qual_t *qual)
126 {
127 xs_suspend();
128 xb_suspend_comms(dev);
129
130 return true;
131 }
132
133 static bool
134 xenbus_resume(device_t dev, const pmf_qual_t *qual)
135 {
136 xb_resume_comms(dev);
137 xs_resume();
138
139 return true;
140 }
141
142 /*
143 * Suspend a xenbus device
144 */
145 bool
146 xenbus_device_suspend(struct xenbus_device *dev) {
147
148 free_otherend_details(dev);
149 return true;
150 }
151
152 /*
153 * Resume a xenbus device
154 */
155 bool
156 xenbus_device_resume(struct xenbus_device *dev) {
157
158 if (dev->xbusd_type == XENBUS_FRONTEND_DEVICE) {
159 read_backend_details(dev);
160 }
161
162 return true;
163 }
164
165 void
166 xenbus_backend_register(struct xenbus_backend_driver *xbakd)
167 {
168 SLIST_INSERT_HEAD(&xenbus_backend_driver_list, xbakd, xbakd_entries);
169 }
170
171 static int
172 read_otherend_details(struct xenbus_device *xendev,
173 const char *id_node, const char *path_node)
174 {
175 int err;
176 unsigned long id;
177
178 err = xenbus_read_ul(NULL, xendev->xbusd_path, id_node, &id, 10);
179 if (err) {
180 printf("reading other end details %s from %s\n",
181 id_node, xendev->xbusd_path);
182 xenbus_dev_fatal(xendev, err,
183 "reading other end details %s from %s",
184 id_node, xendev->xbusd_path);
185 return err;
186 }
187 xendev->xbusd_otherend_id = (int)id;
188
189 err = xenbus_read(NULL, xendev->xbusd_path, path_node,
190 xendev->xbusd_otherend, sizeof(xendev->xbusd_otherend));
191 if (err) {
192 printf("reading other end details %s from %s (%d)\n",
193 path_node, xendev->xbusd_path, err);
194 xenbus_dev_fatal(xendev, err,
195 "reading other end details %s from %s",
196 path_node, xendev->xbusd_path);
197 return err;
198 }
199 DPRINTK("read_otherend_details: read %s/%s returned %s\n",
200 xendev->xbusd_path, path_node, xendev->xbusd_otherend);
201
202 if (strlen(xendev->xbusd_otherend) == 0 ||
203 !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
204 printf("missing other end from %s\n", xendev->xbusd_path);
205 xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
206 xendev->xbusd_path);
207 free_otherend_details(xendev);
208 return ENOENT;
209 }
210
211 return 0;
212 }
213
214 static int
215 read_backend_details(struct xenbus_device *xendev)
216 {
217 return read_otherend_details(xendev, "backend-id", "backend");
218 }
219
220
221 static int
222 read_frontend_details(struct xenbus_device *xendev)
223 {
224 return read_otherend_details(xendev, "frontend-id", "frontend");
225 }
226
227 static void
228 free_otherend_details(struct xenbus_device *dev)
229 {
230 /* Nothing to free */
231 dev->xbusd_otherend[0] = '\0';
232 }
233
234 static void
235 free_otherend_watch(struct xenbus_device *dev)
236 {
237 if (dev->xbusd_otherend_watch.node)
238 xenbus_unwatch_path(&dev->xbusd_otherend_watch);
239 }
240
241 static void
242 otherend_changed(struct xenbus_watch *watch,
243 const char **vec, unsigned int len)
244 {
245 struct xenbus_device *xdev = watch->xbw_dev;
246 XenbusState state;
247
248 /* Protect us against watches firing on old details when the otherend
249 details change, say immediately after a resume. */
250 if (!xdev->xbusd_otherend ||
251 strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
252 strlen(xdev->xbusd_otherend))) {
253 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
254 return;
255 }
256
257 state = xenbus_read_driver_state(xdev->xbusd_otherend);
258
259 DPRINTK("state is %d, %s, %s",
260 state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
261 if (state == XenbusStateClosed) {
262 int error;
263 if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
264 error = xdev->xbusd_u.b.b_detach(
265 xdev->xbusd_u.b.b_cookie);
266 if (error) {
267 printf("could not detach %s: %d\n",
268 xdev->xbusd_path, error);
269 return;
270 }
271 } else {
272 error = config_detach(xdev->xbusd_u.f.f_dev,
273 DETACH_FORCE);
274 if (error) {
275 printf("could not detach %s: %d\n",
276 device_xname(xdev->xbusd_u.f.f_dev), error);
277 return;
278 }
279 }
280 xenbus_free_device(xdev);
281 return;
282 }
283 if (xdev->xbusd_otherend_changed)
284 xdev->xbusd_otherend_changed(
285 (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
286 xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
287 }
288
289 static int
290 watch_otherend(struct xenbus_device *dev)
291 {
292 free_otherend_watch(dev);
293
294 return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
295 &dev->xbusd_otherend_watch,
296 otherend_changed);
297 }
298
299 static struct xenbus_device *
300 xenbus_lookup_device_path(const char *path)
301 {
302 struct xenbus_device *xbusd;
303
304 SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
305 if (strcmp(xbusd->xbusd_path, path) == 0)
306 return xbusd;
307 }
308 return NULL;
309 }
310
311 static int
312 xenbus_probe_device_type(const char *path, const char *type,
313 int (*create)(struct xenbus_device *))
314 {
315 int err, i, pos, msize;
316 int *lookup = NULL;
317 size_t lookup_sz = 0;
318 unsigned long state;
319 char **dir;
320 unsigned int orig_dir_n = 0, dir_n;
321 struct xenbus_device *xbusd;
322 struct xenbusdev_attach_args xa;
323 char *ep;
324
325 DPRINTK("probe %s type %s", path, type);
326 err = xenbus_directory(NULL, path, "", &orig_dir_n, &dir);
327 DPRINTK("directory err %d dir_n %d", err, orig_dir_n);
328 if (err)
329 return err;
330 dir_n = orig_dir_n;
331
332 /* Only sort frontend devices i.e. create == NULL*/
333 if (dir_n > 1 && create == NULL) {
334 int minp;
335 unsigned long minv;
336 unsigned long *id;
337 size_t id_sz;
338
339 lookup_sz = sizeof(int) * dir_n;
340 lookup = kmem_zalloc(lookup_sz, KM_SLEEP);
341
342 id_sz = sizeof(unsigned long) * dir_n;
343 id = kmem_zalloc(id_sz, KM_SLEEP);
344
345 /* Convert string values to numeric; skip invalid */
346 for (i = 0; i < dir_n; i++) {
347 /*
348 * Add one to differentiate numerical zero from invalid
349 * string. Has no effect on sort order.
350 */
351 id[i] = strtoul(dir[i], &ep, 10) + 1;
352 if (dir[i][0] == '\0' || *ep != '\0')
353 id[i] = 0;
354 }
355
356 /* Build lookup table in ascending order */
357 for (pos = 0; pos < dir_n; ) {
358 minv = UINT32_MAX;
359 minp = -1;
360 for (i = 0; i < dir_n; i++) {
361 if (id[i] < minv && id[i] > 0) {
362 minv = id[i];
363 minp = i;
364 }
365 }
366 if (minp >= 0) {
367 lookup[pos++] = minp;
368 id[minp] = 0;
369 }
370 else
371 break;
372 }
373
374 kmem_free(id, id_sz);
375
376 /* Adjust in case we had to skip non-numeric entries */
377 dir_n = pos;
378 }
379
380 for (pos = 0; pos < dir_n; pos++) {
381 err = 0;
382 if (lookup)
383 i = lookup[pos];
384 else
385 i = pos;
386 /*
387 * add size of path to size of xenbus_device. xenbus_device
388 * already has room for one char in xbusd_path.
389 */
390 msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
391 xbusd = kmem_zalloc(msize, KM_SLEEP);
392 xbusd->xbusd_sz = msize;
393 xbusd->xbusd_dmat = xenbus_dmat;
394
395 snprintf(__UNCONST(xbusd->xbusd_path),
396 msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
397 if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
398 /* device already registered */
399 kmem_free(xbusd, xbusd->xbusd_sz);
400 continue;
401 }
402 err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
403 &state, 10);
404 if (err) {
405 aprint_error_dev(xenbus_dev, "can't get state "
406 "for %s (%d)\n", xbusd->xbusd_path, err);
407 kmem_free(xbusd, xbusd->xbusd_sz);
408 err = 0;
409 continue;
410 }
411 if (state != XenbusStateInitialising) {
412 /* device is not new */
413 kmem_free(xbusd, xbusd->xbusd_sz);
414 continue;
415 }
416
417 xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
418 DPRINTK("xenbus_probe_device_type probe %s\n",
419 xbusd->xbusd_path);
420 if (create != NULL) {
421 xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
422 err = read_frontend_details(xbusd);
423 if (err != 0) {
424 aprint_error_dev(xenbus_dev,
425 "can't get frontend details for %s (%d)\n",
426 xbusd->xbusd_path, err);
427 break;
428 }
429 if (create(xbusd)) {
430 kmem_free(xbusd, xbusd->xbusd_sz);
431 continue;
432 }
433 } else {
434 xbusd->xbusd_type = XENBUS_FRONTEND_DEVICE;
435 xa.xa_xbusd = xbusd;
436 xa.xa_type = type;
437 xa.xa_id = strtoul(dir[i], &ep, 0);
438 if (dir[i][0] == '\0' || *ep != '\0') {
439 aprint_error_dev(xenbus_dev,
440 "device type %s: id %s is not a number\n",
441 type, dir[i]);
442 err = EFTYPE;
443 kmem_free(xbusd, xbusd->xbusd_sz);
444 break;
445 }
446 if (strcmp(xa.xa_type, "vbd") == 0) {
447 char dtype[10];
448 if (xenbus_read(NULL, xbusd->xbusd_path,
449 "device-type", dtype, sizeof(dtype)) !=0) {
450 aprint_error_dev(xenbus_dev,
451 "%s: can't read device-type\n",
452 xbusd->xbusd_path);
453 kmem_free(xbusd, xbusd->xbusd_sz);
454 break;
455 }
456 if (vm_guest == VM_GUEST_XENPVHVM &&
457 strcmp(dtype, "cdrom") == 0) {
458 aprint_verbose_dev(xenbus_dev,
459 "ignoring %s type cdrom\n",
460 xbusd->xbusd_path);
461 kmem_free(xbusd, xbusd->xbusd_sz);
462 continue;
463 }
464 }
465 err = read_backend_details(xbusd);
466 if (err != 0) {
467 aprint_error_dev(xenbus_dev,
468 "can't get backend details for %s (%d)\n",
469 xbusd->xbusd_path, err);
470 kmem_free(xbusd, xbusd->xbusd_sz);
471 break;
472 }
473
474 KERNEL_LOCK(1, curlwp);
475 xbusd->xbusd_u.f.f_dev = config_found(xenbus_dev,
476 &xa, xenbus_print, CFARGS_NONE);
477 KERNEL_UNLOCK_ONE(curlwp);
478 if (xbusd->xbusd_u.f.f_dev == NULL) {
479 kmem_free(xbusd, xbusd->xbusd_sz);
480 continue;
481 }
482 }
483 SLIST_INSERT_HEAD(&xenbus_device_list,
484 xbusd, xbusd_entries);
485 watch_otherend(xbusd);
486 }
487 xenbus_directory_free(orig_dir_n, dir);
488 if (lookup)
489 kmem_free(lookup, lookup_sz);
490
491 return err;
492 }
493
494 static int
495 xenbus_print(void *aux, const char *pnp)
496 {
497 struct xenbusdev_attach_args *xa = aux;
498
499 if (pnp) {
500 if (strcmp(xa->xa_type, "vbd") == 0)
501 aprint_normal("xbd");
502 else if (strcmp(xa->xa_type, "vif") == 0)
503 aprint_normal("xennet");
504 else if (strcmp(xa->xa_type, "balloon") == 0)
505 aprint_normal("balloon");
506 else
507 aprint_normal("unknown type %s", xa->xa_type);
508 aprint_normal(" at %s", pnp);
509 }
510 aprint_normal(" id %d", xa->xa_id);
511 return(UNCONF);
512 }
513
514 static int
515 xenbus_probe_frontends(void)
516 {
517 int err;
518 char **dir;
519 unsigned int i, dir_n;
520 char path[30];
521
522 DPRINTK("probe device");
523 err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
524 DPRINTK("directory err %d dir_n %d", err, dir_n);
525 if (err)
526 return err;
527
528 for (i = 0; i < dir_n; i++) {
529 /*
530 * console is configured through xen_start_info when
531 * xencons is attaching to hypervisor, so avoid console
532 * probing when configuring xenbus devices
533 */
534 if (strcmp(dir[i], "console") == 0)
535 continue;
536
537 snprintf(path, sizeof(path), "device/%s", dir[i]);
538 err = xenbus_probe_device_type(path, dir[i], NULL);
539 if (err)
540 break;
541 }
542 xenbus_directory_free(dir_n, dir);
543 return err;
544 }
545
546 static int
547 xenbus_probe_backends(void)
548 {
549 int err;
550 char **dirt, **dirid;
551 unsigned int type, id, dirt_n, dirid_n;
552 char path[30];
553 struct xenbus_backend_driver *xbakd;
554
555 DPRINTK("probe backend");
556 err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
557 DPRINTK("directory err %d dirt_n %d", err, dirt_n);
558 if (err)
559 return err;
560
561 for (type = 0; type < dirt_n; type++) {
562 SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
563 xbakd_entries) {
564 if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
565 break;
566 }
567 if (xbakd == NULL)
568 continue;
569 err = xenbus_directory(NULL, "backend", dirt[type],
570 &dirid_n, &dirid);
571 DPRINTK("directory backend/%s err %d dirid_n %d",
572 dirt[type], err, dirid_n);
573 if (err)
574 goto out;
575
576 for (id = 0; id < dirid_n; id++) {
577 snprintf(path, sizeof(path), "backend/%s/%s",
578 dirt[type], dirid[id]);
579 err = xenbus_probe_device_type(path, dirt[type],
580 xbakd->xbakd_create);
581 if (err)
582 break;
583 }
584 xenbus_directory_free(dirid_n, dirid);
585 }
586
587 out:
588 xenbus_directory_free(dirt_n, dirt);
589 return err;
590 }
591
592 int
593 xenbus_free_device(struct xenbus_device *xbusd)
594 {
595 KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
596 SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
597 free_otherend_watch(xbusd);
598 free_otherend_details(xbusd);
599 xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
600 kmem_free(xbusd, xbusd->xbusd_sz);
601 return 0;
602 }
603
604 static void
605 frontend_changed(struct xenbus_watch *watch,
606 const char **vec, unsigned int len)
607 {
608 DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
609 xenbus_probe_frontends();
610 }
611
612 static void
613 backend_changed(struct xenbus_watch *watch,
614 const char **vec, unsigned int len)
615 {
616 DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
617 xenbus_probe_backends();
618 }
619
620
621 /* We watch for devices appearing and vanishing. */
622 static struct xenbus_watch fe_watch;
623
624 static struct xenbus_watch be_watch;
625
626 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
627 int xenstored_ready = 0;
628 static kmutex_t xenstored_lock;
629 static kcondvar_t xenstored_cv;
630
631 void
632 xenbus_probe(void *unused)
633 {
634 struct xenbusdev_attach_args balloon_xa = {
635 .xa_id = 0,
636 .xa_type = "balloon"
637 };
638
639 KASSERT((xenstored_ready > 0));
640
641 /* Enumerate devices in xenstore. */
642 xenbus_probe_frontends();
643 xenbus_probe_backends();
644
645 /* Watch for changes. */
646 fe_watch.node_sz = strlen("device") + 1;
647 fe_watch.node = kmem_alloc(fe_watch.node_sz, KM_SLEEP);
648 strcpy(fe_watch.node, "device");
649 fe_watch.xbw_callback = frontend_changed;
650 register_xenbus_watch(&fe_watch);
651
652 be_watch.node_sz = strlen("backend") + 1;
653 be_watch.node = kmem_alloc(be_watch.node_sz, KM_SLEEP);
654 strcpy(be_watch.node, "backend");
655 be_watch.xbw_callback = backend_changed;
656 register_xenbus_watch(&be_watch);
657
658 /* attach balloon. */
659 KERNEL_LOCK(1, curlwp);
660 config_found(xenbus_dev, &balloon_xa, xenbus_print,
661 CFARGS_NONE);
662 KERNEL_UNLOCK_ONE(curlwp);
663
664 shutdown_xenbus_setup();
665
666 /* Notify others that xenstore is up */
667 //notifier_call_chain(&xenstore_chain, 0, NULL);
668 }
669
670 void
671 xb_xenstored_make_ready(void)
672 {
673 mutex_enter(&xenstored_lock);
674 xenstored_ready = 1;
675 cv_broadcast(&xenstored_cv);
676 mutex_exit(&xenstored_lock);
677 }
678
679 static void
680 xenbus_probe_init(void *unused)
681 {
682 int err = 0;
683 bool dom0;
684 vaddr_t page = 0;
685
686 DPRINTK("");
687
688 SLIST_INIT(&xenbus_device_list);
689 mutex_init(&xenstored_lock, MUTEX_DEFAULT, IPL_TTY);
690 cv_init(&xenstored_cv, "xsready");
691
692 /*
693 ** Domain0 doesn't have a store_evtchn or store_mfn yet.
694 */
695 dom0 = xendomain_is_dom0();
696 if (dom0) {
697 #if defined(DOM0OPS)
698 paddr_t ma;
699 evtchn_op_t op = { .cmd = 0 };
700
701 /* Allocate page. */
702 page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
703 UVM_KMF_ZERO | UVM_KMF_WIRED);
704 if (!page)
705 panic("can't get xenstore page");
706
707 (void)pmap_extract_ma(pmap_kernel(), page, &ma);
708 xen_start_info.store_mfn = ma >> PAGE_SHIFT;
709 xenstore_interface = (void *)page;
710
711 /* Next allocate a local port which xenstored can bind to */
712 op.cmd = EVTCHNOP_alloc_unbound;
713 op.u.alloc_unbound.dom = DOMID_SELF;
714 op.u.alloc_unbound.remote_dom = 0;
715
716 err = HYPERVISOR_event_channel_op(&op);
717 if (err) {
718 aprint_error_dev(xenbus_dev,
719 "can't register xenstore event\n");
720 goto err0;
721 }
722
723 xen_start_info.store_evtchn = op.u.alloc_unbound.port;
724
725 DELAY(1000);
726 #else /* DOM0OPS */
727 panic("dom0 support not compiled in");
728 #endif /* DOM0OPS */
729 }
730
731 #if NKERNFS > 0
732 /* Publish xenbus and Xenstore info in /kern/xen */
733 xenbus_kernfs_init();
734 #endif
735
736 /* register event handler */
737 xb_init_comms(xenbus_dev);
738
739 /* Initialize the interface to xenstore. */
740 err = xs_init(xenbus_dev);
741 if (err) {
742 aprint_error_dev(xenbus_dev,
743 "Error initializing xenstore comms: %i\n", err);
744 goto err0;
745 }
746
747 if (!dom0) {
748 xenstored_ready = 1;
749 xenbus_probe(NULL);
750 }
751
752 DPRINTK("done");
753 config_pending_decr(xenbus_dev);
754 #ifdef DOM0OPS
755 if (dom0) {
756 mutex_enter(&xenstored_lock);
757 while (xenstored_ready == 0) {
758 cv_wait(&xenstored_cv, &xenstored_lock);
759 mutex_exit(&xenstored_lock);
760 xenbus_probe(NULL);
761 mutex_enter(&xenstored_lock);
762 }
763 mutex_exit(&xenstored_lock);
764 }
765 #endif
766 kthread_exit(0);
767
768 err0:
769 if (page)
770 uvm_km_free(kernel_map, page, PAGE_SIZE,
771 UVM_KMF_ZERO | UVM_KMF_WIRED);
772 kthread_exit(err);
773 }
774
775 /*
776 * Local variables:
777 * c-file-style: "linux"
778 * indent-tabs-mode: t
779 * c-indent-level: 8
780 * c-basic-offset: 8
781 * tab-width: 8
782 * End:
783 */
784