target-s390x: support OC and NC in the EX instruction
[qemu/qmp-unstable.git] / hw / core / qdev-properties-system.c
bloba2e44bd4e8a49e5c5b7a85258ec6059242fd6d08
1 /*
2 * qdev property parsing and global properties
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 "net/net.h"
14 #include "hw/qdev.h"
15 #include "qapi/qmp/qerror.h"
16 #include "sysemu/block-backend.h"
17 #include "sysemu/blockdev.h"
18 #include "hw/block/block.h"
19 #include "net/hub.h"
20 #include "qapi/visitor.h"
21 #include "sysemu/char.h"
22 #include "sysemu/iothread.h"
24 static void get_pointer(Object *obj, Visitor *v, Property *prop,
25 char *(*print)(void *ptr),
26 const char *name, Error **errp)
28 DeviceState *dev = DEVICE(obj);
29 void **ptr = qdev_get_prop_ptr(dev, prop);
30 char *p;
32 p = *ptr ? print(*ptr) : g_strdup("");
33 visit_type_str(v, &p, name, errp);
34 g_free(p);
37 static void set_pointer(Object *obj, Visitor *v, Property *prop,
38 int (*parse)(DeviceState *dev, const char *str,
39 void **ptr),
40 const char *name, Error **errp)
42 DeviceState *dev = DEVICE(obj);
43 Error *local_err = NULL;
44 void **ptr = qdev_get_prop_ptr(dev, prop);
45 char *str;
46 int ret;
48 if (dev->realized) {
49 qdev_prop_set_after_realize(dev, name, errp);
50 return;
53 visit_type_str(v, &str, name, &local_err);
54 if (local_err) {
55 error_propagate(errp, local_err);
56 return;
58 if (!*str) {
59 g_free(str);
60 *ptr = NULL;
61 return;
63 ret = parse(dev, str, ptr);
64 error_set_from_qdev_prop_error(errp, ret, dev, prop, str);
65 g_free(str);
68 /* --- drive --- */
70 static int parse_drive(DeviceState *dev, const char *str, void **ptr)
72 BlockBackend *blk;
74 blk = blk_by_name(str);
75 if (!blk) {
76 return -ENOENT;
78 if (blk_attach_dev(blk, dev) < 0) {
79 return -EEXIST;
81 *ptr = blk;
82 return 0;
85 static void release_drive(Object *obj, const char *name, void *opaque)
87 DeviceState *dev = DEVICE(obj);
88 Property *prop = opaque;
89 BlockBackend **ptr = qdev_get_prop_ptr(dev, prop);
91 if (*ptr) {
92 blk_detach_dev(*ptr, dev);
93 blockdev_auto_del(*ptr);
97 static char *print_drive(void *ptr)
99 return g_strdup(blk_name(ptr));
102 static void get_drive(Object *obj, Visitor *v, void *opaque,
103 const char *name, Error **errp)
105 get_pointer(obj, v, opaque, print_drive, name, errp);
108 static void set_drive(Object *obj, Visitor *v, void *opaque,
109 const char *name, Error **errp)
111 set_pointer(obj, v, opaque, parse_drive, name, errp);
114 PropertyInfo qdev_prop_drive = {
115 .name = "str",
116 .description = "ID of a drive to use as a backend",
117 .get = get_drive,
118 .set = set_drive,
119 .release = release_drive,
122 /* --- character device --- */
124 static int parse_chr(DeviceState *dev, const char *str, void **ptr)
126 CharDriverState *chr = qemu_chr_find(str);
127 if (chr == NULL) {
128 return -ENOENT;
130 if (qemu_chr_fe_claim(chr) != 0) {
131 return -EEXIST;
133 *ptr = chr;
134 return 0;
137 static void release_chr(Object *obj, const char *name, void *opaque)
139 DeviceState *dev = DEVICE(obj);
140 Property *prop = opaque;
141 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
142 CharDriverState *chr = *ptr;
144 if (chr) {
145 qemu_chr_add_handlers(chr, NULL, NULL, NULL, NULL);
146 qemu_chr_fe_release(chr);
151 static char *print_chr(void *ptr)
153 CharDriverState *chr = ptr;
154 const char *val = chr->label ? chr->label : "";
156 return g_strdup(val);
159 static void get_chr(Object *obj, Visitor *v, void *opaque,
160 const char *name, Error **errp)
162 get_pointer(obj, v, opaque, print_chr, name, errp);
165 static void set_chr(Object *obj, Visitor *v, void *opaque,
166 const char *name, Error **errp)
168 set_pointer(obj, v, opaque, parse_chr, name, errp);
171 PropertyInfo qdev_prop_chr = {
172 .name = "str",
173 .description = "ID of a chardev to use as a backend",
174 .get = get_chr,
175 .set = set_chr,
176 .release = release_chr,
179 /* --- netdev device --- */
180 static void get_netdev(Object *obj, Visitor *v, void *opaque,
181 const char *name, Error **errp)
183 DeviceState *dev = DEVICE(obj);
184 Property *prop = opaque;
185 NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
186 char *p = g_strdup(peers_ptr->ncs[0] ? peers_ptr->ncs[0]->name : "");
188 visit_type_str(v, &p, name, errp);
189 g_free(p);
192 static void set_netdev(Object *obj, Visitor *v, void *opaque,
193 const char *name, Error **errp)
195 DeviceState *dev = DEVICE(obj);
196 Property *prop = opaque;
197 NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
198 NetClientState **ncs = peers_ptr->ncs;
199 NetClientState *peers[MAX_QUEUE_NUM];
200 Error *local_err = NULL;
201 int queues, err = 0, i = 0;
202 char *str;
204 if (dev->realized) {
205 qdev_prop_set_after_realize(dev, name, errp);
206 return;
209 visit_type_str(v, &str, name, &local_err);
210 if (local_err) {
211 error_propagate(errp, local_err);
212 return;
215 queues = qemu_find_net_clients_except(str, peers,
216 NET_CLIENT_OPTIONS_KIND_NIC,
217 MAX_QUEUE_NUM);
218 if (queues == 0) {
219 err = -ENOENT;
220 goto out;
223 if (queues > MAX_QUEUE_NUM) {
224 error_setg(errp, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)",
225 str, queues, MAX_QUEUE_NUM);
226 goto out;
229 for (i = 0; i < queues; i++) {
230 if (peers[i] == NULL) {
231 err = -ENOENT;
232 goto out;
235 if (peers[i]->peer) {
236 err = -EEXIST;
237 goto out;
240 if (ncs[i]) {
241 err = -EINVAL;
242 goto out;
245 ncs[i] = peers[i];
246 ncs[i]->queue_index = i;
249 peers_ptr->queues = queues;
251 out:
252 error_set_from_qdev_prop_error(errp, err, dev, prop, str);
253 g_free(str);
256 PropertyInfo qdev_prop_netdev = {
257 .name = "str",
258 .description = "ID of a netdev to use as a backend",
259 .get = get_netdev,
260 .set = set_netdev,
263 /* --- vlan --- */
265 static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
267 NetClientState **ptr = qdev_get_prop_ptr(dev, prop);
269 if (*ptr) {
270 int id;
271 if (!net_hub_id_for_client(*ptr, &id)) {
272 return snprintf(dest, len, "%d", id);
276 return snprintf(dest, len, "<null>");
279 static void get_vlan(Object *obj, Visitor *v, void *opaque,
280 const char *name, Error **errp)
282 DeviceState *dev = DEVICE(obj);
283 Property *prop = opaque;
284 NetClientState **ptr = qdev_get_prop_ptr(dev, prop);
285 int32_t id = -1;
287 if (*ptr) {
288 int hub_id;
289 if (!net_hub_id_for_client(*ptr, &hub_id)) {
290 id = hub_id;
294 visit_type_int32(v, &id, name, errp);
297 static void set_vlan(Object *obj, Visitor *v, void *opaque,
298 const char *name, Error **errp)
300 DeviceState *dev = DEVICE(obj);
301 Property *prop = opaque;
302 NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
303 NetClientState **ptr = &peers_ptr->ncs[0];
304 Error *local_err = NULL;
305 int32_t id;
306 NetClientState *hubport;
308 if (dev->realized) {
309 qdev_prop_set_after_realize(dev, name, errp);
310 return;
313 visit_type_int32(v, &id, name, &local_err);
314 if (local_err) {
315 error_propagate(errp, local_err);
316 return;
318 if (id == -1) {
319 *ptr = NULL;
320 return;
322 if (*ptr) {
323 error_set_from_qdev_prop_error(errp, -EINVAL, dev, prop, name);
324 return;
327 hubport = net_hub_port_find(id);
328 if (!hubport) {
329 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
330 name, prop->info->name);
331 return;
333 *ptr = hubport;
336 PropertyInfo qdev_prop_vlan = {
337 .name = "int32",
338 .description = "Integer VLAN id to connect to",
339 .print = print_vlan,
340 .get = get_vlan,
341 .set = set_vlan,
344 int qdev_prop_set_drive(DeviceState *dev, const char *name,
345 BlockBackend *value)
347 Error *err = NULL;
348 object_property_set_str(OBJECT(dev),
349 value ? blk_name(value) : "", name, &err);
350 if (err) {
351 qerror_report_err(err);
352 error_free(err);
353 return -1;
355 return 0;
358 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name,
359 BlockBackend *value)
361 if (qdev_prop_set_drive(dev, name, value) < 0) {
362 exit(1);
365 void qdev_prop_set_chr(DeviceState *dev, const char *name,
366 CharDriverState *value)
368 assert(!value || value->label);
369 object_property_set_str(OBJECT(dev),
370 value ? value->label : "", name, &error_abort);
373 void qdev_prop_set_netdev(DeviceState *dev, const char *name,
374 NetClientState *value)
376 assert(!value || value->name);
377 object_property_set_str(OBJECT(dev),
378 value ? value->name : "", name, &error_abort);
381 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
383 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
384 if (nd->netdev) {
385 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
387 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
388 object_property_find(OBJECT(dev), "vectors", NULL)) {
389 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
391 nd->instantiated = 1;
394 static int qdev_add_one_global(QemuOpts *opts, void *opaque)
396 GlobalProperty *g;
398 g = g_malloc0(sizeof(*g));
399 g->driver = qemu_opt_get(opts, "driver");
400 g->property = qemu_opt_get(opts, "property");
401 g->value = qemu_opt_get(opts, "value");
402 g->user_provided = true;
403 qdev_prop_register_global(g);
404 return 0;
407 void qemu_add_globals(void)
409 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global, NULL, 0);