1 /* $NetBSD: xenbus_probe.c,v 1.14 2006/09/29 14:36:30 christos 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.14 2006/09/29 14:36:30 christos 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 free(dirt, M_DEVBUF); /* to be checked */ 439 return err; 440 } 441 for (id = 0; id < dirid_n; id++) { 442 snprintf(path, sizeof(path), "backend/%s/%s", 443 dirt[type], dirid[id]); 444 err = xenbus_probe_device_type(path, dirt[type], 445 xbakd->xbakd_create); 446 if (err) 447 break; 448 } 449 free(dirid, M_DEVBUF); 450 } 451 free(dirt, M_DEVBUF); 452 return err; 453 } 454 455 int 456 xenbus_free_device(struct xenbus_device *xbusd) 457 { 458 KASSERT(xenbus_lookup_device_path(xbusd->xbusd_path) == xbusd); 459 SLIST_REMOVE(&xenbus_device_list, xbusd, xenbus_device, xbusd_entries); 460 free_otherend_watch(xbusd); 461 free(xbusd->xbusd_otherend, M_DEVBUF); 462 xenbus_switch_state(xbusd, NULL, XenbusStateClosed); 463 free(xbusd, M_DEVBUF); 464 return 0; 465 } 466 467 static void 468 frontend_changed(struct xenbus_watch *watch, 469 const char **vec, unsigned int len) 470 { 471 DPRINTK("frontend_changed %s\n", vec[XS_WATCH_PATH]); 472 xenbus_probe_frontends(); 473 } 474 475 static void 476 backend_changed(struct xenbus_watch *watch, 477 const char **vec, unsigned int len) 478 { 479 DPRINTK("backend_changed %s\n", vec[XS_WATCH_PATH]); 480 xenbus_probe_backends(); 481 } 482 483 484 /* We watch for devices appearing and vanishing. */ 485 static struct xenbus_watch fe_watch; 486 487 static struct xenbus_watch be_watch; 488 489 /* A flag to determine if xenstored is 'ready' (i.e. has started) */ 490 int xenstored_ready = 0; 491 492 void 493 xenbus_probe(void *unused) 494 { 495 KASSERT((xenstored_ready > 0)); 496 497 /* Enumerate devices in xenstore. */ 498 xenbus_probe_frontends(); 499 xenbus_probe_backends(); 500 501 /* Watch for changes. */ 502 fe_watch.node = malloc(strlen("device" + 1), M_DEVBUF, M_NOWAIT); 503 strcpy(fe_watch.node, "device"); 504 fe_watch.xbw_callback = frontend_changed; 505 register_xenbus_watch(&fe_watch); 506 be_watch.node = malloc(strlen("backend" + 1), M_DEVBUF, M_NOWAIT); 507 strcpy(be_watch.node, "backend"); 508 be_watch.xbw_callback = backend_changed; 509 register_xenbus_watch(&be_watch); 510 shutdown_xenbus_setup(); 511 512 /* Notify others that xenstore is up */ 513 //notifier_call_chain(&xenstore_chain, 0, NULL); 514 } 515 516 static void 517 xenbus_probe_init(void *unused) 518 { 519 int err = 0, dom0; 520 521 DPRINTK(""); 522 523 SLIST_INIT(&xenbus_device_list); 524 525 /* 526 ** Domain0 doesn't have a store_evtchn or store_mfn yet. 527 */ 528 dom0 = (xen_start_info.store_evtchn == 0); 529 if (dom0) { 530 #if defined(DOM0OPS) 531 vaddr_t page; 532 paddr_t ma; 533 evtchn_op_t op = { 0 }; 534 int ret; 535 536 /* Allocate page. */ 537 page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0, 538 UVM_KMF_ZERO | UVM_KMF_WIRED); 539 if (!page) 540 panic("can't get xenstore page"); 541 542 (void)pmap_extract_ma(pmap_kernel(), page, &ma); 543 xen_start_info.store_mfn = ma >> PAGE_SHIFT; 544 xenstore_interface = (void *)page; 545 546 /* Next allocate a local port which xenstored can bind to */ 547 op.cmd = EVTCHNOP_alloc_unbound; 548 op.u.alloc_unbound.dom = DOMID_SELF; 549 op.u.alloc_unbound.remote_dom = 0; 550 551 ret = HYPERVISOR_event_channel_op(&op); 552 if (ret) 553 panic("can't register xenstore event"); 554 xen_start_info.store_evtchn = op.u.alloc_unbound.port; 555 556 /* And finally publish the above info in /kern/xen */ 557 xenbus_kernfs_init(); 558 #else /* DOM0OPS */ 559 return ; /* can't get a working xenstore in this case */ 560 #endif /* DOM0OPS */ 561 } 562 563 /* register event handler */ 564 xb_init_comms((struct device *)xenbus_sc); 565 566 /* Initialize the interface to xenstore. */ 567 err = xs_init(); 568 if (err) { 569 printf("XENBUS: Error initializing xenstore comms: %i\n", err); 570 kthread_exit(err); 571 } 572 573 if (!dom0) { 574 xenstored_ready = 1; 575 xenbus_probe(NULL); 576 } 577 578 DPRINTK("done"); 579 config_pending_decr(); 580 #ifdef DOM0OPS 581 if (dom0) { 582 int s; 583 s = spltty(); 584 while (xenstored_ready == 0) { 585 tsleep(&xenstored_ready, PRIBIO, "xsready", 0); 586 xenbus_probe(NULL); 587 } 588 splx(s); 589 } 590 #endif 591 kthread_exit(0); 592 } 593 594 /* 595 * Local variables: 596 * c-file-style: "linux" 597 * indent-tabs-mode: t 598 * c-indent-level: 8 599 * c-basic-offset: 8 600 * tab-width: 8 601 * End: 602 */ 603