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