]> git.proxmox.com Git - mirror_qemu.git/blame - hw/char/virtio-serial-bus.c
Merge remote-tracking branch 'remotes/mcayland/tags/qemu-sparc.for-upstream-20180914...
[mirror_qemu.git] / hw / char / virtio-serial-bus.c
CommitLineData
98b19252
AS
1/*
2 * A bus for connecting virtio serial and console ports
3 *
71c092e9 4 * Copyright (C) 2009, 2010 Red Hat, Inc.
98b19252
AS
5 *
6 * Author(s):
7 * Amit Shah <amit.shah@redhat.com>
8 *
9 * Some earlier parts are:
10 * Copyright IBM, Corp. 2008
11 * authored by
12 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
6b620ca3
PB
16 *
17 * Contributions after 2012-01-13 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
98b19252
AS
19 */
20
9b8bfe21 21#include "qemu/osdep.h"
da34e65c 22#include "qapi/error.h"
1de7afc9 23#include "qemu/iov.h"
83c9089e 24#include "monitor/monitor.h"
d49b6836 25#include "qemu/error-report.h"
1de7afc9 26#include "qemu/queue.h"
83c9f4ca 27#include "hw/sysbus.h"
49e3fdd7 28#include "trace.h"
0d09e41a 29#include "hw/virtio/virtio-serial.h"
e0ab7fac 30#include "hw/virtio/virtio-access.h"
98b19252 31
43d73554 32static struct VirtIOSerialDevices {
a1857ad1
AS
33 QLIST_HEAD(, VirtIOSerial) devices;
34} vserdevices;
35
98b19252
AS
36static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
37{
38 VirtIOSerialPort *port;
39
055b889f
AS
40 if (id == VIRTIO_CONSOLE_BAD_ID) {
41 return NULL;
42 }
43
98b19252
AS
44 QTAILQ_FOREACH(port, &vser->ports, next) {
45 if (port->id == id)
46 return port;
47 }
48 return NULL;
49}
50
51static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
52{
53 VirtIOSerialPort *port;
54
55 QTAILQ_FOREACH(port, &vser->ports, next) {
56 if (port->ivq == vq || port->ovq == vq)
57 return port;
58 }
59 return NULL;
60}
61
d0a0bfe6
AS
62static VirtIOSerialPort *find_port_by_name(char *name)
63{
64 VirtIOSerial *vser;
65
66 QLIST_FOREACH(vser, &vserdevices.devices, next) {
67 VirtIOSerialPort *port;
68
69 QTAILQ_FOREACH(port, &vser->ports, next) {
b18a755c 70 if (port->name && !strcmp(port->name, name)) {
d0a0bfe6
AS
71 return port;
72 }
73 }
74 }
75 return NULL;
76}
77
09da01c3
SS
78static VirtIOSerialPort *find_first_connected_console(VirtIOSerial *vser)
79{
80 VirtIOSerialPort *port;
81
82 QTAILQ_FOREACH(port, &vser->ports, next) {
83 VirtIOSerialPortClass const *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
84 if (vsc->is_console && port->host_connected) {
85 return port;
86 }
87 }
88 return NULL;
89}
90
6663a195
AS
91static bool use_multiport(VirtIOSerial *vser)
92{
76017fd2 93 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
95129d6f 94 return virtio_vdev_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT);
6663a195
AS
95}
96
98b19252
AS
97static size_t write_to_port(VirtIOSerialPort *port,
98 const uint8_t *buf, size_t size)
99{
51b19ebe 100 VirtQueueElement *elem;
98b19252 101 VirtQueue *vq;
e4d5639d 102 size_t offset;
98b19252
AS
103
104 vq = port->ivq;
105 if (!virtio_queue_ready(vq)) {
106 return 0;
107 }
98b19252 108
e4d5639d 109 offset = 0;
98b19252 110 while (offset < size) {
e4d5639d 111 size_t len;
98b19252 112
51b19ebe
PB
113 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
114 if (!elem) {
98b19252
AS
115 break;
116 }
117
51b19ebe 118 len = iov_from_buf(elem->in_sg, elem->in_num, 0,
dcf6f5e1 119 buf + offset, size - offset);
e4d5639d 120 offset += len;
98b19252 121
51b19ebe
PB
122 virtqueue_push(vq, elem, len);
123 g_free(elem);
98b19252
AS
124 }
125
76017fd2 126 virtio_notify(VIRTIO_DEVICE(port->vser), vq);
98b19252
AS
127 return offset;
128}
129
6bff8656 130static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
a69c7600 131{
51b19ebe 132 VirtQueueElement *elem;
a69c7600 133
7185f931
AS
134 if (!virtio_queue_ready(vq)) {
135 return;
136 }
51b19ebe
PB
137 for (;;) {
138 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
139 if (!elem) {
140 break;
141 }
142 virtqueue_push(vq, elem, 0);
143 g_free(elem);
6bff8656
AS
144 }
145 virtio_notify(vdev, vq);
146}
147
d4c19cde
SH
148static void discard_throttle_data(VirtIOSerialPort *port)
149{
150 if (port->elem) {
151 virtqueue_detach_element(port->ovq, port->elem, 0);
152 g_free(port->elem);
153 port->elem = NULL;
154 }
155}
156
9ed7b059 157static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
6bff8656 158 VirtIODevice *vdev)
a69c7600 159{
f82e35e3 160 VirtIOSerialPortClass *vsc;
a15bb0d6 161
6bff8656 162 assert(port);
fd11a78b 163 assert(virtio_queue_ready(vq));
a69c7600 164
f82e35e3 165 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
a15bb0d6 166
f1925dff 167 while (!port->throttled) {
471344db 168 unsigned int i;
a69c7600 169
f1925dff 170 /* Pop an elem only if we haven't left off a previous one mid-way */
51b19ebe
PB
171 if (!port->elem) {
172 port->elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
173 if (!port->elem) {
f1925dff
AS
174 break;
175 }
176 port->iov_idx = 0;
177 port->iov_offset = 0;
178 }
a69c7600 179
51b19ebe 180 for (i = port->iov_idx; i < port->elem->out_num; i++) {
f1925dff
AS
181 size_t buf_size;
182 ssize_t ret;
183
51b19ebe 184 buf_size = port->elem->out_sg[i].iov_len - port->iov_offset;
f82e35e3 185 ret = vsc->have_data(port,
51b19ebe 186 port->elem->out_sg[i].iov_base
a15bb0d6
MA
187 + port->iov_offset,
188 buf_size);
46764fe0
SH
189 if (!port->elem) { /* bail if we got disconnected */
190 return;
191 }
d6258c93 192 if (port->throttled) {
f1925dff
AS
193 port->iov_idx = i;
194 if (ret > 0) {
195 port->iov_offset += ret;
196 }
197 break;
198 }
199 port->iov_offset = 0;
a69c7600 200 }
f1925dff
AS
201 if (port->throttled) {
202 break;
203 }
51b19ebe
PB
204 virtqueue_push(vq, port->elem, 0);
205 g_free(port->elem);
206 port->elem = NULL;
a69c7600
AS
207 }
208 virtio_notify(vdev, vq);
209}
210
6bff8656 211static void flush_queued_data(VirtIOSerialPort *port)
9ed7b059 212{
a1c59752 213 assert(port);
9ed7b059 214
6b611d3a
AS
215 if (!virtio_queue_ready(port->ovq)) {
216 return;
217 }
76017fd2 218 do_flush_queued_data(port, port->ovq, VIRTIO_DEVICE(port->vser));
9ed7b059
AS
219}
220
4e28976e 221static size_t send_control_msg(VirtIOSerial *vser, void *buf, size_t len)
98b19252 222{
51b19ebe 223 VirtQueueElement *elem;
98b19252 224 VirtQueue *vq;
98b19252 225
4e28976e 226 vq = vser->c_ivq;
98b19252
AS
227 if (!virtio_queue_ready(vq)) {
228 return 0;
229 }
51b19ebe
PB
230
231 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
232 if (!elem) {
98b19252
AS
233 return 0;
234 }
235
78820803 236 /* TODO: detect a buffer that's too short, set NEEDS_RESET */
51b19ebe 237 iov_from_buf(elem->in_sg, elem->in_num, 0, buf, len);
98b19252 238
51b19ebe 239 virtqueue_push(vq, elem, len);
76017fd2 240 virtio_notify(VIRTIO_DEVICE(vser), vq);
51b19ebe
PB
241 g_free(elem);
242
98b19252
AS
243 return len;
244}
245
4e28976e
AS
246static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id,
247 uint16_t event, uint16_t value)
98b19252 248{
e0ab7fac 249 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
98b19252
AS
250 struct virtio_console_control cpkt;
251
e0ab7fac
RR
252 virtio_stl_p(vdev, &cpkt.id, port_id);
253 virtio_stw_p(vdev, &cpkt.event, event);
254 virtio_stw_p(vdev, &cpkt.value, value);
98b19252 255
4e28976e
AS
256 trace_virtio_serial_send_control_event(port_id, event, value);
257 return send_control_msg(vser, &cpkt, sizeof(cpkt));
98b19252
AS
258}
259
260/* Functions for use inside qemu to open and read from/write to ports */
261int virtio_serial_open(VirtIOSerialPort *port)
262{
6663a195
AS
263 /* Don't allow opening an already-open port */
264 if (port->host_connected) {
265 return 0;
266 }
267 /* Send port open notification to the guest */
268 port->host_connected = true;
4e28976e 269 send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
6663a195 270
98b19252
AS
271 return 0;
272}
273
274int virtio_serial_close(VirtIOSerialPort *port)
275{
6663a195 276 port->host_connected = false;
9ed7b059
AS
277 /*
278 * If there's any data the guest sent which the app didn't
279 * consume, reset the throttling flag and discard the data.
280 */
281 port->throttled = false;
d4c19cde 282 discard_throttle_data(port);
76017fd2 283 discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
9ed7b059 284
4e28976e 285 send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 0);
6663a195 286
98b19252
AS
287 return 0;
288}
289
290/* Individual ports/apps call this function to write to the guest. */
291ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
292 size_t size)
293{
6663a195
AS
294 if (!port || !port->host_connected || !port->guest_connected) {
295 return 0;
296 }
98b19252
AS
297 return write_to_port(port, buf, size);
298}
299
300/*
301 * Readiness of the guest to accept data on a port.
302 * Returns max. data the guest can receive
303 */
304size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
305{
76017fd2 306 VirtIODevice *vdev = VIRTIO_DEVICE(port->vser);
98b19252 307 VirtQueue *vq = port->ivq;
ad3005ad 308 unsigned int bytes;
98b19252
AS
309
310 if (!virtio_queue_ready(vq) ||
76017fd2 311 !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) ||
98b19252
AS
312 virtio_queue_empty(vq)) {
313 return 0;
314 }
6663a195
AS
315 if (use_multiport(port->vser) && !port->guest_connected) {
316 return 0;
317 }
e1f7b481 318 virtqueue_get_avail_bytes(vq, &bytes, NULL, 4096, 0);
ad3005ad 319 return bytes;
98b19252
AS
320}
321
199646d8
AL
322static void flush_queued_data_bh(void *opaque)
323{
324 VirtIOSerialPort *port = opaque;
325
326 flush_queued_data(port);
327}
328
9ed7b059
AS
329void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
330{
331 if (!port) {
332 return;
333 }
334
49e3fdd7 335 trace_virtio_serial_throttle_port(port->id, throttle);
9ed7b059
AS
336 port->throttled = throttle;
337 if (throttle) {
338 return;
339 }
199646d8 340 qemu_bh_schedule(port->bh);
9ed7b059
AS
341}
342
98b19252 343/* Guest wants to notify us of some event */
e61da14d 344static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
98b19252 345{
e0ab7fac 346 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
98b19252 347 struct VirtIOSerialPort *port;
f82e35e3 348 VirtIOSerialPortClass *vsc;
98b19252 349 struct virtio_console_control cpkt, *gcpkt;
160600fd
AS
350 uint8_t *buffer;
351 size_t buffer_len;
98b19252
AS
352
353 gcpkt = buf;
98b19252 354
e61da14d
AS
355 if (len < sizeof(cpkt)) {
356 /* The guest sent an invalid control packet */
357 return;
358 }
359
e0ab7fac
RR
360 cpkt.event = virtio_lduw_p(vdev, &gcpkt->event);
361 cpkt.value = virtio_lduw_p(vdev, &gcpkt->value);
98b19252 362
49e3fdd7
AS
363 trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
364
d2e4d08b 365 if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
4048c7c3 366 if (!cpkt.value) {
6daf194d 367 error_report("virtio-serial-bus: Guest failure in adding device %s",
5e52e5f9 368 vser->bus.qbus.name);
d2e4d08b 369 return;
4048c7c3 370 }
055b889f
AS
371 /*
372 * The device is up, we can now tell the device about all the
373 * ports we have here.
374 */
375 QTAILQ_FOREACH(port, &vser->ports, next) {
4e28976e 376 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_ADD, 1);
055b889f 377 }
d2e4d08b
LC
378 return;
379 }
055b889f 380
e0ab7fac 381 port = find_port_by_id(vser, virtio_ldl_p(vdev, &gcpkt->id));
d2e4d08b 382 if (!port) {
95c9cde2 383 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
e0ab7fac 384 virtio_ldl_p(vdev, &gcpkt->id), vser->bus.qbus.name);
d2e4d08b
LC
385 return;
386 }
387
49e3fdd7
AS
388 trace_virtio_serial_handle_control_message_port(port->id);
389
f82e35e3 390 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
d2e4d08b
LC
391
392 switch(cpkt.event) {
98b19252 393 case VIRTIO_CONSOLE_PORT_READY:
4048c7c3 394 if (!cpkt.value) {
6daf194d 395 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
5e52e5f9 396 port->id, vser->bus.qbus.name);
4048c7c3
AS
397 break;
398 }
98b19252
AS
399 /*
400 * Now that we know the guest asked for the port name, we're
401 * sure the guest has initialised whatever state is necessary
402 * for this port. Now's a good time to let the guest know if
403 * this port is a console port so that the guest can hook it
404 * up to hvc.
405 */
f82e35e3 406 if (vsc->is_console) {
4e28976e 407 send_control_event(vser, port->id, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
98b19252 408 }
6663a195 409
160600fd 410 if (port->name) {
e0ab7fac
RR
411 virtio_stl_p(vdev, &cpkt.id, port->id);
412 virtio_stw_p(vdev, &cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
413 virtio_stw_p(vdev, &cpkt.value, 1);
160600fd
AS
414
415 buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
7267c094 416 buffer = g_malloc(buffer_len);
160600fd
AS
417
418 memcpy(buffer, &cpkt, sizeof(cpkt));
419 memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
420 buffer[buffer_len - 1] = 0;
421
4e28976e 422 send_control_msg(vser, buffer, buffer_len);
7267c094 423 g_free(buffer);
160600fd
AS
424 }
425
6663a195 426 if (port->host_connected) {
4e28976e 427 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
6663a195
AS
428 }
429
98b19252
AS
430 /*
431 * When the guest has asked us for this information it means
432 * the guest is all setup and has its virtqueues
433 * initialised. If some app is interested in knowing about
434 * this event, let it know.
435 */
f82e35e3
AL
436 if (vsc->guest_ready) {
437 vsc->guest_ready(port);
98b19252
AS
438 }
439 break;
6663a195
AS
440
441 case VIRTIO_CONSOLE_PORT_OPEN:
442 port->guest_connected = cpkt.value;
b2c1394a 443 if (vsc->set_guest_connected) {
6663a195 444 /* Send the guest opened notification if an app is interested */
b2c1394a 445 vsc->set_guest_connected(port, cpkt.value);
6663a195
AS
446 }
447 break;
98b19252
AS
448 }
449}
450
451static void control_in(VirtIODevice *vdev, VirtQueue *vq)
452{
453}
454
455static void control_out(VirtIODevice *vdev, VirtQueue *vq)
456{
51b19ebe 457 VirtQueueElement *elem;
98b19252 458 VirtIOSerial *vser;
e61da14d
AS
459 uint8_t *buf;
460 size_t len;
98b19252 461
76017fd2 462 vser = VIRTIO_SERIAL(vdev);
98b19252 463
e61da14d
AS
464 len = 0;
465 buf = NULL;
51b19ebe 466 for (;;) {
45270ad8 467 size_t cur_len;
e61da14d 468
51b19ebe
PB
469 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
470 if (!elem) {
471 break;
472 }
473
474 cur_len = iov_size(elem->out_sg, elem->out_num);
e61da14d
AS
475 /*
476 * Allocate a new buf only if we didn't have one previously or
477 * if the size of the buf differs
478 */
479 if (cur_len > len) {
7267c094 480 g_free(buf);
e61da14d 481
7267c094 482 buf = g_malloc(cur_len);
e61da14d
AS
483 len = cur_len;
484 }
51b19ebe 485 iov_to_buf(elem->out_sg, elem->out_num, 0, buf, cur_len);
e61da14d 486
45270ad8 487 handle_control_message(vser, buf, cur_len);
51b19ebe
PB
488 virtqueue_push(vq, elem, 0);
489 g_free(elem);
98b19252 490 }
7267c094 491 g_free(buf);
98b19252
AS
492 virtio_notify(vdev, vq);
493}
494
495/* Guest wrote something to some port. */
496static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
497{
498 VirtIOSerial *vser;
a69c7600 499 VirtIOSerialPort *port;
98b19252 500
76017fd2 501 vser = VIRTIO_SERIAL(vdev);
a69c7600 502 port = find_port_by_vq(vser, vq);
98b19252 503
03ecd2c8 504 if (!port || !port->host_connected) {
6bff8656
AS
505 discard_vq_data(vq, vdev);
506 return;
507 }
e9b382b0
AS
508
509 if (!port->throttled) {
510 do_flush_queued_data(port, vq, vdev);
9ed7b059
AS
511 return;
512 }
98b19252
AS
513}
514
515static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
516{
4add73aa
AS
517 /*
518 * Users of virtio-serial would like to know when guest becomes
519 * writable again -- i.e. if a vq had stuff queued up and the
520 * guest wasn't reading at all, the host would not be able to
521 * write to the vq anymore. Once the guest reads off something,
522 * we can start queueing things up again. However, this call is
523 * made for each buffer addition by the guest -- even though free
524 * buffers existed prior to the current buffer addition. This is
525 * done so as not to maintain previous state, which will need
526 * additional live-migration-related changes.
527 */
528 VirtIOSerial *vser;
529 VirtIOSerialPort *port;
530 VirtIOSerialPortClass *vsc;
531
532 vser = VIRTIO_SERIAL(vdev);
533 port = find_port_by_vq(vser, vq);
534
535 if (!port) {
536 return;
537 }
538 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
539
540 /*
541 * If guest_connected is false, this call is being made by the
542 * early-boot queueing up of descriptors, which is just noise for
543 * the host apps -- don't disturb them in that case.
544 */
545 if (port->guest_connected && port->host_connected && vsc->guest_writable) {
546 vsc->guest_writable(port);
547 }
98b19252
AS
548}
549
9d5b731d
JW
550static uint64_t get_features(VirtIODevice *vdev, uint64_t features,
551 Error **errp)
98b19252 552{
306eb457
AS
553 VirtIOSerial *vser;
554
76017fd2 555 vser = VIRTIO_SERIAL(vdev);
306eb457 556
a06b1dae 557 features |= vser->host_features;
5e52e5f9 558 if (vser->bus.max_nr_ports > 1) {
0cd09c3a 559 virtio_add_feature(&features, VIRTIO_CONSOLE_F_MULTIPORT);
ee4d45be 560 }
98b19252
AS
561 return features;
562}
563
564/* Guest requested config info */
565static void get_config(VirtIODevice *vdev, uint8_t *config_data)
566{
08f432aa
DG
567 VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
568 struct virtio_console_config *config =
569 (struct virtio_console_config *)config_data;
98b19252 570
08f432aa
DG
571 config->cols = 0;
572 config->rows = 0;
573 config->max_nr_ports = virtio_tswap32(vdev,
574 vser->serial.max_virtserial_ports);
98b19252
AS
575}
576
09da01c3
SS
577/* Guest sent new config info */
578static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
579{
580 VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
581 struct virtio_console_config *config =
582 (struct virtio_console_config *)config_data;
09da01c3
SS
583 VirtIOSerialPort *port = find_first_connected_console(vser);
584 VirtIOSerialPortClass *vsc;
d434e5ac 585 uint8_t emerg_wr_lo;
09da01c3 586
d434e5ac 587 if (!virtio_has_feature(vser->host_features,
588 VIRTIO_CONSOLE_F_EMERG_WRITE) || !config->emerg_wr) {
09da01c3
SS
589 return;
590 }
d434e5ac 591
592 emerg_wr_lo = le32_to_cpu(config->emerg_wr);
09da01c3
SS
593 /* Make sure we don't misdetect an emergency write when the guest
594 * does a short config write after an emergency write. */
595 config->emerg_wr = 0;
596 if (!port) {
597 return;
598 }
599 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
600 (void)vsc->have_data(port, &emerg_wr_lo, 1);
601}
602
43997225
AS
603static void guest_reset(VirtIOSerial *vser)
604{
605 VirtIOSerialPort *port;
606 VirtIOSerialPortClass *vsc;
607
608 QTAILQ_FOREACH(port, &vser->ports, next) {
609 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
d4c19cde
SH
610
611 discard_throttle_data(port);
612
43997225
AS
613 if (port->guest_connected) {
614 port->guest_connected = false;
b2c1394a
HG
615 if (vsc->set_guest_connected) {
616 vsc->set_guest_connected(port, false);
617 }
43997225
AS
618 }
619 }
620}
621
62a9fbf7
AL
622static void set_status(VirtIODevice *vdev, uint8_t status)
623{
624 VirtIOSerial *vser;
625 VirtIOSerialPort *port;
626
76017fd2 627 vser = VIRTIO_SERIAL(vdev);
62a9fbf7
AL
628 port = find_port_by_id(vser, 0);
629
630 if (port && !use_multiport(port->vser)
631 && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
632 /*
633 * Non-multiport guests won't be able to tell us guest
634 * open/close status. Such guests can only have a port at id
635 * 0, so set guest_connected for such ports as soon as guest
636 * is up.
637 */
638 port->guest_connected = true;
639 }
43997225
AS
640 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
641 guest_reset(vser);
642 }
55289fb0
PB
643
644 QTAILQ_FOREACH(port, &vser->ports, next) {
645 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
646 if (vsc->enable_backend) {
647 vsc->enable_backend(port, vdev->vm_running);
648 }
649 }
43997225
AS
650}
651
652static void vser_reset(VirtIODevice *vdev)
653{
654 VirtIOSerial *vser;
655
76017fd2 656 vser = VIRTIO_SERIAL(vdev);
43997225 657 guest_reset(vser);
62a9fbf7
AL
658}
659
13c6855a
GK
660static void virtio_serial_save_device(VirtIODevice *vdev, QEMUFile *f)
661{
662 VirtIOSerial *s = VIRTIO_SERIAL(vdev);
6663a195
AS
663 VirtIOSerialPort *port;
664 uint32_t nr_active_ports;
5c1c9bb2 665 unsigned int i, max_nr_ports;
08f432aa 666 struct virtio_console_config config;
98b19252 667
08f432aa
DG
668 /* The config space (ignored on the far end in current versions) */
669 get_config(vdev, (uint8_t *)&config);
670 qemu_put_be16s(f, &config.cols);
671 qemu_put_be16s(f, &config.rows);
672 qemu_put_be32s(f, &config.max_nr_ports);
055b889f
AS
673
674 /* The ports map */
f2f6e00b 675 max_nr_ports = s->serial.max_virtserial_ports;
7b9a27cd 676 for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
055b889f
AS
677 qemu_put_be32s(f, &s->ports_map[i]);
678 }
6663a195 679
055b889f 680 /* Ports */
e245795b 681
6663a195 682 nr_active_ports = 0;
e245795b 683 QTAILQ_FOREACH(port, &s->ports, next) {
6663a195 684 nr_active_ports++;
e245795b 685 }
6663a195
AS
686
687 qemu_put_be32s(f, &nr_active_ports);
688
689 /*
690 * Items in struct VirtIOSerialPort.
691 */
692 QTAILQ_FOREACH(port, &s->ports, next) {
37f95bf3
AS
693 uint32_t elem_popped;
694
6663a195
AS
695 qemu_put_be32s(f, &port->id);
696 qemu_put_byte(f, port->guest_connected);
31abe21f 697 qemu_put_byte(f, port->host_connected);
37f95bf3
AS
698
699 elem_popped = 0;
51b19ebe 700 if (port->elem) {
37f95bf3
AS
701 elem_popped = 1;
702 }
703 qemu_put_be32s(f, &elem_popped);
704 if (elem_popped) {
705 qemu_put_be32s(f, &port->iov_idx);
706 qemu_put_be64s(f, &port->iov_offset);
ab281c17 707 qemu_put_virtqueue_element(f, port->elem);
37f95bf3 708 }
6663a195 709 }
98b19252
AS
710}
711
80dcfb85
AL
712static void virtio_serial_post_load_timer_cb(void *opaque)
713{
c3587ca1 714 uint32_t i;
76017fd2 715 VirtIOSerial *s = VIRTIO_SERIAL(opaque);
80dcfb85
AL
716 VirtIOSerialPort *port;
717 uint8_t host_connected;
bc6b815d 718 VirtIOSerialPortClass *vsc;
80dcfb85 719
bdb917bf
AS
720 if (!s->post_load) {
721 return;
722 }
723 for (i = 0 ; i < s->post_load->nr_active_ports; ++i) {
724 port = s->post_load->connected[i].port;
725 host_connected = s->post_load->connected[i].host_connected;
80dcfb85
AL
726 if (host_connected != port->host_connected) {
727 /*
728 * We have to let the guest know of the host connection
729 * status change
730 */
4e28976e 731 send_control_event(s, port->id, VIRTIO_CONSOLE_PORT_OPEN,
80dcfb85
AL
732 port->host_connected);
733 }
bc6b815d
AL
734 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
735 if (vsc->set_guest_connected) {
736 vsc->set_guest_connected(port, port->guest_connected);
737 }
80dcfb85 738 }
bdb917bf 739 g_free(s->post_load->connected);
bdf4c4ec 740 timer_del(s->post_load->timer);
bc72ad67 741 timer_free(s->post_load->timer);
bdb917bf
AS
742 g_free(s->post_load);
743 s->post_load = NULL;
80dcfb85
AL
744}
745
71945ae1 746static int fetch_active_ports_list(QEMUFile *f,
2e575a86
AS
747 VirtIOSerial *s, uint32_t nr_active_ports)
748{
8607f5c3 749 VirtIODevice *vdev = VIRTIO_DEVICE(s);
2e575a86
AS
750 uint32_t i;
751
bdb917bf
AS
752 s->post_load = g_malloc0(sizeof(*s->post_load));
753 s->post_load->nr_active_ports = nr_active_ports;
754 s->post_load->connected =
755 g_malloc0(sizeof(*s->post_load->connected) * nr_active_ports);
756
bc72ad67 757 s->post_load->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
bdb917bf
AS
758 virtio_serial_post_load_timer_cb,
759 s);
2e575a86
AS
760
761 /* Items in struct VirtIOSerialPort */
762 for (i = 0; i < nr_active_ports; i++) {
763 VirtIOSerialPort *port;
71945ae1 764 uint32_t elem_popped;
2e575a86
AS
765 uint32_t id;
766
767 id = qemu_get_be32(f);
768 port = find_port_by_id(s, id);
769 if (!port) {
770 return -EINVAL;
771 }
772
773 port->guest_connected = qemu_get_byte(f);
bdb917bf
AS
774 s->post_load->connected[i].port = port;
775 s->post_load->connected[i].host_connected = qemu_get_byte(f);
2e575a86 776
71945ae1
DDAG
777 qemu_get_be32s(f, &elem_popped);
778 if (elem_popped) {
779 qemu_get_be32s(f, &port->iov_idx);
780 qemu_get_be64s(f, &port->iov_offset);
2e575a86 781
71945ae1 782 port->elem =
8607f5c3 783 qemu_get_virtqueue_element(vdev, f, sizeof(VirtQueueElement));
2e575a86 784
71945ae1
DDAG
785 /*
786 * Port was throttled on source machine. Let's
787 * unthrottle it here so data starts flowing again.
788 */
789 virtio_serial_throttle_port(port, false);
2e575a86
AS
790 }
791 }
bc72ad67 792 timer_mod(s->post_load->timer, 1);
2e575a86
AS
793 return 0;
794}
795
13c6855a
GK
796static int virtio_serial_load_device(VirtIODevice *vdev, QEMUFile *f,
797 int version_id)
798{
799 VirtIOSerial *s = VIRTIO_SERIAL(vdev);
800 uint32_t max_nr_ports, nr_active_ports, ports_map;
801 unsigned int i;
802 int ret;
803 uint32_t tmp;
98b19252 804
e38e943a
AG
805 /* Unused */
806 qemu_get_be16s(f, (uint16_t *) &tmp);
807 qemu_get_be16s(f, (uint16_t *) &tmp);
808 qemu_get_be32s(f, &tmp);
98b19252 809
f2f6e00b 810 max_nr_ports = s->serial.max_virtserial_ports;
7b9a27cd 811 for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
f83ccb3e 812 qemu_get_be32s(f, &ports_map);
055b889f 813
f83ccb3e 814 if (ports_map != s->ports_map[i]) {
055b889f
AS
815 /*
816 * Ports active on source and destination don't
817 * match. Fail migration.
818 */
055b889f
AS
819 return -EINVAL;
820 }
e245795b
AS
821 }
822
6663a195
AS
823 qemu_get_be32s(f, &nr_active_ports);
824
2e575a86 825 if (nr_active_ports) {
71945ae1 826 ret = fetch_active_ports_list(f, s, nr_active_ports);
2e575a86
AS
827 if (ret) {
828 return ret;
37f95bf3 829 }
6663a195 830 }
98b19252
AS
831 return 0;
832}
833
834static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
835
3cb75a7c
PB
836static Property virtser_props[] = {
837 DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
838 DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
839 DEFINE_PROP_END_OF_LIST()
840};
841
0d936928
AL
842#define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
843#define VIRTIO_SERIAL_BUS(obj) \
844 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
845
846static void virtser_bus_class_init(ObjectClass *klass, void *data)
847{
848 BusClass *k = BUS_CLASS(klass);
849 k->print_dev = virtser_bus_dev_print;
850}
851
852static const TypeInfo virtser_bus_info = {
853 .name = TYPE_VIRTIO_SERIAL_BUS,
854 .parent = TYPE_BUS,
855 .instance_size = sizeof(VirtIOSerialBus),
856 .class_init = virtser_bus_class_init,
98b19252
AS
857};
858
98b19252
AS
859static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
860{
d9eb0be2 861 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(qdev);
98b19252 862
021a1318
MA
863 monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
864 indent, "", port->id,
865 port->guest_connected ? "on" : "off",
866 port->host_connected ? "on" : "off",
867 port->throttled ? "on" : "off");
98b19252
AS
868}
869
055b889f
AS
870/* This function is only used if a port id is not provided by the user */
871static uint32_t find_free_port_id(VirtIOSerial *vser)
872{
5c1c9bb2 873 unsigned int i, max_nr_ports;
055b889f 874
f2f6e00b 875 max_nr_ports = vser->serial.max_virtserial_ports;
7b9a27cd 876 for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
bd2a8884 877 uint32_t map, zeroes;
055b889f
AS
878
879 map = vser->ports_map[i];
bd2a8884
SH
880 zeroes = ctz32(~map);
881 if (zeroes != 32) {
882 return zeroes + i * 32;
055b889f
AS
883 }
884 }
885 return VIRTIO_CONSOLE_BAD_ID;
886}
887
888static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
889{
890 unsigned int i;
891
892 i = port_id / 32;
893 vser->ports_map[i] |= 1U << (port_id % 32);
894}
895
896static void add_port(VirtIOSerial *vser, uint32_t port_id)
897{
898 mark_port_added(vser, port_id);
4e28976e 899 send_control_event(vser, port_id, VIRTIO_CONSOLE_PORT_ADD, 1);
055b889f
AS
900}
901
902static void remove_port(VirtIOSerial *vser, uint32_t port_id)
903{
9ed7b059 904 VirtIOSerialPort *port;
055b889f 905
57d84cf3
AS
906 /*
907 * Don't mark port 0 removed -- we explicitly reserve it for
908 * backward compat with older guests, ensure a virtconsole device
909 * unplug retains the reservation.
910 */
911 if (port_id) {
912 unsigned int i;
913
914 i = port_id / 32;
915 vser->ports_map[i] &= ~(1U << (port_id % 32));
916 }
055b889f 917
9ed7b059 918 port = find_port_by_id(vser, port_id);
91bdd1cf
AS
919 /*
920 * This function is only called from qdev's unplug callback; if we
921 * get a NULL port here, we're in trouble.
922 */
923 assert(port);
924
9ed7b059 925 /* Flush out any unconsumed buffers first */
d4c19cde 926 discard_throttle_data(port);
76017fd2 927 discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
9ed7b059 928
4e28976e 929 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_REMOVE, 1);
055b889f
AS
930}
931
2ef66625 932static void virtser_port_device_realize(DeviceState *dev, Error **errp)
98b19252 933{
2ef66625 934 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
f82e35e3 935 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
2ef66625
AF
936 VirtIOSerialBus *bus = VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev));
937 int max_nr_ports;
98b19252 938 bool plugging_port0;
2ef66625 939 Error *err = NULL;
98b19252
AS
940
941 port->vser = bus->vser;
199646d8 942 port->bh = qemu_bh_new(flush_queued_data_bh, port);
98b19252 943
f82e35e3 944 assert(vsc->have_data);
03ecd2c8 945
98b19252
AS
946 /*
947 * Is the first console port we're seeing? If so, put it up at
948 * location 0. This is done for backward compatibility (old
949 * kernel, new qemu).
950 */
f82e35e3 951 plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
98b19252 952
055b889f 953 if (find_port_by_id(port->vser, port->id)) {
2ef66625
AF
954 error_setg(errp, "virtio-serial-bus: A port already exists at id %u",
955 port->id);
956 return;
98b19252 957 }
98b19252 958
7eb73114 959 if (port->name != NULL && find_port_by_name(port->name)) {
d0a0bfe6
AS
960 error_setg(errp, "virtio-serial-bus: A port already exists by name %s",
961 port->name);
962 return;
963 }
964
055b889f
AS
965 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
966 if (plugging_port0) {
967 port->id = 0;
968 } else {
969 port->id = find_free_port_id(port->vser);
970 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
2ef66625
AF
971 error_setg(errp, "virtio-serial-bus: Maximum port limit for "
972 "this device reached");
973 return;
055b889f
AS
974 }
975 }
976 }
977
f2f6e00b 978 max_nr_ports = port->vser->serial.max_virtserial_ports;
5c1c9bb2 979 if (port->id >= max_nr_ports) {
2ef66625
AF
980 error_setg(errp, "virtio-serial-bus: Out-of-range port id specified, "
981 "max. allowed: %u", max_nr_ports - 1);
982 return;
055b889f
AS
983 }
984
2ef66625
AF
985 vsc->realize(dev, &err);
986 if (err != NULL) {
987 error_propagate(errp, err);
988 return;
98b19252
AS
989 }
990
51b19ebe 991 port->elem = NULL;
0ddef15b
IM
992}
993
994static void virtser_port_device_plug(HotplugHandler *hotplug_dev,
995 DeviceState *dev, Error **errp)
996{
997 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
f1925dff 998
98b19252
AS
999 QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
1000 port->ivq = port->vser->ivqs[port->id];
1001 port->ovq = port->vser->ovqs[port->id];
1002
055b889f
AS
1003 add_port(port->vser, port->id);
1004
98b19252 1005 /* Send an update to the guest about this new port added */
0ddef15b 1006 virtio_notify_config(VIRTIO_DEVICE(hotplug_dev));
98b19252
AS
1007}
1008
2ef66625 1009static void virtser_port_device_unrealize(DeviceState *dev, Error **errp)
98b19252 1010{
2ef66625
AF
1011 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
1012 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
98b19252
AS
1013 VirtIOSerial *vser = port->vser;
1014
199646d8 1015 qemu_bh_delete(port->bh);
055b889f 1016 remove_port(port->vser, port->id);
f146ec9a 1017
98b19252
AS
1018 QTAILQ_REMOVE(&vser->ports, port, next);
1019
2ef66625
AF
1020 if (vsc->unrealize) {
1021 vsc->unrealize(dev, errp);
a15bb0d6 1022 }
98b19252
AS
1023}
1024
86346244 1025static void virtio_serial_device_realize(DeviceState *dev, Error **errp)
98b19252 1026{
86346244 1027 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
b1a20c3f 1028 VirtIOSerial *vser = VIRTIO_SERIAL(dev);
5ab4bb59 1029 uint32_t i, max_supported_ports;
a06b1dae 1030 size_t config_size = sizeof(struct virtio_console_config);
98b19252 1031
34b95b2c 1032 if (!vser->serial.max_virtserial_ports) {
86346244
AF
1033 error_setg(errp, "Maximum number of serial ports not specified");
1034 return;
34b95b2c 1035 }
98b19252 1036
5ab4bb59 1037 /* Each port takes 2 queues, and one pair is for the control queue */
87b3bd1c 1038 max_supported_ports = VIRTIO_QUEUE_MAX / 2 - 1;
5ab4bb59 1039
34b95b2c 1040 if (vser->serial.max_virtserial_ports > max_supported_ports) {
86346244
AF
1041 error_setg(errp, "maximum ports supported: %u", max_supported_ports);
1042 return;
5ab4bb59
AS
1043 }
1044
a06b1dae
SS
1045 if (!virtio_has_feature(vser->host_features,
1046 VIRTIO_CONSOLE_F_EMERG_WRITE)) {
1047 config_size = offsetof(struct virtio_console_config, emerg_wr);
1048 }
34b95b2c 1049 virtio_init(vdev, "virtio-serial", VIRTIO_ID_CONSOLE,
a06b1dae 1050 config_size);
98b19252
AS
1051
1052 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
fb17dfe0 1053 qbus_create_inplace(&vser->bus, sizeof(vser->bus), TYPE_VIRTIO_SERIAL_BUS,
b1a20c3f 1054 dev, vdev->bus_name);
0ddef15b 1055 qbus_set_hotplug_handler(BUS(&vser->bus), DEVICE(vser), errp);
5e52e5f9 1056 vser->bus.vser = vser;
98b19252
AS
1057 QTAILQ_INIT(&vser->ports);
1058
34b95b2c
FK
1059 vser->bus.max_nr_ports = vser->serial.max_virtserial_ports;
1060 vser->ivqs = g_malloc(vser->serial.max_virtserial_ports
1061 * sizeof(VirtQueue *));
1062 vser->ovqs = g_malloc(vser->serial.max_virtserial_ports
1063 * sizeof(VirtQueue *));
98b19252
AS
1064
1065 /* Add a queue for host to guest transfers for port 0 (backward compat) */
1066 vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
1067 /* Add a queue for guest to host transfers for port 0 (backward compat) */
1068 vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
1069
a01a9cb8
AS
1070 /* TODO: host to guest notifications can get dropped
1071 * if the queue fills up. Implement queueing in host,
1072 * this might also make it possible to reduce the control
1073 * queue size: as guest preposts buffers there,
1074 * this will save 4Kbyte of guest memory per entry. */
1075
98b19252 1076 /* control queue: host to guest */
a01a9cb8 1077 vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
98b19252 1078 /* control queue: guest to host */
a01a9cb8 1079 vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
98b19252 1080
5e52e5f9 1081 for (i = 1; i < vser->bus.max_nr_ports; i++) {
98b19252
AS
1082 /* Add a per-port queue for host to guest transfers */
1083 vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
1084 /* Add a per-per queue for guest to host transfers */
1085 vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
1086 }
1087
7b9a27cd 1088 vser->ports_map = g_malloc0((DIV_ROUND_UP(vser->serial.max_virtserial_ports, 32))
a132a679 1089 * sizeof(vser->ports_map[0]));
98b19252
AS
1090 /*
1091 * Reserve location 0 for a console port for backward compat
1092 * (old kernel, new qemu)
1093 */
055b889f 1094 mark_port_added(vser, 0);
98b19252 1095
bdb917bf
AS
1096 vser->post_load = NULL;
1097
a1857ad1 1098 QLIST_INSERT_HEAD(&vserdevices.devices, vser, next);
8b53a865 1099}
f82e35e3 1100
39bffca2
AL
1101static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
1102{
1103 DeviceClass *k = DEVICE_CLASS(klass);
2ef66625 1104
125ee0ed 1105 set_bit(DEVICE_CATEGORY_INPUT, k->categories);
0d936928 1106 k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
2ef66625
AF
1107 k->realize = virtser_port_device_realize;
1108 k->unrealize = virtser_port_device_unrealize;
bce54474 1109 k->props = virtser_props;
39bffca2
AL
1110}
1111
8c43a6f0 1112static const TypeInfo virtio_serial_port_type_info = {
f82e35e3
AL
1113 .name = TYPE_VIRTIO_SERIAL_PORT,
1114 .parent = TYPE_DEVICE,
1115 .instance_size = sizeof(VirtIOSerialPort),
1116 .abstract = true,
1117 .class_size = sizeof(VirtIOSerialPortClass),
39bffca2 1118 .class_init = virtio_serial_port_class_init,
f82e35e3
AL
1119};
1120
306ec6c3 1121static void virtio_serial_device_unrealize(DeviceState *dev, Error **errp)
2cd2b016 1122{
306ec6c3
AF
1123 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1124 VirtIOSerial *vser = VIRTIO_SERIAL(dev);
2cd2b016 1125
a1857ad1
AS
1126 QLIST_REMOVE(vser, next);
1127
2cd2b016
FK
1128 g_free(vser->ivqs);
1129 g_free(vser->ovqs);
1130 g_free(vser->ports_map);
1131 if (vser->post_load) {
1132 g_free(vser->post_load->connected);
bc72ad67
AB
1133 timer_del(vser->post_load->timer);
1134 timer_free(vser->post_load->timer);
2cd2b016
FK
1135 g_free(vser->post_load);
1136 }
f811f970
LP
1137
1138 qbus_set_hotplug_handler(BUS(&vser->bus), NULL, errp);
1139
6a1a8cc7 1140 virtio_cleanup(vdev);
2cd2b016
FK
1141}
1142
42e6c039 1143/* Note: 'console' is used for backwards compatibility */
97eed24f
HP
1144static const VMStateDescription vmstate_virtio_console = {
1145 .name = "virtio-console",
1146 .minimum_version_id = 3,
1147 .version_id = 3,
1148 .fields = (VMStateField[]) {
1149 VMSTATE_VIRTIO_DEVICE,
1150 VMSTATE_END_OF_LIST()
1151 },
1152};
42e6c039 1153
2cd2b016 1154static Property virtio_serial_properties[] = {
448777c4
SZ
1155 DEFINE_PROP_UINT32("max_ports", VirtIOSerial, serial.max_virtserial_ports,
1156 31),
a06b1dae
SS
1157 DEFINE_PROP_BIT64("emergency-write", VirtIOSerial, host_features,
1158 VIRTIO_CONSOLE_F_EMERG_WRITE, true),
2cd2b016
FK
1159 DEFINE_PROP_END_OF_LIST(),
1160};
1161
1162static void virtio_serial_class_init(ObjectClass *klass, void *data)
1163{
1164 DeviceClass *dc = DEVICE_CLASS(klass);
1165 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
0ddef15b 1166 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
86346244 1167
a1857ad1
AS
1168 QLIST_INIT(&vserdevices.devices);
1169
2cd2b016 1170 dc->props = virtio_serial_properties;
42e6c039 1171 dc->vmsd = &vmstate_virtio_console;
125ee0ed 1172 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
86346244 1173 vdc->realize = virtio_serial_device_realize;
306ec6c3 1174 vdc->unrealize = virtio_serial_device_unrealize;
2cd2b016
FK
1175 vdc->get_features = get_features;
1176 vdc->get_config = get_config;
09da01c3 1177 vdc->set_config = set_config;
2cd2b016
FK
1178 vdc->set_status = set_status;
1179 vdc->reset = vser_reset;
13c6855a
GK
1180 vdc->save = virtio_serial_save_device;
1181 vdc->load = virtio_serial_load_device;
0ddef15b
IM
1182 hc->plug = virtser_port_device_plug;
1183 hc->unplug = qdev_simple_device_unplug_cb;
2cd2b016
FK
1184}
1185
1186static const TypeInfo virtio_device_info = {
1187 .name = TYPE_VIRTIO_SERIAL,
1188 .parent = TYPE_VIRTIO_DEVICE,
1189 .instance_size = sizeof(VirtIOSerial),
1190 .class_init = virtio_serial_class_init,
0ddef15b
IM
1191 .interfaces = (InterfaceInfo[]) {
1192 { TYPE_HOTPLUG_HANDLER },
1193 { }
1194 }
2cd2b016
FK
1195};
1196
83f7d43a 1197static void virtio_serial_register_types(void)
f82e35e3 1198{
0d936928 1199 type_register_static(&virtser_bus_info);
f82e35e3 1200 type_register_static(&virtio_serial_port_type_info);
2cd2b016 1201 type_register_static(&virtio_device_info);
f82e35e3
AL
1202}
1203
83f7d43a 1204type_init(virtio_serial_register_types)