xref: /qemu/include/hw/virtio/virtio-serial.h (revision 30b5707c269cac1ad80b72f777e52c8e08b2ff19)
198b19252SAmit Shah /*
298b19252SAmit Shah  * Virtio Serial / Console Support
398b19252SAmit Shah  *
498b19252SAmit Shah  * Copyright IBM, Corp. 2008
571c092e9SAmit Shah  * Copyright Red Hat, Inc. 2009, 2010
698b19252SAmit Shah  *
798b19252SAmit Shah  * Authors:
898b19252SAmit Shah  *  Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
998b19252SAmit Shah  *  Amit Shah <amit.shah@redhat.com>
1098b19252SAmit Shah  *
1198b19252SAmit Shah  * This work is licensed under the terms of the GNU GPL, version 2.  See
1298b19252SAmit Shah  * the COPYING file in the top-level directory.
1398b19252SAmit Shah  *
1498b19252SAmit Shah  */
152a6a4076SMarkus Armbruster 
162a6a4076SMarkus Armbruster #ifndef QEMU_VIRTIO_SERIAL_H
172a6a4076SMarkus Armbruster #define QEMU_VIRTIO_SERIAL_H
1898b19252SAmit Shah 
199b70c179SMichael S. Tsirkin #include "standard-headers/linux/virtio_console.h"
200d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h"
21db1015e9SEduardo Habkost #include "qom/object.h"
2298b19252SAmit Shah 
236b331efbSAmit Shah struct virtio_serial_conf {
246b331efbSAmit Shah     /* Max. number of ports we can have for a virtio-serial device */
256b331efbSAmit Shah     uint32_t max_virtserial_ports;
266b331efbSAmit Shah };
276b331efbSAmit Shah 
28f82e35e3SAnthony Liguori #define TYPE_VIRTIO_SERIAL_PORT "virtio-serial-port"
29c821774aSEduardo Habkost OBJECT_DECLARE_TYPE(VirtIOSerialPort, VirtIOSerialPortClass,
30*30b5707cSEduardo Habkost                     VIRTIO_SERIAL_PORT)
31f82e35e3SAnthony Liguori 
3298b19252SAmit Shah typedef struct VirtIOSerial VirtIOSerial;
33b28b8037SEduardo Habkost 
34b28b8037SEduardo Habkost #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
3598b19252SAmit Shah typedef struct VirtIOSerialBus VirtIOSerialBus;
368110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(VirtIOSerialBus, VIRTIO_SERIAL_BUS,
378110fa1dSEduardo Habkost                          TYPE_VIRTIO_SERIAL_BUS)
38b28b8037SEduardo Habkost 
39f82e35e3SAnthony Liguori 
40db1015e9SEduardo Habkost struct VirtIOSerialPortClass {
41f82e35e3SAnthony Liguori     DeviceClass parent_class;
42f82e35e3SAnthony Liguori 
43f82e35e3SAnthony Liguori     /* Is this a device that binds with hvc in the guest? */
44f82e35e3SAnthony Liguori     bool is_console;
45f82e35e3SAnthony Liguori 
46f82e35e3SAnthony Liguori     /*
472ef66625SAndreas Färber      * The per-port (or per-app) realize function that's called when a
48f82e35e3SAnthony Liguori      * new device is found on the bus.
49f82e35e3SAnthony Liguori      */
502ef66625SAndreas Färber     DeviceRealize realize;
51f82e35e3SAnthony Liguori     /*
522ef66625SAndreas Färber      * Per-port unrealize function that's called when a port gets
53f82e35e3SAnthony Liguori      * hot-unplugged or removed.
54f82e35e3SAnthony Liguori      */
552ef66625SAndreas Färber     DeviceUnrealize unrealize;
56f82e35e3SAnthony Liguori 
57f82e35e3SAnthony Liguori     /* Callbacks for guest events */
58b2c1394aSHans de Goede         /* Guest opened/closed device. */
59b2c1394aSHans de Goede     void (*set_guest_connected)(VirtIOSerialPort *port, int guest_connected);
60f82e35e3SAnthony Liguori 
6155289fb0SPavel Butsykin     /* Enable/disable backend for virtio serial port */
6255289fb0SPavel Butsykin     void (*enable_backend)(VirtIOSerialPort *port, bool enable);
6355289fb0SPavel Butsykin 
64f82e35e3SAnthony Liguori         /* Guest is now ready to accept data (virtqueues set up). */
65f82e35e3SAnthony Liguori     void (*guest_ready)(VirtIOSerialPort *port);
66f82e35e3SAnthony Liguori 
67f82e35e3SAnthony Liguori         /*
684add73aaSAmit Shah          * Guest has enqueued a buffer for the host to write into.
694add73aaSAmit Shah          * Called each time a buffer is enqueued by the guest;
704add73aaSAmit Shah          * irrespective of whether there already were free buffers the
714add73aaSAmit Shah          * host could have consumed.
724add73aaSAmit Shah          *
734add73aaSAmit Shah          * This is dependent on both the guest and host end being
744add73aaSAmit Shah          * connected.
754add73aaSAmit Shah          */
764add73aaSAmit Shah     void (*guest_writable)(VirtIOSerialPort *port);
774add73aaSAmit Shah 
784add73aaSAmit Shah     /*
79f82e35e3SAnthony Liguori      * Guest wrote some data to the port. This data is handed over to
80f82e35e3SAnthony Liguori      * the app via this callback.  The app can return a size less than
81f82e35e3SAnthony Liguori      * 'len'.  In this case, throttling will be enabled for this port.
82f82e35e3SAnthony Liguori      */
83f82e35e3SAnthony Liguori     ssize_t (*have_data)(VirtIOSerialPort *port, const uint8_t *buf,
84f9fb0532SHans de Goede                          ssize_t len);
85db1015e9SEduardo Habkost };
8698b19252SAmit Shah 
8798b19252SAmit Shah /*
8898b19252SAmit Shah  * This is the state that's shared between all the ports.  Some of the
8998b19252SAmit Shah  * state is configurable via command-line options. Some of it can be
9098b19252SAmit Shah  * set by individual devices in their initfn routines. Some of the
9198b19252SAmit Shah  * state is set by the generic qdev device init routine.
9298b19252SAmit Shah  */
9398b19252SAmit Shah struct VirtIOSerialPort {
9498b19252SAmit Shah     DeviceState dev;
9598b19252SAmit Shah 
9698b19252SAmit Shah     QTAILQ_ENTRY(VirtIOSerialPort) next;
9798b19252SAmit Shah 
9898b19252SAmit Shah     /*
9998b19252SAmit Shah      * This field gives us the virtio device as well as the qdev bus
10098b19252SAmit Shah      * that we are associated with
10198b19252SAmit Shah      */
10298b19252SAmit Shah     VirtIOSerial *vser;
10398b19252SAmit Shah 
10498b19252SAmit Shah     VirtQueue *ivq, *ovq;
10598b19252SAmit Shah 
10698b19252SAmit Shah     /*
107160600fdSAmit Shah      * This name is sent to the guest and exported via sysfs.
108160600fdSAmit Shah      * The guest could create symlinks based on this information.
109160600fdSAmit Shah      * The name is in the reverse fqdn format, like org.qemu.console.0
110160600fdSAmit Shah      */
111160600fdSAmit Shah     char *name;
112160600fdSAmit Shah 
113160600fdSAmit Shah     /*
11498b19252SAmit Shah      * This id helps identify ports between the guest and the host.
11598b19252SAmit Shah      * The guest sends a "header" with this id with each data packet
11698b19252SAmit Shah      * that it sends and the host can then find out which associated
11798b19252SAmit Shah      * device to send out this data to
11898b19252SAmit Shah      */
11998b19252SAmit Shah     uint32_t id;
12098b19252SAmit Shah 
121f1925dffSAmit Shah     /*
122f1925dffSAmit Shah      * This is the elem that we pop from the virtqueue.  A slow
123f1925dffSAmit Shah      * backend that consumes guest data (e.g. the file backend for
124f1925dffSAmit Shah      * qemu chardevs) can cause the guest to block till all the output
125f1925dffSAmit Shah      * is flushed.  This isn't desired, so we keep a note of the last
126f1925dffSAmit Shah      * element popped and continue consuming it once the backend
127f1925dffSAmit Shah      * becomes writable again.
128f1925dffSAmit Shah      */
12951b19ebeSPaolo Bonzini     VirtQueueElement *elem;
130f1925dffSAmit Shah 
131f1925dffSAmit Shah     /*
132f1925dffSAmit Shah      * The index and the offset into the iov buffer that was popped in
133f1925dffSAmit Shah      * elem above.
134f1925dffSAmit Shah      */
135f1925dffSAmit Shah     uint32_t iov_idx;
136f1925dffSAmit Shah     uint64_t iov_offset;
137f1925dffSAmit Shah 
138199646d8SAlon Levy     /*
139199646d8SAlon Levy      * When unthrottling we use a bottom-half to call flush_queued_data.
140199646d8SAlon Levy      */
141199646d8SAlon Levy     QEMUBH *bh;
142199646d8SAlon Levy 
1436663a195SAmit Shah     /* Is the corresponding guest device open? */
1446663a195SAmit Shah     bool guest_connected;
1456663a195SAmit Shah     /* Is this device open for IO on the host? */
1466663a195SAmit Shah     bool host_connected;
1479ed7b059SAmit Shah     /* Do apps not want to receive data? */
1489ed7b059SAmit Shah     bool throttled;
14998b19252SAmit Shah };
15098b19252SAmit Shah 
151f1b24e84SKONRAD Frederic /* The virtio-serial bus on top of which the ports will ride as devices */
152f1b24e84SKONRAD Frederic struct VirtIOSerialBus {
153f1b24e84SKONRAD Frederic     BusState qbus;
154f1b24e84SKONRAD Frederic 
155f1b24e84SKONRAD Frederic     /* This is the parent device that provides the bus for ports. */
156f1b24e84SKONRAD Frederic     VirtIOSerial *vser;
157f1b24e84SKONRAD Frederic 
158f1b24e84SKONRAD Frederic     /* The maximum number of ports that can ride on top of this bus */
159f1b24e84SKONRAD Frederic     uint32_t max_nr_ports;
160f1b24e84SKONRAD Frederic };
161f1b24e84SKONRAD Frederic 
162f1b24e84SKONRAD Frederic typedef struct VirtIOSerialPostLoad {
163f1b24e84SKONRAD Frederic     QEMUTimer *timer;
164f1b24e84SKONRAD Frederic     uint32_t nr_active_ports;
165f1b24e84SKONRAD Frederic     struct {
166f1b24e84SKONRAD Frederic         VirtIOSerialPort *port;
167f1b24e84SKONRAD Frederic         uint8_t host_connected;
168f1b24e84SKONRAD Frederic     } *connected;
169f1b24e84SKONRAD Frederic } VirtIOSerialPostLoad;
170f1b24e84SKONRAD Frederic 
171f1b24e84SKONRAD Frederic struct VirtIOSerial {
17276017fd2SKONRAD Frederic     VirtIODevice parent_obj;
173f1b24e84SKONRAD Frederic 
174f1b24e84SKONRAD Frederic     VirtQueue *c_ivq, *c_ovq;
175f1b24e84SKONRAD Frederic     /* Arrays of ivqs and ovqs: one per port */
176f1b24e84SKONRAD Frederic     VirtQueue **ivqs, **ovqs;
177f1b24e84SKONRAD Frederic 
178f1b24e84SKONRAD Frederic     VirtIOSerialBus bus;
179f1b24e84SKONRAD Frederic 
180f1b24e84SKONRAD Frederic     QTAILQ_HEAD(, VirtIOSerialPort) ports;
181f1b24e84SKONRAD Frederic 
182a1857ad1SAmit Shah     QLIST_ENTRY(VirtIOSerial) next;
183a1857ad1SAmit Shah 
184f1b24e84SKONRAD Frederic     /* bitmap for identifying active ports */
185f1b24e84SKONRAD Frederic     uint32_t *ports_map;
186f1b24e84SKONRAD Frederic 
187f1b24e84SKONRAD Frederic     struct VirtIOSerialPostLoad *post_load;
1882cd2b016SKONRAD Frederic 
1892cd2b016SKONRAD Frederic     virtio_serial_conf serial;
190a06b1daeSSascha Silbe 
191a06b1daeSSascha Silbe     uint64_t host_features;
192f1b24e84SKONRAD Frederic };
193f1b24e84SKONRAD Frederic 
19498b19252SAmit Shah /* Interface to the virtio-serial bus */
19598b19252SAmit Shah 
19698b19252SAmit Shah /*
19798b19252SAmit Shah  * Open a connection to the port
19898b19252SAmit Shah  *   Returns 0 on success (always).
19998b19252SAmit Shah  */
20098b19252SAmit Shah int virtio_serial_open(VirtIOSerialPort *port);
20198b19252SAmit Shah 
20298b19252SAmit Shah /*
20398b19252SAmit Shah  * Close the connection to the port
20498b19252SAmit Shah  *   Returns 0 on success (always).
20598b19252SAmit Shah  */
20698b19252SAmit Shah int virtio_serial_close(VirtIOSerialPort *port);
20798b19252SAmit Shah 
20898b19252SAmit Shah /*
20998b19252SAmit Shah  * Send data to Guest
21098b19252SAmit Shah  */
21198b19252SAmit Shah ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
21298b19252SAmit Shah                             size_t size);
21398b19252SAmit Shah 
21498b19252SAmit Shah /*
21598b19252SAmit Shah  * Query whether a guest is ready to receive data.
21698b19252SAmit Shah  */
21798b19252SAmit Shah size_t virtio_serial_guest_ready(VirtIOSerialPort *port);
21898b19252SAmit Shah 
2199ed7b059SAmit Shah /*
2209ed7b059SAmit Shah  * Flow control: Ports can signal to the virtio-serial core to stop
2219ed7b059SAmit Shah  * sending data or re-start sending data, depending on the 'throttle'
2229ed7b059SAmit Shah  * value here.
2239ed7b059SAmit Shah  */
2249ed7b059SAmit Shah void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle);
2259ed7b059SAmit Shah 
2262cd2b016SKONRAD Frederic #define TYPE_VIRTIO_SERIAL "virtio-serial-device"
2278110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(VirtIOSerial, VIRTIO_SERIAL,
2288110fa1dSEduardo Habkost                          TYPE_VIRTIO_SERIAL)
2292cd2b016SKONRAD Frederic 
23098b19252SAmit Shah #endif
231