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 = 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     int err = 0;
484     char *str;
485 
486     if (!visit_type_str(v, name, &str, errp)) {
487         return;
488     }
489 
490     state = audio_state_by_name(str);
491 
492     if (!state) {
493         err = -ENOENT;
494         goto out;
495     }
496     card->state = state;
497 
498 out:
499     error_set_from_qdev_prop_error(errp, err, obj, name, str);
500     g_free(str);
501 }
502 
503 const PropertyInfo qdev_prop_audiodev = {
504     .name = "str",
505     .description = "ID of an audiodev to use as a backend",
506     /* release done on shutdown */
507     .get = get_audiodev,
508     .set = set_audiodev,
509 };
510 
511 bool qdev_prop_set_drive_err(DeviceState *dev, const char *name,
512                              BlockBackend *value, Error **errp)
513 {
514     const char *ref = "";
515 
516     if (value) {
517         ref = blk_name(value);
518         if (!*ref) {
519             const BlockDriverState *bs = blk_bs(value);
520             if (bs) {
521                 ref = bdrv_get_node_name(bs);
522             }
523         }
524     }
525 
526     return object_property_set_str(OBJECT(dev), name, ref, errp);
527 }
528 
529 void qdev_prop_set_drive(DeviceState *dev, const char *name,
530                          BlockBackend *value)
531 {
532     qdev_prop_set_drive_err(dev, name, value, &error_abort);
533 }
534 
535 void qdev_prop_set_chr(DeviceState *dev, const char *name,
536                        Chardev *value)
537 {
538     assert(!value || value->label);
539     object_property_set_str(OBJECT(dev), name, value ? value->label : "",
540                             &error_abort);
541 }
542 
543 void qdev_prop_set_netdev(DeviceState *dev, const char *name,
544                           NetClientState *value)
545 {
546     assert(!value || value->name);
547     object_property_set_str(OBJECT(dev), name, value ? value->name : "",
548                             &error_abort);
549 }
550 
551 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
552 {
553     qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
554     if (nd->netdev) {
555         qdev_prop_set_netdev(dev, "netdev", nd->netdev);
556     }
557     if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
558         object_property_find(OBJECT(dev), "vectors")) {
559         qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
560     }
561     nd->instantiated = 1;
562 }
563 
564 /* --- lost tick policy --- */
565 
566 static void qdev_propinfo_set_losttickpolicy(Object *obj, Visitor *v,
567                                              const char *name, void *opaque,
568                                              Error **errp)
569 {
570     Property *prop = opaque;
571     int *ptr = object_field_prop_ptr(obj, prop);
572     int value;
573 
574     if (!visit_type_enum(v, name, &value, prop->info->enum_table, errp)) {
575         return;
576     }
577 
578     if (value == LOST_TICK_POLICY_SLEW) {
579         MachineState *ms = MACHINE(qdev_get_machine());
580 
581         if (!object_dynamic_cast(OBJECT(ms), TYPE_X86_MACHINE)) {
582             error_setg(errp,
583                        "the 'slew' policy is only available for x86 machines");
584             return;
585         }
586     }
587 
588     *ptr = value;
589 }
590 
591 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
592 
593 const PropertyInfo qdev_prop_losttickpolicy = {
594     .name  = "LostTickPolicy",
595     .enum_table  = &LostTickPolicy_lookup,
596     .get   = qdev_propinfo_get_enum,
597     .set   = qdev_propinfo_set_losttickpolicy,
598     .set_default_value = qdev_propinfo_set_default_value_enum,
599 };
600 
601 /* --- blocksize --- */
602 
603 static void set_blocksize(Object *obj, Visitor *v, const char *name,
604                           void *opaque, Error **errp)
605 {
606     DeviceState *dev = DEVICE(obj);
607     Property *prop = opaque;
608     uint32_t *ptr = object_field_prop_ptr(obj, prop);
609     uint64_t value;
610     Error *local_err = NULL;
611 
612     if (!visit_type_size(v, name, &value, errp)) {
613         return;
614     }
615     check_block_size(dev->id ? : "", name, value, &local_err);
616     if (local_err) {
617         error_propagate(errp, local_err);
618         return;
619     }
620     *ptr = value;
621 }
622 
623 const PropertyInfo qdev_prop_blocksize = {
624     .name  = "size",
625     .description = "A power of two between " MIN_BLOCK_SIZE_STR
626                    " and " MAX_BLOCK_SIZE_STR,
627     .get   = qdev_propinfo_get_size32,
628     .set   = set_blocksize,
629     .set_default_value = qdev_propinfo_set_default_value_uint,
630 };
631 
632 /* --- Block device error handling policy --- */
633 
634 QEMU_BUILD_BUG_ON(sizeof(BlockdevOnError) != sizeof(int));
635 
636 const PropertyInfo qdev_prop_blockdev_on_error = {
637     .name = "BlockdevOnError",
638     .description = "Error handling policy, "
639                    "report/ignore/enospc/stop/auto",
640     .enum_table = &BlockdevOnError_lookup,
641     .get = qdev_propinfo_get_enum,
642     .set = qdev_propinfo_set_enum,
643     .set_default_value = qdev_propinfo_set_default_value_enum,
644 };
645 
646 /* --- BIOS CHS translation */
647 
648 QEMU_BUILD_BUG_ON(sizeof(BiosAtaTranslation) != sizeof(int));
649 
650 const PropertyInfo qdev_prop_bios_chs_trans = {
651     .name = "BiosAtaTranslation",
652     .description = "Logical CHS translation algorithm, "
653                    "auto/none/lba/large/rechs",
654     .enum_table = &BiosAtaTranslation_lookup,
655     .get = qdev_propinfo_get_enum,
656     .set = qdev_propinfo_set_enum,
657     .set_default_value = qdev_propinfo_set_default_value_enum,
658 };
659 
660 /* --- FDC default drive types */
661 
662 const PropertyInfo qdev_prop_fdc_drive_type = {
663     .name = "FdcDriveType",
664     .description = "FDC drive type, "
665                    "144/288/120/none/auto",
666     .enum_table = &FloppyDriveType_lookup,
667     .get = qdev_propinfo_get_enum,
668     .set = qdev_propinfo_set_enum,
669     .set_default_value = qdev_propinfo_set_default_value_enum,
670 };
671 
672 /* --- MultiFDCompression --- */
673 
674 const PropertyInfo qdev_prop_multifd_compression = {
675     .name = "MultiFDCompression",
676     .description = "multifd_compression values, "
677                    "none/zlib/zstd",
678     .enum_table = &MultiFDCompression_lookup,
679     .get = qdev_propinfo_get_enum,
680     .set = qdev_propinfo_set_enum,
681     .set_default_value = qdev_propinfo_set_default_value_enum,
682 };
683 
684 /* --- Reserved Region --- */
685 
686 /*
687  * Accepted syntax:
688  *   <low address>:<high address>:<type>
689  *   where low/high addresses are uint64_t in hexadecimal
690  *   and type is a non-negative decimal integer
691  */
692 static void get_reserved_region(Object *obj, Visitor *v, const char *name,
693                                 void *opaque, Error **errp)
694 {
695     Property *prop = opaque;
696     ReservedRegion *rr = object_field_prop_ptr(obj, prop);
697     char buffer[64];
698     char *p = buffer;
699     int rc;
700 
701     rc = snprintf(buffer, sizeof(buffer), "0x%"PRIx64":0x%"PRIx64":%u",
702                   rr->low, rr->high, rr->type);
703     assert(rc < sizeof(buffer));
704 
705     visit_type_str(v, name, &p, errp);
706 }
707 
708 static void set_reserved_region(Object *obj, Visitor *v, const char *name,
709                                 void *opaque, Error **errp)
710 {
711     Property *prop = opaque;
712     ReservedRegion *rr = object_field_prop_ptr(obj, prop);
713     const char *endptr;
714     char *str;
715     int ret;
716 
717     if (!visit_type_str(v, name, &str, errp)) {
718         return;
719     }
720 
721     ret = qemu_strtou64(str, &endptr, 16, &rr->low);
722     if (ret) {
723         error_setg(errp, "start address of '%s'"
724                    " must be a hexadecimal integer", name);
725         goto out;
726     }
727     if (*endptr != ':') {
728         goto separator_error;
729     }
730 
731     ret = qemu_strtou64(endptr + 1, &endptr, 16, &rr->high);
732     if (ret) {
733         error_setg(errp, "end address of '%s'"
734                    " must be a hexadecimal integer", name);
735         goto out;
736     }
737     if (*endptr != ':') {
738         goto separator_error;
739     }
740 
741     ret = qemu_strtoui(endptr + 1, &endptr, 10, &rr->type);
742     if (ret) {
743         error_setg(errp, "type of '%s'"
744                    " must be a non-negative decimal integer", name);
745     }
746     goto out;
747 
748 separator_error:
749     error_setg(errp, "reserved region fields must be separated with ':'");
750 out:
751     g_free(str);
752     return;
753 }
754 
755 const PropertyInfo qdev_prop_reserved_region = {
756     .name  = "reserved_region",
757     .description = "Reserved Region, example: 0xFEE00000:0xFEEFFFFF:0",
758     .get   = get_reserved_region,
759     .set   = set_reserved_region,
760 };
761 
762 /* --- pci address --- */
763 
764 /*
765  * bus-local address, i.e. "$slot" or "$slot.$fn"
766  */
767 static void set_pci_devfn(Object *obj, Visitor *v, const char *name,
768                           void *opaque, Error **errp)
769 {
770     Property *prop = opaque;
771     int32_t value, *ptr = object_field_prop_ptr(obj, prop);
772     unsigned int slot, fn, n;
773     char *str;
774 
775     if (!visit_type_str(v, name, &str, NULL)) {
776         if (!visit_type_int32(v, name, &value, errp)) {
777             return;
778         }
779         if (value < -1 || value > 255) {
780             error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
781                        name ? name : "null", "a value between -1 and 255");
782             return;
783         }
784         *ptr = value;
785         return;
786     }
787 
788     if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
789         fn = 0;
790         if (sscanf(str, "%x%n", &slot, &n) != 1) {
791             goto invalid;
792         }
793     }
794     if (str[n] != '\0' || fn > 7 || slot > 31) {
795         goto invalid;
796     }
797     *ptr = slot << 3 | fn;
798     g_free(str);
799     return;
800 
801 invalid:
802     error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
803     g_free(str);
804 }
805 
806 static int print_pci_devfn(Object *obj, Property *prop, char *dest,
807                            size_t len)
808 {
809     int32_t *ptr = object_field_prop_ptr(obj, prop);
810 
811     if (*ptr == -1) {
812         return snprintf(dest, len, "<unset>");
813     } else {
814         return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
815     }
816 }
817 
818 const PropertyInfo qdev_prop_pci_devfn = {
819     .name  = "int32",
820     .description = "Slot and optional function number, example: 06.0 or 06",
821     .print = print_pci_devfn,
822     .get   = qdev_propinfo_get_int32,
823     .set   = set_pci_devfn,
824     .set_default_value = qdev_propinfo_set_default_value_int,
825 };
826 
827 /* --- pci host address --- */
828 
829 static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
830                                  void *opaque, Error **errp)
831 {
832     Property *prop = opaque;
833     PCIHostDeviceAddress *addr = object_field_prop_ptr(obj, prop);
834     char buffer[] = "ffff:ff:ff.f";
835     char *p = buffer;
836     int rc = 0;
837 
838     /*
839      * Catch "invalid" device reference from vfio-pci and allow the
840      * default buffer representing the non-existent device to be used.
841      */
842     if (~addr->domain || ~addr->bus || ~addr->slot || ~addr->function) {
843         rc = snprintf(buffer, sizeof(buffer), "%04x:%02x:%02x.%0d",
844                       addr->domain, addr->bus, addr->slot, addr->function);
845         assert(rc == sizeof(buffer) - 1);
846     }
847 
848     visit_type_str(v, name, &p, errp);
849 }
850 
851 /*
852  * Parse [<domain>:]<bus>:<slot>.<func>
853  *   if <domain> is not supplied, it's assumed to be 0.
854  */
855 static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
856                                  void *opaque, Error **errp)
857 {
858     Property *prop = opaque;
859     PCIHostDeviceAddress *addr = object_field_prop_ptr(obj, prop);
860     char *str, *p;
861     char *e;
862     unsigned long val;
863     unsigned long dom = 0, bus = 0;
864     unsigned int slot = 0, func = 0;
865 
866     if (!visit_type_str(v, name, &str, errp)) {
867         return;
868     }
869 
870     p = str;
871     val = strtoul(p, &e, 16);
872     if (e == p || *e != ':') {
873         goto inval;
874     }
875     bus = val;
876 
877     p = e + 1;
878     val = strtoul(p, &e, 16);
879     if (e == p) {
880         goto inval;
881     }
882     if (*e == ':') {
883         dom = bus;
884         bus = val;
885         p = e + 1;
886         val = strtoul(p, &e, 16);
887         if (e == p) {
888             goto inval;
889         }
890     }
891     slot = val;
892 
893     if (*e != '.') {
894         goto inval;
895     }
896     p = e + 1;
897     val = strtoul(p, &e, 10);
898     if (e == p) {
899         goto inval;
900     }
901     func = val;
902 
903     if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) {
904         goto inval;
905     }
906 
907     if (*e) {
908         goto inval;
909     }
910 
911     addr->domain = dom;
912     addr->bus = bus;
913     addr->slot = slot;
914     addr->function = func;
915 
916     g_free(str);
917     return;
918 
919 inval:
920     error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
921     g_free(str);
922 }
923 
924 const PropertyInfo qdev_prop_pci_host_devaddr = {
925     .name = "str",
926     .description = "Address (bus/device/function) of "
927                    "the host device, example: 04:10.0",
928     .get = get_pci_host_devaddr,
929     .set = set_pci_host_devaddr,
930 };
931 
932 /* --- OffAutoPCIBAR off/auto/bar0/bar1/bar2/bar3/bar4/bar5 --- */
933 
934 const PropertyInfo qdev_prop_off_auto_pcibar = {
935     .name = "OffAutoPCIBAR",
936     .description = "off/auto/bar0/bar1/bar2/bar3/bar4/bar5",
937     .enum_table = &OffAutoPCIBAR_lookup,
938     .get = qdev_propinfo_get_enum,
939     .set = qdev_propinfo_set_enum,
940     .set_default_value = qdev_propinfo_set_default_value_enum,
941 };
942 
943 /* --- PCIELinkSpeed 2_5/5/8/16 -- */
944 
945 static void get_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
946                                    void *opaque, Error **errp)
947 {
948     Property *prop = opaque;
949     PCIExpLinkSpeed *p = object_field_prop_ptr(obj, prop);
950     int speed;
951 
952     switch (*p) {
953     case QEMU_PCI_EXP_LNK_2_5GT:
954         speed = PCIE_LINK_SPEED_2_5;
955         break;
956     case QEMU_PCI_EXP_LNK_5GT:
957         speed = PCIE_LINK_SPEED_5;
958         break;
959     case QEMU_PCI_EXP_LNK_8GT:
960         speed = PCIE_LINK_SPEED_8;
961         break;
962     case QEMU_PCI_EXP_LNK_16GT:
963         speed = PCIE_LINK_SPEED_16;
964         break;
965     default:
966         /* Unreachable */
967         abort();
968     }
969 
970     visit_type_enum(v, name, &speed, prop->info->enum_table, errp);
971 }
972 
973 static void set_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
974                                    void *opaque, Error **errp)
975 {
976     Property *prop = opaque;
977     PCIExpLinkSpeed *p = object_field_prop_ptr(obj, prop);
978     int speed;
979 
980     if (!visit_type_enum(v, name, &speed, prop->info->enum_table,
981                          errp)) {
982         return;
983     }
984 
985     switch (speed) {
986     case PCIE_LINK_SPEED_2_5:
987         *p = QEMU_PCI_EXP_LNK_2_5GT;
988         break;
989     case PCIE_LINK_SPEED_5:
990         *p = QEMU_PCI_EXP_LNK_5GT;
991         break;
992     case PCIE_LINK_SPEED_8:
993         *p = QEMU_PCI_EXP_LNK_8GT;
994         break;
995     case PCIE_LINK_SPEED_16:
996         *p = QEMU_PCI_EXP_LNK_16GT;
997         break;
998     default:
999         /* Unreachable */
1000         abort();
1001     }
1002 }
1003 
1004 const PropertyInfo qdev_prop_pcie_link_speed = {
1005     .name = "PCIELinkSpeed",
1006     .description = "2_5/5/8/16",
1007     .enum_table = &PCIELinkSpeed_lookup,
1008     .get = get_prop_pcielinkspeed,
1009     .set = set_prop_pcielinkspeed,
1010     .set_default_value = qdev_propinfo_set_default_value_enum,
1011 };
1012 
1013 /* --- PCIELinkWidth 1/2/4/8/12/16/32 -- */
1014 
1015 static void get_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1016                                    void *opaque, Error **errp)
1017 {
1018     Property *prop = opaque;
1019     PCIExpLinkWidth *p = object_field_prop_ptr(obj, prop);
1020     int width;
1021 
1022     switch (*p) {
1023     case QEMU_PCI_EXP_LNK_X1:
1024         width = PCIE_LINK_WIDTH_1;
1025         break;
1026     case QEMU_PCI_EXP_LNK_X2:
1027         width = PCIE_LINK_WIDTH_2;
1028         break;
1029     case QEMU_PCI_EXP_LNK_X4:
1030         width = PCIE_LINK_WIDTH_4;
1031         break;
1032     case QEMU_PCI_EXP_LNK_X8:
1033         width = PCIE_LINK_WIDTH_8;
1034         break;
1035     case QEMU_PCI_EXP_LNK_X12:
1036         width = PCIE_LINK_WIDTH_12;
1037         break;
1038     case QEMU_PCI_EXP_LNK_X16:
1039         width = PCIE_LINK_WIDTH_16;
1040         break;
1041     case QEMU_PCI_EXP_LNK_X32:
1042         width = PCIE_LINK_WIDTH_32;
1043         break;
1044     default:
1045         /* Unreachable */
1046         abort();
1047     }
1048 
1049     visit_type_enum(v, name, &width, prop->info->enum_table, errp);
1050 }
1051 
1052 static void set_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1053                                    void *opaque, Error **errp)
1054 {
1055     Property *prop = opaque;
1056     PCIExpLinkWidth *p = object_field_prop_ptr(obj, prop);
1057     int width;
1058 
1059     if (!visit_type_enum(v, name, &width, prop->info->enum_table,
1060                          errp)) {
1061         return;
1062     }
1063 
1064     switch (width) {
1065     case PCIE_LINK_WIDTH_1:
1066         *p = QEMU_PCI_EXP_LNK_X1;
1067         break;
1068     case PCIE_LINK_WIDTH_2:
1069         *p = QEMU_PCI_EXP_LNK_X2;
1070         break;
1071     case PCIE_LINK_WIDTH_4:
1072         *p = QEMU_PCI_EXP_LNK_X4;
1073         break;
1074     case PCIE_LINK_WIDTH_8:
1075         *p = QEMU_PCI_EXP_LNK_X8;
1076         break;
1077     case PCIE_LINK_WIDTH_12:
1078         *p = QEMU_PCI_EXP_LNK_X12;
1079         break;
1080     case PCIE_LINK_WIDTH_16:
1081         *p = QEMU_PCI_EXP_LNK_X16;
1082         break;
1083     case PCIE_LINK_WIDTH_32:
1084         *p = QEMU_PCI_EXP_LNK_X32;
1085         break;
1086     default:
1087         /* Unreachable */
1088         abort();
1089     }
1090 }
1091 
1092 const PropertyInfo qdev_prop_pcie_link_width = {
1093     .name = "PCIELinkWidth",
1094     .description = "1/2/4/8/12/16/32",
1095     .enum_table = &PCIELinkWidth_lookup,
1096     .get = get_prop_pcielinkwidth,
1097     .set = set_prop_pcielinkwidth,
1098     .set_default_value = qdev_propinfo_set_default_value_enum,
1099 };
1100 
1101 /* --- UUID --- */
1102 
1103 static void get_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
1104                      Error **errp)
1105 {
1106     Property *prop = opaque;
1107     QemuUUID *uuid = object_field_prop_ptr(obj, prop);
1108     char buffer[UUID_FMT_LEN + 1];
1109     char *p = buffer;
1110 
1111     qemu_uuid_unparse(uuid, buffer);
1112 
1113     visit_type_str(v, name, &p, errp);
1114 }
1115 
1116 #define UUID_VALUE_AUTO        "auto"
1117 
1118 static void set_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
1119                     Error **errp)
1120 {
1121     Property *prop = opaque;
1122     QemuUUID *uuid = object_field_prop_ptr(obj, prop);
1123     char *str;
1124 
1125     if (!visit_type_str(v, name, &str, errp)) {
1126         return;
1127     }
1128 
1129     if (!strcmp(str, UUID_VALUE_AUTO)) {
1130         qemu_uuid_generate(uuid);
1131     } else if (qemu_uuid_parse(str, uuid) < 0) {
1132         error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
1133     }
1134     g_free(str);
1135 }
1136 
1137 static void set_default_uuid_auto(ObjectProperty *op, const Property *prop)
1138 {
1139     object_property_set_default_str(op, UUID_VALUE_AUTO);
1140 }
1141 
1142 const PropertyInfo qdev_prop_uuid = {
1143     .name  = "str",
1144     .description = "UUID (aka GUID) or \"" UUID_VALUE_AUTO
1145         "\" for random value (default)",
1146     .get   = get_uuid,
1147     .set   = set_uuid,
1148     .set_default_value = set_default_uuid_auto,
1149 };
1150