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