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