Statistics
| Branch: | Revision:

root / hw / virtio-serial-bus.c @ ff9d2afa

History | View | Annotate | Download (27.5 kB)

1 98b19252 Amit Shah
/*
2 98b19252 Amit Shah
 * A bus for connecting virtio serial and console ports
3 98b19252 Amit Shah
 *
4 71c092e9 Amit Shah
 * Copyright (C) 2009, 2010 Red Hat, Inc.
5 98b19252 Amit Shah
 *
6 98b19252 Amit Shah
 * Author(s):
7 98b19252 Amit Shah
 *  Amit Shah <amit.shah@redhat.com>
8 98b19252 Amit Shah
 *
9 98b19252 Amit Shah
 * Some earlier parts are:
10 98b19252 Amit Shah
 *  Copyright IBM, Corp. 2008
11 98b19252 Amit Shah
 * authored by
12 98b19252 Amit Shah
 *  Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
13 98b19252 Amit Shah
 *
14 98b19252 Amit Shah
 * This work is licensed under the terms of the GNU GPL, version 2.  See
15 98b19252 Amit Shah
 * the COPYING file in the top-level directory.
16 6b620ca3 Paolo Bonzini
 *
17 6b620ca3 Paolo Bonzini
 * Contributions after 2012-01-13 are licensed under the terms of the
18 6b620ca3 Paolo Bonzini
 * GNU GPL, version 2 or (at your option) any later version.
19 98b19252 Amit Shah
 */
20 98b19252 Amit Shah
21 e4d5639d Amit Shah
#include "iov.h"
22 98b19252 Amit Shah
#include "monitor.h"
23 98b19252 Amit Shah
#include "qemu-queue.h"
24 98b19252 Amit Shah
#include "sysbus.h"
25 49e3fdd7 Amit Shah
#include "trace.h"
26 98b19252 Amit Shah
#include "virtio-serial.h"
27 98b19252 Amit Shah
28 98b19252 Amit Shah
/* The virtio-serial bus on top of which the ports will ride as devices */
29 98b19252 Amit Shah
struct VirtIOSerialBus {
30 98b19252 Amit Shah
    BusState qbus;
31 98b19252 Amit Shah
32 98b19252 Amit Shah
    /* This is the parent device that provides the bus for ports. */
33 98b19252 Amit Shah
    VirtIOSerial *vser;
34 98b19252 Amit Shah
35 98b19252 Amit Shah
    /* The maximum number of ports that can ride on top of this bus */
36 98b19252 Amit Shah
    uint32_t max_nr_ports;
37 98b19252 Amit Shah
};
38 98b19252 Amit Shah
39 98b19252 Amit Shah
struct VirtIOSerial {
40 98b19252 Amit Shah
    VirtIODevice vdev;
41 98b19252 Amit Shah
42 98b19252 Amit Shah
    VirtQueue *c_ivq, *c_ovq;
43 98b19252 Amit Shah
    /* Arrays of ivqs and ovqs: one per port */
44 98b19252 Amit Shah
    VirtQueue **ivqs, **ovqs;
45 98b19252 Amit Shah
46 5e52e5f9 Markus Armbruster
    VirtIOSerialBus bus;
47 98b19252 Amit Shah
48 8b53a865 Amit Shah
    DeviceState *qdev;
49 8b53a865 Amit Shah
50 98b19252 Amit Shah
    QTAILQ_HEAD(, VirtIOSerialPort) ports;
51 055b889f Amit Shah
52 055b889f Amit Shah
    /* bitmap for identifying active ports */
53 055b889f Amit Shah
    uint32_t *ports_map;
54 055b889f Amit Shah
55 98b19252 Amit Shah
    struct virtio_console_config config;
56 98b19252 Amit Shah
};
57 98b19252 Amit Shah
58 98b19252 Amit Shah
static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
59 98b19252 Amit Shah
{
60 98b19252 Amit Shah
    VirtIOSerialPort *port;
61 98b19252 Amit Shah
62 055b889f Amit Shah
    if (id == VIRTIO_CONSOLE_BAD_ID) {
63 055b889f Amit Shah
        return NULL;
64 055b889f Amit Shah
    }
65 055b889f Amit Shah
66 98b19252 Amit Shah
    QTAILQ_FOREACH(port, &vser->ports, next) {
67 98b19252 Amit Shah
        if (port->id == id)
68 98b19252 Amit Shah
            return port;
69 98b19252 Amit Shah
    }
70 98b19252 Amit Shah
    return NULL;
71 98b19252 Amit Shah
}
72 98b19252 Amit Shah
73 98b19252 Amit Shah
static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
74 98b19252 Amit Shah
{
75 98b19252 Amit Shah
    VirtIOSerialPort *port;
76 98b19252 Amit Shah
77 98b19252 Amit Shah
    QTAILQ_FOREACH(port, &vser->ports, next) {
78 98b19252 Amit Shah
        if (port->ivq == vq || port->ovq == vq)
79 98b19252 Amit Shah
            return port;
80 98b19252 Amit Shah
    }
81 98b19252 Amit Shah
    return NULL;
82 98b19252 Amit Shah
}
83 98b19252 Amit Shah
84 6663a195 Amit Shah
static bool use_multiport(VirtIOSerial *vser)
85 6663a195 Amit Shah
{
86 6663a195 Amit Shah
    return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
87 6663a195 Amit Shah
}
88 6663a195 Amit Shah
89 98b19252 Amit Shah
static size_t write_to_port(VirtIOSerialPort *port,
90 98b19252 Amit Shah
                            const uint8_t *buf, size_t size)
91 98b19252 Amit Shah
{
92 98b19252 Amit Shah
    VirtQueueElement elem;
93 98b19252 Amit Shah
    VirtQueue *vq;
94 e4d5639d Amit Shah
    size_t offset;
95 98b19252 Amit Shah
96 98b19252 Amit Shah
    vq = port->ivq;
97 98b19252 Amit Shah
    if (!virtio_queue_ready(vq)) {
98 98b19252 Amit Shah
        return 0;
99 98b19252 Amit Shah
    }
100 98b19252 Amit Shah
101 e4d5639d Amit Shah
    offset = 0;
102 98b19252 Amit Shah
    while (offset < size) {
103 e4d5639d Amit Shah
        size_t len;
104 98b19252 Amit Shah
105 98b19252 Amit Shah
        if (!virtqueue_pop(vq, &elem)) {
106 98b19252 Amit Shah
            break;
107 98b19252 Amit Shah
        }
108 98b19252 Amit Shah
109 dcf6f5e1 Michael Tokarev
        len = iov_from_buf(elem.in_sg, elem.in_num, 0,
110 dcf6f5e1 Michael Tokarev
                           buf + offset, size - offset);
111 e4d5639d Amit Shah
        offset += len;
112 98b19252 Amit Shah
113 98b19252 Amit Shah
        virtqueue_push(vq, &elem, len);
114 98b19252 Amit Shah
    }
115 98b19252 Amit Shah
116 98b19252 Amit Shah
    virtio_notify(&port->vser->vdev, vq);
117 98b19252 Amit Shah
    return offset;
118 98b19252 Amit Shah
}
119 98b19252 Amit Shah
120 6bff8656 Amit Shah
static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
121 a69c7600 Amit Shah
{
122 a69c7600 Amit Shah
    VirtQueueElement elem;
123 a69c7600 Amit Shah
124 7185f931 Amit Shah
    if (!virtio_queue_ready(vq)) {
125 7185f931 Amit Shah
        return;
126 7185f931 Amit Shah
    }
127 6bff8656 Amit Shah
    while (virtqueue_pop(vq, &elem)) {
128 6bff8656 Amit Shah
        virtqueue_push(vq, &elem, 0);
129 6bff8656 Amit Shah
    }
130 6bff8656 Amit Shah
    virtio_notify(vdev, vq);
131 6bff8656 Amit Shah
}
132 6bff8656 Amit Shah
133 9ed7b059 Amit Shah
static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
134 6bff8656 Amit Shah
                                 VirtIODevice *vdev)
