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