21
#include "qemu/osdep.h"
22
#include "qapi/error.h"
24
#include "qemu/main-loop.h"
25
#include "qemu/module.h"
26
#include "migration/qemu-file-types.h"
27
#include "monitor/monitor.h"
28
#include "qemu/error-report.h"
29
#include "qemu/queue.h"
30
#include "hw/qdev-properties.h"
32
#include "hw/virtio/virtio-serial.h"
33
#include "hw/virtio/virtio-access.h"
35
static struct VirtIOSerialDevices {
36
QLIST_HEAD(, VirtIOSerial) devices;
39
static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
41
VirtIOSerialPort *port;
43
if (id == VIRTIO_CONSOLE_BAD_ID) {
47
QTAILQ_FOREACH(port, &vser->ports, next) {
54
static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
56
VirtIOSerialPort *port;
58
QTAILQ_FOREACH(port, &vser->ports, next) {
59
if (port->ivq == vq || port->ovq == vq)
65
static VirtIOSerialPort *find_port_by_name(char *name)
69
QLIST_FOREACH(vser, &vserdevices.devices, next) {
70
VirtIOSerialPort *port;
72
QTAILQ_FOREACH(port, &vser->ports, next) {
73
if (port->name && !strcmp(port->name, name)) {
81
static VirtIOSerialPort *find_first_connected_console(VirtIOSerial *vser)
83
VirtIOSerialPort *port;
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) {
94
static bool use_multiport(VirtIOSerial *vser)
96
VirtIODevice *vdev = VIRTIO_DEVICE(vser);
97
return virtio_vdev_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT);
100
static size_t write_to_port(VirtIOSerialPort *port,
101
const uint8_t *buf, size_t size)
103
VirtQueueElement *elem;
108
if (!virtio_queue_ready(vq)) {
113
while (offset < size) {
116
elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
121
len = iov_from_buf(elem->in_sg, elem->in_num, 0,
122
buf + offset, size - offset);
125
virtqueue_push(vq, elem, len);
129
virtio_notify(VIRTIO_DEVICE(port->vser), vq);
133
static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
135
VirtQueueElement *elem;
137
if (!virtio_queue_ready(vq)) {
141
elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
145
virtqueue_push(vq, elem, 0);
148
virtio_notify(vdev, vq);
151
static void discard_throttle_data(VirtIOSerialPort *port)
154
virtqueue_detach_element(port->ovq, port->elem, 0);
160
static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
163
VirtIOSerialPortClass *vsc;
166
assert(virtio_queue_ready(vq));
168
vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
170
while (!port->throttled) {
175
port->elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
180
port->iov_offset = 0;
183
for (i = port->iov_idx; i < port->elem->out_num; i++) {
187
buf_size = port->elem->out_sg[i].iov_len - port->iov_offset;
188
ret = vsc->have_data(port,
189
port->elem->out_sg[i].iov_base
195
if (port->throttled) {
198
port->iov_offset += ret;
202
port->iov_offset = 0;
204
if (port->throttled) {
207
virtqueue_push(vq, port->elem, 0);
211
virtio_notify(vdev, vq);
214
static void flush_queued_data(VirtIOSerialPort *port)
218
if (!virtio_queue_ready(port->ovq)) {
221
do_flush_queued_data(port, port->ovq, VIRTIO_DEVICE(port->vser));
224
static size_t send_control_msg(VirtIOSerial *vser, void *buf, size_t len)
226
VirtQueueElement *elem;
230
if (!virtio_queue_ready(vq)) {
234
elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
240
iov_from_buf(elem->in_sg, elem->in_num, 0, buf, len);
242
virtqueue_push(vq, elem, len);
243
virtio_notify(VIRTIO_DEVICE(vser), vq);
249
static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id,
250
uint16_t event, uint16_t value)
252
VirtIODevice *vdev = VIRTIO_DEVICE(vser);
253
struct virtio_console_control cpkt;
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);
259
trace_virtio_serial_send_control_event(port_id, event, value);
260
return send_control_msg(vser, &cpkt, sizeof(cpkt));
264
int virtio_serial_open(VirtIOSerialPort *port)
267
if (port->host_connected) {
271
port->host_connected = true;
272
send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
277
int virtio_serial_close(VirtIOSerialPort *port)
279
port->host_connected = false;
284
port->throttled = false;
285
discard_throttle_data(port);
286
discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
288
send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 0);
294
ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
297
if (!port || !port->host_connected || !port->guest_connected) {
300
return write_to_port(port, buf, size);
307
size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
309
VirtIODevice *vdev = VIRTIO_DEVICE(port->vser);
310
VirtQueue *vq = port->ivq;
313
if (!virtio_queue_ready(vq) ||
314
!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) ||
315
virtio_queue_empty(vq)) {
318
if (use_multiport(port->vser) && !port->guest_connected) {
321
virtqueue_get_avail_bytes(vq, &bytes, NULL, 4096, 0);
325
static void flush_queued_data_bh(void *opaque)
327
VirtIOSerialPort *port = opaque;
329
flush_queued_data(port);
332
void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
338
trace_virtio_serial_throttle_port(port->id, throttle);
339
port->throttled = throttle;
343
qemu_bh_schedule(port->bh);
347
static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
349
VirtIODevice *vdev = VIRTIO_DEVICE(vser);
350
struct VirtIOSerialPort *port;
351
VirtIOSerialPortClass *vsc;
352
struct virtio_console_control cpkt, *gcpkt;
358
if (len < sizeof(cpkt)) {
363
cpkt.event = virtio_lduw_p(vdev, &gcpkt->event);
364
cpkt.value = virtio_lduw_p(vdev, &gcpkt->value);
366
trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
368
if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
370
error_report("virtio-serial-bus: Guest failure in adding device %s",
371
vser->bus.qbus.name);
378
QTAILQ_FOREACH(port, &vser->ports, next) {
379
send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_ADD, 1);
384
port = find_port_by_id(vser, virtio_ldl_p(vdev, &gcpkt->id));
386
error_report("virtio-serial-bus: Unexpected port id %u for device %s",
387
virtio_ldl_p(vdev, &gcpkt->id), vser->bus.qbus.name);
391
trace_virtio_serial_handle_control_message_port(port->id);
393
vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
396
case VIRTIO_CONSOLE_PORT_READY:
398
error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
399
port->id, vser->bus.qbus.name);
409
if (vsc->is_console) {
410
send_control_event(vser, port->id, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
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);
418
buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
419
buffer = g_malloc(buffer_len);
421
memcpy(buffer, &cpkt, sizeof(cpkt));
422
memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
423
buffer[buffer_len - 1] = 0;
425
send_control_msg(vser, buffer, buffer_len);
429
if (port->host_connected) {
430
send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
439
if (vsc->guest_ready) {
440
vsc->guest_ready(port);
444
case VIRTIO_CONSOLE_PORT_OPEN:
445
port->guest_connected = cpkt.value;
446
if (vsc->set_guest_connected) {
448
vsc->set_guest_connected(port, cpkt.value);
454
static void control_in(VirtIODevice *vdev, VirtQueue *vq)
458
static void control_out(VirtIODevice *vdev, VirtQueue *vq)
460
VirtQueueElement *elem;
465
vser = VIRTIO_SERIAL(vdev);
472
elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
477
cur_len = iov_size(elem->out_sg, elem->out_num);
485
buf = g_malloc(cur_len);
488
iov_to_buf(elem->out_sg, elem->out_num, 0, buf, cur_len);
490
handle_control_message(vser, buf, cur_len);
491
virtqueue_push(vq, elem, 0);
495
virtio_notify(vdev, vq);
499
static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
502
VirtIOSerialPort *port;
504
vser = VIRTIO_SERIAL(vdev);
505
port = find_port_by_vq(vser, vq);
507
if (!port || !port->host_connected) {
508
discard_vq_data(vq, vdev);
512
if (!port->throttled) {
513
do_flush_queued_data(port, vq, vdev);
518
static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
532
VirtIOSerialPort *port;
533
VirtIOSerialPortClass *vsc;
535
vser = VIRTIO_SERIAL(vdev);
536
port = find_port_by_vq(vser, vq);
541
vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
548
if (port->guest_connected && port->host_connected && vsc->guest_writable) {
549
vsc->guest_writable(port);
553
static uint64_t get_features(VirtIODevice *vdev, uint64_t features,
558
vser = VIRTIO_SERIAL(vdev);
560
features |= vser->host_features;
561
if (vser->bus.max_nr_ports > 1) {
562
virtio_add_feature(&features, VIRTIO_CONSOLE_F_MULTIPORT);
568
static void get_config(VirtIODevice *vdev, uint8_t *config_data)
570
VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
571
struct virtio_console_config *config =
572
(struct virtio_console_config *)config_data;
576
config->max_nr_ports = virtio_tswap32(vdev,
577
vser->serial.max_virtserial_ports);
581
static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
583
VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
584
struct virtio_console_config *config =
585
(struct virtio_console_config *)config_data;
586
VirtIOSerialPort *port = find_first_connected_console(vser);
587
VirtIOSerialPortClass *vsc;
590
if (!virtio_has_feature(vser->host_features,
591
VIRTIO_CONSOLE_F_EMERG_WRITE) || !config->emerg_wr) {
595
emerg_wr_lo = le32_to_cpu(config->emerg_wr);
598
config->emerg_wr = 0;
602
vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
603
(void)vsc->have_data(port, &emerg_wr_lo, 1);
606
static void guest_reset(VirtIOSerial *vser)
608
VirtIOSerialPort *port;
609
VirtIOSerialPortClass *vsc;
611
QTAILQ_FOREACH(port, &vser->ports, next) {
612
vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
614
discard_throttle_data(port);
616
if (port->guest_connected) {
617
port->guest_connected = false;
618
if (vsc->set_guest_connected) {
619
vsc->set_guest_connected(port, false);
625
static void set_status(VirtIODevice *vdev, uint8_t status)
628
VirtIOSerialPort *port;
630
vser = VIRTIO_SERIAL(vdev);
631
port = find_port_by_id(vser, 0);
633
if (port && !use_multiport(port->vser)
634
&& (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
641
port->guest_connected = true;
643
if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
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);
655
static void vser_reset(VirtIODevice *vdev)
659
vser = VIRTIO_SERIAL(vdev);
663
static void virtio_serial_save_device(VirtIODevice *vdev, QEMUFile *f)
665
VirtIOSerial *s = VIRTIO_SERIAL(vdev);
666
VirtIOSerialPort *port;
667
uint32_t nr_active_ports;
668
unsigned int i, max_nr_ports;
669
struct virtio_console_config config;
672
get_config(vdev, (uint8_t *)&config);
673
qemu_put_be16(f, config.cols);
674
qemu_put_be16(f, config.rows);
675
qemu_put_be32(f, config.max_nr_ports);
678
max_nr_ports = s->serial.max_virtserial_ports;
679
for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
680
qemu_put_be32s(f, &s->ports_map[i]);
686
QTAILQ_FOREACH(port, &s->ports, next) {
690
qemu_put_be32s(f, &nr_active_ports);
695
QTAILQ_FOREACH(port, &s->ports, next) {
696
uint32_t elem_popped;
698
qemu_put_be32s(f, &port->id);
699
qemu_put_byte(f, port->guest_connected);
700
qemu_put_byte(f, port->host_connected);
706
qemu_put_be32s(f, &elem_popped);
708
qemu_put_be32s(f, &port->iov_idx);
709
qemu_put_be64s(f, &port->iov_offset);
710
qemu_put_virtqueue_element(vdev, f, port->elem);
715
static void virtio_serial_post_load_timer_cb(void *opaque)
718
VirtIOSerial *s = VIRTIO_SERIAL(opaque);
719
VirtIOSerialPort *port;
720
uint8_t host_connected;
721
VirtIOSerialPortClass *vsc;
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;
729
if (host_connected != port->host_connected) {
734
send_control_event(s, port->id, VIRTIO_CONSOLE_PORT_OPEN,
735
port->host_connected);
737
vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
738
if (vsc->set_guest_connected) {
739
vsc->set_guest_connected(port, port->guest_connected);
742
g_free(s->post_load->connected);
743
timer_free(s->post_load->timer);
744
g_free(s->post_load);
748
static int fetch_active_ports_list(QEMUFile *f,
749
VirtIOSerial *s, uint32_t nr_active_ports)
751
VirtIODevice *vdev = VIRTIO_DEVICE(s);
754
s->post_load = g_malloc0(sizeof(*s->post_load));
755
s->post_load->nr_active_ports = nr_active_ports;
756
s->post_load->connected =
757
g_malloc0(sizeof(*s->post_load->connected) * nr_active_ports);
759
s->post_load->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
760
virtio_serial_post_load_timer_cb,
764
for (i = 0; i < nr_active_ports; i++) {
765
VirtIOSerialPort *port;
766
uint32_t elem_popped;
769
id = qemu_get_be32(f);
770
port = find_port_by_id(s, id);
775
port->guest_connected = qemu_get_byte(f);
776
s->post_load->connected[i].port = port;
777
s->post_load->connected[i].host_connected = qemu_get_byte(f);
779
qemu_get_be32s(f, &elem_popped);
781
qemu_get_be32s(f, &port->iov_idx);
782
qemu_get_be64s(f, &port->iov_offset);
785
qemu_get_virtqueue_element(vdev, f, sizeof(VirtQueueElement));
791
virtio_serial_throttle_port(port, false);
794
timer_mod(s->post_load->timer, 1);
798
static int virtio_serial_load_device(VirtIODevice *vdev, QEMUFile *f,
801
VirtIOSerial *s = VIRTIO_SERIAL(vdev);
802
uint32_t max_nr_ports, nr_active_ports, ports_map;
808
qemu_get_be16s(f, (uint16_t *) &tmp);
809
qemu_get_be16s(f, (uint16_t *) &tmp);
810
qemu_get_be32s(f, &tmp);
812
max_nr_ports = s->serial.max_virtserial_ports;
813
for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
814
qemu_get_be32s(f, &ports_map);
816
if (ports_map != s->ports_map[i]) {
825
qemu_get_be32s(f, &nr_active_ports);
827
if (nr_active_ports) {
828
ret = fetch_active_ports_list(f, s, nr_active_ports);
836
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
838
static Property virtser_props[] = {
839
DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
840
DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
841
DEFINE_PROP_END_OF_LIST()
844
static void virtser_bus_class_init(ObjectClass *klass, void *data)
846
BusClass *k = BUS_CLASS(klass);
847
k->print_dev = virtser_bus_dev_print;
850
static const TypeInfo virtser_bus_info = {
851
.name = TYPE_VIRTIO_SERIAL_BUS,
853
.instance_size = sizeof(VirtIOSerialBus),
854
.class_init = virtser_bus_class_init,
857
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
859
VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(qdev);
861
monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
862
indent, "", port->id,
863
port->guest_connected ? "on" : "off",
864
port->host_connected ? "on" : "off",
865
port->throttled ? "on" : "off");
869
static uint32_t find_free_port_id(VirtIOSerial *vser)
871
unsigned int i, max_nr_ports;
873
max_nr_ports = vser->serial.max_virtserial_ports;
874
for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
875
uint32_t map, zeroes;
877
map = vser->ports_map[i];
878
zeroes = ctz32(~map);
880
return zeroes + i * 32;
883
return VIRTIO_CONSOLE_BAD_ID;
886
static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
891
vser->ports_map[i] |= 1U << (port_id % 32);
894
static void add_port(VirtIOSerial *vser, uint32_t port_id)
896
mark_port_added(vser, port_id);
897
send_control_event(vser, port_id, VIRTIO_CONSOLE_PORT_ADD, 1);
900
static void remove_port(VirtIOSerial *vser, uint32_t port_id)
902
VirtIOSerialPort *port;
913
vser->ports_map[i] &= ~(1U << (port_id % 32));
916
port = find_port_by_id(vser, port_id);
924
discard_throttle_data(port);
925
discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
927
send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_REMOVE, 1);
930
static void virtser_port_device_realize(DeviceState *dev, Error **errp)
932
VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
933
VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
934
VirtIOSerialBus *bus = VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev));
939
port->vser = bus->vser;
941
assert(vsc->have_data);
948
plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
950
if (find_port_by_id(port->vser, port->id)) {
951
error_setg(errp, "virtio-serial-bus: A port already exists at id %u",
956
if (port->name != NULL && find_port_by_name(port->name)) {
957
error_setg(errp, "virtio-serial-bus: A port already exists by name %s",
962
if (port->id == VIRTIO_CONSOLE_BAD_ID) {
963
if (plugging_port0) {
966
port->id = find_free_port_id(port->vser);
967
if (port->id == VIRTIO_CONSOLE_BAD_ID) {
968
error_setg(errp, "virtio-serial-bus: Maximum port limit for "
969
"this device reached");
975
max_nr_ports = port->vser->serial.max_virtserial_ports;
976
if (port->id >= max_nr_ports) {
977
error_setg(errp, "virtio-serial-bus: Out-of-range port id specified, "
978
"max. allowed: %u", max_nr_ports - 1);
982
vsc->realize(dev, &err);
984
error_propagate(errp, err);
988
port->bh = virtio_bh_new_guarded(dev, flush_queued_data_bh, port);
992
static void virtser_port_device_plug(HotplugHandler *hotplug_dev,
993
DeviceState *dev, Error **errp)
995
VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
997
QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
998
port->ivq = port->vser->ivqs[port->id];
999
port->ovq = port->vser->ovqs[port->id];
1001
add_port(port->vser, port->id);
1004
virtio_notify_config(VIRTIO_DEVICE(hotplug_dev));
1007
static void virtser_port_device_unrealize(DeviceState *dev)
1009
VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
1010
VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
1011
VirtIOSerial *vser = port->vser;
1013
qemu_bh_delete(port->bh);
1014
remove_port(port->vser, port->id);
1016
QTAILQ_REMOVE(&vser->ports, port, next);
1018
if (vsc->unrealize) {
1019
vsc->unrealize(dev);
1023
static void virtio_serial_device_realize(DeviceState *dev, Error **errp)
1025
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1026
VirtIOSerial *vser = VIRTIO_SERIAL(dev);
1027
uint32_t i, max_supported_ports;
1028
size_t config_size = sizeof(struct virtio_console_config);
1030
if (!vser->serial.max_virtserial_ports) {
1031
error_setg(errp, "Maximum number of serial ports not specified");
1036
max_supported_ports = VIRTIO_QUEUE_MAX / 2 - 1;
1038
if (vser->serial.max_virtserial_ports > max_supported_ports) {
1039
error_setg(errp, "maximum ports supported: %u", max_supported_ports);
1043
if (!virtio_has_feature(vser->host_features,
1044
VIRTIO_CONSOLE_F_EMERG_WRITE)) {
1045
config_size = offsetof(struct virtio_console_config, emerg_wr);
1047
virtio_init(vdev, VIRTIO_ID_CONSOLE, config_size);
1050
qbus_init(&vser->bus, sizeof(vser->bus), TYPE_VIRTIO_SERIAL_BUS,
1051
dev, vdev->bus_name);
1052
qbus_set_hotplug_handler(BUS(&vser->bus), OBJECT(vser));
1053
vser->bus.vser = vser;
1054
QTAILQ_INIT(&vser->ports);
1056
vser->bus.max_nr_ports = vser->serial.max_virtserial_ports;
1057
vser->ivqs = g_new(VirtQueue *, vser->serial.max_virtserial_ports);
1058
vser->ovqs = g_new(VirtQueue *, vser->serial.max_virtserial_ports);
1061
vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
1063
vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
1072
vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
1074
vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
1076
for (i = 1; i < vser->bus.max_nr_ports; i++) {
1078
vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
1080
vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
1083
vser->ports_map = g_malloc0((DIV_ROUND_UP(vser->serial.max_virtserial_ports, 32))
1084
* sizeof(vser->ports_map[0]));
1089
mark_port_added(vser, 0);
1091
vser->post_load = NULL;
1093
QLIST_INSERT_HEAD(&vserdevices.devices, vser, next);
1096
static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
1098
DeviceClass *k = DEVICE_CLASS(klass);
1100
set_bit(DEVICE_CATEGORY_INPUT, k->categories);
1101
k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
1102
k->realize = virtser_port_device_realize;
1103
k->unrealize = virtser_port_device_unrealize;
1104
device_class_set_props(k, virtser_props);
1107
static const TypeInfo virtio_serial_port_type_info = {
1108
.name = TYPE_VIRTIO_SERIAL_PORT,
1109
.parent = TYPE_DEVICE,
1110
.instance_size = sizeof(VirtIOSerialPort),
1112
.class_size = sizeof(VirtIOSerialPortClass),
1113
.class_init = virtio_serial_port_class_init,
1116
static void virtio_serial_device_unrealize(DeviceState *dev)
1118
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1119
VirtIOSerial *vser = VIRTIO_SERIAL(dev);
1122
QLIST_REMOVE(vser, next);
1124
virtio_delete_queue(vser->c_ivq);
1125
virtio_delete_queue(vser->c_ovq);
1126
for (i = 0; i < vser->bus.max_nr_ports; i++) {
1127
virtio_delete_queue(vser->ivqs[i]);
1128
virtio_delete_queue(vser->ovqs[i]);
1133
g_free(vser->ports_map);
1134
if (vser->post_load) {
1135
g_free(vser->post_load->connected);
1136
timer_free(vser->post_load->timer);
1137
g_free(vser->post_load);
1140
qbus_set_hotplug_handler(BUS(&vser->bus), NULL);
1142
virtio_cleanup(vdev);
1146
static const VMStateDescription vmstate_virtio_console = {
1147
.name = "virtio-console",
1148
.minimum_version_id = 3,
1150
.fields = (const VMStateField[]) {
1151
VMSTATE_VIRTIO_DEVICE,
1152
VMSTATE_END_OF_LIST()
1156
static Property virtio_serial_properties[] = {
1157
DEFINE_PROP_UINT32("max_ports", VirtIOSerial, serial.max_virtserial_ports,
1159
DEFINE_PROP_BIT64("emergency-write", VirtIOSerial, host_features,
1160
VIRTIO_CONSOLE_F_EMERG_WRITE, true),
1161
DEFINE_PROP_END_OF_LIST(),
1164
static void virtio_serial_class_init(ObjectClass *klass, void *data)
1166
DeviceClass *dc = DEVICE_CLASS(klass);
1167
VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1168
HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1170
QLIST_INIT(&vserdevices.devices);
1172
device_class_set_props(dc, virtio_serial_properties);
1173
dc->vmsd = &vmstate_virtio_console;
1174
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1175
vdc->realize = virtio_serial_device_realize;
1176
vdc->unrealize = virtio_serial_device_unrealize;
1177
vdc->get_features = get_features;
1178
vdc->get_config = get_config;
1179
vdc->set_config = set_config;
1180
vdc->set_status = set_status;
1181
vdc->reset = vser_reset;
1182
vdc->save = virtio_serial_save_device;
1183
vdc->load = virtio_serial_load_device;
1184
hc->plug = virtser_port_device_plug;
1185
hc->unplug = qdev_simple_device_unplug_cb;
1188
static const TypeInfo virtio_device_info = {
1189
.name = TYPE_VIRTIO_SERIAL,
1190
.parent = TYPE_VIRTIO_DEVICE,
1191
.instance_size = sizeof(VirtIOSerial),
1192
.class_init = virtio_serial_class_init,
1193
.interfaces = (InterfaceInfo[]) {
1194
{ TYPE_HOTPLUG_HANDLER },
1199
static void virtio_serial_register_types(void)
1201
type_register_static(&virtser_bus_info);
1202
type_register_static(&virtio_serial_port_type_info);
1203
type_register_static(&virtio_device_info);
1206
type_init(virtio_serial_register_types)