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