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