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