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" 1983c9f4caSPaolo Bonzini #include "hw/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 /* 84f82e35e3SAnthony Liguori * The per-port (or per-app) init function that's called when a 85f82e35e3SAnthony Liguori * new device is found on the bus. 86f82e35e3SAnthony Liguori */ 87f82e35e3SAnthony Liguori int (*init)(VirtIOSerialPort *port); 88f82e35e3SAnthony Liguori /* 89f82e35e3SAnthony Liguori * Per-port exit function that's called when a port gets 90f82e35e3SAnthony Liguori * hot-unplugged or removed. 91f82e35e3SAnthony Liguori */ 92f82e35e3SAnthony Liguori int (*exit)(VirtIOSerialPort *port); 93f82e35e3SAnthony Liguori 94f82e35e3SAnthony Liguori /* Callbacks for guest events */ 95f82e35e3SAnthony Liguori /* Guest opened device. */ 96f82e35e3SAnthony Liguori void (*guest_open)(VirtIOSerialPort *port); 97f82e35e3SAnthony Liguori /* Guest closed device. */ 98f82e35e3SAnthony Liguori void (*guest_close)(VirtIOSerialPort *port); 99f82e35e3SAnthony Liguori 100f82e35e3SAnthony Liguori /* Guest is now ready to accept data (virtqueues set up). */ 101f82e35e3SAnthony Liguori void (*guest_ready)(VirtIOSerialPort *port); 102f82e35e3SAnthony Liguori 103f82e35e3SAnthony Liguori /* 104f82e35e3SAnthony Liguori * Guest wrote some data to the port. This data is handed over to 105f82e35e3SAnthony Liguori * the app via this callback. The app can return a size less than 106f82e35e3SAnthony Liguori * 'len'. In this case, throttling will be enabled for this port. 107f82e35e3SAnthony Liguori */ 108f82e35e3SAnthony Liguori ssize_t (*have_data)(VirtIOSerialPort *port, const uint8_t *buf, 109f82e35e3SAnthony Liguori size_t len); 110f82e35e3SAnthony Liguori } VirtIOSerialPortClass; 11198b19252SAmit Shah 11298b19252SAmit Shah /* 11398b19252SAmit Shah * This is the state that's shared between all the ports. Some of the 11498b19252SAmit Shah * state is configurable via command-line options. Some of it can be 11598b19252SAmit Shah * set by individual devices in their initfn routines. Some of the 11698b19252SAmit Shah * state is set by the generic qdev device init routine. 11798b19252SAmit Shah */ 11898b19252SAmit Shah struct VirtIOSerialPort { 11998b19252SAmit Shah DeviceState dev; 12098b19252SAmit Shah 12198b19252SAmit Shah QTAILQ_ENTRY(VirtIOSerialPort) next; 12298b19252SAmit Shah 12398b19252SAmit Shah /* 12498b19252SAmit Shah * This field gives us the virtio device as well as the qdev bus 12598b19252SAmit Shah * that we are associated with 12698b19252SAmit Shah */ 12798b19252SAmit Shah VirtIOSerial *vser; 12898b19252SAmit Shah 12998b19252SAmit Shah VirtQueue *ivq, *ovq; 13098b19252SAmit Shah 13198b19252SAmit Shah /* 132160600fdSAmit Shah * This name is sent to the guest and exported via sysfs. 133160600fdSAmit Shah * The guest could create symlinks based on this information. 134160600fdSAmit Shah * The name is in the reverse fqdn format, like org.qemu.console.0 135160600fdSAmit Shah */ 136160600fdSAmit Shah char *name; 137160600fdSAmit Shah 138160600fdSAmit Shah /* 13998b19252SAmit Shah * This id helps identify ports between the guest and the host. 14098b19252SAmit Shah * The guest sends a "header" with this id with each data packet 14198b19252SAmit Shah * that it sends and the host can then find out which associated 14298b19252SAmit Shah * device to send out this data to 14398b19252SAmit Shah */ 14498b19252SAmit Shah uint32_t id; 14598b19252SAmit Shah 146f1925dffSAmit Shah /* 147f1925dffSAmit Shah * This is the elem that we pop from the virtqueue. A slow 148f1925dffSAmit Shah * backend that consumes guest data (e.g. the file backend for 149f1925dffSAmit Shah * qemu chardevs) can cause the guest to block till all the output 150f1925dffSAmit Shah * is flushed. This isn't desired, so we keep a note of the last 151f1925dffSAmit Shah * element popped and continue consuming it once the backend 152f1925dffSAmit Shah * becomes writable again. 153f1925dffSAmit Shah */ 154f1925dffSAmit Shah VirtQueueElement elem; 155f1925dffSAmit Shah 156f1925dffSAmit Shah /* 157f1925dffSAmit Shah * The index and the offset into the iov buffer that was popped in 158f1925dffSAmit Shah * elem above. 159f1925dffSAmit Shah */ 160f1925dffSAmit Shah uint32_t iov_idx; 161f1925dffSAmit Shah uint64_t iov_offset; 162f1925dffSAmit Shah 163199646d8SAlon Levy /* 164199646d8SAlon Levy * When unthrottling we use a bottom-half to call flush_queued_data. 165199646d8SAlon Levy */ 166199646d8SAlon Levy QEMUBH *bh; 167199646d8SAlon Levy 1686663a195SAmit Shah /* Is the corresponding guest device open? */ 1696663a195SAmit Shah bool guest_connected; 1706663a195SAmit Shah /* Is this device open for IO on the host? */ 1716663a195SAmit Shah bool host_connected; 1729ed7b059SAmit Shah /* Do apps not want to receive data? */ 1739ed7b059SAmit Shah bool throttled; 17498b19252SAmit Shah }; 17598b19252SAmit Shah 176*f1b24e84SKONRAD Frederic /* The virtio-serial bus on top of which the ports will ride as devices */ 177*f1b24e84SKONRAD Frederic struct VirtIOSerialBus { 178*f1b24e84SKONRAD Frederic BusState qbus; 179*f1b24e84SKONRAD Frederic 180*f1b24e84SKONRAD Frederic /* This is the parent device that provides the bus for ports. */ 181*f1b24e84SKONRAD Frederic VirtIOSerial *vser; 182*f1b24e84SKONRAD Frederic 183*f1b24e84SKONRAD Frederic /* The maximum number of ports that can ride on top of this bus */ 184*f1b24e84SKONRAD Frederic uint32_t max_nr_ports; 185*f1b24e84SKONRAD Frederic }; 186*f1b24e84SKONRAD Frederic 187*f1b24e84SKONRAD Frederic typedef struct VirtIOSerialPostLoad { 188*f1b24e84SKONRAD Frederic QEMUTimer *timer; 189*f1b24e84SKONRAD Frederic uint32_t nr_active_ports; 190*f1b24e84SKONRAD Frederic struct { 191*f1b24e84SKONRAD Frederic VirtIOSerialPort *port; 192*f1b24e84SKONRAD Frederic uint8_t host_connected; 193*f1b24e84SKONRAD Frederic } *connected; 194*f1b24e84SKONRAD Frederic } VirtIOSerialPostLoad; 195*f1b24e84SKONRAD Frederic 196*f1b24e84SKONRAD Frederic struct VirtIOSerial { 197*f1b24e84SKONRAD Frederic VirtIODevice vdev; 198*f1b24e84SKONRAD Frederic 199*f1b24e84SKONRAD Frederic VirtQueue *c_ivq, *c_ovq; 200*f1b24e84SKONRAD Frederic /* Arrays of ivqs and ovqs: one per port */ 201*f1b24e84SKONRAD Frederic VirtQueue **ivqs, **ovqs; 202*f1b24e84SKONRAD Frederic 203*f1b24e84SKONRAD Frederic VirtIOSerialBus bus; 204*f1b24e84SKONRAD Frederic 205*f1b24e84SKONRAD Frederic DeviceState *qdev; 206*f1b24e84SKONRAD Frederic 207*f1b24e84SKONRAD Frederic QTAILQ_HEAD(, VirtIOSerialPort) ports; 208*f1b24e84SKONRAD Frederic 209*f1b24e84SKONRAD Frederic /* bitmap for identifying active ports */ 210*f1b24e84SKONRAD Frederic uint32_t *ports_map; 211*f1b24e84SKONRAD Frederic 212*f1b24e84SKONRAD Frederic struct virtio_console_config config; 213*f1b24e84SKONRAD Frederic 214*f1b24e84SKONRAD Frederic struct VirtIOSerialPostLoad *post_load; 215*f1b24e84SKONRAD Frederic }; 216*f1b24e84SKONRAD 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 24998b19252SAmit Shah #endif 250