135 a69c7600 Amit Shah
{
136 f82e35e3 Anthony Liguori
    VirtIOSerialPortClass *vsc;
137 a15bb0d6 Markus Armbruster
138 6bff8656 Amit Shah
    assert(port);
139 fd11a78b Amit Shah
    assert(virtio_queue_ready(vq));
140 a69c7600 Amit Shah
141 f82e35e3 Anthony Liguori
    vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
142 a15bb0d6 Markus Armbruster
143 f1925dff Amit Shah
    while (!port->throttled) {
144 471344db Amit Shah
        unsigned int i;
145 a69c7600 Amit Shah
146 f1925dff Amit Shah
        /* Pop an elem only if we haven't left off a previous one mid-way */
147 f1925dff Amit Shah
        if (!port->elem.out_num) {
148 f1925dff Amit Shah
            if (!virtqueue_pop(vq, &port->elem)) {
149 f1925dff Amit Shah
                break;
150 f1925dff Amit Shah
            }
151 f1925dff Amit Shah
            port->iov_idx = 0;
152 f1925dff Amit Shah
            port->iov_offset = 0;
153 f1925dff Amit Shah
        }
154 a69c7600 Amit Shah
155 f1925dff Amit Shah
        for (i = port->iov_idx; i < port->elem.out_num; i++) {
156 f1925dff Amit Shah
            size_t buf_size;
157 f1925dff Amit Shah
            ssize_t ret;
158 f1925dff Amit Shah
159 f1925dff Amit Shah
            buf_size = port->elem.out_sg[i].iov_len - port->iov_offset;
160 f82e35e3 Anthony Liguori
            ret = vsc->have_data(port,
161 a15bb0d6 Markus Armbruster
                                  port->elem.out_sg[i].iov_base
162 a15bb0d6 Markus Armbruster
                                  + port->iov_offset,
163 a15bb0d6 Markus Armbruster
                                  buf_size);
164 f1925dff Amit Shah
            if (ret < 0 && ret != -EAGAIN) {
165 f1925dff Amit Shah
                /* We don't handle any other type of errors here */
166 f1925dff Amit Shah
                abort();
167 f1925dff Amit Shah
            }
168 f1925dff Amit Shah
            if (ret == -EAGAIN || (ret >= 0 && ret < buf_size)) {
169 ed8e5a85 Christian Borntraeger
                /*
170 ed8e5a85 Christian Borntraeger
                 * this is a temporary check until chardevs can signal to
171 ed8e5a85 Christian Borntraeger
                 * frontends that they are writable again. This prevents
172 ed8e5a85 Christian Borntraeger
                 * the console from going into throttled mode (forever)
173 ed8e5a85 Christian Borntraeger
                 * if virtio-console is connected to a pty without a
174 ed8e5a85 Christian Borntraeger
                 * listener. Otherwise the guest spins forever.
175 ed8e5a85 Christian Borntraeger
                 * We can revert this if
176 ed8e5a85 Christian Borntraeger
                 * 1: chardevs can notify frondends
177 ed8e5a85 Christian Borntraeger
                 * 2: the guest driver does not spin in these cases
178 ed8e5a85 Christian Borntraeger
                 */
179 f82e35e3 Anthony Liguori
                if (!vsc->is_console) {
180 ed8e5a85 Christian Borntraeger
                    virtio_serial_throttle_port(port, true);
181 ed8e5a85 Christian Borntraeger
                }
182 f1925dff Amit Shah
                port->iov_idx = i;
183 f1925dff Amit Shah
                if (ret > 0) {
184 f1925dff Amit Shah
                    port->iov_offset += ret;
185 f1925dff Amit Shah
                }
186 f1925dff Amit Shah
                break;
187 f1925dff Amit Shah
            }
188 f1925dff Amit Shah
            port->iov_offset = 0;
189 a69c7600 Amit Shah
        }
190 f1925dff Amit Shah
        if (port->throttled) {
191 f1925dff Amit Shah
            break;
192 f1925dff Amit Shah
        }
193 f1925dff Amit Shah
        virtqueue_push(vq, &port->elem, 0);
194 f1925dff Amit Shah
        port->elem.out_num = 0;
195 a69c7600 Amit Shah
    }
196 a69c7600 Amit Shah
    virtio_notify(vdev, vq);
197 a69c7600 Amit Shah
}
198 a69c7600 Amit Shah
199 6bff8656 Amit Shah
static void flush_queued_data(VirtIOSerialPort *port)
200 9ed7b059 Amit Shah
{
201 a1c59752 Amit Shah
    assert(port);
202 9ed7b059 Amit Shah
203 6b611d3a Amit Shah
    if (!virtio_queue_ready(port->ovq)) {
204 6b611d3a Amit Shah
        return;
205 6b611d3a Amit Shah
    }
206 6bff8656 Amit Shah
    do_flush_queued_data(port, port->ovq, &port->vser->vdev);
207 9ed7b059 Amit Shah
}
208 9ed7b059 Amit Shah
209 98b19252 Amit Shah
static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
210 98b19252 Amit Shah
{
211 98b19252 Amit Shah
    VirtQueueElement elem;
212 98b19252 Amit Shah
    VirtQueue *vq;
213 98b19252 Amit Shah
    struct virtio_console_control *cpkt;
214 98b19252 Amit Shah
215 98b19252 Amit Shah
    vq = port->vser->c_ivq;
216 98b19252 Amit Shah
    if (!virtio_queue_ready(vq)) {
217 98b19252 Amit Shah
        return 0;
218 98b19252 Amit Shah
    }
219 98b19252 Amit Shah
    if (!virtqueue_pop(vq, &elem)) {
220 98b19252 Amit Shah
        return 0;
221 98b19252 Amit Shah
    }
222 98b19252 Amit Shah
223 98b19252 Amit Shah
    cpkt = (struct virtio_console_control *)buf;
224 98b19252 Amit Shah
    stl_p(&cpkt->id, port->id);
225 98b19252 Amit Shah
    memcpy(elem.in_sg[0].iov_base, buf, len);
226 98b19252 Amit Shah
227 98b19252 Amit Shah
    virtqueue_push(vq, &elem, len);
228 98b19252 Amit Shah
    virtio_notify(&port->vser->vdev, vq);
229 98b19252 Amit Shah
    return len;
230 98b19252 Amit Shah
}
231 98b19252 Amit Shah
232 98b19252 Amit Shah
static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
233 98b19252 Amit Shah
                                 uint16_t value)
