]> git.proxmox.com Git - mirror_qemu.git/blame - hw/core/qdev-properties-system.c
iotests: Test active commit with iothread and background I/O
[mirror_qemu.git] / hw / core / qdev-properties-system.c
CommitLineData
a404b612 1/*
8d76bfe8 2 * qdev property parsing
a404b612
EH
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
18c86e2b 13#include "qemu/osdep.h"
a27bd6c7 14#include "hw/qdev-properties.h"
ce35e229 15#include "hw/qdev-properties-system.h"
da34e65c 16#include "qapi/error.h"
aa1859cc
PMD
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"
501a7ce7 21#include "qapi/qmp/qerror.h"
aa1859cc
PMD
22#include "qemu/ctype.h"
23#include "qemu/cutils.h"
24#include "qemu/units.h"
38255eff 25#include "qemu/uuid.h"
aa1859cc
PMD
26#include "qemu/error-report.h"
27#include "qdev-prop-internal.h"
28
29#include "audio/audio.h"
30#include "chardev/char-fe.h"
4be74634 31#include "sysemu/block-backend.h"
501a7ce7 32#include "sysemu/blockdev.h"
aa1859cc
PMD
33#include "net/net.h"
34#include "hw/pci/pci.h"
edf5ca5d 35#include "hw/pci/pcie.h"
aae16721 36#include "hw/i386/x86.h"
5937835a 37#include "util/block-helpers.h"
a404b612 38
38148159 39static bool check_prop_still_unset(Object *obj, const char *name,
1bc13336 40 const void *old_val, const char *new_val,
d1a58c17 41 bool allow_override, Error **errp)
1bc13336 42{
38148159 43 const GlobalProperty *prop = qdev_find_global_prop(obj, name);
1bc13336 44
d1a58c17 45 if (!old_val || (!prop && allow_override)) {
1bc13336
MA
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
466c2983
MA
61/* --- drive --- */
62
63static void get_drive(Object *obj, Visitor *v, const char *name, void *opaque,
64 Error **errp)
a404b612 65{
466c2983 66 Property *prop = opaque;
1e198715 67 void **ptr = object_field_prop_ptr(obj, prop);
466c2983 68 const char *value;
a404b612
EH
69 char *p;
70
466c2983
MA
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);
51e72bc1 84 visit_type_str(v, name, &p, errp);
7d1de464 85 g_free(p);
a404b612
EH
86}
87
466c2983
MA
88static void set_drive_helper(Object *obj, Visitor *v, const char *name,
89 void *opaque, bool iothread, Error **errp)
a404b612
EH
90{
91 DeviceState *dev = DEVICE(obj);
466c2983 92 Property *prop = opaque;
1e198715 93 void **ptr = object_field_prop_ptr(obj, prop);
a404b612 94 char *str;
466c2983
MA
95 BlockBackend *blk;
96 bool blk_created = false;
97 int ret;
d1a58c17
VSO
98 BlockDriverState *bs;
99 AioContext *ctx;
a404b612 100
668f62ec 101 if (!visit_type_str(v, name, &str, errp)) {
a404b612
EH
102 return;
103 }
466c2983 104
d1a58c17
VSO
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);
84b0475c
MA
126 return;
127 }
128
a404b612
EH
129 if (!*str) {
130 g_free(str);
131 *ptr = NULL;
132 return;
133 }
a404b612 134
4be74634 135 blk = blk_by_name(str);
8daea510 136 if (!blk) {
d1a58c17 137 bs = bdrv_lookup_bs(NULL, str, NULL);
8daea510 138 if (bs) {
307a5f60
KW
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 */
d1a58c17 146 ctx = iothread ? bdrv_get_aio_context(bs) : qemu_get_aio_context();
307a5f60 147 blk = blk_new(ctx, 0, BLK_PERM_ALL);
8daea510 148 blk_created = true;
d7086422
KW
149
150 ret = blk_insert_bs(blk, bs, errp);
151 if (ret < 0) {
152 goto fail;
153 }
8daea510
KW
154 }
155 }
4be74634 156 if (!blk) {
f1fb9f0d 157 error_setg(errp, "Property '%s.%s' can't find value '%s'",
991f0ac9 158 object_get_typename(OBJECT(dev)), name, str);
8daea510 159 goto fail;
a404b612 160 }
4be74634 161 if (blk_attach_dev(blk, dev) < 0) {
62f7dbde
PM
162 DriveInfo *dinfo = blk_legacy_dinfo(blk);
163
a9d52a75 164 if (dinfo && dinfo->type != IF_NONE) {
62f7dbde
PM
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 }
8daea510 173 goto fail;
a404b612 174 }
8daea510 175
4be74634 176 *ptr = blk;
8daea510
KW
177
178fail:
179 if (blk_created) {
180 /* If we need to keep a reference, blk_attach_dev() took it */
181 blk_unref(blk);
182 }
466c2983
MA
183
184 g_free(str);
a404b612
EH
185}
186
466c2983
MA
187static void set_drive(Object *obj, Visitor *v, const char *name, void *opaque,
188 Error **errp)
307a5f60 189{
466c2983 190 set_drive_helper(obj, v, name, opaque, false, errp);
307a5f60
KW
191}
192
466c2983
MA
193static void set_drive_iothread(Object *obj, Visitor *v, const char *name,
194 void *opaque, Error **errp)
307a5f60 195{
466c2983 196 set_drive_helper(obj, v, name, opaque, true, errp);
307a5f60
KW
197}
198
a404b612
EH
199static void release_drive(Object *obj, const char *name, void *opaque)
200{
201 DeviceState *dev = DEVICE(obj);
202 Property *prop = opaque;
1e198715 203 BlockBackend **ptr = object_field_prop_ptr(obj, prop);
a404b612
EH
204
205 if (*ptr) {
9588c589
DL
206 AioContext *ctx = blk_get_aio_context(*ptr);
207
208 aio_context_acquire(ctx);
a404b612 209 blockdev_auto_del(*ptr);
8daea510 210 blk_detach_dev(*ptr, dev);
9588c589 211 aio_context_release(ctx);
a404b612
EH
212 }
213}
214
1b6b7d10 215const PropertyInfo qdev_prop_drive = {
85ca1202 216 .name = "str",
8daea510 217 .description = "Node name or ID of a block device to use as a backend",
d1a58c17 218 .realized_set_allowed = true,
a404b612
EH
219 .get = get_drive,
220 .set = set_drive,
221 .release = release_drive,
222};
223
307a5f60
KW
224const PropertyInfo qdev_prop_drive_iothread = {
225 .name = "str",
226 .description = "Node name or ID of a block device to use as a backend",
d1a58c17 227 .realized_set_allowed = true,
307a5f60
KW
228 .get = get_drive,
229 .set = set_drive_iothread,
230 .release = release_drive,
231};
232
a404b612
EH
233/* --- character device --- */
234
becdfa00
MAL
235static void get_chr(Object *obj, Visitor *v, const char *name, void *opaque,
236 Error **errp)
a404b612 237{
1e198715 238 CharBackend *be = object_field_prop_ptr(obj, opaque);
becdfa00
MAL
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);
a404b612
EH
244}
245
becdfa00
MAL
246static void set_chr(Object *obj, Visitor *v, const char *name, void *opaque,
247 Error **errp)
a404b612 248{
a404b612 249 Property *prop = opaque;
1e198715 250 CharBackend *be = object_field_prop_ptr(obj, prop);
0ec7b3e7 251 Chardev *s;
becdfa00 252 char *str;
a404b612 253
668f62ec 254 if (!visit_type_str(v, name, &str, errp)) {
becdfa00
MAL
255 return;
256 }
a404b612 257
9572a787
MA
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 */
d1a58c17 262 if (!check_prop_still_unset(obj, name, be->chr, str, false, errp)) {
9572a787
MA
263 return;
264 }
265
becdfa00
MAL
266 if (!*str) {
267 g_free(str);
268 be->chr = NULL;
269 return;
270 }
a404b612 271
becdfa00 272 s = qemu_chr_find(str);
becdfa00
MAL
273 if (s == NULL) {
274 error_setg(errp, "Property '%s.%s' can't find value '%s'",
991f0ac9 275 object_get_typename(obj), name, str);
2209401f 276 } else if (!qemu_chr_fe_init(be, s, errp)) {
c39860e6 277 error_prepend(errp, "Property '%s.%s' can't take value '%s': ",
991f0ac9 278 object_get_typename(obj), name, str);
becdfa00 279 }
2209401f 280 g_free(str);
a404b612
EH
281}
282
becdfa00 283static void release_chr(Object *obj, const char *name, void *opaque)
a404b612 284{
becdfa00 285 Property *prop = opaque;
1e198715 286 CharBackend *be = object_field_prop_ptr(obj, prop);
becdfa00 287
1ce2610c 288 qemu_chr_fe_deinit(be, false);
a404b612
EH
289}
290
1b6b7d10 291const PropertyInfo qdev_prop_chr = {
85ca1202 292 .name = "str",
51b2e8c3 293 .description = "ID of a chardev to use as a backend",
a404b612
EH
294 .get = get_chr,
295 .set = set_chr,
296 .release = release_chr,
297};
298
aa1859cc
PMD
299/* --- mac address --- */
300
301/*
302 * accepted syntax versions:
303 * 01:02:03:04:05:06
304 * 01-02-03-04-05-06
305 */
306static void get_mac(Object *obj, Visitor *v, const char *name, void *opaque,
307 Error **errp)
308{
aa1859cc 309 Property *prop = opaque;
1e198715 310 MACAddr *mac = object_field_prop_ptr(obj, prop);
aa1859cc
PMD
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
321static void set_mac(Object *obj, Visitor *v, const char *name, void *opaque,
322 Error **errp)
323{
aa1859cc 324 Property *prop = opaque;
1e198715 325 MACAddr *mac = object_field_prop_ptr(obj, prop);
aa1859cc
PMD
326 int i, pos;
327 char *str;
328 const char *p;
329
aa1859cc
PMD
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
360inval:
e68c2cb7 361 error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
aa1859cc
PMD
362 g_free(str);
363}
364
365const 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
372void 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
a404b612 382/* --- netdev device --- */
d7bce999
EB
383static void get_netdev(Object *obj, Visitor *v, const char *name,
384 void *opaque, Error **errp)
23120b13 385{
23120b13 386 Property *prop = opaque;
1e198715 387 NICPeers *peers_ptr = object_field_prop_ptr(obj, prop);
23120b13
JW
388 char *p = g_strdup(peers_ptr->ncs[0] ? peers_ptr->ncs[0]->name : "");
389
51e72bc1 390 visit_type_str(v, name, &p, errp);
23120b13
JW
391 g_free(p);
392}
a404b612 393
d7bce999
EB
394static void set_netdev(Object *obj, Visitor *v, const char *name,
395 void *opaque, Error **errp)
a404b612 396{
23120b13 397 Property *prop = opaque;
1e198715 398 NICPeers *peers_ptr = object_field_prop_ptr(obj, prop);
1ceef9f2
JW
399 NetClientState **ncs = peers_ptr->ncs;
400 NetClientState *peers[MAX_QUEUE_NUM];
23120b13
JW
401 int queues, err = 0, i = 0;
402 char *str;
403
668f62ec 404 if (!visit_type_str(v, name, &str, errp)) {
23120b13
JW
405 return;
406 }
a404b612 407
1ceef9f2 408 queues = qemu_find_net_clients_except(str, peers,
f394b2e2 409 NET_CLIENT_DRIVER_NIC,
1ceef9f2
JW
410 MAX_QUEUE_NUM);
411 if (queues == 0) {
23120b13
JW
412 err = -ENOENT;
413 goto out;
a404b612 414 }
1ceef9f2
JW
415
416 if (queues > MAX_QUEUE_NUM) {
23120b13
JW
417 error_setg(errp, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)",
418 str, queues, MAX_QUEUE_NUM);
419 goto out;
1ceef9f2
JW
420 }
421
422 for (i = 0; i < queues; i++) {
1ceef9f2 423 if (peers[i]->peer) {
23120b13
JW
424 err = -EEXIST;
425 goto out;
1ceef9f2
JW
426 }
427
1bc13336
MA
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 */
d1a58c17 432 if (!check_prop_still_unset(obj, name, ncs[i], str, false, errp)) {
23120b13 433 goto out;
30c367ed
VY
434 }
435
e287bf7b
KW
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
1ceef9f2
JW
442 ncs[i] = peers[i];
443 ncs[i]->queue_index = i;
a404b612 444 }
1ceef9f2 445
575a1c0e 446 peers_ptr->queues = queues;
1ceef9f2 447
23120b13 448out:
e68c2cb7 449 error_set_from_qdev_prop_error(errp, err, obj, name, str);
23120b13 450 g_free(str);
a404b612
EH
451}
452
1b6b7d10 453const PropertyInfo qdev_prop_netdev = {
85ca1202 454 .name = "str",
51b2e8c3 455 .description = "ID of a netdev to use as a backend",
a404b612
EH
456 .get = get_netdev,
457 .set = set_netdev,
458};
459
a404b612 460
88e47b9a
KZ
461/* --- audiodev --- */
462static void get_audiodev(Object *obj, Visitor *v, const char* name,
463 void *opaque, Error **errp)
464{
88e47b9a 465 Property *prop = opaque;
1e198715 466 QEMUSoundCard *card = object_field_prop_ptr(obj, prop);
88e47b9a
KZ
467 char *p = g_strdup(audio_get_id(card));
468
469 visit_type_str(v, name, &p, errp);
470 g_free(p);
471}
472
473static void set_audiodev(Object *obj, Visitor *v, const char* name,
474 void *opaque, Error **errp)
475{
88e47b9a 476 Property *prop = opaque;
1e198715 477 QEMUSoundCard *card = object_field_prop_ptr(obj, prop);
88e47b9a 478 AudioState *state;
88e47b9a
KZ
479 int err = 0;
480 char *str;
481
668f62ec 482 if (!visit_type_str(v, name, &str, errp)) {
88e47b9a
KZ
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
494out:
e68c2cb7 495 error_set_from_qdev_prop_error(errp, err, obj, name, str);
88e47b9a
KZ
496 g_free(str);
497}
498
499const 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
73ac1aac 507bool qdev_prop_set_drive_err(DeviceState *dev, const char *name,
934df912 508 BlockBackend *value, Error **errp)
a404b612 509{
8daea510
KW
510 const char *ref = "";
511
512 if (value) {
513 ref = blk_name(value);
514 if (!*ref) {
be9721f4 515 const BlockDriverState *bs = blk_bs(value);
8daea510
KW
516 if (bs) {
517 ref = bdrv_get_node_name(bs);
518 }
519 }
520 }
521
73ac1aac 522 return object_property_set_str(OBJECT(dev), name, ref, errp);
a404b612
EH
523}
524
934df912
MA
525void 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
a404b612 531void qdev_prop_set_chr(DeviceState *dev, const char *name,
0ec7b3e7 532 Chardev *value)
a404b612 533{
a404b612 534 assert(!value || value->label);
5325cc34
MA
535 object_property_set_str(OBJECT(dev), name, value ? value->label : "",
536 &error_abort);
a404b612
EH
537}
538
539void qdev_prop_set_netdev(DeviceState *dev, const char *name,
540 NetClientState *value)
541{
a404b612 542 assert(!value || value->name);
5325cc34
MA
543 object_property_set_str(OBJECT(dev), name, value ? value->name : "",
544 &error_abort);
a404b612
EH
545}
546
547void 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 &&
efba1595 554 object_property_find(OBJECT(dev), "vectors")) {
a404b612
EH
555 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
556 }
557 nd->instantiated = 1;
558}
aa1859cc
PMD
559
560/* --- lost tick policy --- */
561
aae16721
TH
562static 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
aa1859cc
PMD
587QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
588
589const PropertyInfo qdev_prop_losttickpolicy = {
590 .name = "LostTickPolicy",
591 .enum_table = &LostTickPolicy_lookup,
592 .get = qdev_propinfo_get_enum,
aae16721 593 .set = qdev_propinfo_set_losttickpolicy,
aa1859cc
PMD
594 .set_default_value = qdev_propinfo_set_default_value_enum,
595};
596
597/* --- blocksize --- */
598
aa1859cc
PMD
599static 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;
1e198715 604 uint32_t *ptr = object_field_prop_ptr(obj, prop);
aa1859cc 605 uint64_t value;
5937835a 606 Error *local_err = NULL;
aa1859cc 607
aa1859cc
PMD
608 if (!visit_type_size(v, name, &value, errp)) {
609 return;
610 }
5937835a
CX
611 check_block_size(dev->id ? : "", name, value, &local_err);
612 if (local_err) {
613 error_propagate(errp, local_err);
aa1859cc
PMD
614 return;
615 }
aa1859cc
PMD
616 *ptr = value;
617}
618
619const 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
630QEMU_BUILD_BUG_ON(sizeof(BlockdevOnError) != sizeof(int));
631
632const 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
644QEMU_BUILD_BUG_ON(sizeof(BiosAtaTranslation) != sizeof(int));
645
646const 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
658const 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
670const 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 */
688static void get_reserved_region(Object *obj, Visitor *v, const char *name,
689 void *opaque, Error **errp)
690{
aa1859cc 691 Property *prop = opaque;
1e198715 692 ReservedRegion *rr = object_field_prop_ptr(obj, prop);
aa1859cc
PMD
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
704static void set_reserved_region(Object *obj, Visitor *v, const char *name,
705 void *opaque, Error **errp)
706{
aa1859cc 707 Property *prop = opaque;
1e198715 708 ReservedRegion *rr = object_field_prop_ptr(obj, prop);
aa1859cc
PMD
709 const char *endptr;
710 char *str;
711 int ret;
712
d1c81c34 713 if (!visit_type_str(v, name, &str, errp)) {
aa1859cc
PMD
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
744separator_error:
745 error_setg(errp, "reserved region fields must be separated with ':'");
746out:
747 g_free(str);
748 return;
749}
750
751const 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 */
763static void set_pci_devfn(Object *obj, Visitor *v, const char *name,
764 void *opaque, Error **errp)
765{
aa1859cc 766 Property *prop = opaque;
1e198715 767 int32_t value, *ptr = object_field_prop_ptr(obj, prop);
aa1859cc
PMD
768 unsigned int slot, fn, n;
769 char *str;
770
aa1859cc
PMD
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,
6cc0667d 777 name ? name : "null", "a value between -1 and 255");
aa1859cc
PMD
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
797invalid:
e68c2cb7 798 error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
aa1859cc
PMD
799 g_free(str);
800}
801
40ea00b0 802static int print_pci_devfn(Object *obj, Property *prop, char *dest,
aa1859cc
PMD
803 size_t len)
804{
1e198715 805 int32_t *ptr = object_field_prop_ptr(obj, prop);
aa1859cc
PMD
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
814const 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
825static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
826 void *opaque, Error **errp)
827{
aa1859cc 828 Property *prop = opaque;
1e198715 829 PCIHostDeviceAddress *addr = object_field_prop_ptr(obj, prop);
aa1859cc
PMD
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 */
851static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
852 void *opaque, Error **errp)
853{
aa1859cc 854 Property *prop = opaque;
1e198715 855 PCIHostDeviceAddress *addr = object_field_prop_ptr(obj, prop);
aa1859cc 856 char *str, *p;
28afbc1f 857 char *e;
aa1859cc
PMD
858 unsigned long val;
859 unsigned long dom = 0, bus = 0;
860 unsigned int slot = 0, func = 0;
861
aa1859cc
PMD
862 if (!visit_type_str(v, name, &str, errp)) {
863 return;
864 }
865
866 p = str;
28afbc1f
MT
867 val = strtoul(p, &e, 16);
868 if (e == p || *e != ':') {
aa1859cc
PMD
869 goto inval;
870 }
871 bus = val;
872
28afbc1f
MT
873 p = e + 1;
874 val = strtoul(p, &e, 16);
875 if (e == p) {
aa1859cc
PMD
876 goto inval;
877 }
878 if (*e == ':') {
879 dom = bus;
880 bus = val;
28afbc1f
MT
881 p = e + 1;
882 val = strtoul(p, &e, 16);
883 if (e == p) {
aa1859cc
PMD
884 goto inval;
885 }
886 }
887 slot = val;
888
889 if (*e != '.') {
890 goto inval;
891 }
28afbc1f
MT
892 p = e + 1;
893 val = strtoul(p, &e, 10);
894 if (e == p) {
aa1859cc
PMD
895 goto inval;
896 }
897 func = val;
898
28afbc1f 899 if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) {
aa1859cc
PMD
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
915inval:
e68c2cb7 916 error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
aa1859cc
PMD
917 g_free(str);
918}
919
920const 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
930const 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
941static void get_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
942 void *opaque, Error **errp)
943{
aa1859cc 944 Property *prop = opaque;
1e198715 945 PCIExpLinkSpeed *p = object_field_prop_ptr(obj, prop);
aa1859cc
PMD
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
991f0ac9 966 visit_type_enum(v, name, &speed, prop->info->enum_table, errp);
aa1859cc
PMD
967}
968
969static void set_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
970 void *opaque, Error **errp)
971{
aa1859cc 972 Property *prop = opaque;
1e198715 973 PCIExpLinkSpeed *p = object_field_prop_ptr(obj, prop);
aa1859cc
PMD
974 int speed;
975
991f0ac9 976 if (!visit_type_enum(v, name, &speed, prop->info->enum_table,
aa1859cc
PMD
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
1000const 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
1011static void get_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1012 void *opaque, Error **errp)
1013{
aa1859cc 1014 Property *prop = opaque;
1e198715 1015 PCIExpLinkWidth *p = object_field_prop_ptr(obj, prop);
aa1859cc
PMD
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
991f0ac9 1045 visit_type_enum(v, name, &width, prop->info->enum_table, errp);
aa1859cc
PMD
1046}
1047
1048static void set_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1049 void *opaque, Error **errp)
1050{
aa1859cc 1051 Property *prop = opaque;
1e198715 1052 PCIExpLinkWidth *p = object_field_prop_ptr(obj, prop);
aa1859cc
PMD
1053 int width;
1054
991f0ac9 1055 if (!visit_type_enum(v, name, &width, prop->info->enum_table,
aa1859cc
PMD
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
1088const 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};
38255eff
EH
1096
1097/* --- UUID --- */
1098
1099static void get_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
1100 Error **errp)
1101{
1102 Property *prop = opaque;
1e198715 1103 QemuUUID *uuid = object_field_prop_ptr(obj, prop);
38255eff
EH
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
1114static void set_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
1115 Error **errp)
1116{
38255eff 1117 Property *prop = opaque;
1e198715 1118 QemuUUID *uuid = object_field_prop_ptr(obj, prop);
38255eff
EH
1119 char *str;
1120
38255eff
EH
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) {
e68c2cb7 1128 error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
38255eff
EH
1129 }
1130 g_free(str);
1131}
1132
1133static void set_default_uuid_auto(ObjectProperty *op, const Property *prop)
1134{
1135 object_property_set_default_str(op, UUID_VALUE_AUTO);
1136}
1137
1138const 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};