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