Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (24.9 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 d2e4d08b Luiz Capitulino
    if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
332 4048c7c3 Amit Shah
        if (!cpkt.value) {
333 6daf194d Markus Armbruster
            error_report("virtio-serial-bus: Guest failure in adding device %s",
334 5e52e5f9 Markus Armbruster
                         vser->bus.qbus.name);
335 d2e4d08b Luiz Capitulino
            return;
336 4048c7c3 Amit Shah
        }
337 055b889f Amit Shah
        /*
338 055b889f Amit Shah
         * The device is up, we can now tell the device about all the
339 055b889f Amit Shah
         * ports we have here.
340 055b889f Amit Shah
         */
341 055b889f Amit Shah
        QTAILQ_FOREACH(port, &vser->ports, next) {
342 055b889f Amit Shah
            send_control_event(port, VIRTIO_CONSOLE_PORT_ADD, 1);
343 055b889f Amit Shah
        }
344 d2e4d08b Luiz Capitulino
        return;
345 d2e4d08b Luiz Capitulino
    }
346 055b889f Amit Shah
347 d2e4d08b Luiz Capitulino
    port = find_port_by_id(vser, ldl_p(&gcpkt->id));
348 d2e4d08b Luiz Capitulino
    if (!port) {
349 d2e4d08b Luiz Capitulino
        error_report("virtio-serial-bus: Unexpected port id %u for device %s\n",
350 d2e4d08b Luiz Capitulino
                     ldl_p(&gcpkt->id), vser->bus.qbus.name);
351 d2e4d08b Luiz Capitulino
        return;
352 d2e4d08b Luiz Capitulino
    }
353 d2e4d08b Luiz Capitulino
354 d2e4d08b Luiz Capitulino
    info = DO_UPCAST(VirtIOSerialPortInfo, qdev, port->dev.info);
355 d2e4d08b Luiz Capitulino
356 d2e4d08b Luiz Capitulino
    switch(cpkt.event) {
357 98b19252 Amit Shah
    case VIRTIO_CONSOLE_PORT_READY:
358 4048c7c3 Amit Shah
        if (!cpkt.value) {
359 6daf194d Markus Armbruster
            error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
360 5e52e5f9 Markus Armbruster
                         port->id, vser->bus.qbus.name);
361 4048c7c3 Amit Shah
            break;
362 4048c7c3 Amit Shah
        }
363 98b19252 Amit Shah
        /*
364 98b19252 Amit Shah
         * Now that we know the guest asked for the port name, we're
365 98b19252 Amit Shah
         * sure the guest has initialised whatever state is necessary
366 98b19252 Amit Shah
         * for this port. Now's a good time to let the guest know if
367 98b19252 Amit Shah
         * this port is a console port so that the guest can hook it
368 98b19252 Amit Shah
         * up to hvc.
369 98b19252 Amit Shah
         */
370 a15bb0d6 Markus Armbruster
        if (info->is_console) {
371 98b19252 Amit Shah
            send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
372 98b19252 Amit Shah
        }
373 6663a195 Amit Shah
374 160600fd Amit Shah
        if (port->name) {
375 160600fd Amit Shah
            stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
376 160600fd Amit Shah
            stw_p(&cpkt.value, 1);
377 160600fd Amit Shah
378 160600fd Amit Shah
            buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
379 160600fd Amit Shah
            buffer = qemu_malloc(buffer_len);
380 160600fd Amit Shah
381 160600fd Amit Shah
            memcpy(buffer, &cpkt, sizeof(cpkt));
382 160600fd Amit Shah
            memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
383 160600fd Amit Shah
            buffer[buffer_len - 1] = 0;
384 160600fd Amit Shah
385 160600fd Amit Shah
            send_control_msg(port, buffer, buffer_len);
386 160600fd Amit Shah
            qemu_free(buffer);
387 160600fd Amit Shah
        }
388 160600fd Amit Shah
389 6663a195 Amit Shah
        if (port->host_connected) {
390 6663a195 Amit Shah
            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
391 6663a195 Amit Shah
        }
392 6663a195 Amit Shah
393 98b19252 Amit Shah
        /*
394 98b19252 Amit Shah
         * When the guest has asked us for this information it means
395 98b19252 Amit Shah
         * the guest is all setup and has its virtqueues
396 98b19252 Amit Shah
         * initialised. If some app is interested in knowing about
397 98b19252 Amit Shah
         * this event, let it know.
398 98b19252 Amit Shah
         */
399 a15bb0d6 Markus Armbruster
        if (info->guest_ready) {
400 a15bb0d6 Markus Armbruster
            info->guest_ready(port);
401 98b19252 Amit Shah
        }
