xenbus_probe.c revision 1.11 1 /* $NetBSD: xenbus_probe.c,v 1.11 2006/05/23 21:07:56 bouyer 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.11 2006/05/23 21:07:56 bouyer Exp $");
33
34 #if 0
35 #define DPRINTK(fmt, args...) \
36 printf("xenbus_probe (%s:%d) " fmt ".\n", __FUNCTION__, __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 <machine/hypervisor.h>
53 #include <machine/xenbus.h>
54 #include <machine/evtchn.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(struct device *, struct cfdata *, void *);
63 static void xenbus_attach(struct device *, struct device *, void *);
64 static int xenbus_print(void *, const char *);
65
66 static void xenbus_kthread_create(void *);
67 static void xenbus_probe_init(void *);
68
69 static struct xenbus_device *xenbus_lookup_device_path(const char *);
70
71 CFATTACH_DECL(xenbus, sizeof(struct device), xenbus_match, xenbus_attach,
72 NULL, NULL);
73
74 struct device *xenbus_sc;
75
76 SLIST_HEAD(, xenbus_device) xenbus_device_list;
77 SLIST_HEAD(, xenbus_backend_driver) xenbus_backend_driver_list =
78 SLIST_HEAD_INITIALIZER(xenbus_backend_driver);
79
80 int
81 xenbus_match(struct device *parent, struct cfdata *match, void *aux)
82 {
83 struct xenbus_attach_args *xa = (struct xenbus_attach_args *)aux;
84
85 if (strcmp(xa->xa_device, "xenbus") == 0)
86 return 1;
87 return 0;
88 }
89
90 static void
91 xenbus_attach(struct device *parent, struct device *self, void *aux)
92 {
93 aprint_normal(": Xen Virtual Bus Interface\n");
94 xenbus_sc = self;
95 config_pending_incr();
96 kthread_create(xenbus_kthread_create, NULL);
97 }
98
99 void
100 xenbus_backend_register(struct xenbus_backend_driver *xbakd)
101 {
102 SLIST_INSERT_HEAD(&xenbus_backend_driver_list, xbakd, xbakd_entries);
103 }
104
105 void
106 xenbus_kthread_create(void *unused)
107 {
108 struct proc *p;
109 int err;
110 err = kthread_create1(xenbus_probe_init, NULL, &p, "xenbus_probe");
111 if (err)
112 printf("kthread_create1(xenbus_probe): %d\n", err);
113 }
114
115 static int
116 read_otherend_details(struct xenbus_device *xendev,
117 const char *id_node, const char *path_node)
118 {
119 int err;
120 char *val, *ep;
121
122 err = xenbus_read(NULL, xendev->xbusd_path, id_node, NULL, &val);
123 if (err) {
124 printf("reading other end details %s from %s\n",
125 id_node, xendev->xbusd_path);
126 xenbus_dev_fatal(xendev, err,
127 "reading other end details %s from %s",
128 id_node, xendev->xbusd_path);
129 return err;
130 }
131 xendev->xbusd_otherend_id = strtoul(val, &ep, 10);
132 if (val[0] == '\0' || *ep != '\0') {
133 printf("reading other end details %s from %s: %s is not a number\n", id_node, xendev->xbusd_path, val);
134 xenbus_dev_fatal(xendev, err,
135 "reading other end details %s from %s: %s is not a number",
136 id_node, xendev->xbusd_path, val);
137 free(val, M_DEVBUF);
138 return EFTYPE;
139 }
140 free(val, M_DEVBUF);
141 err = xenbus_read(NULL, xendev->xbusd_path, path_node, NULL, &val);
142 if (err) {
143 printf("reading other end details %s from %s (%d)\n",
144 path_node, xendev->xbusd_path, err);
145 xenbus_dev_fatal(xendev, err,
146 "reading other end details %s from %s",
147 path_node, xendev->xbusd_path);
148 return err;
149 }
150 DPRINTK("read_otherend_details: read %s/%s returned %s\n",
151 xendev->xbusd_path, path_node, val);
152 xendev->xbusd_otherend = val;
153
154 if (strlen(xendev->xbusd_otherend) == 0 ||
155 !xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
156 printf("missing other end from %s\n", xendev->xbusd_path);
157 xenbus_dev_fatal(xendev, -ENOENT, "missing other end from %s",
158 xendev->xbusd_path);
159 free(xendev->xbusd_otherend, M_DEVBUF);
160 xendev->xbusd_otherend = NULL;
161 return ENOENT;
162 }
163
164 return 0;
165 }
166
167 static int
168 read_backend_details(struct xenbus_device *xendev)
169 {
170 return read_otherend_details(xendev, "backend-id", "backend");
171 }
172
173
174 static int
175 read_frontend_details(struct xenbus_device *xendev)
176 {
177 return read_otherend_details(xendev, "frontend-id", "frontend");
178 }
179
180 #if unused
181 static void
182 free_otherend_details(struct xenbus_device *dev)
183 {
184 free(dev->xbusd_otherend, M_DEVBUF);
185 dev->xbusd_otherend = NULL;
186 }
187 #endif
188
189
190 static void
191 free_otherend_watch(struct xenbus_device *dev)
192 {
193 if (dev->xbusd_otherend_watch.node) {
194 unregister_xenbus_watch(&dev->xbusd_otherend_watch);
195 free(dev->xbusd_otherend_watch.node, M_DEVBUF);
196 dev->xbusd_otherend_watch.node = NULL;
197 }
198 }
199
200 static void
201 otherend_changed(struct xenbus_watch *watch,
202 const char **vec, unsigned int len)
203 {
204 struct xenbus_device *xdev = watch->xbw_dev;
205 XenbusState state;
206
207 /* Protect us against watches firing on old details when the otherend
208 details change, say immediately after a resume. */
209 if (!xdev->xbusd_otherend ||
210 strncmp(xdev->xbusd_otherend, vec[XS_WATCH_PATH],
211 strlen(xdev->xbusd_otherend))) {
212 DPRINTK("Ignoring watch at %s", vec[XS_WATCH_PATH]);
213 return;
214 }
215
216 state = xenbus_read_driver_state(xdev->xbusd_otherend);
217
218 DPRINTK("state is %d, %s, %s",
219 state, xdev->xbusd_otherend_watch.node, vec[XS_WATCH_PATH]);
220 if (state == XenbusStateClosed) {
221 int error;
222 if (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) {
223 error = xdev->xbusd_u.b.b_detach(
224 xdev->xbusd_u.b.b_cookie);
225 if (error) {
226 printf("could not detach %s: %d\n",
227 xdev->xbusd_path, error);
228 return;
229 }
230 } else {
231 error = config_detach(xdev->xbusd_u.f.f_dev,
232 DETACH_FORCE);
233 if (error) {
234 printf("could not detach %s: %d\n",
235 xdev->xbusd_u.f.f_dev->dv_xname, error);
236 return;
237 }
238 }
239 xenbus_free_device(xdev);
240 return;
241 }
242 if (xdev->xbusd_otherend_changed)
243 xdev->xbusd_otherend_changed(
244 (xdev->xbusd_type == XENBUS_BACKEND_DEVICE) ?
245 xdev->xbusd_u.b.b_cookie : xdev->xbusd_u.f.f_dev, state);
246 }
247
248 static int
249 talk_to_otherend(struct xenbus_device *dev)
250 {
251 free_otherend_watch(dev);
252
253 return xenbus_watch_path2(dev, dev->xbusd_otherend, "state",
254 &dev->xbusd_otherend_watch,
255 otherend_changed);
256 }
257
258 static struct xenbus_device *
259 xenbus_lookup_device_path(const char *path)
260 {
261 struct xenbus_device *xbusd;
262
263 SLIST_FOREACH(xbusd, &xenbus_device_list, xbusd_entries) {
264 if (strcmp(xbusd->xbusd_path, path) == 0)
265 return xbusd;
266 }
267 return NULL;
268 }
269
270 static int
271 xenbus_probe_device_type(const char *path, const char *type,
272 int (*create)(struct xenbus_device *))
273 {
274 int err, i, msize;
275 unsigned long state;
276 char **dir;
277 unsigned int dir_n = 0;
278 struct xenbus_device *xbusd;
279 struct xenbusdev_attach_args xa;
280 char *ep;
281
282 DPRINTK("probe %s type %s", path, type);
283 err = xenbus_directory(NULL, path, "", &dir_n, &dir);
284 DPRINTK("directory err %d dir_n %d", err, dir_n);
285 if (err)
286 return err;
287
288 for (i = 0; i < dir_n; i++) {
289 /*
290 * add size of path to size of xenbus_device. xenbus_device
291 * already has room for one char in xbusd_path.
292 */
293 msize = sizeof(*xbusd) + strlen(path) + strlen(dir[i]) + 2;
294 xbusd = malloc(msize, M_DEVBUF, M_WAITOK | M_ZERO);
295 if (xbusd == NULL)
296 panic("can't malloc xbusd");
297
298 snprintf(__UNCONST(xbusd->xbusd_path),
299 msize - sizeof(*xbusd) + 1, "%s/%s", path, dir[i]);
300 if (xenbus_lookup_device_path(xbusd->xbusd_path) != NULL) {
301 /* device already registered */
302 free(xbusd, M_DEVBUF);
303 continue;
304 }
305 err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
306 &state);
307 if (err) {
308 printf("xenbus: can't get state "
309 "for %s (%d)\n", xbusd->xbusd_path, err);
310 free(xbusd, M_DEVBUF);
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_sc,
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 snprintf(path, sizeof(path), "device/%s", dir[i]);
401 err = xenbus_probe_device_type(path, dir[i], NULL);
402 if (err)
403 break;
404 }
405 free(dir, M_DEVBUF);
406 return err;
407 }
408
409 static int
410 xenbus_probe_backends(void)
411 {
412 int err;
413 char **dirt, **dirid;
414 unsigned int type, id, dirt_n, dirid_n;
415 char path[30];
416 struct xenbus_backend_driver *xbakd;
417
418 DPRINTK("probe backend");
419 err = xenbus_directory(NULL, "backend", "", &dirt_n, &dirt);
420 DPRINTK("directory err %d dirt_n %d", err, dirt_n);
421 if (err)
422 return err;
423
424 for (type = 0; type < dirt_n; type++) {
425 SLIST_FOREACH(xbakd, &xenbus_backend_driver_list,
426 xbakd_entries) {
427 if (strcmp(dirt[type], xbakd->xbakd_type) == 0)
428 break;
429 }
430 if (xbakd == NULL)
431 continue;
432 err = xenbus_directory(NULL, "backend", dirt[type],
433 &dirid_n, &dirid);
434 DPRINTK("directory backend/%s err %d dirid_n %d",
435 dirt[type], err, dirid_n);
436 if (err)
437 return err;
438 for (id = 0; id < dirid_n; id++) {
439 snprintf(path, sizeof(path), "backend/%s/%s",
440 dirt[type], dirid[id]);
441 err = xenbus_probe_device_type(path, dirt[type],
442 xbakd->xbakd_create);
443 if (err)
444 break;
445 }
446 free(dirid, M_DEVBUF);
447 }
448 free(dirt, M_DEVBUF);
449 return err;
450 }
451
452 int
453 xenbus_free_device(struct xenbus_device *xbusd)
454 {
455 KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd);
456 SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
457 free_otherend_watch(xbusd);
458 free(xbusd->xbusd_otherend, M_DEVBUF);
459 xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
460 free(xbusd, M_DEVBUF);
461 return 0;
462 }
463
464 static void
465 frontend_changed(struct xenbus_watch *watch,
466 const char **vec, unsigned int len)
467 {
468 DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]);
469 xenbus_probe_frontends();
470 }
471
472 static void
473 backend_changed(struct xenbus_watch *watch,
474 const char **vec, unsigned int len)
475 {
476 DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]);
477 xenbus_probe_backends();
478 }
479
480
481 /* We watch for devices appearing and vanishing. */
482 static struct xenbus_watch fe_watch;
483
484 static struct xenbus_watch be_watch;
485
486 /* A flag to determine if xenstored is 'ready' (i.e. has started) */
487 int xenstored_ready = 0;
488
489 void
490 xenbus_probe(void *unused)
491 {
492 KASSERT((xenstored_ready > 0));
493
494 /* Enumerate devices in xenstore. */
495 xenbus_probe_frontends();
496 xenbus_probe_backends();
497
498 /* Watch for changes. */
499 fe_watch.node = malloc(strlen("device" + 1), M_DEVBUF, M_NOWAIT);
500 strcpy(fe_watch.node, "device");
501 fe_watch.xbw_callback = frontend_changed;
502 register_xenbus_watch(&fe_watch);
503 be_watch.node = malloc(strlen("backend" + 1), M_DEVBUF, M_NOWAIT);
504 strcpy(be_watch.node, "backend");
505 be_watch.xbw_callback = backend_changed;
506 register_xenbus_watch(&be_watch);
507
508 /* Notify others that xenstore is up */
509 //notifier_call_chain(&xenstore_chain, 0, NULL);
510 }
511
512 static void
513 xenbus_probe_init(void *unused)
514 {
515 int err = 0, dom0;
516
517 DPRINTK("");
518
519 SLIST_INIT(&xenbus_device_list);
520
521 /*
522 ** Domain0 doesn't have a store_evtchn or store_mfn yet.
523 */
524 dom0 = (xen_start_info.store_evtchn == 0);
525 if (dom0) {
526 #if defined(DOM0OPS)
527 vaddr_t page;
528 paddr_t ma;
529 evtchn_op_t op = { 0 };
530 int ret;
531
532 /* Allocate page. */
533 page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
534 UVM_KMF_ZERO | UVM_KMF_WIRED);
535 if (!page)
536 panic("can't get xenstore page");
537
538 (void)pmap_extract_ma(pmap_kernel(), page, &ma);
539 xen_start_info.store_mfn = ma >> PAGE_SHIFT;
540 xenstore_interface = (void *)page;
541
542 /* Next allocate a local port which xenstored can bind to */
543 op.cmd = EVTCHNOP_alloc_unbound;
544 op.u.alloc_unbound.dom = DOMID_SELF;
545 op.u.alloc_unbound.remote_dom = 0;
546
547 ret = HYPERVISOR_event_channel_op(&op);
548 if (ret)
549 panic("can't register xenstore event");
550 xen_start_info.store_evtchn = op.u.alloc_unbound.port;
551
552 /* And finally publish the above info in /kern/xen */
553 xenbus_kernfs_init();
554 #else /* DOM0OPS */
555 return ; /* can't get a working xenstore in this case */
556 #endif /* DOM0OPS */
557 }
558
559 /* register event handler */
560 xb_init_comms((struct device *)xenbus_sc);
561
562 /* Initialize the interface to xenstore. */
563 err = xs_init();
564 if (err) {
565 printf("XENBUS: Error initializing xenstore comms: %i\n", err);
566 kthread_exit(err);
567 }
568
569 if (!dom0) {
570 xenstored_ready = 1;
571 xenbus_probe(NULL);
572 }
573
574 DPRINTK("done");
575 config_pending_decr();
576 #ifdef DOM0OPS
577 if (dom0) {
578 int s;
579 s = spltty();
580 while (xenstored_ready == 0) {
581 tsleep(&xenstored_ready, PRIBIO, "xsready", 0);
582 xenbus_probe(NULL);
583 }
584 splx(s);
585 }
586 #endif
587 kthread_exit(0);
588 }
589
590 /*
591 * Local variables:
592 * c-file-style: "linux"
593 * indent-tabs-mode: t
594 * c-indent-level: 8
595 * c-basic-offset: 8
596 * tab-width: 8
597 * End:
598 */
599