234 98b19252 Amit Shah
{
235 98b19252 Amit Shah
    struct virtio_console_control cpkt;
236 98b19252 Amit Shah
237 98b19252 Amit Shah
    stw_p(&cpkt.event, event);
238 98b19252 Amit Shah
    stw_p(&cpkt.value, value);
239 98b19252 Amit Shah
240 49e3fdd7 Amit Shah
    trace_virtio_serial_send_control_event(port->id, event, value);
241 98b19252 Amit Shah
    return send_control_msg(port, &cpkt, sizeof(cpkt));
242 98b19252 Amit Shah
}
243 98b19252 Amit Shah
244 98b19252 Amit Shah
/* Functions for use inside qemu to open and read from/write to ports */
245 98b19252 Amit Shah
int virtio_serial_open(VirtIOSerialPort *port)
246 98b19252 Amit Shah
{
247 6663a195 Amit Shah
    /* Don't allow opening an already-open port */
248 6663a195 Amit Shah
    if (port->host_connected) {
249 6663a195 Amit Shah
        return 0;
250 6663a195 Amit Shah
    }
251 6663a195 Amit Shah
    /* Send port open notification to the guest */
252 6663a195 Amit Shah
    port->host_connected = true;
253 6663a195 Amit Shah
    send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
254 6663a195 Amit Shah
255 98b19252 Amit Shah
    return 0;
256 98b19252 Amit Shah
}
257 98b19252 Amit Shah
258 98b19252 Amit Shah
int virtio_serial_close(VirtIOSerialPort *port)
259 98b19252 Amit Shah
{
260 6663a195 Amit Shah
    port->host_connected = false;
261 9ed7b059 Amit Shah
    /*
262 9ed7b059 Amit Shah
     * If there's any data the guest sent which the app didn't
263 9ed7b059 Amit Shah
     * consume, reset the throttling flag and discard the data.
264 9ed7b059 Amit Shah
     */
265 9ed7b059 Amit Shah
    port->throttled = false;
266 6bff8656 Amit Shah
    discard_vq_data(port->ovq, &port->vser->vdev);
267 9ed7b059 Amit Shah
268 6663a195 Amit Shah
    send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
269 6663a195 Amit Shah
270 98b19252 Amit Shah
    return 0;
271 98b19252 Amit Shah
}
272 98b19252 Amit Shah
273 98b19252 Amit Shah
/* Individual ports/apps call this function to write to the guest. */
274 98b19252 Amit Shah
ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
275 98b19252 Amit Shah
                            size_t size)
276 98b19252 Amit Shah
{
277 6663a195 Amit Shah
    if (!port || !port->host_connected || !port->guest_connected) {
278 6663a195 Amit Shah
        return 0;
279 6663a195 Amit Shah
    }
280 98b19252 Amit Shah
    return write_to_port(port, buf, size);
281 98b19252 Amit Shah
}
282 98b19252 Amit Shah
283 98b19252 Amit Shah
/*
284 98b19252 Amit Shah
 * Readiness of the guest to accept data on a port.
285 98b19252 Amit Shah
 * Returns max. data the guest can receive
286 98b19252 Amit Shah
 */
287 98b19252 Amit Shah
size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
288 98b19252 Amit Shah
{
289 98b19252 Amit Shah
    VirtQueue *vq = port->ivq;
290 98b19252 Amit Shah
291 98b19252 Amit Shah
    if (!virtio_queue_ready(vq) ||
292 98b19252 Amit Shah
        !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
293 98b19252 Amit Shah
        virtio_queue_empty(vq)) {
294 98b19252 Amit Shah
        return 0;
295 98b19252 Amit Shah
    }
296 6663a195 Amit Shah
    if (use_multiport(port->vser) && !port->guest_connected) {
297 6663a195 Amit Shah
        return 0;
298 6663a195 Amit Shah
    }
299 98b19252 Amit Shah
300 98b19252 Amit Shah
    if (virtqueue_avail_bytes(vq, 4096, 0)) {
301 98b19252 Amit Shah
        return 4096;
302 98b19252 Amit Shah
    }
303 98b19252 Amit Shah
    if (virtqueue_avail_bytes(vq, 1, 0)) {
304 98b19252 Amit Shah
        return 1;
305 98b19252 Amit Shah
    }
306 98b19252 Amit Shah
    return 0;
307 98b19252 Amit Shah
}
308 98b19252 Amit Shah
309 199646d8 Alon Levy
static void flush_queued_data_bh(void *opaque)
310 199646d8 Alon Levy
{
311 199646d8 Alon Levy
    VirtIOSerialPort *port = opaque;
312 199646d8 Alon Levy
313 199646d8 Alon Levy
    flush_queued_data(port);
314 199646d8 Alon Levy
}
315 199646d8 Alon Levy
316 9ed7b059 Amit Shah
void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
317 9ed7b059 Amit Shah
{
318 9ed7b059 Amit Shah
    if (!port) {
319 9ed7b059 Amit Shah
        return;
320 9ed7b059 Amit Shah
    }
321 9ed7b059 Amit Shah
322 49e3fdd7 Amit Shah
    trace_virtio_serial_throttle_port(port->id, throttle);
323 9ed7b059 Amit Shah
    port->throttled = throttle;
324 9ed7b059 Amit Shah
    if (throttle) {
325 9ed7b059 Amit Shah
        return;
326 9ed7b059 Amit Shah
    }
327 199646d8 Alon Levy
    qemu_bh_schedule(port->bh);
328 9ed7b059 Amit Shah
}
329 9ed7b059 Amit Shah
330 98b19252 Amit Shah
/* Guest wants to notify us of some event */
331 e61da14d Amit Shah
static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
332 98b19252 Amit Shah
{
333 98b19252 Amit Shah
    struct VirtIOSerialPort *port;
334 f82e35e3 Anthony Liguori
    VirtIOSerialPortClass *vsc;
335 98b19252 Amit Shah
    struct virtio_console_control cpkt, *gcpkt;
336 160600fd Amit Shah
    uint8_t *buffer;
337 160600fd Amit Shah
    size_t buffer_len;
338 98b19252 Amit Shah
339 98b19252 Amit Shah
    gcpkt = buf;
340 98b19252 Amit Shah
341 e61da14d Amit Shah
    if (len < sizeof(cpkt)) {
342 e61da14d Amit Shah
        /* The guest sent an invalid control packet */
343 e61da14d Amit Shah
        return;
344 e61da14d Amit Shah
    }
345 e61da14d Amit Shah
346 98b19252 Amit Shah
    cpkt.event = lduw_p(&gcpkt->event);
347 98b19252 Amit Shah
    cpkt.value = lduw_p(&gcpkt->value);
348 98b19252 Amit Shah
349 49e3fdd7 Amit Shah
    trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
350 49e3fdd7 Amit Shah
351 d2e4d08b Luiz Capitulino
    if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
352 4048c7c3 Amit Shah
        if (!cpkt.value) {
353 6daf194d Markus Armbruster
            error_report("virtio-serial-bus: Guest failure in adding device %s",
354 5e52e5f9 Markus Armbruster
                         vser->bus.qbus.name);
355 d2e4d08b Luiz Capitulino
            return;
356 4048c7c3 Amit Shah
        }
357 055b889f Amit Shah
        /*
358 055b889f Amit Shah
         * The device is up, we can now tell the device about all the
359 055b889f Amit Shah
         * ports we have here.
360 055b889f Amit Shah
         */
361 055b889f Amit Shah
        QTAILQ_FOREACH(port, &vser->ports, next) {
362 055b889f Amit Shah
            send_control_event(port, VIRTIO_CONSOLE_PORT_ADD, 1);
363 055b889f Amit Shah
        }
364 d2e4d08b Luiz Capitulino
        return;
365 d2e4d08b Luiz Capitulino
    }
366 055b889f Amit Shah
367 d2e4d08b Luiz Capitulino
    port = find_port_by_id(vser, ldl_p(&gcpkt->id));
368 d2e4d08b Luiz Capitulino
    if (!port) {
369 95c9cde2 Amit Shah
        error_report("virtio-serial-bus: Unexpected port id %u for device %s",
370 d2e4d08b Luiz Capitulino
                     ldl_p(&gcpkt->id), vser->bus.qbus.name);
371 d2e4d08b Luiz Capitulino
        return;
372 d2e4d08b Luiz Capitulino
    }
373 d2e4d08b Luiz Capitulino
374 49e3fdd7 Amit Shah
    trace_virtio_serial_handle_control_message_port(port->id);
375 49e3fdd7 Amit Shah
376 f82e35e3 Anthony Liguori
    vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
377 d2e4d08b Luiz Capitulino
378 d2e4d08b Luiz Capitulino
    switch(cpkt.event) {
379 98b19252 Amit Shah
    case VIRTIO_CONSOLE_PORT_READY:
380 4048c7c3 Amit Shah
        if (!cpkt.value) {
381 6daf194d Markus Armbruster
            error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
382 5e52e5f9 Markus Armbruster
                         port->id, vser->bus.qbus.name);
383 4048c7c3 Amit Shah
            break;
384 4048c7c3 Amit Shah
        }
