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