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