xenbus_probe.c revision 1.27.2.7 1 /* $NetBSD: xenbus_probe.c,v 1.27.2.7 2011/05/02 22:49:59 jym 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.27.2.7 2011/05/02 22:49:59 jym 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/malloc.h>
45 #include <sys/systm.h>
46 #include <sys/param.h>
47 #include <sys/kthread.h>
48 #include <uvm/uvm.h>
49
50 #include <machine/stdarg.h>
51
52 #include <xen/xen.h> /* for xendomain_is_dom0() */
53 #include <xen/hypervisor.h>
54 #include <xen/xenbus.h>
55 #include <xen/evtchn.h>
56 #include <xen/shutdown_xenbus.h>
57
58 #include "xenbus_comms.h"
59
60 extern struct semaphore xenwatch_mutex;
61
62 #define streq(a, b) (strcmp((a), (b)) == 0)
63
64 static int xenbus_match(device_t, cfdata_t, void *);
65 static void xenbus_attach(device_t, device_t, void *);
66 static int xenbus_print(void *, const char *);
67
68 /* power management, for save/restore */
69 static bool xenbus_suspend(device_t, const pmf_qual_t *);
70 static bool xenbus_resume(device_t, const pmf_qual_t *);
71
72 /* routines gathering device information from XenStore */
73 static int read_otherend_details(struct xenbus_device *,
74 const char *, const char *);
75 static int read_backend_details (struct xenbus_device *);
76 static int read_frontend_details(struct xenbus_device *);
77 static void free_otherend_details(struct xenbus_device *);
78
79 static int watch_otherend (struct xenbus_device *);
80 static void free_otherend_watch(struct xenbus_device *);
81
82 static void xenbus_probe_init(void *);
83
84 static struct xenbus_device *xenbus_lookup_device_path(const char *);
85
86 CFATTACH_DECL_NEW(xenbus, 0, xenbus_match, xenbus_attach,
87 NULL, NULL);
88
89 device_t xenbus_dev;
90
91 SLIST_HEAD(, xenbus_device) xenbus_device_list;
92 SLIST_HEAD(, xenbus_backend_driver) xenbus_backend_driver_list =
93 SLIST_HEAD_INITIALIZER(xenbus_backend_driver);
94
95 int
96 xenbus_match(device_t parent, cfdata_t match, void *aux)
97 {
98 struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
99
100 if (strcmp(xa->xa_device, "xenbus") == 0)
101 return 1;
102 return 0;
103 }
104
105 static void
106 xenbus_attach(device_t parent, device_t self, void *aux)
107 {
108 int err;
109
110 aprint_normal(": Xen Virtual Bus Interface\n");
111 xenbus_dev = self;
112 config_pending_incr();
113
114 err = kthread_create(PRI_NONE, 0, NULL, xenbus_probe_init, NULL,
115 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_init_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 char *val, *ep;
177
178 err = xenbus_read(NULL, xendev->xbusd_path, id_node, NULL, &val);
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 = strtoul(val, &ep, 10);
188 if (val[0] == '\0' || *ep != '\0') {
189 printf("reading other end details %s from %s: %s is not a number\n", id_node, xendev->xbusd_path, val);
190 xenbus_dev_fatal(xendev, err,
191 "reading other end details %s from %s: %s is not a number",
192 id_node, xendev->xbusd_path, val);
193 free(val, M_DEVBUF);
194 return EFTYPE;
195 }
196 free(val, M_DEVBUF);
197 err = xenbus_read(NULL, xendev->xbusd_path, path_node, NULL, &val);
198 if (err) {
199 printf("reading other end details %s from %s (%d)\n",
200 path_node, xendev->xbusd_path, err);
201 xenbus_dev_fatal(xendev, err,
202 "reading other end details %s from %s",
203 path_node, xendev->xbusd_path);
204 return err;
205 }
206 DPRINTK("read_otherend_details: read %s/%s returned %s\n",
207 xendev->xbusd_path, path_node, val);
208 xendev->xbusd_otherend = val;
209
210 if (strlen(xendev->xbusd_otherend) == 0 ||
211 !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
212 printf("missing other end from %s\n", xendev->xbusd_path);
213 xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
214 xendev->xbusd_path);
215 free_otherend_details(xendev);
216 return ENOENT;
217 }
218
219 return 0;
220 }
221
222 static int
223 read_backend_details(struct xenbus_device *xendev)
224 {
225 return read_otherend_details(xendev, "backend-id", "backend");
226 }
227
228
229 static int
230 read_frontend_details(struct xenbus_device *xendev)
231 {
232 return read_otherend_details(xendev, "frontend-id", "frontend");
233 }
234
235 static void
236 free_otherend_details(struct xenbus_device *dev)
237 {
238 free(dev->xbusd_otherend, M_DEVBUF);
239 dev->xbusd_otherend = NULL;
240 }
241
242 static void
243 free_otherend_watch(struct xenbus_device *dev)
244 {
245 if (dev->xbusd_otherend_watch.node) {
246 unregister_xenbus_watch(&dev->xbusd_otherend_watch);
247 free(dev->xbusd_otherend_watch.node, M_DEVBUF);
248 dev->xbusd_otherend_watch.node = NULL;
249 }
250 }
251
252 static void
253 otherend_changed(struct xenbus_watch *watch,
254 const char **vec, unsigned int len)
255 {
256 struct xenbus_device *xdev = watch->xbw_dev;
257 XenbusState state;
258
259 /* Protect us against watches firing on old details when the otherend
260 details change, say immediately after a resume. */
261 if (!xdev->xbusd_otherend ||
262 strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
263 strlen(xdev->xbusd_otherend))) {
264 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
265 return;
266 }
267
268 state = xenbus_read_driver_state(xdev->xbusd_otherend);
269
270 DPRINTK("state is %d, %s, %s",
271 state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
272 if (state == XenbusStateClosed) {
273 int error;
274 if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
275 error = xdev->xbusd_u.b.b_detach(
276 xdev->xbusd_u.b.b_cookie);
277 if (error) {
278 printf("could not detach %s: %d\n",
279 xdev->xbusd_path, error);
280 return;
281 }
282 } else {
283 error = config_detach(xdev->xbusd_u.f.f_dev,
284 DETACH_FORCE);
285 if (error) {
286 printf("could not detach %s: %d\n",
287 device_xname(xdev->xbusd_u.f.f_dev), error);
288 return;
289 }
290 }
291 xenbus_free_device(xdev);
292 return;
293 }
294 if (xdev->xbusd_otherend_changed)
295 xdev->xbusd_otherend_changed(
296 (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
297 xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
298 }
299
300 static int
301 watch_otherend(struct xenbus_device *dev)
302 {
303 free_otherend_watch(dev);
304
305 return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
306 &dev->xbusd_otherend_watch,
307 otherend_changed);
308 }
309
310 static struct xenbus_device *
311 xenbus_lookup_device_path(const char *path)
312 {
313 struct xenbus_device *xbusd;
314
315 SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
316 if (strcmp(xbusd->xbusd_path, path) == 0)
317 return xbusd;
318 }
319 return NULL;
320 }
321
322 static int
323 xenbus_probe_device_type(const char *path, const char *type,
324 int (*create)(struct xenbus_device *))
325 {
326 int err, i, msize;
327 unsigned long state;
328 char **dir;
329 unsigned int dir_n = 0;
330 struct xenbus_device *xbusd;
331 struct xenbusdev_attach_args xa;
332 char *ep;
333
334 DPRINTK("probe %s type %s", path, type);
335 err = xenbus_directory(NULL, path, "", &dir_n, &dir);
336 DPRINTK("directory err %d dir_n %d", err, dir_n);
337 if (err)
338 return err;
339
340 for (i = 0; i < dir_n; i++) {
341 err = 0;
342 /*
343 * add size of path to size of xenbus_device. xenbus_device
344 * already has room for one char in xbusd_path.
345 */
346 msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
347 xbusd = malloc(msize, M_DEVBUF, M_WAITOK | M_ZERO);
348 if (xbusd == NULL)
349 panic("can't malloc xbusd");
350
351 snprintf(__UNCONST(xbusd->xbusd_path),
352 msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
353 if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
354 /* device already registered */
355 free(xbusd, M_DEVBUF);
356 continue;
357 }
358 err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
359 &state, 10);
360 if (err) {
361 printf("xenbus: can't get state "
362 "for %s (%d)\n", xbusd->xbusd_path, err);
363 free(xbusd, M_DEVBUF);
364 err = 0;
365 continue;
366 }
367 if (state != XenbusStateInitialising) {
368 /* device is not new */
369 free(xbusd, M_DEVBUF);
370 continue;
371 }
372
373 xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
374 DPRINTK("xenbus_probe_device_type probe %s\n",
375 xbusd->xbusd_path);
376 if (create != NULL) {
377 xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
378 err = read_frontend_details(xbusd);
379 if (err != 0) {
380 printf("xenbus: can't get frontend details "
381 "for %s (%d)\n", xbusd->xbusd_path, err);
382 break;
383 }
384 if (create(xbusd)) {
385 free(xbusd, M_DEVBUF);
386 continue;
387 }
388 } else {
389 xbusd->xbusd_type = XENBUS_FRONTEND_DEVICE;
390 xa.xa_xbusd = xbusd;
391 xa.xa_type = type;
392 xa.xa_id = strtoul(dir[i], &ep, 0);
393 if (dir[i][0] == '\0' || *ep != '\0') {
394 printf("xenbus device type %s: id %s is not a"
395 " number\n", type, dir[i]);
396 err = EFTYPE;
397 free(xbusd, M_DEVBUF);
398 break;
399 }
400 err = read_backend_details(xbusd);
401 if (err != 0) {
402 printf("xenbus: can't get backend details "
403 "for %s (%d)\n", xbusd->xbusd_path, err);
404 break;
405 }
406 xbusd->xbusd_u.f.f_dev = config_found_ia(xenbus_dev,
407 "xenbus", &xa, xenbus_print);
408 if (xbusd->xbusd_u.f.f_dev == NULL) {
409 free(xbusd, M_DEVBUF);
410 continue;
411 }
412 }
413 SLIST_INSERT_HEAD(&xenbus_device_list,
414 xbusd, xbusd_entries);
415 watch_otherend(xbusd);
416 }
417 free(dir, M_DEVBUF);
418 return err;
419 }
420
421 static int
422 xenbus_print(void *aux, const char *pnp)
423 {
424 struct xenbusdev_attach_args *xa = aux;
425
426 if (pnp) {
427 if (strcmp(xa->xa_type, "vbd") == 0)
428 aprint_normal("xbd");
429 else if (strcmp(xa->xa_type, "vif") == 0)
430 aprint_normal("xennet");
431 else if (strcmp(xa->xa_type, "balloon") == 0)
432 aprint_normal("balloon");
433 else
434 aprint_normal("unknown type %s", xa->xa_type);
435 aprint_normal(" at %s", pnp);
436 }
437 aprint_normal(" id %d", xa->xa_id);
438 return(UNCONF);
439 }
440
441 static int
442 xenbus_probe_frontends(void)
443 {
444 int err;
445 char **dir;
446 unsigned int i, dir_n;
447 char path[30];
448
449 DPRINTK("probe device");
450 err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
451 DPRINTK("directory err %d dir_n %d", err, dir_n);
452 if (err)
453 return err;
454
455 for (i = 0; i < dir_n; i++) {
456 /*
457 * console is configured through xen_start_info when
458 * xencons is attaching to hypervisor, so avoid console
459 * probing when configuring xenbus devices
460 */
461 if (strcmp(dir[i], "console") == 0)
462 continue;
463
464 snprintf(path, sizeof(path), "device/%s", dir[i]);
465 err = xenbus_probe_device_type(path, dir[i], NULL);
466 if (err)
467 break;
468 }
469 free(dir, M_DEVBUF);
470 return err;
471 }
472
473 static int
474 xenbus_probe_backends(void)
475 {
476 int err;
477 char **dirt, **dirid;
478 unsigned int type, id, dirt_n, dirid_n;
479 char path[30];
480 struct xenbus_backend_driver *xbakd;
481
482 DPRINTK("probe backend");
483 err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
484 DPRINTK("directory err %d dirt_n %d", err, dirt_n);
485 if (err)
486 return err;
487
488 for (type = 0; type < dirt_n; type++) {
489 SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
490 xbakd_entries) {
491 if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
492 break;
493 }
494 if (xbakd == NULL)
495 continue;
496 err = xenbus_directory(NULL, "backend", dirt[type],
497 &dirid_n, &dirid);
498 DPRINTK("directory backend/%s err %d dirid_n %d",
499 dirt[type], err, dirid_n);
500 if (err) {
501 free(dirt, M_DEVBUF); /* to be checked */
502 return err;
503 }
504 for (id = 0; id < dirid_n; id++) {
505 snprintf(path, sizeof(path), "backend/%s/%s",
506 dirt[type], dirid[id]);
507 err = xenbus_probe_device_type(path, dirt[type],
508 xbakd->xbakd_create);
509 if (err)
510 break;
511 }
512 free(dirid, M_DEVBUF);
513 }
514 free(dirt, M_DEVBUF);
515 return err;
516 }
517
518 int
519 xenbus_free_device(struct xenbus_device *xbusd)
520 {
521 KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
522 SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
523 free_otherend_watch(xbusd);
524 free_otherend_details(xbusd);
525 xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
526 free(xbusd, M_DEVBUF);
527 return 0;
528 }
529
530 static void
531 frontend_changed(struct xenbus_watch *watch,
532 const char **vec, unsigned int len)
533 {
534 DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
535 xenbus_probe_frontends();
536 }
537
538 static void
539 backend_changed(struct xenbus_watch *watch,
540 const char **vec, unsigned int len)
541 {
542 DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
543 xenbus_probe_backends();
544 }
545
546
547 /* We watch for devices appearing and vanishing. */
548 static struct xenbus_watch fe_watch;
549
550 static struct xenbus_watch be_watch;
551
552 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
553 int xenstored_ready = 0;
554
555 void
556 xenbus_probe(void *unused)
557 {
558 struct xenbusdev_attach_args balloon_xa = {
559 .xa_id = 0,
560 .xa_type = "balloon"
561 };
562
563 KASSERT((xenstored_ready > 0));
564
565 /* Enumerate devices in xenstore. */
566 xenbus_probe_frontends();
567 xenbus_probe_backends();
568
569 /* Watch for changes. */
570 fe_watch.node = malloc(strlen("device" + 1), M_DEVBUF, M_NOWAIT);
571 strcpy(fe_watch.node, "device");
572 fe_watch.xbw_callback = frontend_changed;
573 register_xenbus_watch(&fe_watch);
574 be_watch.node = malloc(strlen("backend" + 1), M_DEVBUF, M_NOWAIT);
575 strcpy(be_watch.node, "backend");
576 be_watch.xbw_callback = backend_changed;
577 register_xenbus_watch(&be_watch);
578
579 /* attach balloon. */
580 config_found_ia(xenbus_dev, "xenbus", &balloon_xa, xenbus_print);
581
582 shutdown_xenbus_setup();
583
584 /* Notify others that xenstore is up */
585 //notifier_call_chain(&xenstore_chain, 0, NULL);
586 }
587
588 static void
589 xenbus_probe_init(void *unused)
590 {
591 int err = 0;
592 bool dom0;
593 vaddr_t page = 0;
594
595 DPRINTK("");
596
597 SLIST_INIT(&xenbus_device_list);
598
599 /*
600 ** Domain0 doesn't have a store_evtchn or store_mfn yet.
601 */
602 dom0 = xendomain_is_dom0();
603 if (dom0) {
604 #if defined(DOM0OPS)
605 paddr_t ma;
606 evtchn_op_t op = { .cmd = 0 };
607
608 /* Allocate page. */
609 page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
610 UVM_KMF_ZERO | UVM_KMF_WIRED);
611 if (!page)
612 panic("can't get xenstore page");
613
614 (void)pmap_extract_ma(pmap_kernel(), page, &ma);
615 xen_start_info.store_mfn = ma >> PAGE_SHIFT;
616 xenstore_interface = (void *)page;
617
618 /* Next allocate a local port which xenstored can bind to */
619 op.cmd = EVTCHNOP_alloc_unbound;
620 op.u.alloc_unbound.dom = DOMID_SELF;
621 op.u.alloc_unbound.remote_dom = 0;
622
623 err = HYPERVISOR_event_channel_op(&op);
624 if (err) {
625 aprint_error_dev(xenbus_dev,
626 "can't register xenstore event\n");
627 goto err0;
628 }
629
630 xen_start_info.store_evtchn = op.u.alloc_unbound.port;
631
632 /* And finally publish the above info in /kern/xen */
633 xenbus_kernfs_init();
634
635 DELAY(1000);
636 #else /* DOM0OPS */
637 kthread_exit(0); /* can't get a working xenstore in this case */
638 #endif /* DOM0OPS */
639 }
640
641 /* register event handler */
642 xb_init_comms(xenbus_dev);
643
644 /* Initialize the interface to xenstore. */
645 err = xs_init(xenbus_dev);
646 if (err) {
647 aprint_error_dev(xenbus_dev,
648 "Error initializing xenstore comms: %i\n", err);
649 goto err0;
650 }
651
652 if (!dom0) {
653 xenstored_ready = 1;
654 xenbus_probe(NULL);
655 }
656
657 DPRINTK("done");
658 config_pending_decr();
659 #ifdef DOM0OPS
660 if (dom0) {
661 int s;
662 s = spltty();
663 while (xenstored_ready == 0) {
664 tsleep(&xenstored_ready, PRIBIO, "xsready", 0);
665 xenbus_probe(NULL);
666 }
667 splx(s);
668 }
669 #endif
670 kthread_exit(0);
671
672 err0:
673 if (page)
674 uvm_km_free(kernel_map, page, PAGE_SIZE,
675 UVM_KMF_ZERO | UVM_KMF_WIRED);
676 kthread_exit(err);
677 }
678
679 /*
680 * Local variables:
681 * c-file-style: "linux"
682 * indent-tabs-mode: t
683 * c-indent-level: 8
684 * c-basic-offset: 8
685 * tab-width: 8
686 * End:
687 */
688