385 98b19252 Amit Shah
        /*
386 98b19252 Amit Shah
         * Now that we know the guest asked for the port name, we're
387 98b19252 Amit Shah
         * sure the guest has initialised whatever state is necessary
388 98b19252 Amit Shah
         * for this port. Now's a good time to let the guest know if
389 98b19252 Amit Shah
         * this port is a console port so that the guest can hook it
390 98b19252 Amit Shah
         * up to hvc.
391 98b19252 Amit Shah
         */
392 f82e35e3 Anthony Liguori
        if (vsc->is_console) {
393 98b19252 Amit Shah
            send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
394 98b19252 Amit Shah
        }
395 6663a195 Amit Shah
396 160600fd Amit Shah
        if (port->name) {
397 160600fd Amit Shah
            stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
398 160600fd Amit Shah
            stw_p(&cpkt.value, 1);
399 160600fd Amit Shah
400 160600fd Amit Shah
            buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
401 7267c094 Anthony Liguori
            buffer = g_malloc(buffer_len);
402 160600fd Amit Shah
403 160600fd Amit Shah
            memcpy(buffer, &cpkt, sizeof(cpkt));
404 160600fd Amit Shah
            memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
405 160600fd Amit Shah
            buffer[buffer_len - 1] = 0;
406 160600fd Amit Shah
407 160600fd Amit Shah
            send_control_msg(port, buffer, buffer_len);
408 7267c094 Anthony Liguori
            g_free(buffer);
409 160600fd Amit Shah
        }
410 160600fd Amit Shah
411 6663a195 Amit Shah
        if (port->host_connected) {
412 6663a195 Amit Shah
            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
413 6663a195 Amit Shah
        }
414 6663a195 Amit Shah
415 98b19252 Amit Shah
        /*
416 98b19252 Amit Shah
         * When the guest has asked us for this information it means
417 98b19252 Amit Shah
         * the guest is all setup and has its virtqueues
418 98b19252 Amit Shah
         * initialised. If some app is interested in knowing about
419 98b19252 Amit Shah
         * this event, let it know.
420 98b19252 Amit Shah
         */
421 f82e35e3 Anthony Liguori
        if (vsc->guest_ready) {
422 f82e35e3 Anthony Liguori
            vsc->guest_ready(port);
423 98b19252 Amit Shah
        }
424 98b19252 Amit Shah
        break;
425 6663a195 Amit Shah
426 6663a195 Amit Shah
    case VIRTIO_CONSOLE_PORT_OPEN:
427 6663a195 Amit Shah
        port->guest_connected = cpkt.value;
428 f82e35e3 Anthony Liguori
        if (cpkt.value && vsc->guest_open) {
429 6663a195 Amit Shah
            /* Send the guest opened notification if an app is interested */
430 f82e35e3 Anthony Liguori
            vsc->guest_open(port);
431 6663a195 Amit Shah
        }
432 6663a195 Amit Shah
433 f82e35e3 Anthony Liguori
        if (!cpkt.value && vsc->guest_close) {
434 6663a195 Amit Shah
            /* Send the guest closed notification if an app is interested */
435 f82e35e3 Anthony Liguori
            vsc->guest_close(port);
436 6663a195 Amit Shah
        }
437 6663a195 Amit Shah
        break;
438 98b19252 Amit Shah
    }
439 98b19252 Amit Shah
}
440 98b19252 Amit Shah
441 98b19252 Amit Shah
static void control_in(VirtIODevice *vdev, VirtQueue *vq)
442 98b19252 Amit Shah
{
443 98b19252 Amit Shah
}
444 98b19252 Amit Shah
445 98b19252 Amit Shah
static void control_out(VirtIODevice *vdev, VirtQueue *vq)
446 98b19252 Amit Shah
{
447 98b19252 Amit Shah
    VirtQueueElement elem;
448 98b19252 Amit Shah
    VirtIOSerial *vser;
449 e61da14d Amit Shah
    uint8_t *buf;
450 e61da14d Amit Shah
    size_t len;
451 98b19252 Amit Shah
452 98b19252 Amit Shah
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
453 98b19252 Amit Shah
454 e61da14d Amit Shah
    len = 0;
455 e61da14d Amit Shah
    buf = NULL;
456 98b19252 Amit Shah
    while (virtqueue_pop(vq, &elem)) {
457 45270ad8 Michael Tokarev
        size_t cur_len;
458 e61da14d Amit Shah
459 e61da14d Amit Shah
        cur_len = iov_size(elem.out_sg, elem.out_num);
460 e61da14d Amit Shah
        /*
461 e61da14d Amit Shah
         * Allocate a new buf only if we didn't have one previously or
462 e61da14d Amit Shah
         * if the size of the buf differs
463 e61da14d Amit Shah
         */
464 e61da14d Amit Shah
        if (cur_len > len) {
465 7267c094 Anthony Liguori
            g_free(buf);
466 e61da14d Amit Shah
467 7267c094 Anthony Liguori
            buf = g_malloc(cur_len);
468 e61da14d Amit Shah
            len = cur_len;
469 e61da14d Amit Shah
        }
470 dcf6f5e1 Michael Tokarev
        iov_to_buf(elem.out_sg, elem.out_num, 0, buf, cur_len);
471 e61da14d Amit Shah
472 45270ad8 Michael Tokarev
        handle_control_message(vser, buf, cur_len);
473 1e4476aa Amit Shah
        virtqueue_push(vq, &elem, 0);
474 98b19252 Amit Shah
    }
475 7267c094 Anthony Liguori
    g_free(buf);
476 98b19252 Amit Shah
    virtio_notify(vdev, vq);
477 98b19252 Amit Shah
}
478 98b19252 Amit Shah
479 98b19252 Amit Shah
/* Guest wrote something to some port. */
480 98b19252 Amit Shah
static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
481 98b19252 Amit Shah
{
482 98b19252 Amit Shah
    VirtIOSerial *vser;
483 a69c7600 Amit Shah
    VirtIOSerialPort *port;
484 98b19252 Amit Shah
485 98b19252 Amit Shah
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
486 a69c7600 Amit Shah
    port = find_port_by_vq(vser, vq);
487 98b19252 Amit Shah
488 03ecd2c8 Amit Shah
    if (!port || !port->host_connected) {
489 6bff8656 Amit Shah
        discard_vq_data(vq, vdev);
490 6bff8656 Amit Shah
        return;
491 6bff8656 Amit Shah
    }
492 e9b382b0 Amit Shah
493 e9b382b0 Amit Shah
    if (!port->throttled) {
494 e9b382b0 Amit Shah
        do_flush_queued_data(port, vq, vdev);
495 9ed7b059 Amit Shah
        return;
496 9ed7b059 Amit Shah
    }
497 98b19252 Amit Shah
}
498 98b19252 Amit Shah
499 98b19252 Amit Shah
static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
500 98b19252 Amit Shah
{
501 98b19252 Amit Shah
}
502 98b19252 Amit Shah
503 98b19252 Amit Shah
static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
504 98b19252 Amit Shah
{
505 306eb457 Amit Shah
    VirtIOSerial *vser;
506 306eb457 Amit Shah
507 306eb457 Amit Shah
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
508 306eb457 Amit Shah
509 5e52e5f9 Markus Armbruster
    if (vser->bus.max_nr_ports > 1) {
510 ee4d45be Michael S. Tsirkin
        features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
511 ee4d45be Michael S. Tsirkin
    }
512 98b19252 Amit Shah
    return features;
513 98b19252 Amit Shah
}
514 98b19252 Amit Shah
515 98b19252 Amit Shah
/* Guest requested config info */
516 98b19252 Amit Shah
static void get_config(VirtIODevice *vdev, uint8_t *config_data)
517 98b19252 Amit Shah
{
518 98b19252 Amit Shah
    VirtIOSerial *vser;
519 98b19252 Amit Shah
520 98b19252 Amit Shah
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
521 98b19252 Amit Shah
    memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
522 98b19252 Amit Shah
}
523 98b19252 Amit Shah
524 98b19252 Amit Shah
static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
525 98b19252 Amit Shah
{
526 98b19252 Amit Shah
    struct virtio_console_config config;
527 98b19252 Amit Shah
528 98b19252 Amit Shah
    memcpy(&config, config_data, sizeof(config));
529 98b19252 Amit Shah
}
530 98b19252 Amit Shah
531 43997225 Amit Shah
static void guest_reset(VirtIOSerial *vser)
532 43997225 Amit Shah
{
533 43997225 Amit Shah
    VirtIOSerialPort *port;
534 43997225 Amit Shah
    VirtIOSerialPortClass *vsc;
535 43997225 Amit Shah
536 43997225 Amit Shah
    QTAILQ_FOREACH(port, &vser->ports, next) {
537 43997225 Amit Shah
        vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
538 43997225 Amit Shah
        if (port->guest_connected) {
539 43997225 Amit Shah
            port->guest_connected = false;
540 43997225 Amit Shah
541 43997225 Amit Shah
            if (vsc->guest_close)
542 43997225 Amit Shah
                vsc->guest_close(port);
543 43997225 Amit Shah
        }
544 43997225 Amit Shah
    }
545 43997225 Amit Shah
}
546 43997225 Amit Shah
547 62a9fbf7 Alon Levy
static void set_status(VirtIODevice *vdev, uint8_t status)
548 62a9fbf7 Alon Levy
{
549 62a9fbf7 Alon Levy
    VirtIOSerial *vser;
550 62a9fbf7 Alon Levy
    VirtIOSerialPort *port;
551 62a9fbf7 Alon Levy
552 62a9fbf7 Alon Levy
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
553 62a9fbf7 Alon Levy
    port = find_port_by_id(vser, 0);
554 62a9fbf7 Alon Levy
555 62a9fbf7 Alon Levy
    if (port && !use_multiport(port->vser)
556 62a9fbf7 Alon Levy
        && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
557 62a9fbf7 Alon Levy
        /*
558 62a9fbf7 Alon Levy
         * Non-multiport guests won't be able to tell us guest
559 62a9fbf7 Alon Levy
         * open/close status.  Such guests can only have a port at id
560 62a9fbf7 Alon Levy
         * 0, so set guest_connected for such ports as soon as guest
561 62a9fbf7 Alon Levy
         * is up.
562 62a9fbf7 Alon Levy
         */
563 62a9fbf7 Alon Levy
        port->guest_connected = true;
564 62a9fbf7 Alon Levy
    }
565 43997225 Amit Shah
    if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
566 43997225 Amit Shah
        guest_reset(vser);
567 43997225 Amit Shah
    }
