1 /* 2 * qdev property parsing 3 * (parts specific for qemu-system-*) 4 * 5 * This file is based on code from hw/qdev-properties.c from 6 * commit 074a86fccd185616469dfcdc0e157f438aebba18, 7 * Copyright (c) Gerd Hoffmann <kraxel@redhat.com> and other contributors. 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "hw/qdev-properties.h" 15 #include "hw/qdev-properties-system.h" 16 #include "qapi/error.h" 17 #include "qapi/visitor.h" 18 #include "qapi/qapi-types-block.h" 19 #include "qapi/qapi-types-machine.h" 20 #include "qapi/qapi-types-migration.h" 21 #include "qapi/qmp/qerror.h" 22 #include "qemu/ctype.h" 23 #include "qemu/cutils.h" 24 #include "qemu/units.h" 25 #include "qemu/uuid.h" 26 #include "qemu/error-report.h" 27 #include "qdev-prop-internal.h" 28 29 #include "audio/audio.h" 30 #include "chardev/char-fe.h" 31 #include "sysemu/block-backend.h" 32 #include "sysemu/blockdev.h" 33 #include "net/net.h" 34 #include "hw/pci/pci.h" 35 #include "hw/pci/pcie.h" 36 #include "util/block-helpers.h" 37 38 static bool check_prop_still_unset(Object *obj, const char *name, 39 const void *old_val, const char *new_val, 40 bool allow_override, Error **errp) 41 { 42 const GlobalProperty *prop = qdev_find_global_prop(obj, name); 43 44 if (!old_val || (!prop && allow_override)) { 45 return true; 46 } 47 48 if (prop) { 49 error_setg(errp, "-global %s.%s=... conflicts with %s=%s", 50 prop->driver, prop->property, name, new_val); 51 } else { 52 /* Error message is vague, but a better one would be hard */ 53 error_setg(errp, "%s=%s conflicts, and override is not implemented", 54 name, new_val); 55 } 56 return false; 57 } 58 59 60 /* --- drive --- */ 61 62 static void get_drive(Object *obj, Visitor *v, const char *name, void *opaque, 63 Error **errp) 64 { 65 Property *prop = opaque; 66 void **ptr = object_field_prop_ptr(obj, prop); 67 const char *value; 68 char *p; 69 70 if (*ptr) { 71 value = blk_name(*ptr); 72 if (!*value) { 73 BlockDriverState *bs = blk_bs(*ptr); 74 if (bs) { 75 value = bdrv_get_node_name(bs); 76 } 77 } 78 } else { 79 value = ""; 80 } 81 82 p = g_strdup(value); 83 visit_type_str(v, name, &p, errp); 84 g_free(p); 85 } 86 87 static void set_drive_helper(Object *obj, Visitor *v, const char *name, 88 void *opaque, bool iothread, Error **errp) 89 { 90 DeviceState *dev = DEVICE(obj); 91 Property *prop = opaque; 92 void **ptr = object_field_prop_ptr(obj, prop); 93 char *str; 94 BlockBackend *blk; 95 bool blk_created = false; 96 int ret; 97 BlockDriverState *bs; 98 AioContext *ctx; 99 100 if (!visit_type_str(v, name, &str, errp)) { 101 return; 102 } 103 104 if (!check_prop_still_unset(obj, name, *ptr, str, true, errp)) { 105 return; 106 } 107 108 if (*ptr) { 109 /* BlockBackend alread exists. So, we want to change attached node */ 110 blk = *ptr; 111 ctx = blk_get_aio_context(blk); 112 bs = bdrv_lookup_bs(NULL, str, errp); 113 if (!bs) { 114 return; 115 } 116 117 if (ctx != bdrv_get_aio_context(bs)) { 118 error_setg(errp, "Different aio context is not supported for new " 119 "node"); 120 } 121 122 aio_context_acquire(ctx); 123 blk_replace_bs(blk, bs, errp); 124 aio_context_release(ctx); 125 return; 126 } 127 128 if (!*str) { 129 g_free(str); 130 *ptr = NULL; 131 return; 132 } 133 134 blk = blk_by_name(str); 135 if (!blk) { 136 bs = bdrv_lookup_bs(NULL, str, NULL); 137 if (bs) { 138 /* 139 * If the device supports iothreads, it will make sure to move the 140 * block node to the right AioContext if necessary (or fail if this 141 * isn't possible because of other users). Devices that are not 142 * aware of iothreads require their BlockBackends to be in the main 143 * AioContext. 144 */ 145 ctx = iothread ? bdrv_get_aio_context(bs) : qemu_get_aio_context(); 146 blk = blk_new(ctx, 0, BLK_PERM_ALL); 147 blk_created = true; 148 149 ret = blk_insert_bs(blk, bs, errp); 150 if (ret < 0) { 151 goto fail; 152 } 153 } 154 } 155 if (!blk) { 156 error_setg(errp, "Property '%s.%s' can't find value '%s'", 157 object_get_typename(OBJECT(dev)), name, str); 158 goto fail; 159 } 160 if (blk_attach_dev(blk, dev) < 0) { 161 DriveInfo *dinfo = blk_legacy_dinfo(blk); 162 163 if (dinfo && dinfo->type != IF_NONE) { 164 error_setg(errp, "Drive '%s' is already in use because " 165 "it has been automatically connected to another " 166 "device (did you need 'if=none' in the drive options?)", 167 str); 168 } else { 169 error_setg(errp, "Drive '%s' is already in use by another device", 170 str); 171 } 172 goto fail; 173 } 174 175 *ptr = blk; 176 177 fail: 178 if (blk_created) { 179 /* If we need to keep a reference, blk_attach_dev() took it */ 180 blk_unref(blk); 181 } 182 183 g_free(str); 184 } 185 186 static void set_drive(Object *obj, Visitor *v, const char *name, void *opaque, 187 Error **errp) 188 { 189 set_drive_helper(obj, v, name, opaque, false, errp); 190 } 191 192 static void set_drive_iothread(Object *obj, Visitor *v, const char *name, 193 void *opaque, Error **errp) 194 { 195 set_drive_helper(obj, v, name, opaque, true, errp); 196 } 197 198 static void release_drive(Object *obj, const char *name, void *opaque) 199 { 200 DeviceState *dev = DEVICE(obj); 201 Property *prop = opaque; 202 BlockBackend **ptr = object_field_prop_ptr(obj, prop); 203 204 if (*ptr) { 205 AioContext *ctx = blk_get_aio_context(*ptr); 206 207 aio_context_acquire(ctx); 208 blockdev_auto_del(*ptr); 209 blk_detach_dev(*ptr, dev); 210 aio_context_release(ctx); 211 } 212 } 213 214 const PropertyInfo qdev_prop_drive = { 215 .name = "str", 216 .description = "Node name or ID of a block device to use as a backend", 217 .realized_set_allowed = true, 218 .get = get_drive, 219 .set = set_drive, 220 .release = release_drive, 221 }; 222 223 const PropertyInfo qdev_prop_drive_iothread = { 224 .name = "str", 225 .description = "Node name or ID of a block device to use as a backend", 226 .realized_set_allowed = true, 227 .get = get_drive, 228 .set = set_drive_iothread, 229 .release = release_drive, 230 }; 231 232 /* --- character device --- */ 233 234 static void get_chr(Object *obj, Visitor *v, const char *name, void *opaque, 235 Error **errp) 236 { 237 CharBackend *be = object_field_prop_ptr(obj, opaque); 238 char *p; 239 240 p = g_strdup(be->chr && be->chr->label ? be->chr->label : ""); 241 visit_type_str(v, name, &p, errp); 242 g_free(p); 243 } 244 245 static void set_chr(Object *obj, Visitor *v, const char *name, void *opaque, 246 Error **errp) 247 { 248 Property *prop = opaque; 249 CharBackend *be = object_field_prop_ptr(obj, prop); 250 Chardev *s; 251 char *str; 252 253 if (!visit_type_str(v, name, &str, errp)) { 254 return; 255 } 256 257 /* 258 * TODO Should this really be an error? If no, the old value 259 * needs to be released before we store the new one. 260 */ 261 if (!check_prop_still_unset(obj, name, be->chr, str, false, errp)) { 262 return; 263 } 264 265 if (!*str) { 266 g_free(str); 267 be->chr = NULL; 268 return; 269 } 270 271 s = qemu_chr_find(str); 272 if (s == NULL) { 273 error_setg(errp, "Property '%s.%s' can't find value '%s'", 274 object_get_typename(obj), name, str); 275 } else if (!qemu_chr_fe_init(be, s, errp)) { 276 error_prepend(errp, "Property '%s.%s' can't take value '%s': ", 277 object_get_typename(obj), name, str); 278 } 279 g_free(str); 280 } 281 282 static void release_chr(Object *obj, const char *name, void *opaque) 283 { 284 Property *prop = opaque; 285 CharBackend *be = object_field_prop_ptr(obj, prop); 286 287 qemu_chr_fe_deinit(be, false); 288 } 289 290 const PropertyInfo qdev_prop_chr = { 291 .name = "str", 292 .description = "ID of a chardev to use as a backend", 293 .get = get_chr, 294 .set = set_chr, 295 .release = release_chr, 296 }; 297 298 /* --- mac address --- */ 299 300 /* 301 * accepted syntax versions: 302 * 01:02:03:04:05:06 303 * 01-02-03-04-05-06 304 */ 305 static void get_mac(Object *obj, Visitor *v, const char *name, void *opaque, 306 Error **errp) 307 { 308 Property *prop = opaque; 309 MACAddr *mac = object_field_prop_ptr(obj, prop); 310 char buffer[2 * 6 + 5 + 1]; 311 char *p = buffer; 312 313 snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x", 314 mac->a[0], mac->a[1], mac->a[2], 315 mac->a[3], mac->a[4], mac->a[5]); 316 317 visit_type_str(v, name, &p, errp); 318 } 319 320 static void set_mac(Object *obj, Visitor *v, const char *name, void *opaque, 321 Error **errp) 322 { 323 Property *prop = opaque; 324 MACAddr *mac = object_field_prop_ptr(obj, prop); 325 int i, pos; 326 char *str; 327 const char *p; 328 329 if (!visit_type_str(v, name, &str, errp)) { 330 return; 331 } 332 333 for (i = 0, pos = 0; i < 6; i++, pos += 3) { 334 long val; 335 336 if (!qemu_isxdigit(str[pos])) { 337 goto inval; 338 } 339 if (!qemu_isxdigit(str[pos + 1])) { 340 goto inval; 341 } 342 if (i == 5) { 343 if (str[pos + 2] != '\0') { 344 goto inval; 345 } 346 } else { 347 if (str[pos + 2] != ':' && str[pos + 2] != '-') { 348 goto inval; 349 } 350 } 351 if (qemu_strtol(str + pos, &p, 16, &val) < 0 || val > 0xff) { 352 goto inval; 353 } 354 mac->a[i] = val; 355 } 356 g_free(str); 357 return; 358 359 inval: 360 error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str); 361 g_free(str); 362 } 363 364 const PropertyInfo qdev_prop_macaddr = { 365 .name = "str", 366 .description = "Ethernet 6-byte MAC Address, example: 52:54:00:12:34:56", 367 .get = get_mac, 368 .set = set_mac, 369 }; 370 371 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, 372 const uint8_t *value) 373 { 374 char str[2 * 6 + 5 + 1]; 375 snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x", 376 value[0], value[1], value[2], value[3], value[4], value[5]); 377 378 object_property_set_str(OBJECT(dev), name, str, &error_abort); 379 } 380 381 /* --- netdev device --- */ 382 static void get_netdev(Object *obj, Visitor *v, const char *name, 383 void *opaque, Error **errp) 384 { 385 Property *prop = opaque; 386 NICPeers *peers_ptr = object_field_prop_ptr(obj, prop); 387 char *p = g_strdup(peers_ptr->ncs[0] ? peers_ptr->ncs[0]->name : ""); 388 389 visit_type_str(v, name, &p, errp); 390 g_free(p); 391 } 392 393 static void set_netdev(Object *obj, Visitor *v, const char *name, 394 void *opaque, Error **errp) 395 { 396 Property *prop = opaque; 397 NICPeers *peers_ptr = object_field_prop_ptr(obj, prop); 398 NetClientState **ncs = peers_ptr->ncs; 399 NetClientState *peers[MAX_QUEUE_NUM]; 400 int queues, err = 0, i = 0; 401 char *str; 402 403 if (!visit_type_str(v, name, &str, errp)) { 404 return; 405 } 406 407 queues = qemu_find_net_clients_except(str, peers, 408 NET_CLIENT_DRIVER_NIC, 409 MAX_QUEUE_NUM); 410 if (queues == 0) { 411 err = -ENOENT; 412 goto out; 413 } 414 415 if (queues > MAX_QUEUE_NUM) { 416 error_setg(errp, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)", 417 str, queues, MAX_QUEUE_NUM); 418 goto out; 419 } 420 421 for (i = 0; i < queues; i++) { 422 if (peers[i]->peer) { 423 err = -EEXIST; 424 goto out; 425 } 426 427 /* 428 * TODO Should this really be an error? If no, the old value 429 * needs to be released before we store the new one. 430 */ 431 if (!check_prop_still_unset(obj, name, ncs[i], str, false, errp)) { 432 goto out; 433 } 434 435 if (peers[i]->info->check_peer_type) { 436 if (!peers[i]->info->check_peer_type(peers[i], obj->class, errp)) { 437 goto out; 438 } 439 } 440 441 ncs[i] = peers[i]; 442 ncs[i]->queue_index = i; 443 } 444 445 peers_ptr->queues = queues; 446 447 out: 448 error_set_from_qdev_prop_error(errp, err, obj, name, str); 449 g_free(str); 450 } 451 452 const PropertyInfo qdev_prop_netdev = { 453 .name = "str", 454 .description = "ID of a netdev to use as a backend", 455 .get = get_netdev, 456 .set = set_netdev, 457 }; 458 459 460 /* --- audiodev --- */ 461 static void get_audiodev(Object *obj, Visitor *v, const char* name, 462 void *opaque, Error **errp) 463 { 464 Property *prop = opaque; 465 QEMUSoundCard *card = object_field_prop_ptr(obj, prop); 466 char *p = g_strdup(audio_get_id(card)); 467 468 visit_type_str(v, name, &p, errp); 469 g_free(p); 470 } 471 472 static void set_audiodev(Object *obj, Visitor *v, const char* name, 473 void *opaque, Error **errp) 474 { 475 Property *prop = opaque; 476 QEMUSoundCard *card = object_field_prop_ptr(obj, prop); 477 AudioState *state; 478 int err = 0; 479 char *str; 480 481 if (!visit_type_str(v, name, &str, errp)) { 482 return; 483 } 484 485 state = audio_state_by_name(str); 486 487 if (!state) { 488 err = -ENOENT; 489 goto out; 490 } 491 card->state = state; 492 493 out: 494 error_set_from_qdev_prop_error(errp, err, obj, name, str); 495 g_free(str); 496 } 497 498 const PropertyInfo qdev_prop_audiodev = { 499 .name = "str", 500 .description = "ID of an audiodev to use as a backend", 501 /* release done on shutdown */ 502 .get = get_audiodev, 503 .set = set_audiodev, 504 }; 505 506 bool qdev_prop_set_drive_err(DeviceState *dev, const char *name, 507 BlockBackend *value, Error **errp) 508 { 509 const char *ref = ""; 510 511 if (value) { 512 ref = blk_name(value); 513 if (!*ref) { 514 const BlockDriverState *bs = blk_bs(value); 515 if (bs) { 516 ref = bdrv_get_node_name(bs); 517 } 518 } 519 } 520 521 return object_property_set_str(OBJECT(dev), name, ref, errp); 522 } 523 524 void qdev_prop_set_drive(DeviceState *dev, const char *name, 525 BlockBackend *value) 526 { 527 qdev_prop_set_drive_err(dev, name, value, &error_abort); 528 } 529 530 void qdev_prop_set_chr(DeviceState *dev, const char *name, 531 Chardev *value) 532 { 533 assert(!value || value->label); 534 object_property_set_str(OBJECT(dev), name, value ? value->label : "", 535 &error_abort); 536 } 537 538 void qdev_prop_set_netdev(DeviceState *dev, const char *name, 539 NetClientState *value) 540 { 541 assert(!value || value->name); 542 object_property_set_str(OBJECT(dev), name, value ? value->name : "", 543 &error_abort); 544 } 545 546 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd) 547 { 548 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a); 549 if (nd->netdev) { 550 qdev_prop_set_netdev(dev, "netdev", nd->netdev); 551 } 552 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED && 553 object_property_find(OBJECT(dev), "vectors")) { 554 qdev_prop_set_uint32(dev, "vectors", nd->nvectors); 555 } 556 nd->instantiated = 1; 557 } 558 559 /* --- lost tick policy --- */ 560 561 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int)); 562 563 const PropertyInfo qdev_prop_losttickpolicy = { 564 .name = "LostTickPolicy", 565 .enum_table = &LostTickPolicy_lookup, 566 .get = qdev_propinfo_get_enum, 567 .set = qdev_propinfo_set_enum, 568 .set_default_value = qdev_propinfo_set_default_value_enum, 569 }; 570 571 /* --- blocksize --- */ 572 573 static void set_blocksize(Object *obj, Visitor *v, const char *name, 574 void *opaque, Error **errp) 575 { 576 DeviceState *dev = DEVICE(obj); 577 Property *prop = opaque; 578 uint32_t *ptr = object_field_prop_ptr(obj, prop); 579 uint64_t value; 580 Error *local_err = NULL; 581 582 if (!visit_type_size(v, name, &value, errp)) { 583 return; 584 } 585 check_block_size(dev->id ? : "", name, value, &local_err); 586 if (local_err) { 587 error_propagate(errp, local_err); 588 return; 589 } 590 *ptr = value; 591 } 592 593 const PropertyInfo qdev_prop_blocksize = { 594 .name = "size", 595 .description = "A power of two between " MIN_BLOCK_SIZE_STR 596 " and " MAX_BLOCK_SIZE_STR, 597 .get = qdev_propinfo_get_size32, 598 .set = set_blocksize, 599 .set_default_value = qdev_propinfo_set_default_value_uint, 600 }; 601 602 /* --- Block device error handling policy --- */ 603 604 QEMU_BUILD_BUG_ON(sizeof(BlockdevOnError) != sizeof(int)); 605 606 const PropertyInfo qdev_prop_blockdev_on_error = { 607 .name = "BlockdevOnError", 608 .description = "Error handling policy, " 609 "report/ignore/enospc/stop/auto", 610 .enum_table = &BlockdevOnError_lookup, 611 .get = qdev_propinfo_get_enum, 612 .set = qdev_propinfo_set_enum, 613 .set_default_value = qdev_propinfo_set_default_value_enum, 614 }; 615 616 /* --- BIOS CHS translation */ 617 618 QEMU_BUILD_BUG_ON(sizeof(BiosAtaTranslation) != sizeof(int)); 619 620 const PropertyInfo qdev_prop_bios_chs_trans = { 621 .name = "BiosAtaTranslation", 622 .description = "Logical CHS translation algorithm, " 623 "auto/none/lba/large/rechs", 624 .enum_table = &BiosAtaTranslation_lookup, 625 .get = qdev_propinfo_get_enum, 626 .set = qdev_propinfo_set_enum, 627 .set_default_value = qdev_propinfo_set_default_value_enum, 628 }; 629 630 /* --- FDC default drive types */ 631 632 const PropertyInfo qdev_prop_fdc_drive_type = { 633 .name = "FdcDriveType", 634 .description = "FDC drive type, " 635 "144/288/120/none/auto", 636 .enum_table = &FloppyDriveType_lookup, 637 .get = qdev_propinfo_get_enum, 638 .set = qdev_propinfo_set_enum, 639 .set_default_value = qdev_propinfo_set_default_value_enum, 640 }; 641 642 /* --- MultiFDCompression --- */ 643 644 const PropertyInfo qdev_prop_multifd_compression = { 645 .name = "MultiFDCompression", 646 .description = "multifd_compression values, " 647 "none/zlib/zstd", 648 .enum_table = &MultiFDCompression_lookup, 649 .get = qdev_propinfo_get_enum, 650 .set = qdev_propinfo_set_enum, 651 .set_default_value = qdev_propinfo_set_default_value_enum, 652 }; 653 654 /* --- Reserved Region --- */ 655 656 /* 657 * Accepted syntax: 658 * <low address>:<high address>:<type> 659 * where low/high addresses are uint64_t in hexadecimal 660 * and type is a non-negative decimal integer 661 */ 662 static void get_reserved_region(Object *obj, Visitor *v, const char *name, 663 void *opaque, Error **errp) 664 { 665 Property *prop = opaque; 666 ReservedRegion *rr = object_field_prop_ptr(obj, prop); 667 char buffer[64]; 668 char *p = buffer; 669 int rc; 670 671 rc = snprintf(buffer, sizeof(buffer), "0x%"PRIx64":0x%"PRIx64":%u", 672 rr->low, rr->high, rr->type); 673 assert(rc < sizeof(buffer)); 674 675 visit_type_str(v, name, &p, errp); 676 } 677 678 static void set_reserved_region(Object *obj, Visitor *v, const char *name, 679 void *opaque, Error **errp) 680 { 681 Property *prop = opaque; 682 ReservedRegion *rr = object_field_prop_ptr(obj, prop); 683 const char *endptr; 684 char *str; 685 int ret; 686 687 if (!visit_type_str(v, name, &str, errp)) { 688 return; 689 } 690 691 ret = qemu_strtou64(str, &endptr, 16, &rr->low); 692 if (ret) { 693 error_setg(errp, "start address of '%s'" 694 " must be a hexadecimal integer", name); 695 goto out; 696 } 697 if (*endptr != ':') { 698 goto separator_error; 699 } 700 701 ret = qemu_strtou64(endptr + 1, &endptr, 16, &rr->high); 702 if (ret) { 703 error_setg(errp, "end address of '%s'" 704 " must be a hexadecimal integer", name); 705 goto out; 706 } 707 if (*endptr != ':') { 708 goto separator_error; 709 } 710 711 ret = qemu_strtoui(endptr + 1, &endptr, 10, &rr->type); 712 if (ret) { 713 error_setg(errp, "type of '%s'" 714 " must be a non-negative decimal integer", name); 715 } 716 goto out; 717 718 separator_error: 719 error_setg(errp, "reserved region fields must be separated with ':'"); 720 out: 721 g_free(str); 722 return; 723 } 724 725 const PropertyInfo qdev_prop_reserved_region = { 726 .name = "reserved_region", 727 .description = "Reserved Region, example: 0xFEE00000:0xFEEFFFFF:0", 728 .get = get_reserved_region, 729 .set = set_reserved_region, 730 }; 731 732 /* --- pci address --- */ 733 734 /* 735 * bus-local address, i.e. "$slot" or "$slot.$fn" 736 */ 737 static void set_pci_devfn(Object *obj, Visitor *v, const char *name, 738 void *opaque, Error **errp) 739 { 740 Property *prop = opaque; 741 int32_t value, *ptr = object_field_prop_ptr(obj, prop); 742 unsigned int slot, fn, n; 743 char *str; 744 745 if (!visit_type_str(v, name, &str, NULL)) { 746 if (!visit_type_int32(v, name, &value, errp)) { 747 return; 748 } 749 if (value < -1 || value > 255) { 750 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 751 name ? name : "null", "a value between -1 and 255"); 752 return; 753 } 754 *ptr = value; 755 return; 756 } 757 758 if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) { 759 fn = 0; 760 if (sscanf(str, "%x%n", &slot, &n) != 1) { 761 goto invalid; 762 } 763 } 764 if (str[n] != '\0' || fn > 7 || slot > 31) { 765 goto invalid; 766 } 767 *ptr = slot << 3 | fn; 768 g_free(str); 769 return; 770 771 invalid: 772 error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str); 773 g_free(str); 774 } 775 776 static int print_pci_devfn(Object *obj, Property *prop, char *dest, 777 size_t len) 778 { 779 int32_t *ptr = object_field_prop_ptr(obj, prop); 780 781 if (*ptr == -1) { 782 return snprintf(dest, len, "<unset>"); 783 } else { 784 return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7); 785 } 786 } 787 788 const PropertyInfo qdev_prop_pci_devfn = { 789 .name = "int32", 790 .description = "Slot and optional function number, example: 06.0 or 06", 791 .print = print_pci_devfn, 792 .get = qdev_propinfo_get_int32, 793 .set = set_pci_devfn, 794 .set_default_value = qdev_propinfo_set_default_value_int, 795 }; 796 797 /* --- pci host address --- */ 798 799 static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name, 800 void *opaque, Error **errp) 801 { 802 Property *prop = opaque; 803 PCIHostDeviceAddress *addr = object_field_prop_ptr(obj, prop); 804 char buffer[] = "ffff:ff:ff.f"; 805 char *p = buffer; 806 int rc = 0; 807 808 /* 809 * Catch "invalid" device reference from vfio-pci and allow the 810 * default buffer representing the non-existent device to be used. 811 */ 812 if (~addr->domain || ~addr->bus || ~addr->slot || ~addr->function) { 813 rc = snprintf(buffer, sizeof(buffer), "%04x:%02x:%02x.%0d", 814 addr->domain, addr->bus, addr->slot, addr->function); 815 assert(rc == sizeof(buffer) - 1); 816 } 817 818 visit_type_str(v, name, &p, errp); 819 } 820 821 /* 822 * Parse [<domain>:]<bus>:<slot>.<func> 823 * if <domain> is not supplied, it's assumed to be 0. 824 */ 825 static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name, 826 void *opaque, Error **errp) 827 { 828 Property *prop = opaque; 829 PCIHostDeviceAddress *addr = object_field_prop_ptr(obj, prop); 830 char *str, *p; 831 char *e; 832 unsigned long val; 833 unsigned long dom = 0, bus = 0; 834 unsigned int slot = 0, func = 0; 835 836 if (!visit_type_str(v, name, &str, errp)) { 837 return; 838 } 839 840 p = str; 841 val = strtoul(p, &e, 16); 842 if (e == p || *e != ':') { 843 goto inval; 844 } 845 bus = val; 846 847 p = e + 1; 848 val = strtoul(p, &e, 16); 849 if (e == p) { 850 goto inval; 851 } 852 if (*e == ':') { 853 dom = bus; 854 bus = val; 855 p = e + 1; 856 val = strtoul(p, &e, 16); 857 if (e == p) { 858 goto inval; 859 } 860 } 861 slot = val; 862 863 if (*e != '.') { 864 goto inval; 865 } 866 p = e + 1; 867 val = strtoul(p, &e, 10); 868 if (e == p) { 869 goto inval; 870 } 871 func = val; 872 873 if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) { 874 goto inval; 875 } 876 877 if (*e) { 878 goto inval; 879 } 880 881 addr->domain = dom; 882 addr->bus = bus; 883 addr->slot = slot; 884 addr->function = func; 885 886 g_free(str); 887 return; 888 889 inval: 890 error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str); 891 g_free(str); 892 } 893 894 const PropertyInfo qdev_prop_pci_host_devaddr = { 895 .name = "str", 896 .description = "Address (bus/device/function) of " 897 "the host device, example: 04:10.0", 898 .get = get_pci_host_devaddr, 899 .set = set_pci_host_devaddr, 900 }; 901 902 /* --- OffAutoPCIBAR off/auto/bar0/bar1/bar2/bar3/bar4/bar5 --- */ 903 904 const PropertyInfo qdev_prop_off_auto_pcibar = { 905 .name = "OffAutoPCIBAR", 906 .description = "off/auto/bar0/bar1/bar2/bar3/bar4/bar5", 907 .enum_table = &OffAutoPCIBAR_lookup, 908 .get = qdev_propinfo_get_enum, 909 .set = qdev_propinfo_set_enum, 910 .set_default_value = qdev_propinfo_set_default_value_enum, 911 }; 912 913 /* --- PCIELinkSpeed 2_5/5/8/16 -- */ 914 915 static void get_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name, 916 void *opaque, Error **errp) 917 { 918 Property *prop = opaque; 919 PCIExpLinkSpeed *p = object_field_prop_ptr(obj, prop); 920 int speed; 921 922 switch (*p) { 923 case QEMU_PCI_EXP_LNK_2_5GT: 924 speed = PCIE_LINK_SPEED_2_5; 925 break; 926 case QEMU_PCI_EXP_LNK_5GT: 927 speed = PCIE_LINK_SPEED_5; 928 break; 929 case QEMU_PCI_EXP_LNK_8GT: 930 speed = PCIE_LINK_SPEED_8; 931 break; 932 case QEMU_PCI_EXP_LNK_16GT: 933 speed = PCIE_LINK_SPEED_16; 934 break; 935 default: 936 /* Unreachable */ 937 abort(); 938 } 939 940 visit_type_enum(v, name, &speed, prop->info->enum_table, errp); 941 } 942 943 static void set_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name, 944 void *opaque, Error **errp) 945 { 946 Property *prop = opaque; 947 PCIExpLinkSpeed *p = object_field_prop_ptr(obj, prop); 948 int speed; 949 950 if (!visit_type_enum(v, name, &speed, prop->info->enum_table, 951 errp)) { 952 return; 953 } 954 955 switch (speed) { 956 case PCIE_LINK_SPEED_2_5: 957 *p = QEMU_PCI_EXP_LNK_2_5GT; 958 break; 959 case PCIE_LINK_SPEED_5: 960 *p = QEMU_PCI_EXP_LNK_5GT; 961 break; 962 case PCIE_LINK_SPEED_8: 963 *p = QEMU_PCI_EXP_LNK_8GT; 964 break; 965 case PCIE_LINK_SPEED_16: 966 *p = QEMU_PCI_EXP_LNK_16GT; 967 break; 968 default: 969 /* Unreachable */ 970 abort(); 971 } 972 } 973 974 const PropertyInfo qdev_prop_pcie_link_speed = { 975 .name = "PCIELinkSpeed", 976 .description = "2_5/5/8/16", 977 .enum_table = &PCIELinkSpeed_lookup, 978 .get = get_prop_pcielinkspeed, 979 .set = set_prop_pcielinkspeed, 980 .set_default_value = qdev_propinfo_set_default_value_enum, 981 }; 982 983 /* --- PCIELinkWidth 1/2/4/8/12/16/32 -- */ 984 985 static void get_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name, 986 void *opaque, Error **errp) 987 { 988 Property *prop = opaque; 989 PCIExpLinkWidth *p = object_field_prop_ptr(obj, prop); 990 int width; 991 992 switch (*p) { 993 case QEMU_PCI_EXP_LNK_X1: 994 width = PCIE_LINK_WIDTH_1; 995 break; 996 case QEMU_PCI_EXP_LNK_X2: 997 width = PCIE_LINK_WIDTH_2; 998 break; 999 case QEMU_PCI_EXP_LNK_X4: 1000 width = PCIE_LINK_WIDTH_4; 1001 break; 1002 case QEMU_PCI_EXP_LNK_X8: 1003 width = PCIE_LINK_WIDTH_8; 1004 break; 1005 case QEMU_PCI_EXP_LNK_X12: 1006 width = PCIE_LINK_WIDTH_12; 1007 break; 1008 case QEMU_PCI_EXP_LNK_X16: 1009 width = PCIE_LINK_WIDTH_16; 1010 break; 1011 case QEMU_PCI_EXP_LNK_X32: 1012 width = PCIE_LINK_WIDTH_32; 1013 break; 1014 default: 1015 /* Unreachable */ 1016 abort(); 1017 } 1018 1019 visit_type_enum(v, name, &width, prop->info->enum_table, errp); 1020 } 1021 1022 static void set_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name, 1023 void *opaque, Error **errp) 1024 { 1025 Property *prop = opaque; 1026 PCIExpLinkWidth *p = object_field_prop_ptr(obj, prop); 1027 int width; 1028 1029 if (!visit_type_enum(v, name, &width, prop->info->enum_table, 1030 errp)) { 1031 return; 1032 } 1033 1034 switch (width) { 1035 case PCIE_LINK_WIDTH_1: 1036 *p = QEMU_PCI_EXP_LNK_X1; 1037 break; 1038 case PCIE_LINK_WIDTH_2: 1039 *p = QEMU_PCI_EXP_LNK_X2; 1040 break; 1041 case PCIE_LINK_WIDTH_4: 1042 *p = QEMU_PCI_EXP_LNK_X4; 1043 break; 1044 case PCIE_LINK_WIDTH_8: 1045 *p = QEMU_PCI_EXP_LNK_X8; 1046 break; 1047 case PCIE_LINK_WIDTH_12: 1048 *p = QEMU_PCI_EXP_LNK_X12; 1049 break; 1050 case PCIE_LINK_WIDTH_16: 1051 *p = QEMU_PCI_EXP_LNK_X16; 1052 break; 1053 case PCIE_LINK_WIDTH_32: 1054 *p = QEMU_PCI_EXP_LNK_X32; 1055 break; 1056 default: 1057 /* Unreachable */ 1058 abort(); 1059 } 1060 } 1061 1062 const PropertyInfo qdev_prop_pcie_link_width = { 1063 .name = "PCIELinkWidth", 1064 .description = "1/2/4/8/12/16/32", 1065 .enum_table = &PCIELinkWidth_lookup, 1066 .get = get_prop_pcielinkwidth, 1067 .set = set_prop_pcielinkwidth, 1068 .set_default_value = qdev_propinfo_set_default_value_enum, 1069 }; 1070 1071 /* --- UUID --- */ 1072 1073 static void get_uuid(Object *obj, Visitor *v, const char *name, void *opaque, 1074 Error **errp) 1075 { 1076 Property *prop = opaque; 1077 QemuUUID *uuid = object_field_prop_ptr(obj, prop); 1078 char buffer[UUID_FMT_LEN + 1]; 1079 char *p = buffer; 1080 1081 qemu_uuid_unparse(uuid, buffer); 1082 1083 visit_type_str(v, name, &p, errp); 1084 } 1085 1086 #define UUID_VALUE_AUTO "auto" 1087 1088 static void set_uuid(Object *obj, Visitor *v, const char *name, void *opaque, 1089 Error **errp) 1090 { 1091 Property *prop = opaque; 1092 QemuUUID *uuid = object_field_prop_ptr(obj, prop); 1093 char *str; 1094 1095 if (!visit_type_str(v, name, &str, errp)) { 1096 return; 1097 } 1098 1099 if (!strcmp(str, UUID_VALUE_AUTO)) { 1100 qemu_uuid_generate(uuid); 1101 } else if (qemu_uuid_parse(str, uuid) < 0) { 1102 error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str); 1103 } 1104 g_free(str); 1105 } 1106 1107 static void set_default_uuid_auto(ObjectProperty *op, const Property *prop) 1108 { 1109 object_property_set_default_str(op, UUID_VALUE_AUTO); 1110 } 1111 1112 const PropertyInfo qdev_prop_uuid = { 1113 .name = "str", 1114 .description = "UUID (aka GUID) or \"" UUID_VALUE_AUTO 1115 "\" for random value (default)", 1116 .get = get_uuid, 1117 .set = set_uuid, 1118 .set_default_value = set_default_uuid_auto, 1119 }; 1120