xref: /netbsd-src/sys/arch/xen/xenbus/xenbus_probe.c (revision d25ffa98a4bfca1fe272f3c182496ec9934faac7)
1 /* $NetBSD: xenbus_probe.c,v 1.32 2011/04/18 01:36:25 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.32 2011/04/18 01:36:25 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 /* routines gathering device information from XenStore */
69 static int  read_otherend_details(struct xenbus_device *,
70 		const char *, const char *);
71 static int  read_backend_details (struct xenbus_device *);
72 static int  read_frontend_details(struct xenbus_device *);
73 static void free_otherend_details(struct xenbus_device *);
74 
75 static int  watch_otherend     (struct xenbus_device *);
76 static void free_otherend_watch(struct xenbus_device *);
77 
78 static void xenbus_probe_init(void *);
79 
80 static struct xenbus_device *xenbus_lookup_device_path(const char *);
81 
82 CFATTACH_DECL_NEW(xenbus, 0, xenbus_match, xenbus_attach,
83     NULL, NULL);
84 
85 device_t xenbus_dev;
86 
87 SLIST_HEAD(, xenbus_device) xenbus_device_list;
88 SLIST_HEAD(, xenbus_backend_driver) xenbus_backend_driver_list =
89 	SLIST_HEAD_INITIALIZER(xenbus_backend_driver);
90 
91 int
92 xenbus_match(device_t parent, cfdata_t match, void *aux)
93 {
94 	struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
95 
96 	if (strcmp(xa->xa_device, "xenbus") == 0)
97 		return 1;
98 	return 0;
99 }
100 
101 static void
102 xenbus_attach(device_t parent, device_t self, void *aux)
103 {
104 	int err;
105 
106 	aprint_normal(": Xen Virtual Bus Interface\n");
107 	xenbus_dev = self;
108 	config_pending_incr();
109 
110 	err = kthread_create(PRI_NONE, 0, NULL, xenbus_probe_init, NULL,
111 	    NULL, "xenbus_probe");
112 	if (err)
113 		aprint_error_dev(xenbus_dev,
114 				"kthread_create(xenbus_probe): %d\n", err);
115 }
116 
117 void
118 xenbus_backend_register(struct xenbus_backend_driver *xbakd)
119 {
120 	SLIST_INSERT_HEAD(&xenbus_backend_driver_list, xbakd, xbakd_entries);
121 }
122 
123 static int
124 read_otherend_details(struct xenbus_device *xendev,
125 				 const char *id_node, const char *path_node)
126 {
127 	int err;
128 	char *val, *ep;
129 
130 	err = xenbus_read(NULL, xendev->xbusd_path, id_node, NULL, &val);
131 	if (err) {
132 		printf("reading other end details %s from %s\n",
133 		    id_node, xendev->xbusd_path);
134 		xenbus_dev_fatal(xendev, err,
135 				 "reading other end details %s from %s",
136 				 id_node, xendev->xbusd_path);
137 		return err;
138 	}
139 	xendev->xbusd_otherend_id = strtoul(val, &ep, 10);
140 	if (val[0] == '\0' || *ep != '\0') {
141 		printf("reading other end details %s from %s: %s is not a number\n", id_node, xendev->xbusd_path, val);
142 		xenbus_dev_fatal(xendev, err,
143 		    "reading other end details %s from %s: %s is not a number",
144 		    id_node, xendev->xbusd_path, val);
145 		free(val, M_DEVBUF);
146 		return EFTYPE;
147 	}
148 	free(val, M_DEVBUF);
149 	err = xenbus_read(NULL, xendev->xbusd_path, path_node, NULL, &val);
150 	if (err) {
151 		printf("reading other end details %s from %s (%d)\n",
152 		    path_node, xendev->xbusd_path, err);
153 		xenbus_dev_fatal(xendev, err,
154 				 "reading other end details %s from %s",
155 				 path_node, xendev->xbusd_path);
156 		return err;
157 	}
158 	DPRINTK("read_otherend_details: read %s/%s returned %s\n",
159 	    xendev->xbusd_path, path_node, val);
160 	xendev->xbusd_otherend = val;
161 
162 	if (strlen(xendev->xbusd_otherend) == 0 ||
163 	    !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
164 		printf("missing other end from %s\n", xendev->xbusd_path);
165 		xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
166 				 xendev->xbusd_path);
167 		free_otherend_details(xendev);
168 		return ENOENT;
169 	}
170 
171 	return 0;
172 }
173 
174 static int
175 read_backend_details(struct xenbus_device *xendev)
176 {
177 	return read_otherend_details(xendev, "backend-id", "backend");
178 }
179 
180 
181 static int
182 read_frontend_details(struct xenbus_device *xendev)
183 {
184 	return read_otherend_details(xendev, "frontend-id", "frontend");
185 }
186 
187 static void
188 free_otherend_details(struct xenbus_device *dev)
189 {
190 	free(dev->xbusd_otherend, M_DEVBUF);
191 	dev->xbusd_otherend = NULL;
192 }
193 
194 static void
195 free_otherend_watch(struct xenbus_device *dev)
196 {
197 	if (dev->xbusd_otherend_watch.node) {
198 		unregister_xenbus_watch(&dev->xbusd_otherend_watch);
199 		free(dev->xbusd_otherend_watch.node, M_DEVBUF);
200 		dev->xbusd_otherend_watch.node = NULL;
201 	}
202 }
203 
204 static void
205 otherend_changed(struct xenbus_watch *watch,
206 			     const char **vec, unsigned int len)
207 {
208 	struct xenbus_device *xdev = watch->xbw_dev;
209 	XenbusState state;
210 
211 	/* Protect us against watches firing on old details when the otherend
212 	   details change, say immediately after a resume. */
213 	if (!xdev->xbusd_otherend ||
214 	    strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
215 		    strlen(xdev->xbusd_otherend))) {
216 		DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
217 		return;
218 	}
219 
220 	state = xenbus_read_driver_state(xdev->xbusd_otherend);
221 
222 	DPRINTK("state is %d, %s, %s",
223 		state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
224 	if (state == XenbusStateClosed) {
225 		int error;
226 		if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
227 			error = xdev->xbusd_u.b.b_detach(
228 			    xdev->xbusd_u.b.b_cookie);
229 			if (error) {
230 				printf("could not detach %s: %d\n",
231 				    xdev->xbusd_path, error);
232 				return;
233 			}
234 		} else {
235 			error = config_detach(xdev->xbusd_u.f.f_dev,
236 			    DETACH_FORCE);
237 			if (error) {
238 				printf("could not detach %s: %d\n",
239 				    device_xname(xdev->xbusd_u.f.f_dev), error);
240 				return;
241 			}
242 		}
243 		xenbus_free_device(xdev);
244 		return;
245 	}
246 	if (xdev->xbusd_otherend_changed)
247 		xdev->xbusd_otherend_changed(
248 		    (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
249 		    xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
250 }
251 
252 static int
253 watch_otherend(struct xenbus_device *dev)
254 {
255 	free_otherend_watch(dev);
256 
257 	return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
258 				  &dev->xbusd_otherend_watch,
259 				  otherend_changed);
260 }
261 
262 static struct xenbus_device *
263 xenbus_lookup_device_path(const char *path)
264 {
265 	struct xenbus_device *xbusd;
266 
267 	SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
268 		if (strcmp(xbusd->xbusd_path, path) == 0)
269 			return xbusd;
270 	}
271 	return NULL;
272 }
273 
274 static int
275 xenbus_probe_device_type(const char *path, const char *type,
276     int (*create)(struct xenbus_device *))
277 {
278 	int err, i, msize;
279 	unsigned long state;
280 	char **dir;
281 	unsigned int dir_n = 0;
282 	struct xenbus_device *xbusd;
283 	struct xenbusdev_attach_args xa;
284 	char *ep;
285 
286 	DPRINTK("probe %s type %s", path, type);
287 	err = xenbus_directory(NULL, path, "", &dir_n, &dir);
288 	DPRINTK("directory err %d dir_n %d", err, dir_n);
289 	if (err)
290 		return err;
291 
292 	for (i = 0; i < dir_n; i++) {
293 		err = 0;
294 		/*
295 		 * add size of path to size of xenbus_device. xenbus_device
296 		 * already has room for one char in xbusd_path.
297 		 */
298 		msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
299 		xbusd = malloc(msize, M_DEVBUF, M_WAITOK | M_ZERO);
300 		if (xbusd == NULL)
301 			panic("can't malloc xbusd");
302 
303 		snprintf(__UNCONST(xbusd->xbusd_path),
304 		    msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
305 		if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
306 			/* device already registered */
307 			free(xbusd, M_DEVBUF);
308 			continue;
309 		}
310 		err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
311 		    &state, 10);
312 		if (err) {
313 			printf("xenbus: can't get state "
314 			    "for %s (%d)\n", xbusd->xbusd_path, err);
315 			free(xbusd, M_DEVBUF);
316 			err = 0;
317 			continue;
318 		}
319 		if (state != XenbusStateInitialising) {
320 			/* device is not new */
321 			free(xbusd, M_DEVBUF);
322 			continue;
323 		}
324 
325 		xbusd->xbusd_otherend_watch.xbw_dev = xbusd;
326 		DPRINTK("xenbus_probe_device_type probe %s\n",
327 		    xbusd->xbusd_path);
328 		if (create != NULL) {
329 			xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
330 			err = read_frontend_details(xbusd);
331 			if (err != 0) {
332 				printf("xenbus: can't get frontend details "
333 				    "for %s (%d)\n", xbusd->xbusd_path, err);
334 				break;
335 			}
336 			if (create(xbusd)) {
337 				free(xbusd, M_DEVBUF);
338 				continue;
339 			}
340 		} else {
341 			xbusd->xbusd_type = XENBUS_FRONTEND_DEVICE;
342 			xa.xa_xbusd = xbusd;
343 			xa.xa_type = type;
344 			xa.xa_id = strtoul(dir[i], &ep, 0);
345 			if (dir[i][0] == '\0' || *ep != '\0') {
346 				printf("xenbus device type %s: id %s is not a"
347 				    " number\n", type, dir[i]);
348 				err = EFTYPE;
349 				free(xbusd, M_DEVBUF);
350 				break;
351 			}
352 			err = read_backend_details(xbusd);
353 			if (err != 0) {
354 				printf("xenbus: can't get backend details "
355 				    "for %s (%d)\n", xbusd->xbusd_path, err);
356 				break;
357 			}
358 			xbusd->xbusd_u.f.f_dev = config_found_ia(xenbus_dev,
359 			    "xenbus", &xa, xenbus_print);
360 			if (xbusd->xbusd_u.f.f_dev == NULL) {
361 				free(xbusd, M_DEVBUF);
362 				continue;
363 			}
364 		}
365 		SLIST_INSERT_HEAD(&xenbus_device_list,
366 		    xbusd, xbusd_entries);
367 		watch_otherend(xbusd);
368 	}
369 	free(dir, M_DEVBUF);
370 	return err;
371 }
372 
373 static int
374 xenbus_print(void *aux, const char *pnp)
375 {
376 	struct xenbusdev_attach_args *xa = aux;
377 
378 	if (pnp) {
379 		if (strcmp(xa->xa_type, "vbd") == 0)
380 			aprint_normal("xbd");
381 		else if (strcmp(xa->xa_type, "vif") == 0)
382 			aprint_normal("xennet");
383 		else if (strcmp(xa->xa_type, "balloon") == 0)
384 			aprint_normal("balloon");
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 	struct xenbusdev_attach_args balloon_xa = {
511 		.xa_id = 0,
512 		.xa_type = "balloon"
513 	};
514 
515 	KASSERT((xenstored_ready > 0));
516 
517 	/* Enumerate devices in xenstore. */
518 	xenbus_probe_frontends();
519 	xenbus_probe_backends();
520 
521 	/* Watch for changes. */
522 	fe_watch.node = malloc(strlen("device" + 1), M_DEVBUF, M_NOWAIT);
523 	strcpy(fe_watch.node, "device");
524 	fe_watch.xbw_callback = frontend_changed;
525 	register_xenbus_watch(&fe_watch);
526 	be_watch.node = malloc(strlen("backend" + 1), M_DEVBUF, M_NOWAIT);
527 	strcpy(be_watch.node, "backend");
528 	be_watch.xbw_callback = backend_changed;
529 	register_xenbus_watch(&be_watch);
530 
531 	/* attach balloon. */
532 	config_found_ia(xenbus_dev, "xenbus", &balloon_xa, xenbus_print);
533 
534 	shutdown_xenbus_setup();
535 
536 	/* Notify others that xenstore is up */
537 	//notifier_call_chain(&xenstore_chain, 0, NULL);
538 }
539 
540 static void
541 xenbus_probe_init(void *unused)
542 {
543 	int err = 0;
544 	bool dom0;
545 	vaddr_t page = 0;
546 
547 	DPRINTK("");
548 
549 	SLIST_INIT(&xenbus_device_list);
550 
551 	/*
552 	** Domain0 doesn't have a store_evtchn or store_mfn yet.
553 	*/
554 	dom0 = xendomain_is_dom0();
555 	if (dom0) {
556 #if defined(DOM0OPS)
557 		paddr_t ma;
558 		evtchn_op_t op = { .cmd = 0 };
559 
560 		/* Allocate page. */
561 		page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
562 		    UVM_KMF_ZERO | UVM_KMF_WIRED);
563 		if (!page)
564 			panic("can't get xenstore page");
565 
566 		(void)pmap_extract_ma(pmap_kernel(), page, &ma);
567 		xen_start_info.store_mfn = ma >> PAGE_SHIFT;
568 		xenstore_interface = (void *)page;
569 
570 		/* Next allocate a local port which xenstored can bind to */
571 		op.cmd = EVTCHNOP_alloc_unbound;
572 		op.u.alloc_unbound.dom        = DOMID_SELF;
573 		op.u.alloc_unbound.remote_dom = 0;
574 
575 		err = HYPERVISOR_event_channel_op(&op);
576 		if (err) {
577 			aprint_error_dev(xenbus_dev,
578 				"can't register xenstore event\n");
579 			goto err0;
580 		}
581 
582 		xen_start_info.store_evtchn = op.u.alloc_unbound.port;
583 
584 		/* And finally publish the above info in /kern/xen */
585 		xenbus_kernfs_init();
586 
587 		DELAY(1000);
588 #else /* DOM0OPS */
589 		kthread_exit(0); /* can't get a working xenstore in this case */
590 #endif /* DOM0OPS */
591 	}
592 
593 	/* register event handler */
594 	xb_init_comms(xenbus_dev);
595 
596 	/* Initialize the interface to xenstore. */
597 	err = xs_init(xenbus_dev);
598 	if (err) {
599 		aprint_error_dev(xenbus_dev,
600 				"Error initializing xenstore comms: %i\n", err);
601 		goto err0;
602 	}
603 
604 	if (!dom0) {
605 		xenstored_ready = 1;
606 		xenbus_probe(NULL);
607 	}
608 
609 	DPRINTK("done");
610 	config_pending_decr();
611 #ifdef DOM0OPS
612 	if (dom0) {
613 		int s;
614 		s = spltty();
615 		while (xenstored_ready == 0) {
616 			tsleep(&xenstored_ready, PRIBIO, "xsready", 0);
617 			xenbus_probe(NULL);
618 		}
619 		splx(s);
620 	}
621 #endif
622 	kthread_exit(0);
623 
624 err0:
625 	if (page)
626 		uvm_km_free(kernel_map, page, PAGE_SIZE,
627 				UVM_KMF_ZERO | UVM_KMF_WIRED);
628 	kthread_exit(err);
629 }
630 
631 /*
632  * Local variables:
633  *  c-file-style: "linux"
634  *  indent-tabs-mode: t
635  *  c-indent-level: 8
636  *  c-basic-offset: 8
637  *  tab-width: 8
638  * End:
639  */
640