qemu-deprecated.texi: Rename the HMP section
[qemu/ar7.git] / hw / core / qdev-properties-system.c
blobb45a7ef54bf345b3806bf127fe0faf1c55cce086
1 /*
2 * qdev property parsing
3 * (parts specific for qemu-system-*)
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.
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.
13 #include "qemu/osdep.h"
14 #include "net/net.h"
15 #include "hw/qdev.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qerror.h"
18 #include "sysemu/block-backend.h"
19 #include "sysemu/blockdev.h"
20 #include "hw/block/block.h"
21 #include "net/hub.h"
22 #include "qapi/visitor.h"
23 #include "chardev/char-fe.h"
24 #include "sysemu/iothread.h"
25 #include "sysemu/tpm_backend.h"
27 static void get_pointer(Object *obj, Visitor *v, Property *prop,
28 char *(*print)(void *ptr),
29 const char *name, Error **errp)
31 DeviceState *dev = DEVICE(obj);
32 void **ptr = qdev_get_prop_ptr(dev, prop);
33 char *p;
35 p = *ptr ? print(*ptr) : g_strdup("");
36 visit_type_str(v, name, &p, errp);
37 g_free(p);
40 static void set_pointer(Object *obj, Visitor *v, Property *prop,
41 void (*parse)(DeviceState *dev, const char *str,
42 void **ptr, const char *propname,
43 Error **errp),
44 const char *name, Error **errp)
46 DeviceState *dev = DEVICE(obj);
47 Error *local_err = NULL;
48 void **ptr = qdev_get_prop_ptr(dev, prop);
49 char *str;
51 if (dev->realized) {
52 qdev_prop_set_after_realize(dev, name, errp);
53 return;
56 visit_type_str(v, name, &str, &local_err);
57 if (local_err) {
58 error_propagate(errp, local_err);
59 return;
61 if (!*str) {
62 g_free(str);
63 *ptr = NULL;
64 return;
66 parse(dev, str, ptr, prop->name, errp);
67 g_free(str);
70 /* --- drive --- */
72 static void parse_drive(DeviceState *dev, const char *str, void **ptr,
73 const char *propname, Error **errp)
75 BlockBackend *blk;
76 bool blk_created = false;
77 int ret;
79 blk = blk_by_name(str);
80 if (!blk) {
81 BlockDriverState *bs = bdrv_lookup_bs(NULL, str, NULL);
82 if (bs) {
83 blk = blk_new(0, BLK_PERM_ALL);
84 blk_created = true;
86 ret = blk_insert_bs(blk, bs, errp);
87 if (ret < 0) {
88 goto fail;
92 if (!blk) {
93 error_setg(errp, "Property '%s.%s' can't find value '%s'",
94 object_get_typename(OBJECT(dev)), propname, str);
95 goto fail;
97 if (blk_attach_dev(blk, dev) < 0) {
98 DriveInfo *dinfo = blk_legacy_dinfo(blk);
100 if (dinfo && dinfo->type != IF_NONE) {
101 error_setg(errp, "Drive '%s' is already in use because "
102 "it has been automatically connected to another "
103 "device (did you need 'if=none' in the drive options?)",
104 str);
105 } else {
106 error_setg(errp, "Drive '%s' is already in use by another device",
107 str);
109 goto fail;
112 *ptr = blk;
114 fail:
115 if (blk_created) {
116 /* If we need to keep a reference, blk_attach_dev() took it */
117 blk_unref(blk);
121 static void release_drive(Object *obj, const char *name, void *opaque)
123 DeviceState *dev = DEVICE(obj);
124 Property *prop = opaque;
125 BlockBackend **ptr = qdev_get_prop_ptr(dev, prop);
127 if (*ptr) {
128 AioContext *ctx = blk_get_aio_context(*ptr);
130 aio_context_acquire(ctx);
131 blockdev_auto_del(*ptr);
132 blk_detach_dev(*ptr, dev);
133 aio_context_release(ctx);
137 static char *print_drive(void *ptr)
139 const char *name;
141 name = blk_name(ptr);
142 if (!*name) {
143 BlockDriverState *bs = blk_bs(ptr);
144 if (bs) {
145 name = bdrv_get_node_name(bs);
148 return g_strdup(name);
151 static void get_drive(Object *obj, Visitor *v, const char *name, void *opaque,
152 Error **errp)
154 get_pointer(obj, v, opaque, print_drive, name, errp);
157 static void set_drive(Object *obj, Visitor *v, const char *name, void *opaque,
158 Error **errp)
160 set_pointer(obj, v, opaque, parse_drive, name, errp);
163 const PropertyInfo qdev_prop_drive = {
164 .name = "str",
165 .description = "Node name or ID of a block device to use as a backend",
166 .get = get_drive,
167 .set = set_drive,
168 .release = release_drive,
171 /* --- character device --- */
173 static void get_chr(Object *obj, Visitor *v, const char *name, void *opaque,
174 Error **errp)
176 DeviceState *dev = DEVICE(obj);
177 CharBackend *be = qdev_get_prop_ptr(dev, opaque);
178 char *p;
180 p = g_strdup(be->chr && be->chr->label ? be->chr->label : "");
181 visit_type_str(v, name, &p, errp);
182 g_free(p);
185 static void set_chr(Object *obj, Visitor *v, const char *name, void *opaque,
186 Error **errp)
188 DeviceState *dev = DEVICE(obj);
189 Error *local_err = NULL;
190 Property *prop = opaque;
191 CharBackend *be = qdev_get_prop_ptr(dev, prop);
192 Chardev *s;
193 char *str;
195 if (dev->realized) {
196 qdev_prop_set_after_realize(dev, name, errp);
197 return;
200 visit_type_str(v, name, &str, &local_err);
201 if (local_err) {
202 error_propagate(errp, local_err);
203 return;
206 if (!*str) {
207 g_free(str);
208 be->chr = NULL;
209 return;
212 s = qemu_chr_find(str);
213 if (s == NULL) {
214 error_setg(errp, "Property '%s.%s' can't find value '%s'",
215 object_get_typename(obj), prop->name, str);
216 } else if (!qemu_chr_fe_init(be, s, errp)) {
217 error_prepend(errp, "Property '%s.%s' can't take value '%s': ",
218 object_get_typename(obj), prop->name, str);
220 g_free(str);
223 static void release_chr(Object *obj, const char *name, void *opaque)
225 DeviceState *dev = DEVICE(obj);
226 Property *prop = opaque;
227 CharBackend *be = qdev_get_prop_ptr(dev, prop);
229 qemu_chr_fe_deinit(be, false);
232 const PropertyInfo qdev_prop_chr = {
233 .name = "str",
234 .description = "ID of a chardev to use as a backend",
235 .get = get_chr,
236 .set = set_chr,
237 .release = release_chr,
240 /* --- netdev device --- */
241 static void get_netdev(Object *obj, Visitor *v, const char *name,
242 void *opaque, Error **errp)
244 DeviceState *dev = DEVICE(obj);
245 Property *prop = opaque;
246 NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
247 char *p = g_strdup(peers_ptr->ncs[0] ? peers_ptr->ncs[0]->name : "");
249 visit_type_str(v, name, &p, errp);
250 g_free(p);
253 static void set_netdev(Object *obj, Visitor *v, const char *name,
254 void *opaque, Error **errp)
256 DeviceState *dev = DEVICE(obj);
257 Property *prop = opaque;
258 NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
259 NetClientState **ncs = peers_ptr->ncs;
260 NetClientState *peers[MAX_QUEUE_NUM];
261 Error *local_err = NULL;
262 int queues, err = 0, i = 0;
263 char *str;
265 if (dev->realized) {
266 qdev_prop_set_after_realize(dev, name, errp);
267 return;
270 visit_type_str(v, name, &str, &local_err);
271 if (local_err) {
272 error_propagate(errp, local_err);
273 return;
276 queues = qemu_find_net_clients_except(str, peers,
277 NET_CLIENT_DRIVER_NIC,
278 MAX_QUEUE_NUM);
279 if (queues == 0) {
280 err = -ENOENT;
281 goto out;
284 if (queues > MAX_QUEUE_NUM) {
285 error_setg(errp, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)",
286 str, queues, MAX_QUEUE_NUM);
287 goto out;
290 for (i = 0; i < queues; i++) {
292 if (peers[i]->peer) {
293 err = -EEXIST;
294 goto out;
297 if (ncs[i]) {
298 err = -EINVAL;
299 goto out;
302 ncs[i] = peers[i];
303 ncs[i]->queue_index = i;
306 peers_ptr->queues = queues;
308 out:
309 error_set_from_qdev_prop_error(errp, err, dev, prop, str);
310 g_free(str);
313 const PropertyInfo qdev_prop_netdev = {
314 .name = "str",
315 .description = "ID of a netdev to use as a backend",
316 .get = get_netdev,
317 .set = set_netdev,
321 void qdev_prop_set_drive(DeviceState *dev, const char *name,
322 BlockBackend *value, Error **errp)
324 const char *ref = "";
326 if (value) {
327 ref = blk_name(value);
328 if (!*ref) {
329 const BlockDriverState *bs = blk_bs(value);
330 if (bs) {
331 ref = bdrv_get_node_name(bs);
336 object_property_set_str(OBJECT(dev), ref, name, errp);
339 void qdev_prop_set_chr(DeviceState *dev, const char *name,
340 Chardev *value)
342 assert(!value || value->label);
343 object_property_set_str(OBJECT(dev),
344 value ? value->label : "", name, &error_abort);
347 void qdev_prop_set_netdev(DeviceState *dev, const char *name,
348 NetClientState *value)
350 assert(!value || value->name);
351 object_property_set_str(OBJECT(dev),
352 value ? value->name : "", name, &error_abort);
355 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
357 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
358 if (nd->netdev) {
359 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
361 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
362 object_property_find(OBJECT(dev), "vectors", NULL)) {
363 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
365 nd->instantiated = 1;