2 * Virtio Serial / Console Support
4 * Copyright IBM, Corp. 2008
5 * Copyright Red Hat, Inc. 2009, 2010
8 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
9 * Amit Shah <amit.shah@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
16 #ifndef QEMU_VIRTIO_SERIAL_H
17 #define QEMU_VIRTIO_SERIAL_H
19 #include "standard-headers/linux/virtio_console.h"
20 #include "hw/virtio/virtio.h"
21 #include "qom/object.h"
23 struct virtio_serial_conf
{
24 /* Max. number of ports we can have for a virtio-serial device */
25 uint32_t max_virtserial_ports
;
28 #define TYPE_VIRTIO_SERIAL_PORT "virtio-serial-port"
29 OBJECT_DECLARE_TYPE(VirtIOSerialPort
, VirtIOSerialPortClass
,
32 typedef struct VirtIOSerial VirtIOSerial
;
34 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
35 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOSerialBus
, VIRTIO_SERIAL_BUS
)
38 struct VirtIOSerialPortClass
{
39 DeviceClass parent_class
;
41 /* Is this a device that binds with hvc in the guest? */
45 * The per-port (or per-app) realize function that's called when a
46 * new device is found on the bus.
48 DeviceRealize realize
;
50 * Per-port unrealize function that's called when a port gets
51 * hot-unplugged or removed.
53 DeviceUnrealize unrealize
;
55 /* Callbacks for guest events */
56 /* Guest opened/closed device. */
57 void (*set_guest_connected
)(VirtIOSerialPort
*port
, int guest_connected
);
59 /* Enable/disable backend for virtio serial port */
60 void (*enable_backend
)(VirtIOSerialPort
*port
, bool enable
);
62 /* Guest is now ready to accept data (virtqueues set up). */
63 void (*guest_ready
)(VirtIOSerialPort
*port
);
66 * Guest has enqueued a buffer for the host to write into.
67 * Called each time a buffer is enqueued by the guest;
68 * irrespective of whether there already were free buffers the
69 * host could have consumed.
71 * This is dependent on both the guest and host end being
74 void (*guest_writable
)(VirtIOSerialPort
*port
);
77 * Guest wrote some data to the port. This data is handed over to
78 * the app via this callback. The app can return a size less than
79 * 'len'. In this case, throttling will be enabled for this port.
81 ssize_t (*have_data
)(VirtIOSerialPort
*port
, const uint8_t *buf
,
86 * This is the state that's shared between all the ports. Some of the
87 * state is configurable via command-line options. Some of it can be
88 * set by individual devices in their initfn routines. Some of the
89 * state is set by the generic qdev device init routine.
91 struct VirtIOSerialPort
{
94 QTAILQ_ENTRY(VirtIOSerialPort
) next
;
97 * This field gives us the virtio device as well as the qdev bus
98 * that we are associated with
102 VirtQueue
*ivq
, *ovq
;
105 * This name is sent to the guest and exported via sysfs.
106 * The guest could create symlinks based on this information.
107 * The name is in the reverse fqdn format, like org.qemu.console.0
112 * This id helps identify ports between the guest and the host.
113 * The guest sends a "header" with this id with each data packet
114 * that it sends and the host can then find out which associated
115 * device to send out this data to
120 * This is the elem that we pop from the virtqueue. A slow
121 * backend that consumes guest data (e.g. the file backend for
122 * qemu chardevs) can cause the guest to block till all the output
123 * is flushed. This isn't desired, so we keep a note of the last
124 * element popped and continue consuming it once the backend
125 * becomes writable again.
127 VirtQueueElement
*elem
;
130 * The index and the offset into the iov buffer that was popped in
137 * When unthrottling we use a bottom-half to call flush_queued_data.
141 /* Is the corresponding guest device open? */
142 bool guest_connected
;
143 /* Is this device open for IO on the host? */
145 /* Do apps not want to receive data? */
149 /* The virtio-serial bus on top of which the ports will ride as devices */
150 struct VirtIOSerialBus
{
153 /* This is the parent device that provides the bus for ports. */
156 /* The maximum number of ports that can ride on top of this bus */
157 uint32_t max_nr_ports
;
160 typedef struct VirtIOSerialPostLoad
{
162 uint32_t nr_active_ports
;
164 VirtIOSerialPort
*port
;
165 uint8_t host_connected
;
167 } VirtIOSerialPostLoad
;
169 struct VirtIOSerial
{
170 VirtIODevice parent_obj
;
172 VirtQueue
*c_ivq
, *c_ovq
;
173 /* Arrays of ivqs and ovqs: one per port */
174 VirtQueue
**ivqs
, **ovqs
;
178 QTAILQ_HEAD(, VirtIOSerialPort
) ports
;
180 QLIST_ENTRY(VirtIOSerial
) next
;
182 /* bitmap for identifying active ports */
185 struct VirtIOSerialPostLoad
*post_load
;
187 virtio_serial_conf serial
;
189 uint64_t host_features
;
192 /* Interface to the virtio-serial bus */
195 * Open a connection to the port
196 * Returns 0 on success (always).
198 int virtio_serial_open(VirtIOSerialPort
*port
);
201 * Close the connection to the port
202 * Returns 0 on success (always).
204 int virtio_serial_close(VirtIOSerialPort
*port
);
209 ssize_t
virtio_serial_write(VirtIOSerialPort
*port
, const uint8_t *buf
,
213 * Query whether a guest is ready to receive data.
215 size_t virtio_serial_guest_ready(VirtIOSerialPort
*port
);
218 * Flow control: Ports can signal to the virtio-serial core to stop
219 * sending data or re-start sending data, depending on the 'throttle'
222 void virtio_serial_throttle_port(VirtIOSerialPort
*port
, bool throttle
);
224 #define TYPE_VIRTIO_SERIAL "virtio-serial-device"
225 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOSerial
, VIRTIO_SERIAL
)