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 1883c9f4caSPaolo Bonzini #include "hw/qdev.h" 190d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h" 2098b19252SAmit Shah 2198b19252SAmit Shah /* == Interface shared between the guest kernel and qemu == */ 2298b19252SAmit Shah 2398b19252SAmit Shah /* The Virtio ID for virtio console / serial ports */ 2498b19252SAmit Shah #define VIRTIO_ID_CONSOLE 3 2598b19252SAmit Shah 2698b19252SAmit Shah /* Features supported */ 2798b19252SAmit Shah #define VIRTIO_CONSOLE_F_MULTIPORT 1 2898b19252SAmit Shah 29055b889fSAmit Shah #define VIRTIO_CONSOLE_BAD_ID (~(uint32_t)0) 30055b889fSAmit Shah 3198b19252SAmit Shah struct virtio_console_config { 3298b19252SAmit Shah /* 3398b19252SAmit Shah * These two fields are used by VIRTIO_CONSOLE_F_SIZE which 3498b19252SAmit Shah * isn't implemented here yet 3598b19252SAmit Shah */ 3698b19252SAmit Shah uint16_t cols; 3798b19252SAmit Shah uint16_t rows; 3898b19252SAmit Shah 3998b19252SAmit Shah uint32_t max_nr_ports; 40541dc0d4SStefan Weil } QEMU_PACKED; 4198b19252SAmit Shah 4298b19252SAmit Shah struct virtio_console_control { 4398b19252SAmit Shah uint32_t id; /* Port number */ 4498b19252SAmit Shah uint16_t event; /* The kind of control event (see below) */ 4598b19252SAmit Shah uint16_t value; /* Extra information for the key */ 4698b19252SAmit Shah }; 4798b19252SAmit Shah 486b331efbSAmit Shah struct virtio_serial_conf { 496b331efbSAmit Shah /* Max. number of ports we can have for a virtio-serial device */ 506b331efbSAmit Shah uint32_t max_virtserial_ports; 516b331efbSAmit Shah }; 526b331efbSAmit Shah 5398b19252SAmit Shah /* Some events for the internal messages (control packets) */ 54055b889fSAmit Shah #define VIRTIO_CONSOLE_DEVICE_READY 0 55055b889fSAmit Shah #define VIRTIO_CONSOLE_PORT_ADD 1 56055b889fSAmit Shah #define VIRTIO_CONSOLE_PORT_REMOVE 2 57055b889fSAmit Shah #define VIRTIO_CONSOLE_PORT_READY 3 58055b889fSAmit Shah #define VIRTIO_CONSOLE_CONSOLE_PORT 4 59055b889fSAmit Shah #define VIRTIO_CONSOLE_RESIZE 5 60055b889fSAmit Shah #define VIRTIO_CONSOLE_PORT_OPEN 6 61055b889fSAmit Shah #define VIRTIO_CONSOLE_PORT_NAME 7 6298b19252SAmit Shah 6398b19252SAmit Shah /* == In-qemu interface == */ 6498b19252SAmit Shah 65f82e35e3SAnthony Liguori #define TYPE_VIRTIO_SERIAL_PORT "virtio-serial-port" 66f82e35e3SAnthony Liguori #define VIRTIO_SERIAL_PORT(obj) \ 67f82e35e3SAnthony Liguori OBJECT_CHECK(VirtIOSerialPort, (obj), TYPE_VIRTIO_SERIAL_PORT) 68f82e35e3SAnthony Liguori #define VIRTIO_SERIAL_PORT_CLASS(klass) \ 69f82e35e3SAnthony Liguori OBJECT_CLASS_CHECK(VirtIOSerialPortClass, (klass), TYPE_VIRTIO_SERIAL_PORT) 70f82e35e3SAnthony Liguori #define VIRTIO_SERIAL_PORT_GET_CLASS(obj) \ 71f82e35e3SAnthony Liguori OBJECT_GET_CLASS(VirtIOSerialPortClass, (obj), TYPE_VIRTIO_SERIAL_PORT) 72f82e35e3SAnthony Liguori 7398b19252SAmit Shah typedef struct VirtIOSerial VirtIOSerial; 7498b19252SAmit Shah typedef struct VirtIOSerialBus VirtIOSerialBus; 7598b19252SAmit Shah typedef struct VirtIOSerialPort VirtIOSerialPort; 76f82e35e3SAnthony Liguori 77f82e35e3SAnthony Liguori typedef struct VirtIOSerialPortClass { 78f82e35e3SAnthony Liguori DeviceClass parent_class; 79f82e35e3SAnthony Liguori 80f82e35e3SAnthony Liguori /* Is this a device that binds with hvc in the guest? */ 81f82e35e3SAnthony Liguori bool is_console; 82f82e35e3SAnthony Liguori 83f82e35e3SAnthony Liguori /* 842ef66625SAndreas Färber * The per-port (or per-app) realize function that's called when a 85f82e35e3SAnthony Liguori * new device is found on the bus. 86f82e35e3SAnthony Liguori */ 872ef66625SAndreas Färber DeviceRealize realize; 88f82e35e3SAnthony Liguori /* 892ef66625SAndreas Färber * Per-port unrealize function that's called when a port gets 90f82e35e3SAnthony Liguori * hot-unplugged or removed. 91f82e35e3SAnthony Liguori */ 922ef66625SAndreas Färber DeviceUnrealize unrealize; 93f82e35e3SAnthony Liguori 94f82e35e3SAnthony Liguori /* Callbacks for guest events */ 95b2c1394aSHans de Goede /* Guest opened/closed device. */ 96b2c1394aSHans de Goede void (*set_guest_connected)(VirtIOSerialPort *port, int guest_connected); 97f82e35e3SAnthony Liguori 98f82e35e3SAnthony Liguori /* Guest is now ready to accept data (virtqueues set up). */ 99f82e35e3SAnthony Liguori void (*guest_ready)(VirtIOSerialPort *port); 100f82e35e3SAnthony Liguori 101f82e35e3SAnthony Liguori /* 102f82e35e3SAnthony Liguori * Guest wrote some data to the port. This data is handed over to 103f82e35e3SAnthony Liguori * the app via this callback. The app can return a size less than 104f82e35e3SAnthony Liguori * 'len'. In this case, throttling will be enabled for this port. 105f82e35e3SAnthony Liguori */ 106f82e35e3SAnthony Liguori ssize_t (*have_data)(VirtIOSerialPort *port, const uint8_t *buf, 107f9fb0532SHans de Goede ssize_t len); 108f82e35e3SAnthony Liguori } VirtIOSerialPortClass; 10998b19252SAmit Shah 11098b19252SAmit Shah /* 11198b19252SAmit Shah * This is the state that's shared between all the ports. Some of the 11298b19252SAmit Shah * state is configurable via command-line options. Some of it can be 11398b19252SAmit Shah * set by individual devices in their initfn routines. Some of the 11498b19252SAmit Shah * state is set by the generic qdev device init routine. 11598b19252SAmit Shah */ 11698b19252SAmit Shah struct VirtIOSerialPort { 11798b19252SAmit Shah DeviceState dev; 11898b19252SAmit Shah 11998b19252SAmit Shah QTAILQ_ENTRY(VirtIOSerialPort) next; 12098b19252SAmit Shah 12198b19252SAmit Shah /* 12298b19252SAmit Shah * This field gives us the virtio device as well as the qdev bus 12398b19252SAmit Shah * that we are associated with 12498b19252SAmit Shah */ 12598b19252SAmit Shah VirtIOSerial *vser; 12698b19252SAmit Shah 12798b19252SAmit Shah VirtQueue *ivq, *ovq; 12898b19252SAmit Shah 12998b19252SAmit Shah /* 130160600fdSAmit Shah * This name is sent to the guest and exported via sysfs. 131160600fdSAmit Shah * The guest could create symlinks based on this information. 132160600fdSAmit Shah * The name is in the reverse fqdn format, like org.qemu.console.0 133160600fdSAmit Shah */ 134160600fdSAmit Shah char *name; 135160600fdSAmit Shah 136160600fdSAmit Shah /* 13798b19252SAmit Shah * This id helps identify ports between the guest and the host. 13898b19252SAmit Shah * The guest sends a "header" with this id with each data packet 13998b19252SAmit Shah * that it sends and the host can then find out which associated 14098b19252SAmit Shah * device to send out this data to 14198b19252SAmit Shah */ 14298b19252SAmit Shah uint32_t id; 14398b19252SAmit Shah 144f1925dffSAmit Shah /* 145f1925dffSAmit Shah * This is the elem that we pop from the virtqueue. A slow 146f1925dffSAmit Shah * backend that consumes guest data (e.g. the file backend for 147f1925dffSAmit Shah * qemu chardevs) can cause the guest to block till all the output 148f1925dffSAmit Shah * is flushed. This isn't desired, so we keep a note of the last 149f1925dffSAmit Shah * element popped and continue consuming it once the backend 150f1925dffSAmit Shah * becomes writable again. 151f1925dffSAmit Shah */ 152f1925dffSAmit Shah VirtQueueElement elem; 153f1925dffSAmit Shah 154f1925dffSAmit Shah /* 155f1925dffSAmit Shah * The index and the offset into the iov buffer that was popped in 156f1925dffSAmit Shah * elem above. 157f1925dffSAmit Shah */ 158f1925dffSAmit Shah uint32_t iov_idx; 159f1925dffSAmit Shah uint64_t iov_offset; 160f1925dffSAmit Shah 161199646d8SAlon Levy /* 162199646d8SAlon Levy * When unthrottling we use a bottom-half to call flush_queued_data. 163199646d8SAlon Levy */ 164199646d8SAlon Levy QEMUBH *bh; 165199646d8SAlon Levy 1666663a195SAmit Shah /* Is the corresponding guest device open? */ 1676663a195SAmit Shah bool guest_connected; 1686663a195SAmit Shah /* Is this device open for IO on the host? */ 1696663a195SAmit Shah bool host_connected; 1709ed7b059SAmit Shah /* Do apps not want to receive data? */ 1719ed7b059SAmit Shah bool throttled; 17298b19252SAmit Shah }; 17398b19252SAmit Shah 174f1b24e84SKONRAD Frederic /* The virtio-serial bus on top of which the ports will ride as devices */ 175f1b24e84SKONRAD Frederic struct VirtIOSerialBus { 176f1b24e84SKONRAD Frederic BusState qbus; 177f1b24e84SKONRAD Frederic 178f1b24e84SKONRAD Frederic /* This is the parent device that provides the bus for ports. */ 179f1b24e84SKONRAD Frederic VirtIOSerial *vser; 180f1b24e84SKONRAD Frederic 181f1b24e84SKONRAD Frederic /* The maximum number of ports that can ride on top of this bus */ 182f1b24e84SKONRAD Frederic uint32_t max_nr_ports; 183f1b24e84SKONRAD Frederic }; 184f1b24e84SKONRAD Frederic 185f1b24e84SKONRAD Frederic typedef struct VirtIOSerialPostLoad { 186f1b24e84SKONRAD Frederic QEMUTimer *timer; 187f1b24e84SKONRAD Frederic uint32_t nr_active_ports; 188f1b24e84SKONRAD Frederic struct { 189f1b24e84SKONRAD Frederic VirtIOSerialPort *port; 190f1b24e84SKONRAD Frederic uint8_t host_connected; 191f1b24e84SKONRAD Frederic } *connected; 192f1b24e84SKONRAD Frederic } VirtIOSerialPostLoad; 193f1b24e84SKONRAD Frederic 194f1b24e84SKONRAD Frederic struct VirtIOSerial { 19576017fd2SKONRAD Frederic VirtIODevice parent_obj; 196f1b24e84SKONRAD Frederic 197f1b24e84SKONRAD Frederic VirtQueue *c_ivq, *c_ovq; 198f1b24e84SKONRAD Frederic /* Arrays of ivqs and ovqs: one per port */ 199f1b24e84SKONRAD Frederic VirtQueue **ivqs, **ovqs; 200f1b24e84SKONRAD Frederic 201f1b24e84SKONRAD Frederic VirtIOSerialBus bus; 202f1b24e84SKONRAD Frederic 203f1b24e84SKONRAD Frederic QTAILQ_HEAD(, VirtIOSerialPort) ports; 204f1b24e84SKONRAD Frederic 205*a1857ad1SAmit Shah QLIST_ENTRY(VirtIOSerial) next; 206*a1857ad1SAmit Shah 207f1b24e84SKONRAD Frederic /* bitmap for identifying active ports */ 208f1b24e84SKONRAD Frederic uint32_t *ports_map; 209f1b24e84SKONRAD Frederic 210f1b24e84SKONRAD Frederic struct virtio_console_config config; 211f1b24e84SKONRAD Frederic 212f1b24e84SKONRAD Frederic struct VirtIOSerialPostLoad *post_load; 2132cd2b016SKONRAD Frederic 2142cd2b016SKONRAD Frederic virtio_serial_conf serial; 215f1b24e84SKONRAD Frederic }; 216f1b24e84SKONRAD Frederic 21798b19252SAmit Shah /* Interface to the virtio-serial bus */ 21898b19252SAmit Shah 21998b19252SAmit Shah /* 22098b19252SAmit Shah * Open a connection to the port 22198b19252SAmit Shah * Returns 0 on success (always). 22298b19252SAmit Shah */ 22398b19252SAmit Shah int virtio_serial_open(VirtIOSerialPort *port); 22498b19252SAmit Shah 22598b19252SAmit Shah /* 22698b19252SAmit Shah * Close the connection to the port 22798b19252SAmit Shah * Returns 0 on success (always). 22898b19252SAmit Shah */ 22998b19252SAmit Shah int virtio_serial_close(VirtIOSerialPort *port); 23098b19252SAmit Shah 23198b19252SAmit Shah /* 23298b19252SAmit Shah * Send data to Guest 23398b19252SAmit Shah */ 23498b19252SAmit Shah ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf, 23598b19252SAmit Shah size_t size); 23698b19252SAmit Shah 23798b19252SAmit Shah /* 23898b19252SAmit Shah * Query whether a guest is ready to receive data. 23998b19252SAmit Shah */ 24098b19252SAmit Shah size_t virtio_serial_guest_ready(VirtIOSerialPort *port); 24198b19252SAmit Shah 2429ed7b059SAmit Shah /* 2439ed7b059SAmit Shah * Flow control: Ports can signal to the virtio-serial core to stop 2449ed7b059SAmit Shah * sending data or re-start sending data, depending on the 'throttle' 2459ed7b059SAmit Shah * value here. 2469ed7b059SAmit Shah */ 2479ed7b059SAmit Shah void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle); 2489ed7b059SAmit Shah 2492cd2b016SKONRAD Frederic #define TYPE_VIRTIO_SERIAL "virtio-serial-device" 2502cd2b016SKONRAD Frederic #define VIRTIO_SERIAL(obj) \ 2512cd2b016SKONRAD Frederic OBJECT_CHECK(VirtIOSerial, (obj), TYPE_VIRTIO_SERIAL) 2522cd2b016SKONRAD Frederic 2532cd2b016SKONRAD Frederic #define DEFINE_VIRTIO_SERIAL_PROPERTIES(_state, _field) \ 2542cd2b016SKONRAD Frederic DEFINE_PROP_UINT32("max_ports", _state, _field.max_virtserial_ports, 31) 2552cd2b016SKONRAD Frederic 25698b19252SAmit Shah #endif 257