Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Virtio Serial / Console Support |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2008 |
Amit Shah | 71c092e | 2010-04-27 18:04:02 +0530 | [diff] [blame] | 5 | * Copyright Red Hat, Inc. 2009, 2010 |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 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 | |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 18 | #include "qdev.h" |
| 19 | #include "virtio.h" |
| 20 | |
| 21 | /* == Interface shared between the guest kernel and qemu == */ |
| 22 | |
| 23 | /* The Virtio ID for virtio console / serial ports */ |
| 24 | #define VIRTIO_ID_CONSOLE 3 |
| 25 | |
| 26 | /* Features supported */ |
| 27 | #define VIRTIO_CONSOLE_F_MULTIPORT 1 |
| 28 | |
Amit Shah | 055b889 | 2010-04-27 18:03:59 +0530 | [diff] [blame] | 29 | #define VIRTIO_CONSOLE_BAD_ID (~(uint32_t)0) |
| 30 | |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 31 | struct virtio_console_config { |
| 32 | /* |
| 33 | * These two fields are used by VIRTIO_CONSOLE_F_SIZE which |
| 34 | * isn't implemented here yet |
| 35 | */ |
| 36 | uint16_t cols; |
| 37 | uint16_t rows; |
| 38 | |
| 39 | uint32_t max_nr_ports; |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 40 | } __attribute__((packed)); |
| 41 | |
| 42 | struct virtio_console_control { |
| 43 | uint32_t id; /* Port number */ |
| 44 | uint16_t event; /* The kind of control event (see below) */ |
| 45 | uint16_t value; /* Extra information for the key */ |
| 46 | }; |
| 47 | |
Amit Shah | 6b331ef | 2011-02-03 11:22:32 +0530 | [diff] [blame] | 48 | struct virtio_serial_conf { |
| 49 | /* Max. number of ports we can have for a virtio-serial device */ |
| 50 | uint32_t max_virtserial_ports; |
| 51 | }; |
| 52 | |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 53 | /* Some events for the internal messages (control packets) */ |
Amit Shah | 055b889 | 2010-04-27 18:03:59 +0530 | [diff] [blame] | 54 | #define VIRTIO_CONSOLE_DEVICE_READY 0 |
| 55 | #define VIRTIO_CONSOLE_PORT_ADD 1 |
| 56 | #define VIRTIO_CONSOLE_PORT_REMOVE 2 |
| 57 | #define VIRTIO_CONSOLE_PORT_READY 3 |
| 58 | #define VIRTIO_CONSOLE_CONSOLE_PORT 4 |
| 59 | #define VIRTIO_CONSOLE_RESIZE 5 |
| 60 | #define VIRTIO_CONSOLE_PORT_OPEN 6 |
| 61 | #define VIRTIO_CONSOLE_PORT_NAME 7 |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 62 | |
| 63 | /* == In-qemu interface == */ |
| 64 | |
| 65 | typedef struct VirtIOSerial VirtIOSerial; |
| 66 | typedef struct VirtIOSerialBus VirtIOSerialBus; |
| 67 | typedef struct VirtIOSerialPort VirtIOSerialPort; |
| 68 | typedef struct VirtIOSerialPortInfo VirtIOSerialPortInfo; |
| 69 | |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 70 | /* |
| 71 | * This is the state that's shared between all the ports. Some of the |
| 72 | * state is configurable via command-line options. Some of it can be |
| 73 | * set by individual devices in their initfn routines. Some of the |
| 74 | * state is set by the generic qdev device init routine. |
| 75 | */ |
| 76 | struct VirtIOSerialPort { |
| 77 | DeviceState dev; |
| 78 | VirtIOSerialPortInfo *info; |
| 79 | |
| 80 | QTAILQ_ENTRY(VirtIOSerialPort) next; |
| 81 | |
| 82 | /* |
| 83 | * This field gives us the virtio device as well as the qdev bus |
| 84 | * that we are associated with |
| 85 | */ |
| 86 | VirtIOSerial *vser; |
| 87 | |
| 88 | VirtQueue *ivq, *ovq; |
| 89 | |
| 90 | /* |
Amit Shah | 160600f | 2010-01-20 00:36:54 +0530 | [diff] [blame] | 91 | * This name is sent to the guest and exported via sysfs. |
| 92 | * The guest could create symlinks based on this information. |
| 93 | * The name is in the reverse fqdn format, like org.qemu.console.0 |
| 94 | */ |
| 95 | char *name; |
| 96 | |
| 97 | /* |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 98 | * This id helps identify ports between the guest and the host. |
| 99 | * The guest sends a "header" with this id with each data packet |
| 100 | * that it sends and the host can then find out which associated |
| 101 | * device to send out this data to |
| 102 | */ |
| 103 | uint32_t id; |
| 104 | |
Amit Shah | f1925df | 2010-12-10 16:51:14 +0530 | [diff] [blame] | 105 | /* |
| 106 | * This is the elem that we pop from the virtqueue. A slow |
| 107 | * backend that consumes guest data (e.g. the file backend for |
| 108 | * qemu chardevs) can cause the guest to block till all the output |
| 109 | * is flushed. This isn't desired, so we keep a note of the last |
| 110 | * element popped and continue consuming it once the backend |
| 111 | * becomes writable again. |
| 112 | */ |
| 113 | VirtQueueElement elem; |
| 114 | |
| 115 | /* |
| 116 | * The index and the offset into the iov buffer that was popped in |
| 117 | * elem above. |
| 118 | */ |
| 119 | uint32_t iov_idx; |
| 120 | uint64_t iov_offset; |
| 121 | |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 122 | /* Identify if this is a port that binds with hvc in the guest */ |
| 123 | uint8_t is_console; |
Amit Shah | 6663a19 | 2010-01-20 00:36:53 +0530 | [diff] [blame] | 124 | |
| 125 | /* Is the corresponding guest device open? */ |
| 126 | bool guest_connected; |
| 127 | /* Is this device open for IO on the host? */ |
| 128 | bool host_connected; |
Amit Shah | 9ed7b05 | 2010-04-27 18:04:11 +0530 | [diff] [blame] | 129 | /* Do apps not want to receive data? */ |
| 130 | bool throttled; |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | struct VirtIOSerialPortInfo { |
| 134 | DeviceInfo qdev; |
| 135 | /* |
| 136 | * The per-port (or per-app) init function that's called when a |
| 137 | * new device is found on the bus. |
| 138 | */ |
Gerd Hoffmann | a43f9c9 | 2011-02-24 11:14:12 +0530 | [diff] [blame] | 139 | int (*init)(VirtIOSerialPort *port); |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 140 | /* |
| 141 | * Per-port exit function that's called when a port gets |
| 142 | * hot-unplugged or removed. |
| 143 | */ |
Gerd Hoffmann | a43f9c9 | 2011-02-24 11:14:12 +0530 | [diff] [blame] | 144 | int (*exit)(VirtIOSerialPort *port); |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 145 | |
| 146 | /* Callbacks for guest events */ |
| 147 | /* Guest opened device. */ |
| 148 | void (*guest_open)(VirtIOSerialPort *port); |
| 149 | /* Guest closed device. */ |
| 150 | void (*guest_close)(VirtIOSerialPort *port); |
| 151 | |
| 152 | /* Guest is now ready to accept data (virtqueues set up). */ |
| 153 | void (*guest_ready)(VirtIOSerialPort *port); |
| 154 | |
| 155 | /* |
| 156 | * Guest wrote some data to the port. This data is handed over to |
Amit Shah | e300ac2 | 2010-12-13 17:50:07 +0530 | [diff] [blame] | 157 | * the app via this callback. The app can return a size less than |
| 158 | * 'len'. In this case, throttling will be enabled for this port. |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 159 | */ |
Amit Shah | e300ac2 | 2010-12-13 17:50:07 +0530 | [diff] [blame] | 160 | ssize_t (*have_data)(VirtIOSerialPort *port, const uint8_t *buf, |
| 161 | size_t len); |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | /* Interface to the virtio-serial bus */ |
| 165 | |
| 166 | /* |
| 167 | * Individual ports/apps should call this function to register the port |
| 168 | * with the virtio-serial bus |
| 169 | */ |
| 170 | void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info); |
| 171 | |
| 172 | /* |
| 173 | * Open a connection to the port |
| 174 | * Returns 0 on success (always). |
| 175 | */ |
| 176 | int virtio_serial_open(VirtIOSerialPort *port); |
| 177 | |
| 178 | /* |
| 179 | * Close the connection to the port |
| 180 | * Returns 0 on success (always). |
| 181 | */ |
| 182 | int virtio_serial_close(VirtIOSerialPort *port); |
| 183 | |
| 184 | /* |
| 185 | * Send data to Guest |
| 186 | */ |
| 187 | ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf, |
| 188 | size_t size); |
| 189 | |
| 190 | /* |
| 191 | * Query whether a guest is ready to receive data. |
| 192 | */ |
| 193 | size_t virtio_serial_guest_ready(VirtIOSerialPort *port); |
| 194 | |
Amit Shah | 9ed7b05 | 2010-04-27 18:04:11 +0530 | [diff] [blame] | 195 | /* |
| 196 | * Flow control: Ports can signal to the virtio-serial core to stop |
| 197 | * sending data or re-start sending data, depending on the 'throttle' |
| 198 | * value here. |
| 199 | */ |
| 200 | void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle); |
| 201 | |
Amit Shah | 98b1925 | 2010-01-20 00:36:52 +0530 | [diff] [blame] | 202 | #endif |