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