568 43997225 Amit Shah
}
569 43997225 Amit Shah
570 43997225 Amit Shah
static void vser_reset(VirtIODevice *vdev)
571 43997225 Amit Shah
{
572 43997225 Amit Shah
    VirtIOSerial *vser;
573 43997225 Amit Shah
574 43997225 Amit Shah
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
575 43997225 Amit Shah
    guest_reset(vser);
576 62a9fbf7 Alon Levy
}
577 62a9fbf7 Alon Levy
578 98b19252 Amit Shah
static void virtio_serial_save(QEMUFile *f, void *opaque)
579 98b19252 Amit Shah
{
580 98b19252 Amit Shah
    VirtIOSerial *s = opaque;
581 6663a195 Amit Shah
    VirtIOSerialPort *port;
582 6663a195 Amit Shah
    uint32_t nr_active_ports;
583 5c1c9bb2 Alexey Kardashevskiy
    unsigned int i, max_nr_ports;
584 98b19252 Amit Shah
585 98b19252 Amit Shah
    /* The virtio device */
586 98b19252 Amit Shah
    virtio_save(&s->vdev, f);
587 98b19252 Amit Shah
588 98b19252 Amit Shah
    /* The config space */
589 98b19252 Amit Shah
    qemu_put_be16s(f, &s->config.cols);
590 98b19252 Amit Shah
    qemu_put_be16s(f, &s->config.rows);
591 6663a195 Amit Shah
592 055b889f Amit Shah
    qemu_put_be32s(f, &s->config.max_nr_ports);
593 055b889f Amit Shah
594 055b889f Amit Shah
    /* The ports map */
595 5c1c9bb2 Alexey Kardashevskiy
    max_nr_ports = tswap32(s->config.max_nr_ports);
596 5c1c9bb2 Alexey Kardashevskiy
    for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
597 055b889f Amit Shah
        qemu_put_be32s(f, &s->ports_map[i]);
598 055b889f Amit Shah
    }
599 6663a195 Amit Shah
600 055b889f Amit Shah
    /* Ports */
601 e245795b Amit Shah
602 6663a195 Amit Shah
    nr_active_ports = 0;
603 e245795b Amit Shah
    QTAILQ_FOREACH(port, &s->ports, next) {
604 6663a195 Amit Shah
        nr_active_ports++;
605 e245795b Amit Shah
    }
606 6663a195 Amit Shah
607 6663a195 Amit Shah
    qemu_put_be32s(f, &nr_active_ports);
608 6663a195 Amit Shah
609 6663a195 Amit Shah
    /*
610 6663a195 Amit Shah
     * Items in struct VirtIOSerialPort.
611 6663a195 Amit Shah
     */
612 6663a195 Amit Shah
    QTAILQ_FOREACH(port, &s->ports, next) {
613 37f95bf3 Amit Shah
        uint32_t elem_popped;
614 37f95bf3 Amit Shah
615 6663a195 Amit Shah
        qemu_put_be32s(f, &port->id);
616 6663a195 Amit Shah
        qemu_put_byte(f, port->guest_connected);
617 31abe21f Amit Shah
        qemu_put_byte(f, port->host_connected);
618 37f95bf3 Amit Shah
619 37f95bf3 Amit Shah
        elem_popped = 0;
620 37f95bf3 Amit Shah
        if (port->elem.out_num) {
621 37f95bf3 Amit Shah
            elem_popped = 1;
622 37f95bf3 Amit Shah
        }
623 37f95bf3 Amit Shah
        qemu_put_be32s(f, &elem_popped);
624 37f95bf3 Amit Shah
        if (elem_popped) {
625 37f95bf3 Amit Shah
            qemu_put_be32s(f, &port->iov_idx);
626 37f95bf3 Amit Shah
            qemu_put_be64s(f, &port->iov_offset);
627 37f95bf3 Amit Shah
628 37f95bf3 Amit Shah
            qemu_put_buffer(f, (unsigned char *)&port->elem,
629 37f95bf3 Amit Shah
                            sizeof(port->elem));
630 37f95bf3 Amit Shah
        }
631 6663a195 Amit Shah
    }
