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