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