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