1 /* 2 * Copyright (c) 2018 Citrix Systems Inc. 3 * 4 * This work is licensed under the terms of the GNU GPL, version 2 or later. 5 * See the COPYING file in the top-level directory. 6 */ 7 8 #include "qemu/osdep.h" 9 #include "qemu/main-loop.h" 10 #include "qemu/module.h" 11 #include "qemu/uuid.h" 12 #include "hw/qdev-properties.h" 13 #include "hw/sysbus.h" 14 #include "hw/xen/xen.h" 15 #include "hw/xen/xen-backend.h" 16 #include "hw/xen/xen-bus.h" 17 #include "hw/xen/xen-bus-helper.h" 18 #include "monitor/monitor.h" 19 #include "qapi/error.h" 20 #include "qapi/qmp/qdict.h" 21 #include "sysemu/sysemu.h" 22 #include "trace.h" 23 24 static char *xen_device_get_backend_path(XenDevice *xendev) 25 { 26 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); 27 XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev); 28 const char *type = object_get_typename(OBJECT(xendev)); 29 const char *backend = xendev_class->backend; 30 31 if (!backend) { 32 backend = type; 33 } 34 35 return g_strdup_printf("/local/domain/%u/backend/%s/%u/%s", 36 xenbus->backend_id, backend, xendev->frontend_id, 37 xendev->name); 38 } 39 40 static char *xen_device_get_frontend_path(XenDevice *xendev) 41 { 42 XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev); 43 const char *type = object_get_typename(OBJECT(xendev)); 44 const char *device = xendev_class->device; 45 46 if (!device) { 47 device = type; 48 } 49 50 return g_strdup_printf("/local/domain/%u/device/%s/%s", 51 xendev->frontend_id, device, xendev->name); 52 } 53 54 static void xen_device_unplug(XenDevice *xendev, Error **errp) 55 { 56 ERRP_GUARD(); 57 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); 58 const char *type = object_get_typename(OBJECT(xendev)); 59 xs_transaction_t tid; 60 61 trace_xen_device_unplug(type, xendev->name); 62 63 /* Mimic the way the Xen toolstack does an unplug */ 64 again: 65 tid = qemu_xen_xs_transaction_start(xenbus->xsh); 66 if (tid == XBT_NULL) { 67 error_setg_errno(errp, errno, "failed xs_transaction_start"); 68 return; 69 } 70 71 xs_node_printf(xenbus->xsh, tid, xendev->backend_path, "online", 72 errp, "%u", 0); 73 if (*errp) { 74 goto abort; 75 } 76 77 xs_node_printf(xenbus->xsh, tid, xendev->backend_path, "state", 78 errp, "%u", XenbusStateClosing); 79 if (*errp) { 80 goto abort; 81 } 82 83 if (!qemu_xen_xs_transaction_end(xenbus->xsh, tid, false)) { 84 if (errno == EAGAIN) { 85 goto again; 86 } 87 88 error_setg_errno(errp, errno, "failed xs_transaction_end"); 89 } 90 91 return; 92 93 abort: 94 /* 95 * We only abort if there is already a failure so ignore any error 96 * from ending the transaction. 97 */ 98 qemu_xen_xs_transaction_end(xenbus->xsh, tid, true); 99 } 100 101 static void xen_bus_print_dev(Monitor *mon, DeviceState *dev, int indent) 102 { 103 XenDevice *xendev = XEN_DEVICE(dev); 104 105 monitor_printf(mon, "%*sname = '%s' frontend_id = %u\n", 106 indent, "", xendev->name, xendev->frontend_id); 107 } 108 109 static char *xen_bus_get_dev_path(DeviceState *dev) 110 { 111 return xen_device_get_backend_path(XEN_DEVICE(dev)); 112 } 113 114 static void xen_bus_backend_create(XenBus *xenbus, const char *type, 115 const char *name, char *path, 116 Error **errp) 117 { 118 ERRP_GUARD(); 119 xs_transaction_t tid; 120 char **key; 121 QDict *opts; 122 unsigned int i, n; 123 124 trace_xen_bus_backend_create(type, path); 125 126 again: 127 tid = qemu_xen_xs_transaction_start(xenbus->xsh); 128 if (tid == XBT_NULL) { 129 error_setg(errp, "failed xs_transaction_start"); 130 return; 131 } 132 133 key = qemu_xen_xs_directory(xenbus->xsh, tid, path, &n); 134 if (!key) { 135 if (!qemu_xen_xs_transaction_end(xenbus->xsh, tid, true)) { 136 error_setg_errno(errp, errno, "failed xs_transaction_end"); 137 } 138 return; 139 } 140 141 opts = qdict_new(); 142 for (i = 0; i < n; i++) { 143 char *val; 144 145 /* 146 * Assume anything found in the xenstore backend area, other than 147 * the keys created for a generic XenDevice, are parameters 148 * to be used to configure the backend. 149 */ 150 if (!strcmp(key[i], "state") || 151 !strcmp(key[i], "online") || 152 !strcmp(key[i], "frontend") || 153 !strcmp(key[i], "frontend-id") || 154 !strcmp(key[i], "hotplug-status")) 155 continue; 156 157 if (xs_node_scanf(xenbus->xsh, tid, path, key[i], NULL, "%ms", 158 &val) == 1) { 159 qdict_put_str(opts, key[i], val); 160 free(val); 161 } 162 } 163 164 free(key); 165 166 if (!qemu_xen_xs_transaction_end(xenbus->xsh, tid, false)) { 167 qobject_unref(opts); 168 169 if (errno == EAGAIN) { 170 goto again; 171 } 172 173 error_setg_errno(errp, errno, "failed xs_transaction_end"); 174 return; 175 } 176 177 xen_backend_device_create(xenbus, type, name, opts, errp); 178 qobject_unref(opts); 179 180 if (*errp) { 181 error_prepend(errp, "failed to create '%s' device '%s': ", type, name); 182 } 183 } 184 185 static void xen_bus_type_enumerate(XenBus *xenbus, const char *type) 186 { 187 char *domain_path = g_strdup_printf("backend/%s/%u", type, xen_domid); 188 char **backend; 189 unsigned int i, n; 190 191 trace_xen_bus_type_enumerate(type); 192 193 backend = qemu_xen_xs_directory(xenbus->xsh, XBT_NULL, domain_path, &n); 194 if (!backend) { 195 goto out; 196 } 197 198 for (i = 0; i < n; i++) { 199 char *backend_path = g_strdup_printf("%s/%s", domain_path, 200 backend[i]); 201 enum xenbus_state state; 202 unsigned int online; 203 204 if (xs_node_scanf(xenbus->xsh, XBT_NULL, backend_path, "state", 205 NULL, "%u", &state) != 1) 206 state = XenbusStateUnknown; 207 208 if (xs_node_scanf(xenbus->xsh, XBT_NULL, backend_path, "online", 209 NULL, "%u", &online) != 1) 210 online = 0; 211 212 if (online && state == XenbusStateInitialising) { 213 Error *local_err = NULL; 214 215 xen_bus_backend_create(xenbus, type, backend[i], backend_path, 216 &local_err); 217 if (local_err) { 218 error_report_err(local_err); 219 } 220 } 221 222 g_free(backend_path); 223 } 224 225 free(backend); 226 227 out: 228 g_free(domain_path); 229 } 230 231 static void xen_bus_enumerate(XenBus *xenbus) 232 { 233 char **type; 234 unsigned int i, n; 235 236 trace_xen_bus_enumerate(); 237 238 type = qemu_xen_xs_directory(xenbus->xsh, XBT_NULL, "backend", &n); 239 if (!type) { 240 return; 241 } 242 243 for (i = 0; i < n; i++) { 244 xen_bus_type_enumerate(xenbus, type[i]); 245 } 246 247 free(type); 248 } 249 250 static void xen_bus_device_cleanup(XenDevice *xendev) 251 { 252 const char *type = object_get_typename(OBJECT(xendev)); 253 Error *local_err = NULL; 254 255 trace_xen_bus_device_cleanup(type, xendev->name); 256 257 g_assert(!xendev->backend_online); 258 259 if (!xen_backend_try_device_destroy(xendev, &local_err)) { 260 object_unparent(OBJECT(xendev)); 261 } 262 263 if (local_err) { 264 error_report_err(local_err); 265 } 266 } 267 268 static void xen_bus_cleanup(XenBus *xenbus) 269 { 270 XenDevice *xendev, *next; 271 272 trace_xen_bus_cleanup(); 273 274 QLIST_FOREACH_SAFE(xendev, &xenbus->inactive_devices, list, next) { 275 g_assert(xendev->inactive); 276 QLIST_REMOVE(xendev, list); 277 xen_bus_device_cleanup(xendev); 278 } 279 } 280 281 static void xen_bus_backend_changed(void *opaque, const char *path) 282 { 283 XenBus *xenbus = opaque; 284 285 xen_bus_enumerate(xenbus); 286 xen_bus_cleanup(xenbus); 287 } 288 289 static void xen_bus_unrealize(BusState *bus) 290 { 291 XenBus *xenbus = XEN_BUS(bus); 292 293 trace_xen_bus_unrealize(); 294 295 if (xenbus->backend_watch) { 296 unsigned int i; 297 298 for (i = 0; i < xenbus->backend_types; i++) { 299 if (xenbus->backend_watch[i]) { 300 xs_node_unwatch(xenbus->xsh, xenbus->backend_watch[i]); 301 } 302 } 303 304 g_free(xenbus->backend_watch); 305 xenbus->backend_watch = NULL; 306 } 307 308 if (xenbus->xsh) { 309 qemu_xen_xs_close(xenbus->xsh); 310 } 311 } 312 313 static void xen_bus_realize(BusState *bus, Error **errp) 314 { 315 char *key = g_strdup_printf("%u", xen_domid); 316 XenBus *xenbus = XEN_BUS(bus); 317 unsigned int domid; 318 const char **type; 319 unsigned int i; 320 Error *local_err = NULL; 321 322 trace_xen_bus_realize(); 323 324 xenbus->xsh = qemu_xen_xs_open(); 325 if (!xenbus->xsh) { 326 error_setg_errno(errp, errno, "failed xs_open"); 327 goto fail; 328 } 329 330 if (xs_node_scanf(xenbus->xsh, XBT_NULL, "", /* domain root node */ 331 "domid", NULL, "%u", &domid) == 1) { 332 xenbus->backend_id = domid; 333 } else { 334 xenbus->backend_id = 0; /* Assume lack of node means dom0 */ 335 } 336 337 module_call_init(MODULE_INIT_XEN_BACKEND); 338 339 type = xen_backend_get_types(&xenbus->backend_types); 340 xenbus->backend_watch = g_new(struct qemu_xs_watch *, 341 xenbus->backend_types); 342 343 for (i = 0; i < xenbus->backend_types; i++) { 344 char *node = g_strdup_printf("backend/%s", type[i]); 345 346 xenbus->backend_watch[i] = 347 xs_node_watch(xenbus->xsh, node, key, xen_bus_backend_changed, 348 xenbus, &local_err); 349 if (local_err) { 350 /* This need not be treated as a hard error so don't propagate */ 351 error_reportf_err(local_err, 352 "failed to set up '%s' enumeration watch: ", 353 type[i]); 354 } 355 356 g_free(node); 357 } 358 359 g_free(type); 360 g_free(key); 361 return; 362 363 fail: 364 xen_bus_unrealize(bus); 365 g_free(key); 366 } 367 368 static void xen_bus_unplug_request(HotplugHandler *hotplug, 369 DeviceState *dev, 370 Error **errp) 371 { 372 XenDevice *xendev = XEN_DEVICE(dev); 373 374 xen_device_unplug(xendev, errp); 375 } 376 377 static void xen_bus_class_init(ObjectClass *class, void *data) 378 { 379 BusClass *bus_class = BUS_CLASS(class); 380 HotplugHandlerClass *hotplug_class = HOTPLUG_HANDLER_CLASS(class); 381 382 bus_class->print_dev = xen_bus_print_dev; 383 bus_class->get_dev_path = xen_bus_get_dev_path; 384 bus_class->realize = xen_bus_realize; 385 bus_class->unrealize = xen_bus_unrealize; 386 387 hotplug_class->unplug_request = xen_bus_unplug_request; 388 } 389 390 static const TypeInfo xen_bus_type_info = { 391 .name = TYPE_XEN_BUS, 392 .parent = TYPE_BUS, 393 .instance_size = sizeof(XenBus), 394 .class_size = sizeof(XenBusClass), 395 .class_init = xen_bus_class_init, 396 .interfaces = (InterfaceInfo[]) { 397 { TYPE_HOTPLUG_HANDLER }, 398 { } 399 }, 400 }; 401 402 void xen_device_backend_printf(XenDevice *xendev, const char *key, 403 const char *fmt, ...) 404 { 405 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); 406 Error *local_err = NULL; 407 va_list ap; 408 409 g_assert(xenbus->xsh); 410 411 va_start(ap, fmt); 412 xs_node_vprintf(xenbus->xsh, XBT_NULL, xendev->backend_path, key, 413 &local_err, fmt, ap); 414 va_end(ap); 415 416 if (local_err) { 417 error_report_err(local_err); 418 } 419 } 420 421 G_GNUC_SCANF(3, 4) 422 static int xen_device_backend_scanf(XenDevice *xendev, const char *key, 423 const char *fmt, ...) 424 { 425 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); 426 va_list ap; 427 int rc; 428 429 g_assert(xenbus->xsh); 430 431 va_start(ap, fmt); 432 rc = xs_node_vscanf(xenbus->xsh, XBT_NULL, xendev->backend_path, key, 433 NULL, fmt, ap); 434 va_end(ap); 435 436 return rc; 437 } 438 439 void xen_device_backend_set_state(XenDevice *xendev, 440 enum xenbus_state state) 441 { 442 const char *type = object_get_typename(OBJECT(xendev)); 443 444 if (xendev->backend_state == state) { 445 return; 446 } 447 448 trace_xen_device_backend_state(type, xendev->name, 449 xs_strstate(state)); 450 451 xendev->backend_state = state; 452 xen_device_backend_printf(xendev, "state", "%u", state); 453 } 454 455 enum xenbus_state xen_device_backend_get_state(XenDevice *xendev) 456 { 457 return xendev->backend_state; 458 } 459 460 static void xen_device_backend_set_online(XenDevice *xendev, bool online) 461 { 462 const char *type = object_get_typename(OBJECT(xendev)); 463 464 if (xendev->backend_online == online) { 465 return; 466 } 467 468 trace_xen_device_backend_online(type, xendev->name, online); 469 470 xendev->backend_online = online; 471 xen_device_backend_printf(xendev, "online", "%u", online); 472 } 473 474 /* 475 * Tell from the state whether the frontend is likely alive, 476 * i.e. it will react to a change of state of the backend. 477 */ 478 static bool xen_device_frontend_is_active(XenDevice *xendev) 479 { 480 switch (xendev->frontend_state) { 481 case XenbusStateInitWait: 482 case XenbusStateInitialised: 483 case XenbusStateConnected: 484 case XenbusStateClosing: 485 return true; 486 default: 487 return false; 488 } 489 } 490 491 static void xen_device_backend_changed(void *opaque, const char *path) 492 { 493 XenDevice *xendev = opaque; 494 const char *type = object_get_typename(OBJECT(xendev)); 495 enum xenbus_state state; 496 unsigned int online; 497 498 trace_xen_device_backend_changed(type, xendev->name); 499 500 if (xen_device_backend_scanf(xendev, "state", "%u", &state) != 1) { 501 state = XenbusStateUnknown; 502 } 503 504 xen_device_backend_set_state(xendev, state); 505 506 if (xen_device_backend_scanf(xendev, "online", "%u", &online) != 1) { 507 online = 0; 508 } 509 510 xen_device_backend_set_online(xendev, !!online); 511 512 /* 513 * If the toolstack (or unplug request callback) has set the backend 514 * state to Closing, but there is no active frontend then set the 515 * backend state to Closed. 516 */ 517 if (state == XenbusStateClosing && 518 !xen_device_frontend_is_active(xendev)) { 519 xen_device_backend_set_state(xendev, XenbusStateClosed); 520 } 521 522 /* 523 * If a backend is still 'online' then we should leave it alone but, 524 * if a backend is not 'online', then the device is a candidate 525 * for destruction. Hence add it to the 'inactive' list to be cleaned 526 * by xen_bus_cleanup(). 527 */ 528 if (!online && 529 (state == XenbusStateClosed || state == XenbusStateInitialising || 530 state == XenbusStateInitWait || state == XenbusStateUnknown) && 531 !xendev->inactive) { 532 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); 533 534 xendev->inactive = true; 535 QLIST_INSERT_HEAD(&xenbus->inactive_devices, xendev, list); 536 537 /* 538 * Re-write the state to cause a XenBus backend_watch notification, 539 * resulting in a call to xen_bus_cleanup(). 540 */ 541 xen_device_backend_printf(xendev, "state", "%u", state); 542 } 543 } 544 545 static void xen_device_backend_create(XenDevice *xendev, Error **errp) 546 { 547 ERRP_GUARD(); 548 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); 549 550 xendev->backend_path = xen_device_get_backend_path(xendev); 551 552 g_assert(xenbus->xsh); 553 554 xs_node_create(xenbus->xsh, XBT_NULL, xendev->backend_path, 555 xenbus->backend_id, xendev->frontend_id, XS_PERM_READ, errp); 556 if (*errp) { 557 error_prepend(errp, "failed to create backend: "); 558 return; 559 } 560 561 xendev->backend_state_watch = 562 xs_node_watch(xendev->xsh, xendev->backend_path, 563 "state", xen_device_backend_changed, xendev, 564 errp); 565 if (*errp) { 566 error_prepend(errp, "failed to watch backend state: "); 567 return; 568 } 569 570 xendev->backend_online_watch = 571 xs_node_watch(xendev->xsh, xendev->backend_path, 572 "online", xen_device_backend_changed, xendev, 573 errp); 574 if (*errp) { 575 error_prepend(errp, "failed to watch backend online: "); 576 return; 577 } 578 } 579 580 static void xen_device_backend_destroy(XenDevice *xendev) 581 { 582 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); 583 Error *local_err = NULL; 584 585 if (xendev->backend_online_watch) { 586 xs_node_unwatch(xendev->xsh, xendev->backend_online_watch); 587 xendev->backend_online_watch = NULL; 588 } 589 590 if (xendev->backend_state_watch) { 591 xs_node_unwatch(xendev->xsh, xendev->backend_state_watch); 592 xendev->backend_state_watch = NULL; 593 } 594 595 if (!xendev->backend_path) { 596 return; 597 } 598 599 g_assert(xenbus->xsh); 600 601 xs_node_destroy(xenbus->xsh, XBT_NULL, xendev->backend_path, 602 &local_err); 603 g_free(xendev->backend_path); 604 xendev->backend_path = NULL; 605 606 if (local_err) { 607 error_report_err(local_err); 608 } 609 } 610 611 void xen_device_frontend_printf(XenDevice *xendev, const char *key, 612 const char *fmt, ...) 613 { 614 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); 615 Error *local_err = NULL; 616 va_list ap; 617 618 g_assert(xenbus->xsh); 619 620 va_start(ap, fmt); 621 xs_node_vprintf(xenbus->xsh, XBT_NULL, xendev->frontend_path, key, 622 &local_err, fmt, ap); 623 va_end(ap); 624 625 if (local_err) { 626 error_report_err(local_err); 627 } 628 } 629 630 int xen_device_frontend_scanf(XenDevice *xendev, const char *key, 631 const char *fmt, ...) 632 { 633 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); 634 va_list ap; 635 int rc; 636 637 g_assert(xenbus->xsh); 638 639 va_start(ap, fmt); 640 rc = xs_node_vscanf(xenbus->xsh, XBT_NULL, xendev->frontend_path, key, 641 NULL, fmt, ap); 642 va_end(ap); 643 644 return rc; 645 } 646 647 static void xen_device_frontend_set_state(XenDevice *xendev, 648 enum xenbus_state state, 649 bool publish) 650 { 651 const char *type = object_get_typename(OBJECT(xendev)); 652 653 if (xendev->frontend_state == state) { 654 return; 655 } 656 657 trace_xen_device_frontend_state(type, xendev->name, 658 xs_strstate(state)); 659 660 xendev->frontend_state = state; 661 if (publish) { 662 xen_device_frontend_printf(xendev, "state", "%u", state); 663 } 664 } 665 666 static void xen_device_frontend_changed(void *opaque, const char *path) 667 { 668 XenDevice *xendev = opaque; 669 XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev); 670 const char *type = object_get_typename(OBJECT(xendev)); 671 enum xenbus_state state; 672 673 trace_xen_device_frontend_changed(type, xendev->name); 674 675 if (xen_device_frontend_scanf(xendev, "state", "%u", &state) != 1) { 676 state = XenbusStateUnknown; 677 } 678 679 xen_device_frontend_set_state(xendev, state, false); 680 681 if (state == XenbusStateInitialising && 682 xendev->backend_state == XenbusStateClosed && 683 xendev->backend_online) { 684 /* 685 * The frontend is re-initializing so switch back to 686 * InitWait. 687 */ 688 xen_device_backend_set_state(xendev, XenbusStateInitWait); 689 return; 690 } 691 692 if (xendev_class->frontend_changed) { 693 Error *local_err = NULL; 694 695 xendev_class->frontend_changed(xendev, state, &local_err); 696 697 if (local_err) { 698 error_reportf_err(local_err, "frontend change error: "); 699 } 700 } 701 } 702 703 static bool xen_device_frontend_exists(XenDevice *xendev) 704 { 705 enum xenbus_state state; 706 707 return (xen_device_frontend_scanf(xendev, "state", "%u", &state) == 1); 708 } 709 710 static void xen_device_frontend_create(XenDevice *xendev, Error **errp) 711 { 712 ERRP_GUARD(); 713 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); 714 715 xendev->frontend_path = xen_device_get_frontend_path(xendev); 716 717 /* 718 * The frontend area may have already been created by a legacy 719 * toolstack. 720 */ 721 if (!xen_device_frontend_exists(xendev)) { 722 g_assert(xenbus->xsh); 723 724 xs_node_create(xenbus->xsh, XBT_NULL, xendev->frontend_path, 725 xendev->frontend_id, xenbus->backend_id, 726 XS_PERM_READ | XS_PERM_WRITE, errp); 727 if (*errp) { 728 error_prepend(errp, "failed to create frontend: "); 729 return; 730 } 731 } 732 733 xendev->frontend_state_watch = 734 xs_node_watch(xendev->xsh, xendev->frontend_path, "state", 735 xen_device_frontend_changed, xendev, errp); 736 if (*errp) { 737 error_prepend(errp, "failed to watch frontend state: "); 738 } 739 } 740 741 static void xen_device_frontend_destroy(XenDevice *xendev) 742 { 743 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); 744 Error *local_err = NULL; 745 746 if (xendev->frontend_state_watch) { 747 xs_node_unwatch(xendev->xsh, xendev->frontend_state_watch); 748 xendev->frontend_state_watch = NULL; 749 } 750 751 if (!xendev->frontend_path) { 752 return; 753 } 754 755 g_assert(xenbus->xsh); 756 757 xs_node_destroy(xenbus->xsh, XBT_NULL, xendev->frontend_path, 758 &local_err); 759 g_free(xendev->frontend_path); 760 xendev->frontend_path = NULL; 761 762 if (local_err) { 763 error_report_err(local_err); 764 } 765 } 766 767 void xen_device_set_max_grant_refs(XenDevice *xendev, unsigned int nr_refs, 768 Error **errp) 769 { 770 if (qemu_xen_gnttab_set_max_grants(xendev->xgth, nr_refs)) { 771 error_setg_errno(errp, errno, "xengnttab_set_max_grants failed"); 772 } 773 } 774 775 void *xen_device_map_grant_refs(XenDevice *xendev, uint32_t *refs, 776 unsigned int nr_refs, int prot, 777 Error **errp) 778 { 779 void *map = qemu_xen_gnttab_map_refs(xendev->xgth, nr_refs, 780 xendev->frontend_id, refs, prot); 781 782 if (!map) { 783 error_setg_errno(errp, errno, 784 "xengnttab_map_domain_grant_refs failed"); 785 } 786 787 return map; 788 } 789 790 void xen_device_unmap_grant_refs(XenDevice *xendev, void *map, uint32_t *refs, 791 unsigned int nr_refs, Error **errp) 792 { 793 if (qemu_xen_gnttab_unmap(xendev->xgth, map, refs, nr_refs)) { 794 error_setg_errno(errp, errno, "xengnttab_unmap failed"); 795 } 796 } 797 798 void xen_device_copy_grant_refs(XenDevice *xendev, bool to_domain, 799 XenDeviceGrantCopySegment segs[], 800 unsigned int nr_segs, Error **errp) 801 { 802 qemu_xen_gnttab_grant_copy(xendev->xgth, to_domain, xendev->frontend_id, 803 (XenGrantCopySegment *)segs, nr_segs, errp); 804 } 805 806 struct XenEventChannel { 807 QLIST_ENTRY(XenEventChannel) list; 808 AioContext *ctx; 809 xenevtchn_handle *xeh; 810 evtchn_port_t local_port; 811 XenEventHandler handler; 812 void *opaque; 813 }; 814 815 static bool xen_device_poll(void *opaque) 816 { 817 XenEventChannel *channel = opaque; 818 819 return channel->handler(channel->opaque); 820 } 821 822 static void xen_device_event(void *opaque) 823 { 824 XenEventChannel *channel = opaque; 825 unsigned long port = qemu_xen_evtchn_pending(channel->xeh); 826 827 if (port == channel->local_port) { 828 xen_device_poll(channel); 829 830 qemu_xen_evtchn_unmask(channel->xeh, port); 831 } 832 } 833 834 void xen_device_set_event_channel_context(XenDevice *xendev, 835 XenEventChannel *channel, 836 AioContext *ctx, 837 Error **errp) 838 { 839 if (!channel) { 840 error_setg(errp, "bad channel"); 841 return; 842 } 843 844 if (channel->ctx) 845 aio_set_fd_handler(channel->ctx, qemu_xen_evtchn_fd(channel->xeh), true, 846 NULL, NULL, NULL, NULL, NULL); 847 848 channel->ctx = ctx; 849 aio_set_fd_handler(channel->ctx, qemu_xen_evtchn_fd(channel->xeh), true, 850 xen_device_event, NULL, xen_device_poll, NULL, channel); 851 } 852 853 XenEventChannel *xen_device_bind_event_channel(XenDevice *xendev, 854 unsigned int port, 855 XenEventHandler handler, 856 void *opaque, Error **errp) 857 { 858 XenEventChannel *channel = g_new0(XenEventChannel, 1); 859 xenevtchn_port_or_error_t local_port; 860 861 channel->xeh = qemu_xen_evtchn_open(); 862 if (!channel->xeh) { 863 error_setg_errno(errp, errno, "failed xenevtchn_open"); 864 goto fail; 865 } 866 867 local_port = qemu_xen_evtchn_bind_interdomain(channel->xeh, 868 xendev->frontend_id, 869 port); 870 if (local_port < 0) { 871 error_setg_errno(errp, errno, "xenevtchn_bind_interdomain failed"); 872 goto fail; 873 } 874 875 channel->local_port = local_port; 876 channel->handler = handler; 877 channel->opaque = opaque; 878 879 /* Only reason for failure is a NULL channel */ 880 xen_device_set_event_channel_context(xendev, channel, 881 qemu_get_aio_context(), 882 &error_abort); 883 884 QLIST_INSERT_HEAD(&xendev->event_channels, channel, list); 885 886 return channel; 887 888 fail: 889 if (channel->xeh) { 890 qemu_xen_evtchn_close(channel->xeh); 891 } 892 893 g_free(channel); 894 895 return NULL; 896 } 897 898 void xen_device_notify_event_channel(XenDevice *xendev, 899 XenEventChannel *channel, 900 Error **errp) 901 { 902 if (!channel) { 903 error_setg(errp, "bad channel"); 904 return; 905 } 906 907 if (qemu_xen_evtchn_notify(channel->xeh, channel->local_port) < 0) { 908 error_setg_errno(errp, errno, "xenevtchn_notify failed"); 909 } 910 } 911 912 void xen_device_unbind_event_channel(XenDevice *xendev, 913 XenEventChannel *channel, 914 Error **errp) 915 { 916 if (!channel) { 917 error_setg(errp, "bad channel"); 918 return; 919 } 920 921 QLIST_REMOVE(channel, list); 922 923 aio_set_fd_handler(channel->ctx, qemu_xen_evtchn_fd(channel->xeh), true, 924 NULL, NULL, NULL, NULL, NULL); 925 926 if (qemu_xen_evtchn_unbind(channel->xeh, channel->local_port) < 0) { 927 error_setg_errno(errp, errno, "xenevtchn_unbind failed"); 928 } 929 930 qemu_xen_evtchn_close(channel->xeh); 931 g_free(channel); 932 } 933 934 static void xen_device_unrealize(DeviceState *dev) 935 { 936 XenDevice *xendev = XEN_DEVICE(dev); 937 XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev); 938 const char *type = object_get_typename(OBJECT(xendev)); 939 XenEventChannel *channel, *next; 940 941 if (!xendev->name) { 942 return; 943 } 944 945 trace_xen_device_unrealize(type, xendev->name); 946 947 if (xendev->exit.notify) { 948 qemu_remove_exit_notifier(&xendev->exit); 949 xendev->exit.notify = NULL; 950 } 951 952 if (xendev_class->unrealize) { 953 xendev_class->unrealize(xendev); 954 } 955 956 /* Make sure all event channels are cleaned up */ 957 QLIST_FOREACH_SAFE(channel, &xendev->event_channels, list, next) { 958 xen_device_unbind_event_channel(xendev, channel, NULL); 959 } 960 961 xen_device_frontend_destroy(xendev); 962 xen_device_backend_destroy(xendev); 963 964 if (xendev->xgth) { 965 qemu_xen_gnttab_close(xendev->xgth); 966 xendev->xgth = NULL; 967 } 968 969 if (xendev->xsh) { 970 qemu_xen_xs_close(xendev->xsh); 971 xendev->xsh = NULL; 972 } 973 974 g_free(xendev->name); 975 xendev->name = NULL; 976 } 977 978 static void xen_device_exit(Notifier *n, void *data) 979 { 980 XenDevice *xendev = container_of(n, XenDevice, exit); 981 982 xen_device_unrealize(DEVICE(xendev)); 983 } 984 985 static void xen_device_realize(DeviceState *dev, Error **errp) 986 { 987 ERRP_GUARD(); 988 XenDevice *xendev = XEN_DEVICE(dev); 989 XenDeviceClass *xendev_class = XEN_DEVICE_GET_CLASS(xendev); 990 XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); 991 const char *type = object_get_typename(OBJECT(xendev)); 992 993 if (xendev->frontend_id == DOMID_INVALID) { 994 xendev->frontend_id = xen_domid; 995 } 996 997 if (xendev->frontend_id >= DOMID_FIRST_RESERVED) { 998 error_setg(errp, "invalid frontend-id"); 999 goto unrealize; 1000 } 1001 1002 if (!xendev_class->get_name) { 1003 error_setg(errp, "get_name method not implemented"); 1004 goto unrealize; 1005 } 1006 1007 xendev->name = xendev_class->get_name(xendev, errp); 1008 if (*errp) { 1009 error_prepend(errp, "failed to get device name: "); 1010 goto unrealize; 1011 } 1012 1013 trace_xen_device_realize(type, xendev->name); 1014 1015 xendev->xsh = qemu_xen_xs_open(); 1016 if (!xendev->xsh) { 1017 error_setg_errno(errp, errno, "failed xs_open"); 1018 goto unrealize; 1019 } 1020 1021 xendev->xgth = qemu_xen_gnttab_open(); 1022 if (!xendev->xgth) { 1023 error_setg_errno(errp, errno, "failed xengnttab_open"); 1024 goto unrealize; 1025 } 1026 1027 xen_device_backend_create(xendev, errp); 1028 if (*errp) { 1029 goto unrealize; 1030 } 1031 1032 xen_device_frontend_create(xendev, errp); 1033 if (*errp) { 1034 goto unrealize; 1035 } 1036 1037 xen_device_backend_printf(xendev, "frontend", "%s", 1038 xendev->frontend_path); 1039 xen_device_backend_printf(xendev, "frontend-id", "%u", 1040 xendev->frontend_id); 1041 xen_device_backend_printf(xendev, "hotplug-status", "connected"); 1042 1043 xen_device_backend_set_online(xendev, true); 1044 xen_device_backend_set_state(xendev, XenbusStateInitWait); 1045 1046 if (!xen_device_frontend_exists(xendev)) { 1047 xen_device_frontend_printf(xendev, "backend", "%s", 1048 xendev->backend_path); 1049 xen_device_frontend_printf(xendev, "backend-id", "%u", 1050 xenbus->backend_id); 1051 1052 xen_device_frontend_set_state(xendev, XenbusStateInitialising, true); 1053 } 1054 1055 if (xendev_class->realize) { 1056 xendev_class->realize(xendev, errp); 1057 if (*errp) { 1058 goto unrealize; 1059 } 1060 } 1061 1062 xendev->exit.notify = xen_device_exit; 1063 qemu_add_exit_notifier(&xendev->exit); 1064 return; 1065 1066 unrealize: 1067 xen_device_unrealize(dev); 1068 } 1069 1070 static Property xen_device_props[] = { 1071 DEFINE_PROP_UINT16("frontend-id", XenDevice, frontend_id, 1072 DOMID_INVALID), 1073 DEFINE_PROP_END_OF_LIST() 1074 }; 1075 1076 static void xen_device_class_init(ObjectClass *class, void *data) 1077 { 1078 DeviceClass *dev_class = DEVICE_CLASS(class); 1079 1080 dev_class->realize = xen_device_realize; 1081 dev_class->unrealize = xen_device_unrealize; 1082 device_class_set_props(dev_class, xen_device_props); 1083 dev_class->bus_type = TYPE_XEN_BUS; 1084 } 1085 1086 static const TypeInfo xen_device_type_info = { 1087 .name = TYPE_XEN_DEVICE, 1088 .parent = TYPE_DEVICE, 1089 .instance_size = sizeof(XenDevice), 1090 .abstract = true, 1091 .class_size = sizeof(XenDeviceClass), 1092 .class_init = xen_device_class_init, 1093 }; 1094 1095 typedef struct XenBridge { 1096 SysBusDevice busdev; 1097 } XenBridge; 1098 1099 #define TYPE_XEN_BRIDGE "xen-bridge" 1100 1101 static const TypeInfo xen_bridge_type_info = { 1102 .name = TYPE_XEN_BRIDGE, 1103 .parent = TYPE_SYS_BUS_DEVICE, 1104 .instance_size = sizeof(XenBridge), 1105 }; 1106 1107 static void xen_register_types(void) 1108 { 1109 type_register_static(&xen_bridge_type_info); 1110 type_register_static(&xen_bus_type_info); 1111 type_register_static(&xen_device_type_info); 1112 } 1113 1114 type_init(xen_register_types) 1115 1116 void xen_bus_init(void) 1117 { 1118 DeviceState *dev = qdev_new(TYPE_XEN_BRIDGE); 1119 BusState *bus = qbus_new(TYPE_XEN_BUS, dev, NULL); 1120 1121 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 1122 qbus_set_bus_hotplug_handler(bus); 1123 } 1124