Statistics
| Branch: | Revision:

root / hw / virtio-serial-bus.c @ 26d53979

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