xref: /qemu/include/hw/virtio/virtio-serial.h (revision 9b70c1790acacae54d559d38ca69186a85040bb8)
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  */
1598b19252SAmit Shah #ifndef _QEMU_VIRTIO_SERIAL_H
1698b19252SAmit Shah #define _QEMU_VIRTIO_SERIAL_H
1798b19252SAmit Shah 
18*9b70c179SMichael S. Tsirkin #include "standard-headers/linux/virtio_console.h"
1983c9f4caSPaolo Bonzini #include "hw/qdev.h"
200d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h"
2198b19252SAmit Shah 
226b331efbSAmit Shah struct virtio_serial_conf {
236b331efbSAmit Shah     /* Max. number of ports we can have for a virtio-serial device */
246b331efbSAmit Shah     uint32_t max_virtserial_ports;
256b331efbSAmit Shah };
266b331efbSAmit Shah 
27f82e35e3SAnthony Liguori #define TYPE_VIRTIO_SERIAL_PORT "virtio-serial-port"
28f82e35e3SAnthony Liguori #define VIRTIO_SERIAL_PORT(obj) \
29f82e35e3SAnthony Liguori      OBJECT_CHECK(VirtIOSerialPort, (obj), TYPE_VIRTIO_SERIAL_PORT)
30f82e35e3SAnthony Liguori #define VIRTIO_SERIAL_PORT_CLASS(klass) \
31f82e35e3SAnthony Liguori      OBJECT_CLASS_CHECK(VirtIOSerialPortClass, (klass), TYPE_VIRTIO_SERIAL_PORT)
32f82e35e3SAnthony Liguori #define VIRTIO_SERIAL_PORT_GET_CLASS(obj) \
33f82e35e3SAnthony Liguori      OBJECT_GET_CLASS(VirtIOSerialPortClass, (obj), TYPE_VIRTIO_SERIAL_PORT)
34f82e35e3SAnthony Liguori 
3598b19252SAmit Shah typedef struct VirtIOSerial VirtIOSerial;
3698b19252SAmit Shah typedef struct VirtIOSerialBus VirtIOSerialBus;
3798b19252SAmit Shah typedef struct VirtIOSerialPort VirtIOSerialPort;
38f82e35e3SAnthony Liguori 
39f82e35e3SAnthony Liguori typedef struct VirtIOSerialPortClass {
40f82e35e3SAnthony Liguori     DeviceClass parent_class;
41f82e35e3SAnthony Liguori 
42f82e35e3SAnthony Liguori     /* Is this a device that binds with hvc in the guest? */
43f82e35e3SAnthony Liguori     bool is_console;
44f82e35e3SAnthony Liguori 
45f82e35e3SAnthony Liguori     /*
462ef66625SAndreas Färber      * The per-port (or per-app) realize function that's called when a
47f82e35e3SAnthony Liguori      * new device is found on the bus.
48f82e35e3SAnthony Liguori      */
492ef66625SAndreas Färber     DeviceRealize realize;
50f82e35e3SAnthony Liguori     /*
512ef66625SAndreas Färber      * Per-port unrealize function that's called when a port gets
52f82e35e3SAnthony Liguori      * hot-unplugged or removed.
53f82e35e3SAnthony Liguori      */
542ef66625SAndreas Färber     DeviceUnrealize unrealize;
55f82e35e3SAnthony Liguori 
56f82e35e3SAnthony Liguori     /* Callbacks for guest events */
57b2c1394aSHans de Goede         /* Guest opened/closed device. */
58b2c1394aSHans de Goede     void (*set_guest_connected)(VirtIOSerialPort *port, int guest_connected);
59f82e35e3SAnthony Liguori 
60f82e35e3SAnthony Liguori         /* Guest is now ready to accept data (virtqueues set up). */
61f82e35e3SAnthony Liguori     void (*guest_ready)(VirtIOSerialPort *port);
62f82e35e3SAnthony Liguori 
63f82e35e3SAnthony Liguori     /*
64f82e35e3SAnthony Liguori      * Guest wrote some data to the port. This data is handed over to
65f82e35e3SAnthony Liguori      * the app via this callback.  The app can return a size less than
66f82e35e3SAnthony Liguori      * 'len'.  In this case, throttling will be enabled for this port.
67f82e35e3SAnthony Liguori      */
68f82e35e3SAnthony Liguori     ssize_t (*have_data)(VirtIOSerialPort *port, const uint8_t *buf,
69f9fb0532SHans de Goede                          ssize_t len);
70f82e35e3SAnthony Liguori } VirtIOSerialPortClass;
7198b19252SAmit Shah 
7298b19252SAmit Shah /*
7398b19252SAmit Shah  * This is the state that's shared between all the ports.  Some of the
7498b19252SAmit Shah  * state is configurable via command-line options. Some of it can be
7598b19252SAmit Shah  * set by individual devices in their initfn routines. Some of the
7698b19252SAmit Shah  * state is set by the generic qdev device init routine.
7798b19252SAmit Shah  */
7898b19252SAmit Shah struct VirtIOSerialPort {
7998b19252SAmit Shah     DeviceState dev;
8098b19252SAmit Shah 
8198b19252SAmit Shah     QTAILQ_ENTRY(VirtIOSerialPort) next;
8298b19252SAmit Shah 
8398b19252SAmit Shah     /*
8498b19252SAmit Shah      * This field gives us the virtio device as well as the qdev bus
8598b19252SAmit Shah      * that we are associated with
8698b19252SAmit Shah      */
8798b19252SAmit Shah     VirtIOSerial *vser;
8898b19252SAmit Shah 
8998b19252SAmit Shah     VirtQueue *ivq, *ovq;
9098b19252SAmit Shah 
9198b19252SAmit Shah     /*
92160600fdSAmit Shah      * This name is sent to the guest and exported via sysfs.
93160600fdSAmit Shah      * The guest could create symlinks based on this information.
94160600fdSAmit Shah      * The name is in the reverse fqdn format, like org.qemu.console.0
95160600fdSAmit Shah      */
96160600fdSAmit Shah     char *name;
97160600fdSAmit Shah 
98160600fdSAmit Shah     /*
9998b19252SAmit Shah      * This id helps identify ports between the guest and the host.
10098b19252SAmit Shah      * The guest sends a "header" with this id with each data packet
10198b19252SAmit Shah      * that it sends and the host can then find out which associated
10298b19252SAmit Shah      * device to send out this data to
10398b19252SAmit Shah      */
10498b19252SAmit Shah     uint32_t id;
10598b19252SAmit Shah 
106f1925dffSAmit Shah     /*
107f1925dffSAmit Shah      * This is the elem that we pop from the virtqueue.  A slow
108f1925dffSAmit Shah      * backend that consumes guest data (e.g. the file backend for
109f1925dffSAmit Shah      * qemu chardevs) can cause the guest to block till all the output
110f1925dffSAmit Shah      * is flushed.  This isn't desired, so we keep a note of the last
111f1925dffSAmit Shah      * element popped and continue consuming it once the backend
112f1925dffSAmit Shah      * becomes writable again.
113f1925dffSAmit Shah      */
114f1925dffSAmit Shah     VirtQueueElement elem;
115f1925dffSAmit Shah 
116f1925dffSAmit Shah     /*
117f1925dffSAmit Shah      * The index and the offset into the iov buffer that was popped in
118f1925dffSAmit Shah      * elem above.
119f1925dffSAmit Shah      */
120f1925dffSAmit Shah     uint32_t iov_idx;
121f1925dffSAmit Shah     uint64_t iov_offset;
122f1925dffSAmit Shah 
123199646d8SAlon Levy     /*
124199646d8SAlon Levy      * When unthrottling we use a bottom-half to call flush_queued_data.
125199646d8SAlon Levy      */
126199646d8SAlon Levy     QEMUBH *bh;
127199646d8SAlon Levy 
1286663a195SAmit Shah     /* Is the corresponding guest device open? */
1296663a195SAmit Shah     bool guest_connected;
1306663a195SAmit Shah     /* Is this device open for IO on the host? */
1316663a195SAmit Shah     bool host_connected;
1329ed7b059SAmit Shah     /* Do apps not want to receive data? */
1339ed7b059SAmit Shah     bool throttled;
13498b19252SAmit Shah };
13598b19252SAmit Shah 
136f1b24e84SKONRAD Frederic /* The virtio-serial bus on top of which the ports will ride as devices */
137f1b24e84SKONRAD Frederic struct VirtIOSerialBus {
138f1b24e84SKONRAD Frederic     BusState qbus;
139f1b24e84SKONRAD Frederic 
140f1b24e84SKONRAD Frederic     /* This is the parent device that provides the bus for ports. */
141f1b24e84SKONRAD Frederic     VirtIOSerial *vser;
142f1b24e84SKONRAD Frederic 
143f1b24e84SKONRAD Frederic     /* The maximum number of ports that can ride on top of this bus */
144f1b24e84SKONRAD Frederic     uint32_t max_nr_ports;
145f1b24e84SKONRAD Frederic };
146f1b24e84SKONRAD Frederic 
147f1b24e84SKONRAD Frederic typedef struct VirtIOSerialPostLoad {
148f1b24e84SKONRAD Frederic     QEMUTimer *timer;
149f1b24e84SKONRAD Frederic     uint32_t nr_active_ports;
150f1b24e84SKONRAD Frederic     struct {
151f1b24e84SKONRAD Frederic         VirtIOSerialPort *port;
152f1b24e84SKONRAD Frederic         uint8_t host_connected;
153f1b24e84SKONRAD Frederic     } *connected;
154f1b24e84SKONRAD Frederic } VirtIOSerialPostLoad;
155f1b24e84SKONRAD Frederic 
156f1b24e84SKONRAD Frederic struct VirtIOSerial {
15776017fd2SKONRAD Frederic     VirtIODevice parent_obj;
158f1b24e84SKONRAD Frederic 
159f1b24e84SKONRAD Frederic     VirtQueue *c_ivq, *c_ovq;
160f1b24e84SKONRAD Frederic     /* Arrays of ivqs and ovqs: one per port */
161f1b24e84SKONRAD Frederic     VirtQueue **ivqs, **ovqs;
162f1b24e84SKONRAD Frederic 
163f1b24e84SKONRAD Frederic     VirtIOSerialBus bus;
164f1b24e84SKONRAD Frederic 
165f1b24e84SKONRAD Frederic     QTAILQ_HEAD(, VirtIOSerialPort) ports;
166f1b24e84SKONRAD Frederic 
167a1857ad1SAmit Shah     QLIST_ENTRY(VirtIOSerial) next;
168a1857ad1SAmit Shah 
169f1b24e84SKONRAD Frederic     /* bitmap for identifying active ports */
170f1b24e84SKONRAD Frederic     uint32_t *ports_map;
171f1b24e84SKONRAD Frederic 
172f1b24e84SKONRAD Frederic     struct VirtIOSerialPostLoad *post_load;
1732cd2b016SKONRAD Frederic 
1742cd2b016SKONRAD Frederic     virtio_serial_conf serial;
175f1b24e84SKONRAD Frederic };
176f1b24e84SKONRAD Frederic 
17798b19252SAmit Shah /* Interface to the virtio-serial bus */
17898b19252SAmit Shah 
17998b19252SAmit Shah /*
18098b19252SAmit Shah  * Open a connection to the port
18198b19252SAmit Shah  *   Returns 0 on success (always).
18298b19252SAmit Shah  */
18398b19252SAmit Shah int virtio_serial_open(VirtIOSerialPort *port);
18498b19252SAmit Shah 
18598b19252SAmit Shah /*
18698b19252SAmit Shah  * Close the connection to the port
18798b19252SAmit Shah  *   Returns 0 on success (always).
18898b19252SAmit Shah  */
18998b19252SAmit Shah int virtio_serial_close(VirtIOSerialPort *port);
19098b19252SAmit Shah 
19198b19252SAmit Shah /*
19298b19252SAmit Shah  * Send data to Guest
19398b19252SAmit Shah  */
19498b19252SAmit Shah ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
19598b19252SAmit Shah                             size_t size);
19698b19252SAmit Shah 
19798b19252SAmit Shah /*
19898b19252SAmit Shah  * Query whether a guest is ready to receive data.
19998b19252SAmit Shah  */
20098b19252SAmit Shah size_t virtio_serial_guest_ready(VirtIOSerialPort *port);
20198b19252SAmit Shah 
2029ed7b059SAmit Shah /*
2039ed7b059SAmit Shah  * Flow control: Ports can signal to the virtio-serial core to stop
2049ed7b059SAmit Shah  * sending data or re-start sending data, depending on the 'throttle'
2059ed7b059SAmit Shah  * value here.
2069ed7b059SAmit Shah  */
2079ed7b059SAmit Shah void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle);
2089ed7b059SAmit Shah 
2092cd2b016SKONRAD Frederic #define TYPE_VIRTIO_SERIAL "virtio-serial-device"
2102cd2b016SKONRAD Frederic #define VIRTIO_SERIAL(obj) \
2112cd2b016SKONRAD Frederic         OBJECT_CHECK(VirtIOSerial, (obj), TYPE_VIRTIO_SERIAL)
2122cd2b016SKONRAD Frederic 
2132cd2b016SKONRAD Frederic #define DEFINE_VIRTIO_SERIAL_PROPERTIES(_state, _field) \
2142cd2b016SKONRAD Frederic         DEFINE_PROP_UINT32("max_ports", _state, _field.max_virtserial_ports, 31)
2152cd2b016SKONRAD Frederic 
21698b19252SAmit Shah #endif
217