xref: /qemu/include/hw/virtio/virtio-serial.h (revision f1b24e840f9f85a2bb3912e4507d887e7858219d)
1 /*
2  * Virtio Serial / Console Support
3  *
4  * Copyright IBM, Corp. 2008
5  * Copyright Red Hat, Inc. 2009, 2010
6  *
7  * Authors:
8  *  Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
9  *  Amit Shah <amit.shah@redhat.com>
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2.  See
12  * the COPYING file in the top-level directory.
13  *
14  */
15 #ifndef _QEMU_VIRTIO_SERIAL_H
16 #define _QEMU_VIRTIO_SERIAL_H
17 
18 #include "hw/qdev.h"
19 #include "hw/virtio.h"
20 
21 /* == Interface shared between the guest kernel and qemu == */
22 
23 /* The Virtio ID for virtio console / serial ports */
24 #define VIRTIO_ID_CONSOLE		3
25 
26 /* Features supported */
27 #define VIRTIO_CONSOLE_F_MULTIPORT	1
28 
29 #define VIRTIO_CONSOLE_BAD_ID           (~(uint32_t)0)
30 
31 struct virtio_console_config {
32     /*
33      * These two fields are used by VIRTIO_CONSOLE_F_SIZE which
34      * isn't implemented here yet
35      */
36     uint16_t cols;
37     uint16_t rows;
38 
39     uint32_t max_nr_ports;
40 } QEMU_PACKED;
41 
42 struct virtio_console_control {
43     uint32_t id;		/* Port number */
44     uint16_t event;		/* The kind of control event (see below) */
45     uint16_t value;		/* Extra information for the key */
46 };
47 
48 struct virtio_serial_conf {
49     /* Max. number of ports we can have for a virtio-serial device */
50     uint32_t max_virtserial_ports;
51 };
52 
53 /* Some events for the internal messages (control packets) */
54 #define VIRTIO_CONSOLE_DEVICE_READY	0
55 #define VIRTIO_CONSOLE_PORT_ADD		1
56 #define VIRTIO_CONSOLE_PORT_REMOVE	2
57 #define VIRTIO_CONSOLE_PORT_READY	3
58 #define VIRTIO_CONSOLE_CONSOLE_PORT	4
59 #define VIRTIO_CONSOLE_RESIZE		5
60 #define VIRTIO_CONSOLE_PORT_OPEN	6
61 #define VIRTIO_CONSOLE_PORT_NAME	7
62 
63 /* == In-qemu interface == */
64 
65 #define TYPE_VIRTIO_SERIAL_PORT "virtio-serial-port"
66 #define VIRTIO_SERIAL_PORT(obj) \
67      OBJECT_CHECK(VirtIOSerialPort, (obj), TYPE_VIRTIO_SERIAL_PORT)
68 #define VIRTIO_SERIAL_PORT_CLASS(klass) \
69      OBJECT_CLASS_CHECK(VirtIOSerialPortClass, (klass), TYPE_VIRTIO_SERIAL_PORT)
70 #define VIRTIO_SERIAL_PORT_GET_CLASS(obj) \
71      OBJECT_GET_CLASS(VirtIOSerialPortClass, (obj), TYPE_VIRTIO_SERIAL_PORT)
72 
73 typedef struct VirtIOSerial VirtIOSerial;
74 typedef struct VirtIOSerialBus VirtIOSerialBus;
75 typedef struct VirtIOSerialPort VirtIOSerialPort;
76 
77 typedef struct VirtIOSerialPortClass {
78     DeviceClass parent_class;
79 
80     /* Is this a device that binds with hvc in the guest? */
81     bool is_console;
82 
83     /*
84      * The per-port (or per-app) init function that's called when a
85      * new device is found on the bus.
86      */
87     int (*init)(VirtIOSerialPort *port);
88     /*
89      * Per-port exit function that's called when a port gets
90      * hot-unplugged or removed.
91      */
92     int (*exit)(VirtIOSerialPort *port);
93 
94     /* Callbacks for guest events */
95         /* Guest opened device. */
96     void (*guest_open)(VirtIOSerialPort *port);
97         /* Guest closed device. */
98     void (*guest_close)(VirtIOSerialPort *port);
99 
100         /* Guest is now ready to accept data (virtqueues set up). */
101     void (*guest_ready)(VirtIOSerialPort *port);
102 
103     /*
104      * Guest wrote some data to the port. This data is handed over to
105      * the app via this callback.  The app can return a size less than
106      * 'len'.  In this case, throttling will be enabled for this port.
107      */
108     ssize_t (*have_data)(VirtIOSerialPort *port, const uint8_t *buf,
109                          size_t len);
110 } VirtIOSerialPortClass;
111 
112 /*
113  * This is the state that's shared between all the ports.  Some of the
114  * state is configurable via command-line options. Some of it can be
115  * set by individual devices in their initfn routines. Some of the
116  * state is set by the generic qdev device init routine.
117  */
118 struct VirtIOSerialPort {
119     DeviceState dev;
120 
121     QTAILQ_ENTRY(VirtIOSerialPort) next;
122 
123     /*
124      * This field gives us the virtio device as well as the qdev bus
125      * that we are associated with
126      */
127     VirtIOSerial *vser;
128 
129     VirtQueue *ivq, *ovq;
130 
131     /*
132      * This name is sent to the guest and exported via sysfs.
133      * The guest could create symlinks based on this information.
134      * The name is in the reverse fqdn format, like org.qemu.console.0
135      */
136     char *name;
137 
138     /*
139      * This id helps identify ports between the guest and the host.
140      * The guest sends a "header" with this id with each data packet
141      * that it sends and the host can then find out which associated
142      * device to send out this data to
143      */
144     uint32_t id;
145 
146     /*
147      * This is the elem that we pop from the virtqueue.  A slow
148      * backend that consumes guest data (e.g. the file backend for
149      * qemu chardevs) can cause the guest to block till all the output
150      * is flushed.  This isn't desired, so we keep a note of the last
151      * element popped and continue consuming it once the backend
152      * becomes writable again.
153      */
154     VirtQueueElement elem;
155 
156     /*
157      * The index and the offset into the iov buffer that was popped in
158      * elem above.
159      */
160     uint32_t iov_idx;
161     uint64_t iov_offset;
162 
163     /*
164      * When unthrottling we use a bottom-half to call flush_queued_data.
165      */
166     QEMUBH *bh;
167 
168     /* Is the corresponding guest device open? */
169     bool guest_connected;
170     /* Is this device open for IO on the host? */
171     bool host_connected;
172     /* Do apps not want to receive data? */
173     bool throttled;
174 };
175 
176 /* The virtio-serial bus on top of which the ports will ride as devices */
177 struct VirtIOSerialBus {
178     BusState qbus;
179 
180     /* This is the parent device that provides the bus for ports. */
181     VirtIOSerial *vser;
182 
183     /* The maximum number of ports that can ride on top of this bus */
184     uint32_t max_nr_ports;
185 };
186 
187 typedef struct VirtIOSerialPostLoad {
188     QEMUTimer *timer;
189     uint32_t nr_active_ports;
190     struct {
191         VirtIOSerialPort *port;
192         uint8_t host_connected;
193     } *connected;
194 } VirtIOSerialPostLoad;
195 
196 struct VirtIOSerial {
197     VirtIODevice vdev;
198 
199     VirtQueue *c_ivq, *c_ovq;
200     /* Arrays of ivqs and ovqs: one per port */
201     VirtQueue **ivqs, **ovqs;
202 
203     VirtIOSerialBus bus;
204 
205     DeviceState *qdev;
206 
207     QTAILQ_HEAD(, VirtIOSerialPort) ports;
208 
209     /* bitmap for identifying active ports */
210     uint32_t *ports_map;
211 
212     struct virtio_console_config config;
213 
214     struct VirtIOSerialPostLoad *post_load;
215 };
216 
217 /* Interface to the virtio-serial bus */
218 
219 /*
220  * Open a connection to the port
221  *   Returns 0 on success (always).
222  */
223 int virtio_serial_open(VirtIOSerialPort *port);
224 
225 /*
226  * Close the connection to the port
227  *   Returns 0 on success (always).
228  */
229 int virtio_serial_close(VirtIOSerialPort *port);
230 
231 /*
232  * Send data to Guest
233  */
234 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
235                             size_t size);
236 
237 /*
238  * Query whether a guest is ready to receive data.
239  */
240 size_t virtio_serial_guest_ready(VirtIOSerialPort *port);
241 
242 /*
243  * Flow control: Ports can signal to the virtio-serial core to stop
244  * sending data or re-start sending data, depending on the 'throttle'
245  * value here.
246  */
247 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle);
248 
249 #endif
250