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