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