]> Git Repo - qemu.git/blame - hw/char/virtio-serial-bus.c
sysbus: add irq_routing_notifier
[qemu.git] / hw / char / virtio-serial-bus.c
CommitLineData
98b19252
AS
1/*
2 * A bus for connecting virtio serial and console ports
3 *
71c092e9 4 * Copyright (C) 2009, 2010 Red Hat, Inc.
98b19252
AS
5 *
6 * Author(s):
7 * Amit Shah <[email protected]>
8 *
9 * Some earlier parts are:
10 * Copyright IBM, Corp. 2008
11 * authored by
12 * Christian Ehrhardt <[email protected]>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
6b620ca3
PB
16 *
17 * Contributions after 2012-01-13 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
98b19252
AS
19 */
20
1de7afc9 21#include "qemu/iov.h"
83c9089e 22#include "monitor/monitor.h"
d49b6836 23#include "qemu/error-report.h"
1de7afc9 24#include "qemu/queue.h"
83c9f4ca 25#include "hw/sysbus.h"
49e3fdd7 26#include "trace.h"
0d09e41a 27#include "hw/virtio/virtio-serial.h"
e0ab7fac 28#include "hw/virtio/virtio-access.h"
98b19252 29
43d73554 30static struct VirtIOSerialDevices {
a1857ad1
AS
31 QLIST_HEAD(, VirtIOSerial) devices;
32} vserdevices;
33
98b19252
AS
34static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
35{
36 VirtIOSerialPort *port;
37
055b889f
AS
38 if (id == VIRTIO_CONSOLE_BAD_ID) {
39 return NULL;
40 }
41
98b19252
AS
42 QTAILQ_FOREACH(port, &vser->ports, next) {
43 if (port->id == id)
44 return port;
45 }
46 return NULL;
47}
48
49static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
50{
51 VirtIOSerialPort *port;
52
53 QTAILQ_FOREACH(port, &vser->ports, next) {
54 if (port->ivq == vq || port->ovq == vq)
55 return port;
56 }
57 return NULL;
58}
59
d0a0bfe6
AS
60static VirtIOSerialPort *find_port_by_name(char *name)
61{
62 VirtIOSerial *vser;
63
64 QLIST_FOREACH(vser, &vserdevices.devices, next) {
65 VirtIOSerialPort *port;
66
67 QTAILQ_FOREACH(port, &vser->ports, next) {
b18a755c 68 if (port->name && !strcmp(port->name, name)) {
d0a0bfe6
AS
69 return port;
70 }
71 }
72 }
73 return NULL;
74}
75
6663a195
AS
76static bool use_multiport(VirtIOSerial *vser)
77{
76017fd2 78 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
ef546f12 79 return virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT);
6663a195
AS
80}
81
98b19252
AS
82static size_t write_to_port(VirtIOSerialPort *port,
83 const uint8_t *buf, size_t size)
84{
85 VirtQueueElement elem;
86 VirtQueue *vq;
e4d5639d 87 size_t offset;
98b19252
AS
88
89 vq = port->ivq;
90 if (!virtio_queue_ready(vq)) {
91 return 0;
92 }
98b19252 93
e4d5639d 94 offset = 0;
98b19252 95 while (offset < size) {
e4d5639d 96 size_t len;
98b19252
AS
97
98 if (!virtqueue_pop(vq, &elem)) {
99 break;
100 }
101
dcf6f5e1
MT
102 len = iov_from_buf(elem.in_sg, elem.in_num, 0,
103 buf + offset, size - offset);
e4d5639d 104 offset += len;
98b19252 105
98b19252
AS
106 virtqueue_push(vq, &elem, len);
107 }
108
76017fd2 109 virtio_notify(VIRTIO_DEVICE(port->vser), vq);
98b19252
AS
110 return offset;
111}
112
6bff8656 113static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
a69c7600
AS
114{
115 VirtQueueElement elem;
116
7185f931
AS
117 if (!virtio_queue_ready(vq)) {
118 return;
119 }
6bff8656
AS
120 while (virtqueue_pop(vq, &elem)) {
121 virtqueue_push(vq, &elem, 0);
122 }
123 virtio_notify(vdev, vq);
124}
125
9ed7b059 126static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
6bff8656 127 VirtIODevice *vdev)
a69c7600 128{
f82e35e3 129 VirtIOSerialPortClass *vsc;
a15bb0d6 130
6bff8656 131 assert(port);
fd11a78b 132 assert(virtio_queue_ready(vq));
a69c7600 133
f82e35e3 134 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
a15bb0d6 135
f1925dff 136 while (!port->throttled) {
471344db 137 unsigned int i;
a69c7600 138
f1925dff
AS
139 /* Pop an elem only if we haven't left off a previous one mid-way */
140 if (!port->elem.out_num) {
141 if (!virtqueue_pop(vq, &port->elem)) {
142 break;
143 }
144 port->iov_idx = 0;
145 port->iov_offset = 0;
146 }
a69c7600 147
f1925dff
AS
148 for (i = port->iov_idx; i < port->elem.out_num; i++) {
149 size_t buf_size;
150 ssize_t ret;
151
152 buf_size = port->elem.out_sg[i].iov_len - port->iov_offset;
f82e35e3 153 ret = vsc->have_data(port,
a15bb0d6
MA
154 port->elem.out_sg[i].iov_base
155 + port->iov_offset,
156 buf_size);
d6258c93 157 if (port->throttled) {
f1925dff
AS
158 port->iov_idx = i;
159 if (ret > 0) {
160 port->iov_offset += ret;
161 }
162 break;
163 }
164 port->iov_offset = 0;
a69c7600 165 }
f1925dff
AS
166 if (port->throttled) {
167 break;
168 }
169 virtqueue_push(vq, &port->elem, 0);
170 port->elem.out_num = 0;
a69c7600
AS
171 }
172 virtio_notify(vdev, vq);
173}
174
6bff8656 175static void flush_queued_data(VirtIOSerialPort *port)
9ed7b059 176{
a1c59752 177 assert(port);
9ed7b059 178
6b611d3a
AS
179 if (!virtio_queue_ready(port->ovq)) {
180 return;
181 }
76017fd2 182 do_flush_queued_data(port, port->ovq, VIRTIO_DEVICE(port->vser));
9ed7b059
AS
183}
184
4e28976e 185static size_t send_control_msg(VirtIOSerial *vser, void *buf, size_t len)
98b19252
AS
186{
187 VirtQueueElement elem;
188 VirtQueue *vq;
98b19252 189
4e28976e 190 vq = vser->c_ivq;
98b19252
AS
191 if (!virtio_queue_ready(vq)) {
192 return 0;
193 }
194 if (!virtqueue_pop(vq, &elem)) {
195 return 0;
196 }
197
98b19252
AS
198 memcpy(elem.in_sg[0].iov_base, buf, len);
199
200 virtqueue_push(vq, &elem, len);
76017fd2 201 virtio_notify(VIRTIO_DEVICE(vser), vq);
98b19252
AS
202 return len;
203}
204
4e28976e
AS
205static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id,
206 uint16_t event, uint16_t value)
98b19252 207{
e0ab7fac 208 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
98b19252
AS
209 struct virtio_console_control cpkt;
210
e0ab7fac
RR
211 virtio_stl_p(vdev, &cpkt.id, port_id);
212 virtio_stw_p(vdev, &cpkt.event, event);
213 virtio_stw_p(vdev, &cpkt.value, value);
98b19252 214
4e28976e
AS
215 trace_virtio_serial_send_control_event(port_id, event, value);
216 return send_control_msg(vser, &cpkt, sizeof(cpkt));
98b19252
AS
217}
218
219/* Functions for use inside qemu to open and read from/write to ports */
220int virtio_serial_open(VirtIOSerialPort *port)
221{
6663a195
AS
222 /* Don't allow opening an already-open port */
223 if (port->host_connected) {
224 return 0;
225 }
226 /* Send port open notification to the guest */
227 port->host_connected = true;
4e28976e 228 send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
6663a195 229
98b19252
AS
230 return 0;
231}
232
233int virtio_serial_close(VirtIOSerialPort *port)
234{
6663a195 235 port->host_connected = false;
9ed7b059
AS
236 /*
237 * If there's any data the guest sent which the app didn't
238 * consume, reset the throttling flag and discard the data.
239 */
240 port->throttled = false;
76017fd2 241 discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
9ed7b059 242
4e28976e 243 send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 0);
6663a195 244
98b19252
AS
245 return 0;
246}
247
248/* Individual ports/apps call this function to write to the guest. */
249ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
250 size_t size)
251{
6663a195
AS
252 if (!port || !port->host_connected || !port->guest_connected) {
253 return 0;
254 }
98b19252
AS
255 return write_to_port(port, buf, size);
256}
257
258/*
259 * Readiness of the guest to accept data on a port.
260 * Returns max. data the guest can receive
261 */
262size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
263{
76017fd2 264 VirtIODevice *vdev = VIRTIO_DEVICE(port->vser);
98b19252 265 VirtQueue *vq = port->ivq;
ad3005ad 266 unsigned int bytes;
98b19252
AS
267
268 if (!virtio_queue_ready(vq) ||
76017fd2 269 !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) ||
98b19252
AS
270 virtio_queue_empty(vq)) {
271 return 0;
272 }
6663a195
AS
273 if (use_multiport(port->vser) && !port->guest_connected) {
274 return 0;
275 }
e1f7b481 276 virtqueue_get_avail_bytes(vq, &bytes, NULL, 4096, 0);
ad3005ad 277 return bytes;
98b19252
AS
278}
279
199646d8
AL
280static void flush_queued_data_bh(void *opaque)
281{
282 VirtIOSerialPort *port = opaque;
283
284 flush_queued_data(port);
285}
286
9ed7b059
AS
287void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
288{
289 if (!port) {
290 return;
291 }
292
49e3fdd7 293 trace_virtio_serial_throttle_port(port->id, throttle);
9ed7b059
AS
294 port->throttled = throttle;
295 if (throttle) {
296 return;
297 }
199646d8 298 qemu_bh_schedule(port->bh);
9ed7b059
AS
299}
300
98b19252 301/* Guest wants to notify us of some event */
e61da14d 302static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
98b19252 303{
e0ab7fac 304 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
98b19252 305 struct VirtIOSerialPort *port;
f82e35e3 306 VirtIOSerialPortClass *vsc;
98b19252 307 struct virtio_console_control cpkt, *gcpkt;
160600fd
AS
308 uint8_t *buffer;
309 size_t buffer_len;
98b19252
AS
310
311 gcpkt = buf;
98b19252 312
e61da14d
AS
313 if (len < sizeof(cpkt)) {
314 /* The guest sent an invalid control packet */
315 return;
316 }
317
e0ab7fac
RR
318 cpkt.event = virtio_lduw_p(vdev, &gcpkt->event);
319 cpkt.value = virtio_lduw_p(vdev, &gcpkt->value);
98b19252 320
49e3fdd7
AS
321 trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
322
d2e4d08b 323 if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
4048c7c3 324 if (!cpkt.value) {
6daf194d 325 error_report("virtio-serial-bus: Guest failure in adding device %s",
5e52e5f9 326 vser->bus.qbus.name);
d2e4d08b 327 return;
4048c7c3 328 }
055b889f
AS
329 /*
330 * The device is up, we can now tell the device about all the
331 * ports we have here.
332 */
333 QTAILQ_FOREACH(port, &vser->ports, next) {
4e28976e 334 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_ADD, 1);
055b889f 335 }
d2e4d08b
LC
336 return;
337 }
055b889f 338
e0ab7fac 339 port = find_port_by_id(vser, virtio_ldl_p(vdev, &gcpkt->id));
d2e4d08b 340 if (!port) {
95c9cde2 341 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
e0ab7fac 342 virtio_ldl_p(vdev, &gcpkt->id), vser->bus.qbus.name);
d2e4d08b
LC
343 return;
344 }
345
49e3fdd7
AS
346 trace_virtio_serial_handle_control_message_port(port->id);
347
f82e35e3 348 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
d2e4d08b
LC
349
350 switch(cpkt.event) {
98b19252 351 case VIRTIO_CONSOLE_PORT_READY:
4048c7c3 352 if (!cpkt.value) {
6daf194d 353 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
5e52e5f9 354 port->id, vser->bus.qbus.name);
4048c7c3
AS
355 break;
356 }
98b19252
AS
357 /*
358 * Now that we know the guest asked for the port name, we're
359 * sure the guest has initialised whatever state is necessary
360 * for this port. Now's a good time to let the guest know if
361 * this port is a console port so that the guest can hook it
362 * up to hvc.
363 */
f82e35e3 364 if (vsc->is_console) {
4e28976e 365 send_control_event(vser, port->id, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
98b19252 366 }
6663a195 367
160600fd 368 if (port->name) {
e0ab7fac
RR
369 virtio_stl_p(vdev, &cpkt.id, port->id);
370 virtio_stw_p(vdev, &cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
371 virtio_stw_p(vdev, &cpkt.value, 1);
160600fd
AS
372
373 buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
7267c094 374 buffer = g_malloc(buffer_len);
160600fd
AS
375
376 memcpy(buffer, &cpkt, sizeof(cpkt));
377 memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
378 buffer[buffer_len - 1] = 0;
379
4e28976e 380 send_control_msg(vser, buffer, buffer_len);
7267c094 381 g_free(buffer);
160600fd
AS
382 }
383
6663a195 384 if (port->host_connected) {
4e28976e 385 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
6663a195
AS
386 }
387
98b19252
AS
388 /*
389 * When the guest has asked us for this information it means
390 * the guest is all setup and has its virtqueues
391 * initialised. If some app is interested in knowing about
392 * this event, let it know.
393 */
f82e35e3
AL
394 if (vsc->guest_ready) {
395 vsc->guest_ready(port);
98b19252
AS
396 }
397 break;
6663a195
AS
398
399 case VIRTIO_CONSOLE_PORT_OPEN:
400 port->guest_connected = cpkt.value;
b2c1394a 401 if (vsc->set_guest_connected) {
6663a195 402 /* Send the guest opened notification if an app is interested */
b2c1394a 403 vsc->set_guest_connected(port, cpkt.value);
6663a195
AS
404 }
405 break;
98b19252
AS
406 }
407}
408
409static void control_in(VirtIODevice *vdev, VirtQueue *vq)
410{
411}
412
413static void control_out(VirtIODevice *vdev, VirtQueue *vq)
414{
415 VirtQueueElement elem;
416 VirtIOSerial *vser;
e61da14d
AS
417 uint8_t *buf;
418 size_t len;
98b19252 419
76017fd2 420 vser = VIRTIO_SERIAL(vdev);
98b19252 421
e61da14d
AS
422 len = 0;
423 buf = NULL;
98b19252 424 while (virtqueue_pop(vq, &elem)) {
45270ad8 425 size_t cur_len;
e61da14d
AS
426
427 cur_len = iov_size(elem.out_sg, elem.out_num);
428 /*
429 * Allocate a new buf only if we didn't have one previously or
430 * if the size of the buf differs
431 */
432 if (cur_len > len) {
7267c094 433 g_free(buf);
e61da14d 434
7267c094 435 buf = g_malloc(cur_len);
e61da14d
AS
436 len = cur_len;
437 }
dcf6f5e1 438 iov_to_buf(elem.out_sg, elem.out_num, 0, buf, cur_len);
e61da14d 439
45270ad8 440 handle_control_message(vser, buf, cur_len);
1e4476aa 441 virtqueue_push(vq, &elem, 0);
98b19252 442 }
7267c094 443 g_free(buf);
98b19252
AS
444 virtio_notify(vdev, vq);
445}
446
447/* Guest wrote something to some port. */
448static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
449{
450 VirtIOSerial *vser;
a69c7600 451 VirtIOSerialPort *port;
98b19252 452
76017fd2 453 vser = VIRTIO_SERIAL(vdev);
a69c7600 454 port = find_port_by_vq(vser, vq);
98b19252 455
03ecd2c8 456 if (!port || !port->host_connected) {
6bff8656
AS
457 discard_vq_data(vq, vdev);
458 return;
459 }
e9b382b0
AS
460
461 if (!port->throttled) {
462 do_flush_queued_data(port, vq, vdev);
9ed7b059
AS
463 return;
464 }
98b19252
AS
465}
466
467static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
468{
4add73aa
AS
469 /*
470 * Users of virtio-serial would like to know when guest becomes
471 * writable again -- i.e. if a vq had stuff queued up and the
472 * guest wasn't reading at all, the host would not be able to
473 * write to the vq anymore. Once the guest reads off something,
474 * we can start queueing things up again. However, this call is
475 * made for each buffer addition by the guest -- even though free
476 * buffers existed prior to the current buffer addition. This is
477 * done so as not to maintain previous state, which will need
478 * additional live-migration-related changes.
479 */
480 VirtIOSerial *vser;
481 VirtIOSerialPort *port;
482 VirtIOSerialPortClass *vsc;
483
484 vser = VIRTIO_SERIAL(vdev);
485 port = find_port_by_vq(vser, vq);
486
487 if (!port) {
488 return;
489 }
490 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
491
492 /*
493 * If guest_connected is false, this call is being made by the
494 * early-boot queueing up of descriptors, which is just noise for
495 * the host apps -- don't disturb them in that case.
496 */
497 if (port->guest_connected && port->host_connected && vsc->guest_writable) {
498 vsc->guest_writable(port);
499 }
98b19252
AS
500}
501
019a3edb 502static uint64_t get_features(VirtIODevice *vdev, uint64_t features)
98b19252 503{
306eb457
AS
504 VirtIOSerial *vser;
505
76017fd2 506 vser = VIRTIO_SERIAL(vdev);
306eb457 507
5e52e5f9 508 if (vser->bus.max_nr_ports > 1) {
0cd09c3a 509 virtio_add_feature(&features, VIRTIO_CONSOLE_F_MULTIPORT);
ee4d45be 510 }
98b19252
AS
511 return features;
512}
513
514/* Guest requested config info */
515static void get_config(VirtIODevice *vdev, uint8_t *config_data)
516{
08f432aa
DG
517 VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
518 struct virtio_console_config *config =
519 (struct virtio_console_config *)config_data;
98b19252 520
08f432aa
DG
521 config->cols = 0;
522 config->rows = 0;
523 config->max_nr_ports = virtio_tswap32(vdev,
524 vser->serial.max_virtserial_ports);
98b19252
AS
525}
526
43997225
AS
527static void guest_reset(VirtIOSerial *vser)
528{
529 VirtIOSerialPort *port;
530 VirtIOSerialPortClass *vsc;
531
532 QTAILQ_FOREACH(port, &vser->ports, next) {
533 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
534 if (port->guest_connected) {
535 port->guest_connected = false;
b2c1394a
HG
536 if (vsc->set_guest_connected) {
537 vsc->set_guest_connected(port, false);
538 }
43997225
AS
539 }
540 }
541}
542
62a9fbf7
AL
543static void set_status(VirtIODevice *vdev, uint8_t status)
544{
545 VirtIOSerial *vser;
546 VirtIOSerialPort *port;
547
76017fd2 548 vser = VIRTIO_SERIAL(vdev);
62a9fbf7
AL
549 port = find_port_by_id(vser, 0);
550
551 if (port && !use_multiport(port->vser)
552 && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
553 /*
554 * Non-multiport guests won't be able to tell us guest
555 * open/close status. Such guests can only have a port at id
556 * 0, so set guest_connected for such ports as soon as guest
557 * is up.
558 */
559 port->guest_connected = true;
560 }
43997225
AS
561 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
562 guest_reset(vser);
563 }
564}
565
566static void vser_reset(VirtIODevice *vdev)
567{
568 VirtIOSerial *vser;
569
76017fd2 570 vser = VIRTIO_SERIAL(vdev);
43997225 571 guest_reset(vser);
62a9fbf7
AL
572}
573
98b19252
AS
574static void virtio_serial_save(QEMUFile *f, void *opaque)
575{
13c6855a
GK
576 /* The virtio device */
577 virtio_save(VIRTIO_DEVICE(opaque), f);
578}
579
580static void virtio_serial_save_device(VirtIODevice *vdev, QEMUFile *f)
581{
582 VirtIOSerial *s = VIRTIO_SERIAL(vdev);
6663a195
AS
583 VirtIOSerialPort *port;
584 uint32_t nr_active_ports;
5c1c9bb2 585 unsigned int i, max_nr_ports;
08f432aa 586 struct virtio_console_config config;
98b19252 587
08f432aa
DG
588 /* The config space (ignored on the far end in current versions) */
589 get_config(vdev, (uint8_t *)&config);
590 qemu_put_be16s(f, &config.cols);
591 qemu_put_be16s(f, &config.rows);
592 qemu_put_be32s(f, &config.max_nr_ports);
055b889f
AS
593
594 /* The ports map */
f2f6e00b 595 max_nr_ports = s->serial.max_virtserial_ports;
5c1c9bb2 596 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
055b889f
AS
597 qemu_put_be32s(f, &s->ports_map[i]);
598 }
6663a195 599
055b889f 600 /* Ports */
e245795b 601
6663a195 602 nr_active_ports = 0;
e245795b 603 QTAILQ_FOREACH(port, &s->ports, next) {
6663a195 604 nr_active_ports++;
e245795b 605 }
6663a195
AS
606
607 qemu_put_be32s(f, &nr_active_ports);
608
609 /*
610 * Items in struct VirtIOSerialPort.
611 */
612 QTAILQ_FOREACH(port, &s->ports, next) {
37f95bf3
AS
613 uint32_t elem_popped;
614
6663a195
AS
615 qemu_put_be32s(f, &port->id);
616 qemu_put_byte(f, port->guest_connected);
31abe21f 617 qemu_put_byte(f, port->host_connected);
37f95bf3
AS
618
619 elem_popped = 0;
620 if (port->elem.out_num) {
621 elem_popped = 1;
622 }
623 qemu_put_be32s(f, &elem_popped);
624 if (elem_popped) {
625 qemu_put_be32s(f, &port->iov_idx);
626 qemu_put_be64s(f, &port->iov_offset);
627
628 qemu_put_buffer(f, (unsigned char *)&port->elem,
629 sizeof(port->elem));
630 }
6663a195 631 }
98b19252
AS
632}
633
80dcfb85
AL
634static void virtio_serial_post_load_timer_cb(void *opaque)
635{
c3587ca1 636 uint32_t i;
76017fd2 637 VirtIOSerial *s = VIRTIO_SERIAL(opaque);
80dcfb85
AL
638 VirtIOSerialPort *port;
639 uint8_t host_connected;
bc6b815d 640 VirtIOSerialPortClass *vsc;
80dcfb85 641
bdb917bf
AS
642 if (!s->post_load) {
643 return;
644 }
645 for (i = 0 ; i < s->post_load->nr_active_ports; ++i) {
646 port = s->post_load->connected[i].port;
647 host_connected = s->post_load->connected[i].host_connected;
80dcfb85
AL
648 if (host_connected != port->host_connected) {
649 /*
650 * We have to let the guest know of the host connection
651 * status change
652 */
4e28976e 653 send_control_event(s, port->id, VIRTIO_CONSOLE_PORT_OPEN,
80dcfb85
AL
654 port->host_connected);
655 }
bc6b815d
AL
656 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
657 if (vsc->set_guest_connected) {
658 vsc->set_guest_connected(port, port->guest_connected);
659 }
80dcfb85 660 }
bdb917bf 661 g_free(s->post_load->connected);
bc72ad67 662 timer_free(s->post_load->timer);
bdb917bf
AS
663 g_free(s->post_load);
664 s->post_load = NULL;
80dcfb85
AL
665}
666
2e575a86
AS
667static int fetch_active_ports_list(QEMUFile *f, int version_id,
668 VirtIOSerial *s, uint32_t nr_active_ports)
669{
670 uint32_t i;
671
bdb917bf
AS
672 s->post_load = g_malloc0(sizeof(*s->post_load));
673 s->post_load->nr_active_ports = nr_active_ports;
674 s->post_load->connected =
675 g_malloc0(sizeof(*s->post_load->connected) * nr_active_ports);
676
bc72ad67 677 s->post_load->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
bdb917bf
AS
678 virtio_serial_post_load_timer_cb,
679 s);
2e575a86
AS
680
681 /* Items in struct VirtIOSerialPort */
682 for (i = 0; i < nr_active_ports; i++) {
683 VirtIOSerialPort *port;
684 uint32_t id;
685
686 id = qemu_get_be32(f);
687 port = find_port_by_id(s, id);
688 if (!port) {
689 return -EINVAL;
690 }
691
692 port->guest_connected = qemu_get_byte(f);
bdb917bf
AS
693 s->post_load->connected[i].port = port;
694 s->post_load->connected[i].host_connected = qemu_get_byte(f);
2e575a86
AS
695
696 if (version_id > 2) {
697 uint32_t elem_popped;
698
699 qemu_get_be32s(f, &elem_popped);
700 if (elem_popped) {
701 qemu_get_be32s(f, &port->iov_idx);
702 qemu_get_be64s(f, &port->iov_offset);
703
704 qemu_get_buffer(f, (unsigned char *)&port->elem,
705 sizeof(port->elem));
706 virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
707 port->elem.in_num, 1);
708 virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
709 port->elem.out_num, 1);
710
711 /*
712 * Port was throttled on source machine. Let's
713 * unthrottle it here so data starts flowing again.
714 */
715 virtio_serial_throttle_port(port, false);
716 }
717 }
718 }
bc72ad67 719 timer_mod(s->post_load->timer, 1);
2e575a86
AS
720 return 0;
721}
722
98b19252
AS
723static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
724{
37f95bf3 725 if (version_id > 3) {
98b19252
AS
726 return -EINVAL;
727 }
6663a195 728
98b19252 729 /* The virtio device */
13c6855a
GK
730 return virtio_load(VIRTIO_DEVICE(opaque), f, version_id);
731}
732
733static int virtio_serial_load_device(VirtIODevice *vdev, QEMUFile *f,
734 int version_id)
735{
736 VirtIOSerial *s = VIRTIO_SERIAL(vdev);
737 uint32_t max_nr_ports, nr_active_ports, ports_map;
738 unsigned int i;
739 int ret;
740 uint32_t tmp;
98b19252
AS
741
742 if (version_id < 2) {
743 return 0;
744 }
745
e38e943a
AG
746 /* Unused */
747 qemu_get_be16s(f, (uint16_t *) &tmp);
748 qemu_get_be16s(f, (uint16_t *) &tmp);
749 qemu_get_be32s(f, &tmp);
98b19252 750
f2f6e00b 751 max_nr_ports = s->serial.max_virtserial_ports;
055b889f 752 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
f83ccb3e 753 qemu_get_be32s(f, &ports_map);
055b889f 754
f83ccb3e 755 if (ports_map != s->ports_map[i]) {
055b889f
AS
756 /*
757 * Ports active on source and destination don't
758 * match. Fail migration.
759 */
055b889f
AS
760 return -EINVAL;
761 }
e245795b
AS
762 }
763
6663a195
AS
764 qemu_get_be32s(f, &nr_active_ports);
765
2e575a86
AS
766 if (nr_active_ports) {
767 ret = fetch_active_ports_list(f, version_id, s, nr_active_ports);
768 if (ret) {
769 return ret;
37f95bf3 770 }
6663a195 771 }
98b19252
AS
772 return 0;
773}
774
775static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
776
3cb75a7c
PB
777static Property virtser_props[] = {
778 DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
779 DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
780 DEFINE_PROP_END_OF_LIST()
781};
782
0d936928
AL
783#define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
784#define VIRTIO_SERIAL_BUS(obj) \
785 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
786
787static void virtser_bus_class_init(ObjectClass *klass, void *data)
788{
789 BusClass *k = BUS_CLASS(klass);
790 k->print_dev = virtser_bus_dev_print;
791}
792
793static const TypeInfo virtser_bus_info = {
794 .name = TYPE_VIRTIO_SERIAL_BUS,
795 .parent = TYPE_BUS,
796 .instance_size = sizeof(VirtIOSerialBus),
797 .class_init = virtser_bus_class_init,
98b19252
AS
798};
799
98b19252
AS
800static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
801{
a43f9c90 802 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
98b19252 803
021a1318
MA
804 monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
805 indent, "", port->id,
806 port->guest_connected ? "on" : "off",
807 port->host_connected ? "on" : "off",
808 port->throttled ? "on" : "off");
98b19252
AS
809}
810
055b889f
AS
811/* This function is only used if a port id is not provided by the user */
812static uint32_t find_free_port_id(VirtIOSerial *vser)
813{
5c1c9bb2 814 unsigned int i, max_nr_ports;
055b889f 815
f2f6e00b 816 max_nr_ports = vser->serial.max_virtserial_ports;
5c1c9bb2 817 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
bd2a8884 818 uint32_t map, zeroes;
055b889f
AS
819
820 map = vser->ports_map[i];
bd2a8884
SH
821 zeroes = ctz32(~map);
822 if (zeroes != 32) {
823 return zeroes + i * 32;
055b889f
AS
824 }
825 }
826 return VIRTIO_CONSOLE_BAD_ID;
827}
828
829static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
830{
831 unsigned int i;
832
833 i = port_id / 32;
834 vser->ports_map[i] |= 1U << (port_id % 32);
835}
836
837static void add_port(VirtIOSerial *vser, uint32_t port_id)
838{
839 mark_port_added(vser, port_id);
4e28976e 840 send_control_event(vser, port_id, VIRTIO_CONSOLE_PORT_ADD, 1);
055b889f
AS
841}
842
843static void remove_port(VirtIOSerial *vser, uint32_t port_id)
844{
9ed7b059 845 VirtIOSerialPort *port;
055b889f 846
57d84cf3
AS
847 /*
848 * Don't mark port 0 removed -- we explicitly reserve it for
849 * backward compat with older guests, ensure a virtconsole device
850 * unplug retains the reservation.
851 */
852 if (port_id) {
853 unsigned int i;
854
855 i = port_id / 32;
856 vser->ports_map[i] &= ~(1U << (port_id % 32));
857 }
055b889f 858
9ed7b059 859 port = find_port_by_id(vser, port_id);
91bdd1cf
AS
860 /*
861 * This function is only called from qdev's unplug callback; if we
862 * get a NULL port here, we're in trouble.
863 */
864 assert(port);
865
9ed7b059 866 /* Flush out any unconsumed buffers first */
76017fd2 867 discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
9ed7b059 868
4e28976e 869 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_REMOVE, 1);
055b889f
AS
870}
871
2ef66625 872static void virtser_port_device_realize(DeviceState *dev, Error **errp)
98b19252 873{
2ef66625 874 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
f82e35e3 875 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
2ef66625
AF
876 VirtIOSerialBus *bus = VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev));
877 int max_nr_ports;
98b19252 878 bool plugging_port0;
2ef66625 879 Error *err = NULL;
98b19252
AS
880
881 port->vser = bus->vser;
199646d8 882 port->bh = qemu_bh_new(flush_queued_data_bh, port);
98b19252 883
f82e35e3 884 assert(vsc->have_data);
03ecd2c8 885
98b19252
AS
886 /*
887 * Is the first console port we're seeing? If so, put it up at
888 * location 0. This is done for backward compatibility (old
889 * kernel, new qemu).
890 */
f82e35e3 891 plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
98b19252 892
055b889f 893 if (find_port_by_id(port->vser, port->id)) {
2ef66625
AF
894 error_setg(errp, "virtio-serial-bus: A port already exists at id %u",
895 port->id);
896 return;
98b19252 897 }
98b19252 898
7eb73114 899 if (port->name != NULL && find_port_by_name(port->name)) {
d0a0bfe6
AS
900 error_setg(errp, "virtio-serial-bus: A port already exists by name %s",
901 port->name);
902 return;
903 }
904
055b889f
AS
905 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
906 if (plugging_port0) {
907 port->id = 0;
908 } else {
909 port->id = find_free_port_id(port->vser);
910 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
2ef66625
AF
911 error_setg(errp, "virtio-serial-bus: Maximum port limit for "
912 "this device reached");
913 return;
055b889f
AS
914 }
915 }
916 }
917
f2f6e00b 918 max_nr_ports = port->vser->serial.max_virtserial_ports;
5c1c9bb2 919 if (port->id >= max_nr_ports) {
2ef66625
AF
920 error_setg(errp, "virtio-serial-bus: Out-of-range port id specified, "
921 "max. allowed: %u", max_nr_ports - 1);
922 return;
055b889f
AS
923 }
924
2ef66625
AF
925 vsc->realize(dev, &err);
926 if (err != NULL) {
927 error_propagate(errp, err);
928 return;
98b19252
AS
929 }
930
f1925dff 931 port->elem.out_num = 0;
0ddef15b
IM
932}
933
934static void virtser_port_device_plug(HotplugHandler *hotplug_dev,
935 DeviceState *dev, Error **errp)
936{
937 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
f1925dff 938
98b19252
AS
939 QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
940 port->ivq = port->vser->ivqs[port->id];
941 port->ovq = port->vser->ovqs[port->id];
942
055b889f
AS
943 add_port(port->vser, port->id);
944
98b19252 945 /* Send an update to the guest about this new port added */
0ddef15b 946 virtio_notify_config(VIRTIO_DEVICE(hotplug_dev));
98b19252
AS
947}
948
2ef66625 949static void virtser_port_device_unrealize(DeviceState *dev, Error **errp)
98b19252 950{
2ef66625
AF
951 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
952 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
98b19252
AS
953 VirtIOSerial *vser = port->vser;
954
199646d8 955 qemu_bh_delete(port->bh);
055b889f 956 remove_port(port->vser, port->id);
f146ec9a 957
98b19252
AS
958 QTAILQ_REMOVE(&vser->ports, port, next);
959
2ef66625
AF
960 if (vsc->unrealize) {
961 vsc->unrealize(dev, errp);
a15bb0d6 962 }
98b19252
AS
963}
964
86346244 965static void virtio_serial_device_realize(DeviceState *dev, Error **errp)
98b19252 966{
86346244 967 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
b1a20c3f 968 VirtIOSerial *vser = VIRTIO_SERIAL(dev);
5ab4bb59 969 uint32_t i, max_supported_ports;
98b19252 970
34b95b2c 971 if (!vser->serial.max_virtserial_ports) {
86346244
AF
972 error_setg(errp, "Maximum number of serial ports not specified");
973 return;
34b95b2c 974 }
98b19252 975
5ab4bb59 976 /* Each port takes 2 queues, and one pair is for the control queue */
87b3bd1c 977 max_supported_ports = VIRTIO_QUEUE_MAX / 2 - 1;
5ab4bb59 978
34b95b2c 979 if (vser->serial.max_virtserial_ports > max_supported_ports) {
86346244
AF
980 error_setg(errp, "maximum ports supported: %u", max_supported_ports);
981 return;
5ab4bb59
AS
982 }
983
7976a6d2
MT
984 /* We don't support emergency write, skip it for now. */
985 /* TODO: cleaner fix, depending on host features. */
34b95b2c 986 virtio_init(vdev, "virtio-serial", VIRTIO_ID_CONSOLE,
7976a6d2 987 offsetof(struct virtio_console_config, emerg_wr));
98b19252
AS
988
989 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
fb17dfe0 990 qbus_create_inplace(&vser->bus, sizeof(vser->bus), TYPE_VIRTIO_SERIAL_BUS,
b1a20c3f 991 dev, vdev->bus_name);
0ddef15b 992 qbus_set_hotplug_handler(BUS(&vser->bus), DEVICE(vser), errp);
5e52e5f9 993 vser->bus.vser = vser;
98b19252
AS
994 QTAILQ_INIT(&vser->ports);
995
34b95b2c
FK
996 vser->bus.max_nr_ports = vser->serial.max_virtserial_ports;
997 vser->ivqs = g_malloc(vser->serial.max_virtserial_ports
998 * sizeof(VirtQueue *));
999 vser->ovqs = g_malloc(vser->serial.max_virtserial_ports
1000 * sizeof(VirtQueue *));
98b19252
AS
1001
1002 /* Add a queue for host to guest transfers for port 0 (backward compat) */
1003 vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
1004 /* Add a queue for guest to host transfers for port 0 (backward compat) */
1005 vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
1006
a01a9cb8
AS
1007 /* TODO: host to guest notifications can get dropped
1008 * if the queue fills up. Implement queueing in host,
1009 * this might also make it possible to reduce the control
1010 * queue size: as guest preposts buffers there,
1011 * this will save 4Kbyte of guest memory per entry. */
1012
98b19252 1013 /* control queue: host to guest */
a01a9cb8 1014 vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
98b19252 1015 /* control queue: guest to host */
a01a9cb8 1016 vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
98b19252 1017
5e52e5f9 1018 for (i = 1; i < vser->bus.max_nr_ports; i++) {
98b19252
AS
1019 /* Add a per-port queue for host to guest transfers */
1020 vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
1021 /* Add a per-per queue for guest to host transfers */
1022 vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
1023 }
1024
34b95b2c 1025 vser->ports_map = g_malloc0(((vser->serial.max_virtserial_ports + 31) / 32)
a132a679 1026 * sizeof(vser->ports_map[0]));
98b19252
AS
1027 /*
1028 * Reserve location 0 for a console port for backward compat
1029 * (old kernel, new qemu)
1030 */
055b889f 1031 mark_port_added(vser, 0);
98b19252 1032
bdb917bf
AS
1033 vser->post_load = NULL;
1034
98b19252
AS
1035 /*
1036 * Register for the savevm section with the virtio-console name
1037 * to preserve backward compat
1038 */
b1a20c3f 1039 register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
98b19252 1040 virtio_serial_load, vser);
a1857ad1
AS
1041
1042 QLIST_INSERT_HEAD(&vserdevices.devices, vser, next);
8b53a865 1043}
f82e35e3 1044
39bffca2
AL
1045static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
1046{
1047 DeviceClass *k = DEVICE_CLASS(klass);
2ef66625 1048
125ee0ed 1049 set_bit(DEVICE_CATEGORY_INPUT, k->categories);
0d936928 1050 k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
2ef66625
AF
1051 k->realize = virtser_port_device_realize;
1052 k->unrealize = virtser_port_device_unrealize;
bce54474 1053 k->props = virtser_props;
39bffca2
AL
1054}
1055
8c43a6f0 1056static const TypeInfo virtio_serial_port_type_info = {
f82e35e3
AL
1057 .name = TYPE_VIRTIO_SERIAL_PORT,
1058 .parent = TYPE_DEVICE,
1059 .instance_size = sizeof(VirtIOSerialPort),
1060 .abstract = true,
1061 .class_size = sizeof(VirtIOSerialPortClass),
39bffca2 1062 .class_init = virtio_serial_port_class_init,
f82e35e3
AL
1063};
1064
306ec6c3 1065static void virtio_serial_device_unrealize(DeviceState *dev, Error **errp)
2cd2b016 1066{
306ec6c3
AF
1067 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1068 VirtIOSerial *vser = VIRTIO_SERIAL(dev);
2cd2b016 1069
a1857ad1
AS
1070 QLIST_REMOVE(vser, next);
1071
306ec6c3 1072 unregister_savevm(dev, "virtio-console", vser);
2cd2b016
FK
1073
1074 g_free(vser->ivqs);
1075 g_free(vser->ovqs);
1076 g_free(vser->ports_map);
1077 if (vser->post_load) {
1078 g_free(vser->post_load->connected);
bc72ad67
AB
1079 timer_del(vser->post_load->timer);
1080 timer_free(vser->post_load->timer);
2cd2b016
FK
1081 g_free(vser->post_load);
1082 }
6a1a8cc7 1083 virtio_cleanup(vdev);
2cd2b016
FK
1084}
1085
1086static Property virtio_serial_properties[] = {
448777c4
SZ
1087 DEFINE_PROP_UINT32("max_ports", VirtIOSerial, serial.max_virtserial_ports,
1088 31),
2cd2b016
FK
1089 DEFINE_PROP_END_OF_LIST(),
1090};
1091
1092static void virtio_serial_class_init(ObjectClass *klass, void *data)
1093{
1094 DeviceClass *dc = DEVICE_CLASS(klass);
1095 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
0ddef15b 1096 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
86346244 1097
a1857ad1
AS
1098 QLIST_INIT(&vserdevices.devices);
1099
2cd2b016 1100 dc->props = virtio_serial_properties;
125ee0ed 1101 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
86346244 1102 vdc->realize = virtio_serial_device_realize;
306ec6c3 1103 vdc->unrealize = virtio_serial_device_unrealize;
2cd2b016
FK
1104 vdc->get_features = get_features;
1105 vdc->get_config = get_config;
2cd2b016
FK
1106 vdc->set_status = set_status;
1107 vdc->reset = vser_reset;
13c6855a
GK
1108 vdc->save = virtio_serial_save_device;
1109 vdc->load = virtio_serial_load_device;
0ddef15b
IM
1110 hc->plug = virtser_port_device_plug;
1111 hc->unplug = qdev_simple_device_unplug_cb;
2cd2b016
FK
1112}
1113
1114static const TypeInfo virtio_device_info = {
1115 .name = TYPE_VIRTIO_SERIAL,
1116 .parent = TYPE_VIRTIO_DEVICE,
1117 .instance_size = sizeof(VirtIOSerial),
1118 .class_init = virtio_serial_class_init,
0ddef15b
IM
1119 .interfaces = (InterfaceInfo[]) {
1120 { TYPE_HOTPLUG_HANDLER },
1121 { }
1122 }
2cd2b016
FK
1123};
1124
83f7d43a 1125static void virtio_serial_register_types(void)
f82e35e3 1126{
0d936928 1127 type_register_static(&virtser_bus_info);
f82e35e3 1128 type_register_static(&virtio_serial_port_type_info);
2cd2b016 1129 type_register_static(&virtio_device_info);
f82e35e3
AL
1130}
1131
83f7d43a 1132type_init(virtio_serial_register_types)
This page took 0.798732 seconds and 4 git commands to generate.