632 98b19252 Amit Shah
}
633 98b19252 Amit Shah
634 98b19252 Amit Shah
static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
635 98b19252 Amit Shah
{
636 98b19252 Amit Shah
    VirtIOSerial *s = opaque;
637 6663a195 Amit Shah
    VirtIOSerialPort *port;
638 f83ccb3e Markus Armbruster
    uint32_t max_nr_ports, nr_active_ports, ports_map;
639 6663a195 Amit Shah
    unsigned int i;
640 2a633c46 Orit Wassermann
    int ret;
641 98b19252 Amit Shah
642 37f95bf3 Amit Shah
    if (version_id > 3) {
643 98b19252 Amit Shah
        return -EINVAL;
644 98b19252 Amit Shah
    }
645 6663a195 Amit Shah
646 98b19252 Amit Shah
    /* The virtio device */
647 2a633c46 Orit Wassermann
    ret = virtio_load(&s->vdev, f);
648 2a633c46 Orit Wassermann
    if (ret) {
649 2a633c46 Orit Wassermann
        return ret;
650 2a633c46 Orit Wassermann
    }
651 98b19252 Amit Shah
652 98b19252 Amit Shah
    if (version_id < 2) {
653 98b19252 Amit Shah
        return 0;
654 98b19252 Amit Shah
    }
655 98b19252 Amit Shah
656 98b19252 Amit Shah
    /* The config space */
657 98b19252 Amit Shah
    qemu_get_be16s(f, &s->config.cols);
658 98b19252 Amit Shah
    qemu_get_be16s(f, &s->config.rows);
659 295587f7 Amit Shah
660 055b889f Amit Shah
    qemu_get_be32s(f, &max_nr_ports);
661 5c1c9bb2 Alexey Kardashevskiy
    tswap32s(&max_nr_ports);
662 5c1c9bb2 Alexey Kardashevskiy
    if (max_nr_ports > tswap32(s->config.max_nr_ports)) {
663 055b889f Amit Shah
        /* Source could have had more ports than us. Fail migration. */
664 295587f7 Amit Shah
        return -EINVAL;
665 295587f7 Amit Shah
    }
666 98b19252 Amit Shah
667 055b889f Amit Shah
    for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
668 f83ccb3e Markus Armbruster
        qemu_get_be32s(f, &ports_map);
669 055b889f Amit Shah
670 f83ccb3e Markus Armbruster
        if (ports_map != s->ports_map[i]) {
671 055b889f Amit Shah
            /*
672 055b889f Amit Shah
             * Ports active on source and destination don't
673 055b889f Amit Shah
             * match. Fail migration.
674 055b889f Amit Shah
             */
675 055b889f Amit Shah
            return -EINVAL;
676 055b889f Amit Shah
        }
677 e245795b Amit Shah
    }
678 e245795b Amit Shah
679 6663a195 Amit Shah
    qemu_get_be32s(f, &nr_active_ports);
680 6663a195 Amit Shah
681 6663a195 Amit Shah
    /* Items in struct VirtIOSerialPort */
682 6663a195 Amit Shah
    for (i = 0; i < nr_active_ports; i++) {
683 6663a195 Amit Shah
        uint32_t id;
684 31abe21f Amit Shah
        bool host_connected;
685 6663a195 Amit Shah
686 6663a195 Amit Shah
        id = qemu_get_be32(f);
687 6663a195 Amit Shah
        port = find_port_by_id(s, id);
688 fbe0c559 Michael S. Tsirkin
        if (!port) {
689 fbe0c559 Michael S. Tsirkin
            return -EINVAL;
690 fbe0c559 Michael S. Tsirkin
        }
691 6663a195 Amit Shah
692 6663a195 Amit Shah
        port->guest_connected = qemu_get_byte(f);
693 31abe21f Amit Shah
        host_connected = qemu_get_byte(f);
694 31abe21f Amit Shah
        if (host_connected != port->host_connected) {
695 31abe21f Amit Shah
            /*
696 31abe21f Amit Shah
             * We have to let the guest know of the host connection
697 31abe21f Amit Shah
             * status change
698 31abe21f Amit Shah
             */
699 31abe21f Amit Shah
            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
700 31abe21f Amit Shah
                               port->host_connected);
701 31abe21f Amit Shah
        }
702 37f95bf3 Amit Shah
703 37f95bf3 Amit Shah
        if (version_id > 2) {
704 37f95bf3 Amit Shah
            uint32_t elem_popped;
705 37f95bf3 Amit Shah
706 37f95bf3 Amit Shah
            qemu_get_be32s(f, &elem_popped);
707 37f95bf3 Amit Shah
            if (elem_popped) {
708 37f95bf3 Amit Shah
                qemu_get_be32s(f, &port->iov_idx);
709 37f95bf3 Amit Shah
                qemu_get_be64s(f, &port->iov_offset);
710 37f95bf3 Amit Shah
711 37f95bf3 Amit Shah
                qemu_get_buffer(f, (unsigned char *)&port->elem,
712 37f95bf3 Amit Shah
                                sizeof(port->elem));
713 37f95bf3 Amit Shah
                virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
714 37f95bf3 Amit Shah
                                 port->elem.in_num, 1);
715 37f95bf3 Amit Shah
                virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
716 37f95bf3 Amit Shah
                                 port->elem.out_num, 1);
717 37f95bf3 Amit Shah
718 37f95bf3 Amit Shah
                /*
719 37f95bf3 Amit Shah
                 *  Port was throttled on source machine.  Let's
720 37f95bf3 Amit Shah
                 *  unthrottle it here so data starts flowing again.
721 37f95bf3 Amit Shah
                 */
722 37f95bf3 Amit Shah
                virtio_serial_throttle_port(port, false);
723 37f95bf3 Amit Shah
            }
724 37f95bf3 Amit Shah
        }
725 6663a195 Amit Shah
    }
726 98b19252 Amit Shah
    return 0;
727 98b19252 Amit Shah
}
728 98b19252 Amit Shah
729 98b19252 Amit Shah
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
730 98b19252 Amit Shah
731 3cb75a7c Paolo Bonzini
static Property virtser_props[] = {
732 3cb75a7c Paolo Bonzini
    DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
733 3cb75a7c Paolo Bonzini
    DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
734 3cb75a7c Paolo Bonzini
    DEFINE_PROP_END_OF_LIST()
735 3cb75a7c Paolo Bonzini
};
736 3cb75a7c Paolo Bonzini
737 0d936928 Anthony Liguori
#define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
738 0d936928 Anthony Liguori
#define VIRTIO_SERIAL_BUS(obj) \
739 0d936928 Anthony Liguori
      OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
740 0d936928 Anthony Liguori
741 0d936928 Anthony Liguori
static void virtser_bus_class_init(ObjectClass *klass, void *data)
742 0d936928 Anthony Liguori
{
743 0d936928 Anthony Liguori
    BusClass *k = BUS_CLASS(klass);
744 0d936928 Anthony Liguori
    k->print_dev = virtser_bus_dev_print;
745 0d936928 Anthony Liguori
}
746 0d936928 Anthony Liguori
747 0d936928 Anthony Liguori
static const TypeInfo virtser_bus_info = {
748 0d936928 Anthony Liguori
    .name = TYPE_VIRTIO_SERIAL_BUS,
749 0d936928 Anthony Liguori
    .parent = TYPE_BUS,
750 0d936928 Anthony Liguori
    .instance_size = sizeof(VirtIOSerialBus),
751 0d936928 Anthony Liguori
    .class_init = virtser_bus_class_init,
752 98b19252 Amit Shah
};
753 98b19252 Amit Shah
754 98b19252 Amit Shah
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
755 98b19252 Amit Shah
{
756 a43f9c90 Gerd Hoffmann
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
757 98b19252 Amit Shah
758 021a1318 Markus Armbruster
    monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
759 021a1318 Markus Armbruster
                   indent, "", port->id,
760 021a1318 Markus Armbruster
                   port->guest_connected ? "on" : "off",
761 021a1318 Markus Armbruster
                   port->host_connected ? "on" : "off",
762 021a1318 Markus Armbruster
                   port->throttled ? "on" : "off");
763 98b19252 Amit Shah
}
764 98b19252 Amit Shah
765 055b889f Amit Shah
/* This function is only used if a port id is not provided by the user */
766 055b889f Amit Shah
static uint32_t find_free_port_id(VirtIOSerial *vser)
767 055b889f Amit Shah
{
768 5c1c9bb2 Alexey Kardashevskiy
    unsigned int i, max_nr_ports;
769 055b889f Amit Shah
770 5c1c9bb2 Alexey Kardashevskiy
    max_nr_ports = tswap32(vser->config.max_nr_ports);
771 5c1c9bb2 Alexey Kardashevskiy
    for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
772 055b889f Amit Shah
        uint32_t map, bit;
773 055b889f Amit Shah
774 055b889f Amit Shah
        map = vser->ports_map[i];
775 055b889f Amit Shah
        bit = ffs(~map);
776 055b889f Amit Shah
        if (bit) {
777 055b889f Amit Shah
            return (bit - 1) + i * 32;
778 055b889f Amit Shah
        }
779 055b889f Amit Shah
    }
