xref: /qemu/include/hw/virtio/virtio-serial.h (revision 8110fa1d)
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 
16 #ifndef QEMU_VIRTIO_SERIAL_H
17 #define QEMU_VIRTIO_SERIAL_H
18 
19 #include "standard-headers/linux/virtio_console.h"
20 #include "hw/virtio/virtio.h"
21 #include "qom/object.h"
22 
23 struct virtio_serial_conf {
24     /* Max. number of ports we can have for a virtio-serial device */
25     uint32_t max_virtserial_ports;
26 };
27 
28 #define TYPE_VIRTIO_SERIAL_PORT "virtio-serial-port"
29 typedef struct VirtIOSerialPort VirtIOSerialPort;
30 typedef struct VirtIOSerialPortClass VirtIOSerialPortClass;
31 DECLARE_OBJ_CHECKERS(VirtIOSerialPort, VirtIOSerialPortClass,
32                      VIRTIO_SERIAL_PORT, TYPE_VIRTIO_SERIAL_PORT)
33 
34 typedef struct VirtIOSerial VirtIOSerial;
35 
36 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
37 typedef struct VirtIOSerialBus VirtIOSerialBus;
38 DECLARE_INSTANCE_CHECKER(VirtIOSerialBus, VIRTIO_SERIAL_BUS,
39                          TYPE_VIRTIO_SERIAL_BUS)
40 
41 
42 struct VirtIOSerialPortClass {
43     DeviceClass parent_class;
44 
45     /* Is this a device that binds with hvc in the guest? */
46     bool is_console;
47 
48     /*
49      * The per-port (or per-app) realize function that's called when a
50      * new device is found on the bus.
51      */
52     DeviceRealize realize;
53     /*
54      * Per-port unrealize function that's called when a port gets
55      * hot-unplugged or removed.
56      */
57     DeviceUnrealize unrealize;
58 
59     /* Callbacks for guest events */
60         /* Guest opened/closed device. */
61     void (*set_guest_connected)(VirtIOSerialPort *port, int guest_connected);
62 
63     /* Enable/disable backend for virtio serial port */
64     void (*enable_backend)(VirtIOSerialPort *port, bool enable);
65 
66         /* Guest is now ready to accept data (virtqueues set up). */
67     void (*guest_ready)(VirtIOSerialPort *port);
68 
69         /*
70          * Guest has enqueued a buffer for the host to write into.
71          * Called each time a buffer is enqueued by the guest;
72          * irrespective of whether there already were free buffers the
73          * host could have consumed.
74          *
75          * This is dependent on both the guest and host end being
76          * connected.
77          */
78     void (*guest_writable)(VirtIOSerialPort *port);
79 
80     /*
81      * Guest wrote some data to the port. This data is handed over to
82      * the app via this callback.  The app can return a size less than
83      * 'len'.  In this case, throttling will be enabled for this port.
84      */
85     ssize_t (*have_data)(VirtIOSerialPort *port, const uint8_t *buf,
86                          ssize_t len);
87 };
88 
89 /*
90  * This is the state that's shared between all the ports.  Some of the
91  * state is configurable via command-line options. Some of it can be
92  * set by individual devices in their initfn routines. Some of the
93  * state is set by the generic qdev device init routine.
94  */
95 struct VirtIOSerialPort {
96     DeviceState dev;
97 
98     QTAILQ_ENTRY(VirtIOSerialPort) next;
99 
100     /*
101      * This field gives us the virtio device as well as the qdev bus
102      * that we are associated with
103      */
104     VirtIOSerial *vser;
105 
106     VirtQueue *ivq, *ovq;
107 
108     /*
109      * This name is sent to the guest and exported via sysfs.
110      * The guest could create symlinks based on this information.
111      * The name is in the reverse fqdn format, like org.qemu.console.0
112      */
113     char *name;
114 
115     /*
116      * This id helps identify ports between the guest and the host.
117      * The guest sends a "header" with this id with each data packet
118      * that it sends and the host can then find out which associated
119      * device to send out this data to
120      */
121     uint32_t id;
122 
123     /*
124      * This is the elem that we pop from the virtqueue.  A slow
125      * backend that consumes guest data (e.g. the file backend for
126      * qemu chardevs) can cause the guest to block till all the output
127      * is flushed.  This isn't desired, so we keep a note of the last
128      * element popped and continue consuming it once the backend
129      * becomes writable again.
130      */
131     VirtQueueElement *elem;
132 
133     /*
134      * The index and the offset into the iov buffer that was popped in
135      * elem above.
136      */
137     uint32_t iov_idx;
138     uint64_t iov_offset;
139 
140     /*
141      * When unthrottling we use a bottom-half to call flush_queued_data.
142      */
143     QEMUBH *bh;
144 
145     /* Is the corresponding guest device open? */
146     bool guest_connected;
147     /* Is this device open for IO on the host? */
148     bool host_connected;
149     /* Do apps not want to receive data? */
150     bool throttled;
151 };
152 
153 /* The virtio-serial bus on top of which the ports will ride as devices */
154 struct VirtIOSerialBus {
155     BusState qbus;
156 
157     /* This is the parent device that provides the bus for ports. */
158     VirtIOSerial *vser;
159 
160     /* The maximum number of ports that can ride on top of this bus */
161     uint32_t max_nr_ports;
162 };
163 
164 typedef struct VirtIOSerialPostLoad {
165     QEMUTimer *timer;
166     uint32_t nr_active_ports;
167     struct {
168         VirtIOSerialPort *port;
169         uint8_t host_connected;
170     } *connected;
171 } VirtIOSerialPostLoad;
172 
173 struct VirtIOSerial {
174     VirtIODevice parent_obj;
175 
176     VirtQueue *c_ivq, *c_ovq;
177     /* Arrays of ivqs and ovqs: one per port */
178     VirtQueue **ivqs, **ovqs;
179 
180     VirtIOSerialBus bus;
181 
182     QTAILQ_HEAD(, VirtIOSerialPort) ports;
183 
184     QLIST_ENTRY(VirtIOSerial) next;
185 
186     /* bitmap for identifying active ports */
187     uint32_t *ports_map;
188 
189     struct VirtIOSerialPostLoad *post_load;
190 
191     virtio_serial_conf serial;
192 
193     uint64_t host_features;
194 };
195 
196 /* Interface to the virtio-serial bus */
197 
198 /*
199  * Open a connection to the port
200  *   Returns 0 on success (always).
201  */
202 int virtio_serial_open(VirtIOSerialPort *port);
203 
204 /*
205  * Close the connection to the port
206  *   Returns 0 on success (always).
207  */
208 int virtio_serial_close(VirtIOSerialPort *port);
209 
210 /*
211  * Send data to Guest
212  */
213 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
214                             size_t size);
215 
216 /*
217  * Query whether a guest is ready to receive data.
218  */
219 size_t virtio_serial_guest_ready(VirtIOSerialPort *port);
220 
221 /*
222  * Flow control: Ports can signal to the virtio-serial core to stop
223  * sending data or re-start sending data, depending on the 'throttle'
224  * value here.
225  */
226 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle);
227 
228 #define TYPE_VIRTIO_SERIAL "virtio-serial-device"
229 DECLARE_INSTANCE_CHECKER(VirtIOSerial, VIRTIO_SERIAL,
230                          TYPE_VIRTIO_SERIAL)
231 
232 #endif
233