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