Statistics
| Branch: | Revision:

root / hw / virtio-serial.h @ 93148aa5

History | View | Annotate | Download (5.9 kB)

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