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