402 98b19252 Amit Shah
        break;
403 6663a195 Amit Shah
404 6663a195 Amit Shah
    case VIRTIO_CONSOLE_PORT_OPEN:
405 6663a195 Amit Shah
        port->guest_connected = cpkt.value;
406 a15bb0d6 Markus Armbruster
        if (cpkt.value && info->guest_open) {
407 6663a195 Amit Shah
            /* Send the guest opened notification if an app is interested */
408 a15bb0d6 Markus Armbruster
            info->guest_open(port);
409 6663a195 Amit Shah
        }
410 6663a195 Amit Shah
411 a15bb0d6 Markus Armbruster
        if (!cpkt.value && info->guest_close) {
412 6663a195 Amit Shah
            /* Send the guest closed notification if an app is interested */
413 a15bb0d6 Markus Armbruster
            info->guest_close(port);
414 6663a195 Amit Shah
        }
415 6663a195 Amit Shah
        break;
416 98b19252 Amit Shah
    }
417 98b19252 Amit Shah
}
418 98b19252 Amit Shah
419 98b19252 Amit Shah
static void control_in(VirtIODevice *vdev, VirtQueue *vq)
420 98b19252 Amit Shah
{
421 98b19252 Amit Shah
}
422 98b19252 Amit Shah
423 98b19252 Amit Shah
static void control_out(VirtIODevice *vdev, VirtQueue *vq)
424 98b19252 Amit Shah
{
425 98b19252 Amit Shah
    VirtQueueElement elem;
426 98b19252 Amit Shah
    VirtIOSerial *vser;
427 e61da14d Amit Shah
    uint8_t *buf;
428 e61da14d Amit Shah
    size_t len;
429 98b19252 Amit Shah
430 98b19252 Amit Shah
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
431 98b19252 Amit Shah
432 e61da14d Amit Shah
    len = 0;
433 e61da14d Amit Shah
    buf = NULL;
434 98b19252 Amit Shah
    while (virtqueue_pop(vq, &elem)) {
435 e61da14d Amit Shah
        size_t cur_len, copied;
436 e61da14d Amit Shah
437 e61da14d Amit Shah
        cur_len = iov_size(elem.out_sg, elem.out_num);
438 e61da14d Amit Shah
        /*
439 e61da14d Amit Shah
         * Allocate a new buf only if we didn't have one previously or
440 e61da14d Amit Shah
         * if the size of the buf differs
441 e61da14d Amit Shah
         */
442 e61da14d Amit Shah
        if (cur_len > len) {
443 e61da14d Amit Shah
            qemu_free(buf);
444 e61da14d Amit Shah
445 e61da14d Amit Shah
            buf = qemu_malloc(cur_len);
446 e61da14d Amit Shah
            len = cur_len;
447 e61da14d Amit Shah
        }
448 e61da14d Amit Shah
        copied = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, len);
449 e61da14d Amit Shah
450 e61da14d Amit Shah
        handle_control_message(vser, buf, copied);
451 1e4476aa Amit Shah
        virtqueue_push(vq, &elem, 0);
452 98b19252 Amit Shah
    }
453 e61da14d Amit Shah
    qemu_free(buf);
454 98b19252 Amit Shah
    virtio_notify(vdev, vq);