780 055b889f Amit Shah
    return VIRTIO_CONSOLE_BAD_ID;
781 055b889f Amit Shah
}
782 055b889f Amit Shah
783 055b889f Amit Shah
static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
784 055b889f Amit Shah
{
785 055b889f Amit Shah
    unsigned int i;
786 055b889f Amit Shah
787 055b889f Amit Shah
    i = port_id / 32;
788 055b889f Amit Shah
    vser->ports_map[i] |= 1U << (port_id % 32);
789 055b889f Amit Shah
}
790 055b889f Amit Shah
791 055b889f Amit Shah
static void add_port(VirtIOSerial *vser, uint32_t port_id)
792 055b889f Amit Shah
{
793 055b889f Amit Shah
    mark_port_added(vser, port_id);
794 055b889f Amit Shah
795 055b889f Amit Shah
    send_control_event(find_port_by_id(vser, port_id),
796 055b889f Amit Shah
                       VIRTIO_CONSOLE_PORT_ADD, 1);
797 055b889f Amit Shah
}
798 055b889f Amit Shah
799 055b889f Amit Shah
static void remove_port(VirtIOSerial *vser, uint32_t port_id)
800 055b889f Amit Shah
{
801 9ed7b059 Amit Shah
    VirtIOSerialPort *port;
802 055b889f Amit Shah
    unsigned int i;
803 055b889f Amit Shah
804 055b889f Amit Shah
    i = port_id / 32;
805 055b889f Amit Shah
    vser->ports_map[i] &= ~(1U << (port_id % 32));
806 055b889f Amit Shah
807 9ed7b059 Amit Shah
    port = find_port_by_id(vser, port_id);
808 9ed7b059 Amit Shah
    /* Flush out any unconsumed buffers first */
809 6bff8656 Amit Shah
    discard_vq_data(port->ovq, &port->vser->vdev);
810 9ed7b059 Amit Shah
811 9ed7b059 Amit Shah
    send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
812 055b889f Amit Shah
}
813 055b889f Amit Shah
814 d307af79 Anthony Liguori
static int virtser_port_qdev_init(DeviceState *qdev)
815 98b19252 Amit Shah
{
816 a43f9c90 Gerd Hoffmann
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
817 f82e35e3 Anthony Liguori
    VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
818 98b19252 Amit Shah
    VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
819 5c1c9bb2 Alexey Kardashevskiy
    int ret, max_nr_ports;
820 98b19252 Amit Shah
    bool plugging_port0;
821 98b19252 Amit Shah
822 98b19252 Amit Shah
    port->vser = bus->vser;
823 199646d8 Alon Levy
    port->bh = qemu_bh_new(flush_queued_data_bh, port);
824 98b19252 Amit Shah
825 f82e35e3 Anthony Liguori
    assert(vsc->have_data);
826 03ecd2c8 Amit Shah
827 98b19252 Amit Shah
    /*
828 98b19252 Amit Shah
     * Is the first console port we're seeing? If so, put it up at
829 98b19252 Amit Shah
     * location 0. This is done for backward compatibility (old
830 98b19252 Amit Shah
     * kernel, new qemu).
831 98b19252 Amit Shah
     */
832 f82e35e3 Anthony Liguori
    plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
833 98b19252 Amit Shah
834 055b889f Amit Shah
    if (find_port_by_id(port->vser, port->id)) {
835 6daf194d Markus Armbruster
        error_report("virtio-serial-bus: A port already exists at id %u",
836 055b889f Amit Shah
                     port->id);
837 98b19252 Amit Shah
        return -1;
838 98b19252 Amit Shah
    }
839 98b19252 Amit Shah
840 055b889f Amit Shah
    if (port->id == VIRTIO_CONSOLE_BAD_ID) {
841 055b889f Amit Shah
        if (plugging_port0) {
842 055b889f Amit Shah
            port->id = 0;
843 055b889f Amit Shah
        } else {
844 055b889f Amit Shah
            port->id = find_free_port_id(port->vser);
845 055b889f Amit Shah
            if (port->id == VIRTIO_CONSOLE_BAD_ID) {
846 6daf194d Markus Armbruster
                error_report("virtio-serial-bus: Maximum port limit for this device reached");
847 055b889f Amit Shah
                return -1;
848 055b889f Amit Shah
            }
849 055b889f Amit Shah
        }
850 055b889f Amit Shah
    }
851 055b889f Amit Shah
852 5c1c9bb2 Alexey Kardashevskiy
    max_nr_ports = tswap32(port->vser->config.max_nr_ports);
853 5c1c9bb2 Alexey Kardashevskiy
    if (port->id >= max_nr_ports) {
854 6daf194d Markus Armbruster
        error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
855 5c1c9bb2 Alexey Kardashevskiy
                     max_nr_ports - 1);
856 055b889f Amit Shah
        return -1;
857 055b889f Amit Shah
    }
858 055b889f Amit Shah
859 f82e35e3 Anthony Liguori
    ret = vsc->init(port);
860 98b19252 Amit Shah
    if (ret) {
861 98b19252 Amit Shah
        return ret;
862 98b19252 Amit Shah
    }
863 98b19252 Amit Shah
864 f1925dff Amit Shah
    port->elem.out_num = 0;
865 f1925dff Amit Shah
866 98b19252 Amit Shah
    QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
867 98b19252 Amit Shah
    port->ivq = port->vser->ivqs[port->id];
868 98b19252 Amit Shah
    port->ovq = port->vser->ovqs[port->id];
869 98b19252 Amit Shah
870 055b889f Amit Shah
    add_port(port->vser, port->id);
871 055b889f Amit Shah
872 98b19252 Amit Shah
    /* Send an update to the guest about this new port added */
873 98b19252 Amit Shah
    virtio_notify_config(&port->vser->vdev);
874 98b19252 Amit Shah
875 98b19252 Amit Shah
    return ret;
876 98b19252 Amit Shah
}
877 98b19252 Amit Shah
878 98b19252 Amit Shah
static int virtser_port_qdev_exit(DeviceState *qdev)
879 98b19252 Amit Shah
{
880 a43f9c90 Gerd Hoffmann
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
881 f82e35e3 Anthony Liguori
    VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
882 98b19252 Amit Shah
    VirtIOSerial *vser = port->vser;
883 98b19252 Amit Shah
884 199646d8 Alon Levy
    qemu_bh_delete(port->bh);
885 055b889f Amit Shah
    remove_port(port->vser, port->id);
886 f146ec9a Amit Shah
887 98b19252 Amit Shah
    QTAILQ_REMOVE(&vser->ports, port, next);
888 98b19252 Amit Shah
889 f82e35e3 Anthony Liguori
    if (vsc->exit) {
890 f82e35e3 Anthony Liguori
        vsc->exit(port);
891 a15bb0d6 Markus Armbruster
    }
892 98b19252 Amit Shah
    return 0;
893 98b19252 Amit Shah
}
894 98b19252 Amit Shah
895 6b331efb Amit Shah
VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
896 98b19252 Amit Shah
{
897 98b19252 Amit Shah
    VirtIOSerial *vser;
898 98b19252 Amit Shah
    VirtIODevice *vdev;
899 5ab4bb59 Amit Shah
    uint32_t i, max_supported_ports;
900 98b19252 Amit Shah
901 6b331efb Amit Shah
    if (!conf->max_virtserial_ports)
902 98b19252 Amit Shah
        return NULL;
903 98b19252 Amit Shah
904 5ab4bb59 Amit Shah
    /* Each port takes 2 queues, and one pair is for the control queue */
905 5ab4bb59 Amit Shah
    max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1;
906 5ab4bb59 Amit Shah
907 6b331efb Amit Shah
    if (conf->max_virtserial_ports > max_supported_ports) {
908 5ab4bb59 Amit Shah
        error_report("maximum ports supported: %u", max_supported_ports);
909 5ab4bb59 Amit Shah
        return NULL;
910 5ab4bb59 Amit Shah
    }
911 5ab4bb59 Amit Shah
912 98b19252 Amit Shah
    vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
913 98b19252 Amit Shah
                              sizeof(struct virtio_console_config),
914 98b19252 Amit Shah
                              sizeof(VirtIOSerial));
