xenbus_probe.c revision 1.29 1 /* $NetBSD: xenbus_probe.c,v 1.29 2011/03/30 22:34:03 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.29 2011/03/30 22:34:03 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 /* 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 /*
298 * add size of path to size of xenbus_device. xenbus_device
299 * already has room for one char in xbusd_path.
300 */
301 msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
302 xbusd = malloc(msize, M_DEVBUF, M_WAITOK | M_ZERO);
303 if (xbusd == NULL)
304 panic("can't malloc xbusd");
305
306 snprintf(__UNCONST(xbusd->xbusd_path),
307 msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
308 if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
309 /* device already registered */
310 free(xbusd, M_DEVBUF);
311 continue;
312 }
313 err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
314 &state, 10);
315 if (err) {
316 printf("xenbus: can't get state "
317 "for %s (%d)\n", xbusd->xbusd_path, err);
318 free(xbusd, M_DEVBUF);
319 continue;
320 }
321 if (state != XenbusStateInitialising) {
322 /* device is not new */
323 free(xbusd, M_DEVBUF);
324 continue;
325 }
326
327 xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
328 DPRINTK("xenbus_probe_device_type probe %s\n",
329 xbusd->xbusd_path);
330 if (create != NULL) {
331 xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
332 err = read_frontend_details(xbusd);
333 if (err != 0) {
334 printf("xenbus: can't get frontend details "
335 "for %s (%d)\n", xbusd->xbusd_path, err);
336 break;
337 }
338 if (create(xbusd)) {
339 free(xbusd, M_DEVBUF);
340 continue;
341 }
342 } else {
343 xbusd->xbusd_type = XENBUS_FRONTEND_DEVICE;
344 xa.xa_xbusd = xbusd;
345 xa.xa_type = type;
346 xa.xa_id = strtoul(dir[i], &ep, 0);
347 if (dir[i][0] == '\0' || *ep != '\0') {
348 printf("xenbus device type %s: id %s is not a"
349 " number\n", type, dir[i]);
350 err = EFTYPE;
351 free(xbusd, M_DEVBUF);
352 break;
353 }
354 err = read_backend_details(xbusd);
355 if (err != 0) {
356 printf("xenbus: can't get backend details "
357 "for %s (%d)\n", xbusd->xbusd_path, err);
358 break;
359 }
360 xbusd->xbusd_u.f.f_dev = config_found_ia(xenbus_dev,
361 "xenbus", &xa, xenbus_print);
362 if (xbusd->xbusd_u.f.f_dev == NULL) {
363 free(xbusd, M_DEVBUF);
364 continue;
365 }
366 }
367 SLIST_INSERT_HEAD(&xenbus_device_list,
368 xbusd, xbusd_entries);
369 watch_otherend(xbusd);
370 }
371 free(dir, M_DEVBUF);
372 return err;
373 }
374
375 static int
376 xenbus_print(void *aux, const char *pnp)
377 {
378 struct xenbusdev_attach_args *xa = aux;
379
380 if (pnp) {
381 if (strcmp(xa->xa_type, "vbd") == 0)
382 aprint_normal("xbd");
383 else if (strcmp(xa->xa_type, "vif") == 0)
384 aprint_normal("xennet");
385 else
386 aprint_normal("unknown type %s", xa->xa_type);
387 aprint_normal(" at %s", pnp);
388 }
389 aprint_normal(" id %d", xa->xa_id);
390 return(UNCONF);
391 }
392
393 static int
394 xenbus_probe_frontends(void)
395 {
396 int err;
397 char **dir;
398 unsigned int i, dir_n;
399 char path[30];
400
401 DPRINTK("probe device");
402 err = xenbus_directory(NULL, "device", "", &dir_n, &dir);
403 DPRINTK("directory err %d dir_n %d", err, dir_n);
404 if (err)
405 return err;
406
407 for (i = 0; i < dir_n; i++) {
408 /*
409 * console is configured through xen_start_info when
410 * xencons is attaching to hypervisor, so avoid console
411 * probing when configuring xenbus devices
412 */
413 if (strcmp(dir[i], "console") == 0)
414 continue;
415
416 snprintf(path, sizeof(path), "device/%s", dir[i]);
417 err = xenbus_probe_device_type(path, dir[i], NULL);
418 if (err)
419 break;
420 }
421 free(dir, M_DEVBUF);
422 return err;
423 }
424
425 static int
426 xenbus_probe_backends(void)
427 {
428 int err;
429 char **dirt, **dirid;
430 unsigned int type, id, dirt_n, dirid_n;
431 char path[30];
432 struct xenbus_backend_driver *xbakd;
433
434 DPRINTK("probe backend");
435 err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
436 DPRINTK("directory err %d dirt_n %d", err, dirt_n);
437 if (err)
438 return err;
439
440 for (type = 0; type < dirt_n; type++) {
441 SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
442 xbakd_entries) {
443 if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
444 break;
445 }
446 if (xbakd == NULL)
447 continue;
448 err = xenbus_directory(NULL, "backend", dirt[type],
449 &dirid_n, &dirid);
450 DPRINTK("directory backend/%s err %d dirid_n %d",
451 dirt[type], err, dirid_n);
452 if (err) {
453 free(dirt, M_DEVBUF); /* to be checked */
454 return err;
455 }
456 for (id = 0; id < dirid_n; id++) {
457 snprintf(path, sizeof(path), "backend/%s/%s",
458 dirt[type], dirid[id]);
459 err = xenbus_probe_device_type(path, dirt[type],
460 xbakd->xbakd_create);
461 if (err)
462 break;
463 }
464 free(dirid, M_DEVBUF);
465 }
466 free(dirt, M_DEVBUF);
467 return err;
468 }
469
470 int
471 xenbus_free_device(struct xenbus_device *xbusd)
472 {
473 KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
474 SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
475 free_otherend_watch(xbusd);
476 free_otherend_details(xbusd);
477 xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
478 free(xbusd, M_DEVBUF);
479 return 0;
480 }
481
482 static void
483 frontend_changed(struct xenbus_watch *watch,
484 const char **vec, unsigned int len)
485 {
486 DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
487 xenbus_probe_frontends();
488 }
489
490 static void
491 backend_changed(struct xenbus_watch *watch,
492 const char **vec, unsigned int len)
493 {
494 DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
495 xenbus_probe_backends();
496 }
497
498
499 /* We watch for devices appearing and vanishing. */
500 static struct xenbus_watch fe_watch;
501
502 static struct xenbus_watch be_watch;
503
504 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
505 int xenstored_ready = 0;
506
507 void
508 xenbus_probe(void *unused)
509 {
510 KASSERT((xenstored_ready > 0));
511
512 /* Enumerate devices in xenstore. */
513 xenbus_probe_frontends();
514 xenbus_probe_backends();
515
516 /* Watch for changes. */
517 fe_watch.node = malloc(strlen("device" + 1), M_DEVBUF, M_NOWAIT);
518 strcpy(fe_watch.node, "device");
519 fe_watch.xbw_callback = frontend_changed;
520 register_xenbus_watch(&fe_watch);
521 be_watch.node = malloc(strlen("backend" + 1), M_DEVBUF, M_NOWAIT);
522 strcpy(be_watch.node, "backend");
523 be_watch.xbw_callback = backend_changed;
524 register_xenbus_watch(&be_watch);
525 shutdown_xenbus_setup();
526
527 #ifdef XEN_BALLOON
528 balloon_xenbus_setup();
529 #endif
530
531 /* Notify others that xenstore is up */
532 //notifier_call_chain(&xenstore_chain, 0, NULL);
533 }
534
535 static void
536 xenbus_probe_init(void *unused)
537 {
538 int err = 0;
539 bool dom0;
540 vaddr_t page = 0;
541
542 DPRINTK("");
543
544 SLIST_INIT(&xenbus_device_list);
545
546 /*
547 ** Domain0 doesn't have a store_evtchn or store_mfn yet.
548 */
549 dom0 = xendomain_is_dom0();
550 if (dom0) {
551 #if defined(DOM0OPS)
552 paddr_t ma;
553 evtchn_op_t op = { .cmd = 0 };
554
555 /* Allocate page. */
556 page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
557 UVM_KMF_ZERO | UVM_KMF_WIRED);
558 if (!page)
559 panic("can't get xenstore page");
560
561 (void)pmap_extract_ma(pmap_kernel(), page, &ma);
562 xen_start_info.store_mfn = ma >> PAGE_SHIFT;
563 xenstore_interface = (void *)page;
564
565 /* Next allocate a local port which xenstored can bind to */
566 op.cmd = EVTCHNOP_alloc_unbound;
567 op.u.alloc_unbound.dom = DOMID_SELF;
568 op.u.alloc_unbound.remote_dom = 0;
569
570 err = HYPERVISOR_event_channel_op(&op);
571 if (err) {
572 aprint_error_dev(xenbus_dev,
573 "can't register xenstore event\n");
574 goto err0;
575 }
576
577 xen_start_info.store_evtchn = op.u.alloc_unbound.port;
578
579 /* And finally publish the above info in /kern/xen */
580 xenbus_kernfs_init();
581
582 DELAY(1000);
583 #else /* DOM0OPS */
584 kthread_exit(0); /* can't get a working xenstore in this case */
585 #endif /* DOM0OPS */
586 }
587
588 /* register event handler */
589 xb_init_comms(xenbus_dev);
590
591 /* Initialize the interface to xenstore. */
592 err = xs_init(xenbus_dev);
593 if (err) {
594 aprint_error_dev(xenbus_dev,
595 "Error initializing xenstore comms: %i\n", err);
596 goto err0;
597 }
598
599 if (!dom0) {
600 xenstored_ready = 1;
601 xenbus_probe(NULL);
602 }
603
604 DPRINTK("done");
605 config_pending_decr();
606 #ifdef DOM0OPS
607 if (dom0) {
608 int s;
609 s = spltty();
610 while (xenstored_ready == 0) {
611 tsleep(&xenstored_ready, PRIBIO, "xsready", 0);
612 xenbus_probe(NULL);
613 }
614 splx(s);
615 }
616 #endif
617 kthread_exit(0);
618
619 err0:
620 if (page)
621 uvm_km_free(kernel_map, page, PAGE_SIZE,
622 UVM_KMF_ZERO | UVM_KMF_WIRED);
623 kthread_exit(err);
624 }
625
626 /*
627 * Local variables:
628 * c-file-style: "linux"
629 * indent-tabs-mode: t
630 * c-indent-level: 8
631 * c-basic-offset: 8
632 * tab-width: 8
633 * End:
634 */
635