455 98b19252 Amit Shah
}
456 98b19252 Amit Shah
457 98b19252 Amit Shah
/* Guest wrote something to some port. */
458 98b19252 Amit Shah
static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
459 98b19252 Amit Shah
{
460 98b19252 Amit Shah
    VirtIOSerial *vser;
461 a69c7600 Amit Shah
    VirtIOSerialPort *port;
462 a15bb0d6 Markus Armbruster
    VirtIOSerialPortInfo *info;
463 98b19252 Amit Shah
464 98b19252 Amit Shah
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
465 a69c7600 Amit Shah
    port = find_port_by_vq(vser, vq);
466 a15bb0d6 Markus Armbruster
    info = port ? DO_UPCAST(VirtIOSerialPortInfo, qdev, port->dev.info) : NULL;
467 98b19252 Amit Shah
468 a15bb0d6 Markus Armbruster
    if (!port || !port->host_connected || !info->have_data) {
469 6bff8656 Amit Shah
        discard_vq_data(vq, vdev);
470 6bff8656 Amit Shah
        return;
471 6bff8656 Amit Shah
    }
472 e9b382b0 Amit Shah
473 e9b382b0 Amit Shah
    if (!port->throttled) {
474 e9b382b0 Amit Shah
        do_flush_queued_data(port, vq, vdev);
475 9ed7b059 Amit Shah
        return;
476 9ed7b059 Amit Shah
    }
477 98b19252 Amit Shah
}
478 98b19252 Amit Shah
479 98b19252 Amit Shah
static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
480 98b19252 Amit Shah
{
481 98b19252 Amit Shah
}
482 98b19252 Amit Shah
483 98b19252 Amit Shah
static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
484 98b19252 Amit Shah
{
485 306eb457 Amit Shah
    VirtIOSerial *vser;
486 306eb457 Amit Shah
487 306eb457 Amit Shah
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
488 306eb457 Amit Shah
489 5e52e5f9 Markus Armbruster
    if (vser->bus.max_nr_ports > 1) {
490 ee4d45be Michael S. Tsirkin
        features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
491 ee4d45be Michael S. Tsirkin
    }
492 98b19252 Amit Shah
    return features;
493 98b19252 Amit Shah
}
494 98b19252 Amit Shah
495 98b19252 Amit Shah
/* Guest requested config info */
496 98b19252 Amit Shah
static void get_config(VirtIODevice *vdev, uint8_t *config_data)
497 98b19252 Amit Shah
{
498 98b19252 Amit Shah
    VirtIOSerial *vser;
499 98b19252 Amit Shah
500 98b19252 Amit Shah
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
501 98b19252 Amit Shah
    memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
502 98b19252 Amit Shah
}
503 98b19252 Amit Shah
504 98b19252 Amit Shah
static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
505 98b19252 Amit Shah
{
506 98b19252 Amit Shah
    struct virtio_console_config config;
507 98b19252 Amit Shah
508 98b19252 Amit Shah
    memcpy(&config, config_data, sizeof(config));
509 98b19252 Amit Shah
}
510 98b19252 Amit Shah
511 98b19252 Amit Shah
static void virtio_serial_save(QEMUFile *f, void *opaque)
512 98b19252 Amit Shah
{
513 98b19252 Amit Shah
    VirtIOSerial *s = opaque;
514 6663a195 Amit Shah
    VirtIOSerialPort *port;
515 6663a195 Amit Shah
    uint32_t nr_active_ports;
516 5c1c9bb2 Alexey Kardashevskiy
    unsigned int i, max_nr_ports;
517 98b19252 Amit Shah
518 98b19252 Amit Shah
    /* The virtio device */
519 98b19252 Amit Shah
    virtio_save(&s->vdev, f);
520 98b19252 Amit Shah
521 98b19252 Amit Shah
    /* The config space */
522 98b19252 Amit Shah
    qemu_put_be16s(f, &s->config.cols);
523 98b19252 Amit Shah
    qemu_put_be16s(f, &s->config.rows);
524 6663a195 Amit Shah
525 055b889f Amit Shah
    qemu_put_be32s(f, &s->config.max_nr_ports);
526 055b889f Amit Shah
527 055b889f Amit Shah
    /* The ports map */
528 5c1c9bb2 Alexey Kardashevskiy
    max_nr_ports = tswap32(s->config.max_nr_ports);
529 5c1c9bb2 Alexey Kardashevskiy
    for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
530 055b889f Amit Shah
        qemu_put_be32s(f, &s->ports_map[i]);
531 055b889f Amit Shah
    }
532 6663a195 Amit Shah
533 055b889f Amit Shah
    /* Ports */
534 e245795b Amit Shah
535 6663a195 Amit Shah
    nr_active_ports = 0;
536 e245795b Amit Shah
    QTAILQ_FOREACH(port, &s->ports, next) {
537 6663a195 Amit Shah
        nr_active_ports++;
538 e245795b Amit Shah
    }
539 6663a195 Amit Shah
540 6663a195 Amit Shah
    qemu_put_be32s(f, &nr_active_ports);
541 6663a195 Amit Shah
542 6663a195 Amit Shah
    /*
543 6663a195 Amit Shah
     * Items in struct VirtIOSerialPort.
544 6663a195 Amit Shah
     */
545 6663a195 Amit Shah
    QTAILQ_FOREACH(port, &s->ports, next) {
546 37f95bf3 Amit Shah
        uint32_t elem_popped;
547 37f95bf3 Amit Shah
548 6663a195 Amit Shah
        qemu_put_be32s(f, &port->id);
549 6663a195 Amit Shah
        qemu_put_byte(f, port->guest_connected);
550 31abe21f Amit Shah
        qemu_put_byte(f, port->host_connected);
551 37f95bf3 Amit Shah
552 37f95bf3 Amit Shah
        elem_popped = 0;
553 37f95bf3 Amit Shah
        if (port->elem.out_num) {
554 37f95bf3 Amit Shah
            elem_popped = 1;
555 37f95bf3 Amit Shah
        }
556 37f95bf3 Amit Shah
        qemu_put_be32s(f, &elem_popped);
557 37f95bf3 Amit Shah
        if (elem_popped) {
558 37f95bf3 Amit Shah
            qemu_put_be32s(f, &port->iov_idx);
559 37f95bf3 Amit Shah
            qemu_put_be64s(f, &port->iov_offset);
560 37f95bf3 Amit Shah
561 37f95bf3 Amit Shah
            qemu_put_buffer(f, (unsigned char *)&port->elem,
562 37f95bf3 Amit Shah
                            sizeof(port->elem));
563 37f95bf3 Amit Shah
        }
564 6663a195 Amit Shah
    }