915 98b19252 Amit Shah
916 98b19252 Amit Shah
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
917 98b19252 Amit Shah
918 98b19252 Amit Shah
    /* Spawn a new virtio-serial bus on which the ports will ride as devices */
919 0d936928 Anthony Liguori
    qbus_create_inplace(&vser->bus.qbus, TYPE_VIRTIO_SERIAL_BUS, dev, NULL);
920 5e52e5f9 Markus Armbruster
    vser->bus.qbus.allow_hotplug = 1;
921 5e52e5f9 Markus Armbruster
    vser->bus.vser = vser;
922 98b19252 Amit Shah
    QTAILQ_INIT(&vser->ports);
923 98b19252 Amit Shah
924 5e52e5f9 Markus Armbruster
    vser->bus.max_nr_ports = conf->max_virtserial_ports;
925 7267c094 Anthony Liguori
    vser->ivqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
926 7267c094 Anthony Liguori
    vser->ovqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
927 98b19252 Amit Shah
928 98b19252 Amit Shah
    /* Add a queue for host to guest transfers for port 0 (backward compat) */
929 98b19252 Amit Shah
    vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
930 98b19252 Amit Shah
    /* Add a queue for guest to host transfers for port 0 (backward compat) */
931 98b19252 Amit Shah
    vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
932 98b19252 Amit Shah
933 a01a9cb8 Amit Shah
    /* TODO: host to guest notifications can get dropped
934 a01a9cb8 Amit Shah
     * if the queue fills up. Implement queueing in host,
935 a01a9cb8 Amit Shah
     * this might also make it possible to reduce the control
936 a01a9cb8 Amit Shah
     * queue size: as guest preposts buffers there,
937 a01a9cb8 Amit Shah
     * this will save 4Kbyte of guest memory per entry. */
938 a01a9cb8 Amit Shah
939 98b19252 Amit Shah
    /* control queue: host to guest */
940 a01a9cb8 Amit Shah
    vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
941 98b19252 Amit Shah
    /* control queue: guest to host */
942 a01a9cb8 Amit Shah
    vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
943 98b19252 Amit Shah
944 5e52e5f9 Markus Armbruster
    for (i = 1; i < vser->bus.max_nr_ports; i++) {
945 98b19252 Amit Shah
        /* Add a per-port queue for host to guest transfers */
946 98b19252 Amit Shah
        vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
947 98b19252 Amit Shah
        /* Add a per-per queue for guest to host transfers */
948 98b19252 Amit Shah
        vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
949 98b19252 Amit Shah
    }
950 98b19252 Amit Shah
951 5c1c9bb2 Alexey Kardashevskiy
    vser->config.max_nr_ports = tswap32(conf->max_virtserial_ports);
952 7267c094 Anthony Liguori
    vser->ports_map = g_malloc0(((conf->max_virtserial_ports + 31) / 32)
953 a132a679 Alon Levy
        * sizeof(vser->ports_map[0]));
954 98b19252 Amit Shah
    /*
955 98b19252 Amit Shah
     * Reserve location 0 for a console port for backward compat
956 98b19252 Amit Shah
     * (old kernel, new qemu)
957 98b19252 Amit Shah
     */
958 055b889f Amit Shah
    mark_port_added(vser, 0);
959 98b19252 Amit Shah
960 98b19252 Amit Shah
    vser->vdev.get_features = get_features;
961 98b19252 Amit Shah
    vser->vdev.get_config = get_config;
962 98b19252 Amit Shah
    vser->vdev.set_config = set_config;
963 62a9fbf7 Alon Levy
    vser->vdev.set_status = set_status;
964 43997225 Amit Shah
    vser->vdev.reset = vser_reset;
965 98b19252 Amit Shah
966 8b53a865 Amit Shah
    vser->qdev = dev;
967 8b53a865 Amit Shah
968 98b19252 Amit Shah
    /*
969 98b19252 Amit Shah
     * Register for the savevm section with the virtio-console name
970 98b19252 Amit Shah
     * to preserve backward compat
971 98b19252 Amit Shah
     */
972 37f95bf3 Amit Shah
    register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
973 98b19252 Amit Shah
                    virtio_serial_load, vser);
974 98b19252 Amit Shah
975 98b19252 Amit Shah
    return vdev;
976 98b19252 Amit Shah
}
977 8b53a865 Amit Shah
978 8b53a865 Amit Shah
void virtio_serial_exit(VirtIODevice *vdev)
979 8b53a865 Amit Shah
{
980 8b53a865 Amit Shah
    VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
981 8b53a865 Amit Shah
982 8b53a865 Amit Shah
    unregister_savevm(vser->qdev, "virtio-console", vser);
983 8b53a865 Amit Shah
984 7267c094 Anthony Liguori
    g_free(vser->ivqs);
985 7267c094 Anthony Liguori
    g_free(vser->ovqs);
986 7267c094 Anthony Liguori
    g_free(vser->ports_map);
987 8b53a865 Amit Shah
988 8b53a865 Amit Shah
    virtio_cleanup(vdev);
989 8b53a865 Amit Shah
}
990 f82e35e3 Anthony Liguori
991 39bffca2 Anthony Liguori
static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
992 39bffca2 Anthony Liguori
{
993 39bffca2 Anthony Liguori
    DeviceClass *k = DEVICE_CLASS(klass);
994 39bffca2 Anthony Liguori
    k->init = virtser_port_qdev_init;
995 0d936928 Anthony Liguori
    k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
996 39bffca2 Anthony Liguori
    k->exit = virtser_port_qdev_exit;
997 39bffca2 Anthony Liguori
    k->unplug = qdev_simple_unplug_cb;
998 bce54474 Paolo Bonzini
    k->props = virtser_props;
999 39bffca2 Anthony Liguori
}
1000 39bffca2 Anthony Liguori
1001 f82e35e3 Anthony Liguori
static TypeInfo virtio_serial_port_type_info = {
1002 f82e35e3 Anthony Liguori
    .name = TYPE_VIRTIO_SERIAL_PORT,
1003 f82e35e3 Anthony Liguori
    .parent = TYPE_DEVICE,
1004 f82e35e3 Anthony Liguori
    .instance_size = sizeof(VirtIOSerialPort),
1005 f82e35e3 Anthony Liguori
    .abstract = true,
1006 f82e35e3 Anthony Liguori
    .class_size = sizeof(VirtIOSerialPortClass),
1007 39bffca2 Anthony Liguori
    .class_init = virtio_serial_port_class_init,
1008 f82e35e3 Anthony Liguori
};
1009 f82e35e3 Anthony Liguori
1010 83f7d43a Andreas Färber
static void virtio_serial_register_types(void)
1011 f82e35e3 Anthony Liguori
{
1012 0d936928 Anthony Liguori
    type_register_static(&virtser_bus_info);
1013 f82e35e3 Anthony Liguori
    type_register_static(&virtio_serial_port_type_info);
1014 f82e35e3 Anthony Liguori
}
1015 f82e35e3 Anthony Liguori
1016 83f7d43a Andreas Färber
type_init(virtio_serial_register_types)