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