565 98b19252 Amit Shah
}
566 98b19252 Amit Shah
567 98b19252 Amit Shah
static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
568 98b19252 Amit Shah
{
569 98b19252 Amit Shah
    VirtIOSerial *s = opaque;
570 6663a195 Amit Shah
    VirtIOSerialPort *port;
571 f83ccb3e Markus Armbruster
    uint32_t max_nr_ports, nr_active_ports, ports_map;
572 6663a195 Amit Shah
    unsigned int i;
573 98b19252 Amit Shah
574 37f95bf3 Amit Shah
    if (version_id > 3) {
575 98b19252 Amit Shah
        return -EINVAL;
576 98b19252 Amit Shah
    }
577 6663a195 Amit Shah
578 98b19252 Amit Shah
    /* The virtio device */
579 98b19252 Amit Shah
    virtio_load(&s->vdev, f);
580 98b19252 Amit Shah
581 98b19252 Amit Shah
    if (version_id < 2) {
582 98b19252 Amit Shah
        return 0;
583 98b19252 Amit Shah
    }
584 98b19252 Amit Shah
585 98b19252 Amit Shah
    /* The config space */
586 98b19252 Amit Shah
    qemu_get_be16s(f, &s->config.cols);
587 98b19252 Amit Shah
    qemu_get_be16s(f, &s->config.rows);
588 295587f7 Amit Shah
589 055b889f Amit Shah
    qemu_get_be32s(f, &max_nr_ports);
590 5c1c9bb2 Alexey Kardashevskiy
    tswap32s(&max_nr_ports);
591 5c1c9bb2 Alexey Kardashevskiy
    if (max_nr_ports > tswap32(s->config.max_nr_ports)) {
592 055b889f Amit Shah
        /* Source could have had more ports than us. Fail migration. */
593 295587f7 Amit Shah
        return -EINVAL;
594 295587f7 Amit Shah
    }
595 98b19252 Amit Shah
596 055b889f Amit Shah
    for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
597 f83ccb3e Markus Armbruster
        qemu_get_be32s(f, &ports_map);
598 055b889f Amit Shah
599 f83ccb3e Markus Armbruster
        if (ports_map != s->ports_map[i]) {
600 055b889f Amit Shah
            /*
601 055b889f Amit Shah
             * Ports active on source and destination don't
602 055b889f Amit Shah
             * match. Fail migration.
603 055b889f Amit Shah
             */
604 055b889f Amit Shah
            return -EINVAL;
605 055b889f Amit Shah
        }
606 e245795b Amit Shah
    }
607 e245795b Amit Shah
608 6663a195 Amit Shah
    qemu_get_be32s(f, &nr_active_ports);
609 6663a195 Amit Shah
610 6663a195 Amit Shah
    /* Items in struct VirtIOSerialPort */
611 6663a195 Amit Shah
    for (i = 0; i < nr_active_ports; i++) {
612 6663a195 Amit Shah
        uint32_t id;
613 31abe21f Amit Shah
        bool host_connected;
614 6663a195 Amit Shah
615 6663a195 Amit Shah
        id = qemu_get_be32(f);
616 6663a195 Amit Shah
        port = find_port_by_id(s, id);
617 fbe0c559 Michael S. Tsirkin
        if (!port) {
618 fbe0c559 Michael S. Tsirkin
            return -EINVAL;
619 fbe0c559 Michael S. Tsirkin
        }
620 6663a195 Amit Shah
621 6663a195 Amit Shah
        port->guest_connected = qemu_get_byte(f);
622 31abe21f Amit Shah
        host_connected = qemu_get_byte(f);
623 31abe21f Amit Shah
        if (host_connected != port->host_connected) {
624 31abe21f Amit Shah
            /*
625 31abe21f Amit Shah
             * We have to let the guest know of the host connection
626 31abe21f Amit Shah
             * status change
627 31abe21f Amit Shah
             */
628 31abe21f Amit Shah
            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
629 31abe21f Amit Shah
                               port->host_connected);
630 31abe21f Amit Shah
        }
