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