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