631 37f95bf3 Amit Shah
632 37f95bf3 Amit Shah
        if (version_id > 2) {
633 37f95bf3 Amit Shah
            uint32_t elem_popped;
634 37f95bf3 Amit Shah
635 37f95bf3 Amit Shah
            qemu_get_be32s(f, &elem_popped);
636 37f95bf3 Amit Shah
            if (elem_popped) {
637 37f95bf3 Amit Shah
                qemu_get_be32s(f, &port->iov_idx);
638 37f95bf3 Amit Shah
                qemu_get_be64s(f, &port->iov_offset);
639 37f95bf3 Amit Shah
640 37f95bf3 Amit Shah
                qemu_get_buffer(f, (unsigned char *)&port->elem,
641 37f95bf3 Amit Shah
                                sizeof(port->elem));
642 37f95bf3 Amit Shah
                virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
643 37f95bf3 Amit Shah
                                 port->elem.in_num, 1);
644 37f95bf3 Amit Shah
                virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
645 37f95bf3 Amit Shah
                                 port->elem.out_num, 1);
646 37f95bf3 Amit Shah
647 37f95bf3 Amit Shah
                /*
648 37f95bf3 Amit Shah
                 *  Port was throttled on source machine.  Let's
649 37f95bf3 Amit Shah
                 *  unthrottle it here so data starts flowing again.
650 37f95bf3 Amit Shah
                 */
651 37f95bf3 Amit Shah
                virtio_serial_throttle_port(port, false);
652 37f95bf3 Amit Shah
            }
653 37f95bf3 Amit Shah
        }
654 6663a195 Amit Shah
    }
655 98b19252 Amit Shah
    return 0;
656 98b19252 Amit Shah
}
657 98b19252 Amit Shah
658 98b19252 Amit Shah
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
659 98b19252 Amit Shah
660 98b19252 Amit Shah
static struct BusInfo virtser_bus_info = {
661 98b19252 Amit Shah
    .name      = "virtio-serial-bus",
662 98b19252 Amit Shah
    .size      = sizeof(VirtIOSerialBus),
663 98b19252 Amit Shah
    .print_dev = virtser_bus_dev_print,
664 98b19252 Amit Shah
};
665 98b19252 Amit Shah
666 98b19252 Amit Shah
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
667 98b19252 Amit Shah
{
668 a43f9c90 Gerd Hoffmann
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
669 98b19252 Amit Shah
670 98b19252 Amit Shah
    monitor_printf(mon, "%*s dev-prop-int: id: %u\n",
671 98b19252 Amit Shah
                   indent, "", port->id);
672 6663a195 Amit Shah
    monitor_printf(mon, "%*s dev-prop-int: guest_connected: %d\n",
673 6663a195 Amit Shah
                   indent, "", port->guest_connected);
674 6663a195 Amit Shah
    monitor_printf(mon, "%*s dev-prop-int: host_connected: %d\n",
675 6663a195 Amit Shah
                   indent, "", port->host_connected);
676 9ed7b059 Amit Shah
    monitor_printf(mon, "%*s dev-prop-int: throttled: %d\n",
677 9ed7b059 Amit Shah
                   indent, "", port->throttled);
678 98b19252 Amit Shah
}
679 98b19252 Amit Shah
680 055b889f Amit Shah
/* This function is only used if a port id is not provided by the user */
681 055b889f Amit Shah
static uint32_t find_free_port_id(VirtIOSerial *vser)
682 055b889f Amit Shah
{
683 5c1c9bb2 Alexey Kardashevskiy
    unsigned int i, max_nr_ports;
684 055b889f Amit Shah
685 5c1c9bb2 Alexey Kardashevskiy
    max_nr_ports = tswap32(vser->config.max_nr_ports);
686 5c1c9bb2 Alexey Kardashevskiy
    for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
687 055b889f Amit Shah
        uint32_t map, bit;
688 055b889f Amit Shah
689 055b889f Amit Shah
        map = vser->ports_map[i];
690 055b889f Amit Shah
        bit = ffs(~map);
691 055b889f Amit Shah
        if (bit) {
692 055b889f Amit Shah
            return (bit - 1) + i * 32;
693 055b889f Amit Shah
        }
694 055b889f Amit Shah
    }
695 055b889f Amit Shah
    return VIRTIO_CONSOLE_BAD_ID;
