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 /* --- Reserved Region --- */
677 
678 /*
679  * Accepted syntax:
680  *   <low address>:<high address>:<type>
681  *   where low/high addresses are uint64_t in hexadecimal
682  *   and type is a non-negative decimal integer
683  */
684 static void get_reserved_region(Object *obj, Visitor *v, const char *name,
685                                 void *opaque, Error **errp)
686 {
687     Property *prop = opaque;
688     ReservedRegion *rr = object_field_prop_ptr(obj, prop);
689     char buffer[64];
690     char *p = buffer;
691     int rc;
692 
693     rc = snprintf(buffer, sizeof(buffer), "0x%"PRIx64":0x%"PRIx64":%u",
694                   rr->low, rr->high, rr->type);
695     assert(rc < sizeof(buffer));
696 
697     visit_type_str(v, name, &p, errp);
698 }
699 
700 static void set_reserved_region(Object *obj, Visitor *v, const char *name,
701                                 void *opaque, Error **errp)
702 {
703     Property *prop = opaque;
704     ReservedRegion *rr = object_field_prop_ptr(obj, prop);
705     const char *endptr;
706     char *str;
707     int ret;
708 
709     if (!visit_type_str(v, name, &str, errp)) {
710         return;
711     }
712 
713     ret = qemu_strtou64(str, &endptr, 16, &rr->low);
714     if (ret) {
715         error_setg(errp, "start address of '%s'"
716                    " must be a hexadecimal integer", name);
717         goto out;
718     }
719     if (*endptr != ':') {
720         goto separator_error;
721     }
722 
723     ret = qemu_strtou64(endptr + 1, &endptr, 16, &rr->high);
724     if (ret) {
725         error_setg(errp, "end address of '%s'"
726                    " must be a hexadecimal integer", name);
727         goto out;
728     }
729     if (*endptr != ':') {
730         goto separator_error;
731     }
732 
733     ret = qemu_strtoui(endptr + 1, &endptr, 10, &rr->type);
734     if (ret) {
735         error_setg(errp, "type of '%s'"
736                    " must be a non-negative decimal integer", name);
737     }
738     goto out;
739 
740 separator_error:
741     error_setg(errp, "reserved region fields must be separated with ':'");
742 out:
743     g_free(str);
744     return;
745 }
746 
747 const PropertyInfo qdev_prop_reserved_region = {
748     .name  = "reserved_region",
749     .description = "Reserved Region, example: 0xFEE00000:0xFEEFFFFF:0",
750     .get   = get_reserved_region,
751     .set   = set_reserved_region,
752 };
753 
754 /* --- pci address --- */
755 
756 /*
757  * bus-local address, i.e. "$slot" or "$slot.$fn"
758  */
759 static void set_pci_devfn(Object *obj, Visitor *v, const char *name,
760                           void *opaque, Error **errp)
761 {
762     Property *prop = opaque;
763     int32_t value, *ptr = object_field_prop_ptr(obj, prop);
764     unsigned int slot, fn, n;
765     char *str;
766 
767     if (!visit_type_str(v, name, &str, NULL)) {
768         if (!visit_type_int32(v, name, &value, errp)) {
769             return;
770         }
771         if (value < -1 || value > 255) {
772             error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
773                        name ? name : "null", "a value between -1 and 255");
774             return;
775         }
776         *ptr = value;
777         return;
778     }
779 
780     if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
781         fn = 0;
782         if (sscanf(str, "%x%n", &slot, &n) != 1) {
783             goto invalid;
784         }
785     }
786     if (str[n] != '\0' || fn > 7 || slot > 31) {
787         goto invalid;
788     }
789     *ptr = slot << 3 | fn;
790     g_free(str);
791     return;
792 
793 invalid:
794     error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
795     g_free(str);
796 }
797 
798 static int print_pci_devfn(Object *obj, Property *prop, char *dest,
799                            size_t len)
800 {
801     int32_t *ptr = object_field_prop_ptr(obj, prop);
802 
803     if (*ptr == -1) {
804         return snprintf(dest, len, "<unset>");
805     } else {
806         return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
807     }
808 }
809 
810 const PropertyInfo qdev_prop_pci_devfn = {
811     .name  = "int32",
812     .description = "Slot and optional function number, example: 06.0 or 06",
813     .print = print_pci_devfn,
814     .get   = qdev_propinfo_get_int32,
815     .set   = set_pci_devfn,
816     .set_default_value = qdev_propinfo_set_default_value_int,
817 };
818 
819 /* --- pci host address --- */
820 
821 static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
822                                  void *opaque, Error **errp)
823 {
824     Property *prop = opaque;
825     PCIHostDeviceAddress *addr = object_field_prop_ptr(obj, prop);
826     char buffer[] = "ffff:ff:ff.f";
827     char *p = buffer;
828     int rc = 0;
829 
830     /*
831      * Catch "invalid" device reference from vfio-pci and allow the
832      * default buffer representing the non-existent device to be used.
833      */
834     if (~addr->domain || ~addr->bus || ~addr->slot || ~addr->function) {
835         rc = snprintf(buffer, sizeof(buffer), "%04x:%02x:%02x.%0d",
836                       addr->domain, addr->bus, addr->slot, addr->function);
837         assert(rc == sizeof(buffer) - 1);
838     }
839 
840     visit_type_str(v, name, &p, errp);
841 }
842 
843 /*
844  * Parse [<domain>:]<bus>:<slot>.<func>
845  *   if <domain> is not supplied, it's assumed to be 0.
846  */
847 static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
848                                  void *opaque, Error **errp)
849 {
850     Property *prop = opaque;
851     PCIHostDeviceAddress *addr = object_field_prop_ptr(obj, prop);
852     char *str, *p;
853     char *e;
854     unsigned long val;
855     unsigned long dom = 0, bus = 0;
856     unsigned int slot = 0, func = 0;
857 
858     if (!visit_type_str(v, name, &str, errp)) {
859         return;
860     }
861 
862     p = str;
863     val = strtoul(p, &e, 16);
864     if (e == p || *e != ':') {
865         goto inval;
866     }
867     bus = val;
868 
869     p = e + 1;
870     val = strtoul(p, &e, 16);
871     if (e == p) {
872         goto inval;
873     }
874     if (*e == ':') {
875         dom = bus;
876         bus = val;
877         p = e + 1;
878         val = strtoul(p, &e, 16);
879         if (e == p) {
880             goto inval;
881         }
882     }
883     slot = val;
884 
885     if (*e != '.') {
886         goto inval;
887     }
888     p = e + 1;
889     val = strtoul(p, &e, 10);
890     if (e == p) {
891         goto inval;
892     }
893     func = val;
894 
895     if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) {
896         goto inval;
897     }
898 
899     if (*e) {
900         goto inval;
901     }
902 
903     addr->domain = dom;
904     addr->bus = bus;
905     addr->slot = slot;
906     addr->function = func;
907 
908     g_free(str);
909     return;
910 
911 inval:
912     error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
913     g_free(str);
914 }
915 
916 const PropertyInfo qdev_prop_pci_host_devaddr = {
917     .name = "str",
918     .description = "Address (bus/device/function) of "
919                    "the host device, example: 04:10.0",
920     .get = get_pci_host_devaddr,
921     .set = set_pci_host_devaddr,
922 };
923 
924 /* --- OffAutoPCIBAR off/auto/bar0/bar1/bar2/bar3/bar4/bar5 --- */
925 
926 const PropertyInfo qdev_prop_off_auto_pcibar = {
927     .name = "OffAutoPCIBAR",
928     .description = "off/auto/bar0/bar1/bar2/bar3/bar4/bar5",
929     .enum_table = &OffAutoPCIBAR_lookup,
930     .get = qdev_propinfo_get_enum,
931     .set = qdev_propinfo_set_enum,
932     .set_default_value = qdev_propinfo_set_default_value_enum,
933 };
934 
935 /* --- PCIELinkSpeed 2_5/5/8/16 -- */
936 
937 static void get_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
938                                    void *opaque, Error **errp)
939 {
940     Property *prop = opaque;
941     PCIExpLinkSpeed *p = object_field_prop_ptr(obj, prop);
942     int speed;
943 
944     switch (*p) {
945     case QEMU_PCI_EXP_LNK_2_5GT:
946         speed = PCIE_LINK_SPEED_2_5;
947         break;
948     case QEMU_PCI_EXP_LNK_5GT:
949         speed = PCIE_LINK_SPEED_5;
950         break;
951     case QEMU_PCI_EXP_LNK_8GT:
952         speed = PCIE_LINK_SPEED_8;
953         break;
954     case QEMU_PCI_EXP_LNK_16GT:
955         speed = PCIE_LINK_SPEED_16;
956         break;
957     default:
958         /* Unreachable */
959         abort();
960     }
961 
962     visit_type_enum(v, name, &speed, prop->info->enum_table, errp);
963 }
964 
965 static void set_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
966                                    void *opaque, Error **errp)
967 {
968     Property *prop = opaque;
969     PCIExpLinkSpeed *p = object_field_prop_ptr(obj, prop);
970     int speed;
971 
972     if (!visit_type_enum(v, name, &speed, prop->info->enum_table,
973                          errp)) {
974         return;
975     }
976 
977     switch (speed) {
978     case PCIE_LINK_SPEED_2_5:
979         *p = QEMU_PCI_EXP_LNK_2_5GT;
980         break;
981     case PCIE_LINK_SPEED_5:
982         *p = QEMU_PCI_EXP_LNK_5GT;
983         break;
984     case PCIE_LINK_SPEED_8:
985         *p = QEMU_PCI_EXP_LNK_8GT;
986         break;
987     case PCIE_LINK_SPEED_16:
988         *p = QEMU_PCI_EXP_LNK_16GT;
989         break;
990     default:
991         /* Unreachable */
992         abort();
993     }
994 }
995 
996 const PropertyInfo qdev_prop_pcie_link_speed = {
997     .name = "PCIELinkSpeed",
998     .description = "2_5/5/8/16",
999     .enum_table = &PCIELinkSpeed_lookup,
1000     .get = get_prop_pcielinkspeed,
1001     .set = set_prop_pcielinkspeed,
1002     .set_default_value = qdev_propinfo_set_default_value_enum,
1003 };
1004 
1005 /* --- PCIELinkWidth 1/2/4/8/12/16/32 -- */
1006 
1007 static void get_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1008                                    void *opaque, Error **errp)
1009 {
1010     Property *prop = opaque;
1011     PCIExpLinkWidth *p = object_field_prop_ptr(obj, prop);
1012     int width;
1013 
1014     switch (*p) {
1015     case QEMU_PCI_EXP_LNK_X1:
1016         width = PCIE_LINK_WIDTH_1;
1017         break;
1018     case QEMU_PCI_EXP_LNK_X2:
1019         width = PCIE_LINK_WIDTH_2;
1020         break;
1021     case QEMU_PCI_EXP_LNK_X4:
1022         width = PCIE_LINK_WIDTH_4;
1023         break;
1024     case QEMU_PCI_EXP_LNK_X8:
1025         width = PCIE_LINK_WIDTH_8;
1026         break;
1027     case QEMU_PCI_EXP_LNK_X12:
1028         width = PCIE_LINK_WIDTH_12;
1029         break;
1030     case QEMU_PCI_EXP_LNK_X16:
1031         width = PCIE_LINK_WIDTH_16;
1032         break;
1033     case QEMU_PCI_EXP_LNK_X32:
1034         width = PCIE_LINK_WIDTH_32;
1035         break;
1036     default:
1037         /* Unreachable */
1038         abort();
1039     }
1040 
1041     visit_type_enum(v, name, &width, prop->info->enum_table, errp);
1042 }
1043 
1044 static void set_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1045                                    void *opaque, Error **errp)
1046 {
1047     Property *prop = opaque;
1048     PCIExpLinkWidth *p = object_field_prop_ptr(obj, prop);
1049     int width;
1050 
1051     if (!visit_type_enum(v, name, &width, prop->info->enum_table,
1052                          errp)) {
1053         return;
1054     }
1055 
1056     switch (width) {
1057     case PCIE_LINK_WIDTH_1:
1058         *p = QEMU_PCI_EXP_LNK_X1;
1059         break;
1060     case PCIE_LINK_WIDTH_2:
1061         *p = QEMU_PCI_EXP_LNK_X2;
1062         break;
1063     case PCIE_LINK_WIDTH_4:
1064         *p = QEMU_PCI_EXP_LNK_X4;
1065         break;
1066     case PCIE_LINK_WIDTH_8:
1067         *p = QEMU_PCI_EXP_LNK_X8;
1068         break;
1069     case PCIE_LINK_WIDTH_12:
1070         *p = QEMU_PCI_EXP_LNK_X12;
1071         break;
1072     case PCIE_LINK_WIDTH_16:
1073         *p = QEMU_PCI_EXP_LNK_X16;
1074         break;
1075     case PCIE_LINK_WIDTH_32:
1076         *p = QEMU_PCI_EXP_LNK_X32;
1077         break;
1078     default:
1079         /* Unreachable */
1080         abort();
1081     }
1082 }
1083 
1084 const PropertyInfo qdev_prop_pcie_link_width = {
1085     .name = "PCIELinkWidth",
1086     .description = "1/2/4/8/12/16/32",
1087     .enum_table = &PCIELinkWidth_lookup,
1088     .get = get_prop_pcielinkwidth,
1089     .set = set_prop_pcielinkwidth,
1090     .set_default_value = qdev_propinfo_set_default_value_enum,
1091 };
1092 
1093 /* --- UUID --- */
1094 
1095 static void get_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
1096                      Error **errp)
1097 {
1098     Property *prop = opaque;
1099     QemuUUID *uuid = object_field_prop_ptr(obj, prop);
1100     char buffer[UUID_FMT_LEN + 1];
1101     char *p = buffer;
1102 
1103     qemu_uuid_unparse(uuid, buffer);
1104 
1105     visit_type_str(v, name, &p, errp);
1106 }
1107 
1108 #define UUID_VALUE_AUTO        "auto"
1109 
1110 static void set_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
1111                     Error **errp)
1112 {
1113     Property *prop = opaque;
1114     QemuUUID *uuid = object_field_prop_ptr(obj, prop);
1115     char *str;
1116 
1117     if (!visit_type_str(v, name, &str, errp)) {
1118         return;
1119     }
1120 
1121     if (!strcmp(str, UUID_VALUE_AUTO)) {
1122         qemu_uuid_generate(uuid);
1123     } else if (qemu_uuid_parse(str, uuid) < 0) {
1124         error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
1125     }
1126     g_free(str);
1127 }
1128 
1129 static void set_default_uuid_auto(ObjectProperty *op, const Property *prop)
1130 {
1131     object_property_set_default_str(op, UUID_VALUE_AUTO);
1132 }
1133 
1134 const PropertyInfo qdev_prop_uuid = {
1135     .name  = "str",
1136     .description = "UUID (aka GUID) or \"" UUID_VALUE_AUTO
1137         "\" for random value (default)",
1138     .get   = get_uuid,
1139     .set   = set_uuid,
1140     .set_default_value = set_default_uuid_auto,
1141 };
1142