696 055b889f Amit Shah
}
697 055b889f Amit Shah
698 055b889f Amit Shah
static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
699 055b889f Amit Shah
{
700 055b889f Amit Shah
    unsigned int i;
701 055b889f Amit Shah
702 055b889f Amit Shah
    i = port_id / 32;
703 055b889f Amit Shah
    vser->ports_map[i] |= 1U << (port_id % 32);
704 055b889f Amit Shah
}
705 055b889f Amit Shah
706 055b889f Amit Shah
static void add_port(VirtIOSerial *vser, uint32_t port_id)
707 055b889f Amit Shah
{
708 055b889f Amit Shah
    mark_port_added(vser, port_id);
709 055b889f Amit Shah
710 055b889f Amit Shah
    send_control_event(find_port_by_id(vser, port_id),
711 055b889f Amit Shah
                       VIRTIO_CONSOLE_PORT_ADD, 1);
712 055b889f Amit Shah
}
713 055b889f Amit Shah
714 055b889f Amit Shah
static void remove_port(VirtIOSerial *vser, uint32_t port_id)
715 055b889f Amit Shah
{
716 9ed7b059 Amit Shah
    VirtIOSerialPort *port;
717 055b889f Amit Shah
    unsigned int i;
718 055b889f Amit Shah
719 055b889f Amit Shah
    i = port_id / 32;
720 055b889f Amit Shah
    vser->ports_map[i] &= ~(1U << (port_id % 32));
721 055b889f Amit Shah
722 9ed7b059 Amit Shah
    port = find_port_by_id(vser, port_id);
723 9ed7b059 Amit Shah
    /* Flush out any unconsumed buffers first */
724 6bff8656 Amit Shah
    discard_vq_data(port->ovq, &port->vser->vdev);
725 9ed7b059 Amit Shah
726 9ed7b059 Amit Shah
    send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
727 055b889f Amit Shah
}
728 055b889f Amit Shah
729 98b19252 Amit Shah
static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
730 98b19252 Amit Shah
{
731 a43f9c90 Gerd Hoffmann
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
732 98b19252 Amit Shah
    VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev, base);
733 98b19252 Amit Shah
    VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
734 5c1c9bb2 Alexey Kardashevskiy
    int ret, max_nr_ports;
735 98b19252 Amit Shah
    bool plugging_port0;
736 98b19252 Amit Shah
737 98b19252 Amit Shah
    port->vser = bus->vser;
738 199646d8 Alon Levy
    port->bh = qemu_bh_new(flush_queued_data_bh, port);
739 98b19252 Amit Shah
740 98b19252 Amit Shah
    /*
741 98b19252 Amit Shah
     * Is the first console port we're seeing? If so, put it up at
742 98b19252 Amit Shah
     * location 0. This is done for backward compatibility (old
743 98b19252 Amit Shah
     * kernel, new qemu).
744 98b19252 Amit Shah
     */
745 2a3d57ce Markus Armbruster
    plugging_port0 = info->is_console && !find_port_by_id(port->vser, 0);
746 98b19252 Amit Shah
747 055b889f Amit Shah
    if (find_port_by_id(port->vser, port->id)) {
748 6daf194d Markus Armbruster
        error_report("virtio-serial-bus: A port already exists at id %u",
749 055b889f Amit Shah
                     port->id);
750 98b19252 Amit Shah
        return -1;
751 98b19252 Amit Shah
    }
752 98b19252 Amit Shah
753 055b889f Amit Shah
    if (port->id == VIRTIO_CONSOLE_BAD_ID) {
754 055b889f Amit Shah
        if (plugging_port0) {
755 055b889f Amit Shah
            port->id = 0;
756 055b889f Amit Shah
        } else {
757 055b889f Amit Shah
            port->id = find_free_port_id(port->vser);
758 055b889f Amit Shah
            if (port->id == VIRTIO_CONSOLE_BAD_ID) {
759 6daf194d Markus Armbruster
                error_report("virtio-serial-bus: Maximum port limit for this device reached");
760 055b889f Amit Shah
                return -1;
761 055b889f Amit Shah
            }
762 055b889f Amit Shah
        }
763 055b889f Amit Shah
    }
764 055b889f Amit Shah
765 5c1c9bb2 Alexey Kardashevskiy
    max_nr_ports = tswap32(port->vser->config.max_nr_ports);
766 5c1c9bb2 Alexey Kardashevskiy
    if (port->id >= max_nr_ports) {
767 6daf194d Markus Armbruster
        error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
768 5c1c9bb2 Alexey Kardashevskiy
                     max_nr_ports - 1);
769 055b889f Amit Shah
        return -1;
770 055b889f Amit Shah
    }
771 055b889f Amit Shah
772 a43f9c90 Gerd Hoffmann
    ret = info->init(port);
773 98b19252 Amit Shah
    if (ret) {
774 98b19252 Amit Shah
        return ret;
775 98b19252 Amit Shah
    }
776 98b19252 Amit Shah
777 6663a195 Amit Shah
    if (!use_multiport(port->vser)) {
778 6663a195 Amit Shah
        /*
779 6663a195 Amit Shah
         * Allow writes to guest in this case; we have no way of
780 6663a195 Amit Shah
         * knowing if a guest port is connected.
781 6663a195 Amit Shah
         */
782 6663a195 Amit Shah
        port->guest_connected = true;
783 6663a195 Amit Shah
    }
784 6663a195 Amit Shah
785 f1925dff Amit Shah
    port->elem.out_num = 0;
786 f1925dff Amit Shah
787 98b19252 Amit Shah
    QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
788 98b19252 Amit Shah
    port->ivq = port->vser->ivqs[port->id];
789 98b19252 Amit Shah
    port->ovq = port->vser->ovqs[port->id];
790 98b19252 Amit Shah
791 055b889f Amit Shah
    add_port(port->vser, port->id);
792 055b889f Amit Shah
793 98b19252 Amit Shah
    /* Send an update to the guest about this new port added */
794 98b19252 Amit Shah
    virtio_notify_config(&port->vser->vdev);
795 98b19252 Amit Shah
796 98b19252 Amit Shah
    return ret;
797 98b19252 Amit Shah
}
798 98b19252 Amit Shah
799 98b19252 Amit Shah
static int virtser_port_qdev_exit(DeviceState *qdev)
800 98b19252 Amit Shah
{
801 a43f9c90 Gerd Hoffmann
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
802 a15bb0d6 Markus Armbruster
    VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev,
803 a15bb0d6 Markus Armbruster
                                           port->dev.info);
804 98b19252 Amit Shah
    VirtIOSerial *vser = port->vser;
805 98b19252 Amit Shah
806 199646d8 Alon Levy
    qemu_bh_delete(port->bh);
807 055b889f Amit Shah
    remove_port(port->vser, port->id);
808 f146ec9a Amit Shah
809 98b19252 Amit Shah
    QTAILQ_REMOVE(&vser->ports, port, next);
810 98b19252 Amit Shah
811 a15bb0d6 Markus Armbruster
    if (info->exit) {
812 a15bb0d6 Markus Armbruster
        info->exit(port);
813 a15bb0d6 Markus Armbruster
    }
814 98b19252 Amit Shah
    return 0;
815 98b19252 Amit Shah
}
816 98b19252 Amit Shah
817 98b19252 Amit Shah
void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info)
818 98b19252 Amit Shah
{
819 98b19252 Amit Shah
    info->qdev.init = virtser_port_qdev_init;
820 98b19252 Amit Shah
    info->qdev.bus_info = &virtser_bus_info;
821 98b19252 Amit Shah
    info->qdev.exit = virtser_port_qdev_exit;
822 98b19252 Amit Shah
    info->qdev.unplug = qdev_simple_unplug_cb;
823 98b19252 Amit Shah
    qdev_register(&info->qdev);
824 98b19252 Amit Shah
}
825 98b19252 Amit Shah
826 6b331efb Amit Shah
VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
827 98b19252 Amit Shah
{
828 98b19252 Amit Shah
    VirtIOSerial *vser;
829 98b19252 Amit Shah
    VirtIODevice *vdev;
830 5ab4bb59 Amit Shah
    uint32_t i, max_supported_ports;
831 98b19252 Amit Shah
832 6b331efb Amit Shah
    if (!conf->max_virtserial_ports)
833 98b19252 Amit Shah
        return NULL;
834 98b19252 Amit Shah
835 5ab4bb59 Amit Shah
    /* Each port takes 2 queues, and one pair is for the control queue */
836 5ab4bb59 Amit Shah
    max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1;
837 5ab4bb59 Amit Shah
838 6b331efb Amit Shah
    if (conf->max_virtserial_ports > max_supported_ports) {
839 5ab4bb59 Amit Shah
        error_report("maximum ports supported: %u", max_supported_ports);
840 5ab4bb59 Amit Shah
        return NULL;
841 5ab4bb59 Amit Shah
    }
842 5ab4bb59 Amit Shah
843 98b19252 Amit Shah
    vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
844 98b19252 Amit Shah
                              sizeof(struct virtio_console_config),
845 98b19252 Amit Shah
                              sizeof(VirtIOSerial));
846 98b19252 Amit Shah
847 98b19252 Amit Shah
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
848 98b19252 Amit Shah
849 98b19252 Amit Shah
    /* Spawn a new virtio-serial bus on which the ports will ride as devices */
850 5e52e5f9 Markus Armbruster
    qbus_create_inplace(&vser->bus.qbus, &virtser_bus_info, dev, NULL);
851 5e52e5f9 Markus Armbruster
    vser->bus.qbus.allow_hotplug = 1;
852 5e52e5f9 Markus Armbruster
    vser->bus.vser = vser;
853 98b19252 Amit Shah
    QTAILQ_INIT(&vser->ports);
854 98b19252 Amit Shah
855 5e52e5f9 Markus Armbruster
    vser->bus.max_nr_ports = conf->max_virtserial_ports;
856 6b331efb Amit Shah
    vser->ivqs = qemu_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
857 6b331efb Amit Shah
    vser->ovqs = qemu_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
858 98b19252 Amit Shah
859 98b19252 Amit Shah
    /* Add a queue for host to guest transfers for port 0 (backward compat) */
860 98b19252 Amit Shah
    vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
861 98b19252 Amit Shah
    /* Add a queue for guest to host transfers for port 0 (backward compat) */
862 98b19252 Amit Shah
    vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
863 98b19252 Amit Shah
864 a01a9cb8 Amit Shah
    /* TODO: host to guest notifications can get dropped
865 a01a9cb8 Amit Shah
     * if the queue fills up. Implement queueing in host,
866 a01a9cb8 Amit Shah
     * this might also make it possible to reduce the control
867 a01a9cb8 Amit Shah
     * queue size: as guest preposts buffers there,
868 a01a9cb8 Amit Shah
     * this will save 4Kbyte of guest memory per entry. */
869 a01a9cb8 Amit Shah
870 98b19252 Amit Shah
    /* control queue: host to guest */
871 a01a9cb8 Amit Shah
    vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
872 98b19252 Amit Shah
    /* control queue: guest to host */
873 a01a9cb8 Amit Shah
    vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
874 98b19252 Amit Shah
875 5e52e5f9 Markus Armbruster
    for (i = 1; i < vser->bus.max_nr_ports; i++) {
876 98b19252 Amit Shah
        /* Add a per-port queue for host to guest transfers */
877 98b19252 Amit Shah
        vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
878 98b19252 Amit Shah
        /* Add a per-per queue for guest to host transfers */
879 98b19252 Amit Shah
        vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
880 98b19252 Amit Shah
    }
881 98b19252 Amit Shah
882 5c1c9bb2 Alexey Kardashevskiy
    vser->config.max_nr_ports = tswap32(conf->max_virtserial_ports);
883 6b331efb Amit Shah
    vser->ports_map = qemu_mallocz(((conf->max_virtserial_ports + 31) / 32)
884 a132a679 Alon Levy
        * sizeof(vser->ports_map[0]));
885 98b19252 Amit Shah
    /*
886 98b19252 Amit Shah
     * Reserve location 0 for a console port for backward compat
887 98b19252 Amit Shah
     * (old kernel, new qemu)
888 98b19252 Amit Shah
     */
889 055b889f Amit Shah
    mark_port_added(vser, 0);
890 98b19252 Amit Shah
891 98b19252 Amit Shah
    vser->vdev.get_features = get_features;
892 98b19252 Amit Shah
    vser->vdev.get_config = get_config;
893 98b19252 Amit Shah
    vser->vdev.set_config = set_config;
894 98b19252 Amit Shah
895 8b53a865 Amit Shah
    vser->qdev = dev;
896 8b53a865 Amit Shah
897 98b19252 Amit Shah
    /*
898 98b19252 Amit Shah
     * Register for the savevm section with the virtio-console name
899 98b19252 Amit Shah
     * to preserve backward compat
900 98b19252 Amit Shah
     */
901 37f95bf3 Amit Shah
    register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
902 98b19252 Amit Shah
                    virtio_serial_load, vser);
903 98b19252 Amit Shah
904 98b19252 Amit Shah
    return vdev;
905 98b19252 Amit Shah
}
906 8b53a865 Amit Shah
907 8b53a865 Amit Shah
void virtio_serial_exit(VirtIODevice *vdev)
908 8b53a865 Amit Shah
{
909 8b53a865 Amit Shah
    VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
910 8b53a865 Amit Shah
911 8b53a865 Amit Shah
    unregister_savevm(vser->qdev, "virtio-console", vser);
912 8b53a865 Amit Shah
913 8b53a865 Amit Shah
    qemu_free(vser->ivqs);
914 8b53a865 Amit Shah
    qemu_free(vser->ovqs);
915 8b53a865 Amit Shah
    qemu_free(vser->ports_map);
916 8b53a865 Amit Shah
917 8b53a865 Amit Shah
    virtio_cleanup(vdev);
918 8b53a865 Amit Shah
}