1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "qemu/osdep.h"
22#include "qapi/error.h"
23#include "qemu/iov.h"
24#include "monitor/monitor.h"
25#include "qemu/error-report.h"
26#include "qemu/queue.h"
27#include "hw/sysbus.h"
28#include "trace.h"
29#include "hw/virtio/virtio-serial.h"
30#include "hw/virtio/virtio-access.h"
31
32static struct VirtIOSerialDevices {
33 QLIST_HEAD(, VirtIOSerial) devices;
34} vserdevices;
35
36static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
37{
38 VirtIOSerialPort *port;
39
40 if (id == VIRTIO_CONSOLE_BAD_ID) {
41 return NULL;
42 }
43
44 QTAILQ_FOREACH(port, &vser->ports, next) {
45 if (port->id == id)
46 return port;
47 }
48 return NULL;
49}
50
51static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
52{
53 VirtIOSerialPort *port;
54
55 QTAILQ_FOREACH(port, &vser->ports, next) {
56 if (port->ivq == vq || port->ovq == vq)
57 return port;
58 }
59 return NULL;
60}
61
62static VirtIOSerialPort *find_port_by_name(char *name)
63{
64 VirtIOSerial *vser;
65
66 QLIST_FOREACH(vser, &vserdevices.devices, next) {
67 VirtIOSerialPort *port;
68
69 QTAILQ_FOREACH(port, &vser->ports, next) {
70 if (port->name && !strcmp(port->name, name)) {
71 return port;
72 }
73 }
74 }
75 return NULL;
76}
77
78static VirtIOSerialPort *find_first_connected_console(VirtIOSerial *vser)
79{
80 VirtIOSerialPort *port;
81
82 QTAILQ_FOREACH(port, &vser->ports, next) {
83 VirtIOSerialPortClass const *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
84 if (vsc->is_console && port->host_connected) {
85 return port;
86 }
87 }
88 return NULL;
89}
90
91static bool use_multiport(VirtIOSerial *vser)
92{
93 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
94 return virtio_vdev_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT);
95}
96
97static size_t write_to_port(VirtIOSerialPort *port,
98 const uint8_t *buf, size_t size)
99{
100 VirtQueueElement *elem;
101 VirtQueue *vq;
102 size_t offset;
103
104 vq = port->ivq;
105 if (!virtio_queue_ready(vq)) {
106 return 0;
107 }
108
109 offset = 0;
110 while (offset < size) {
111 size_t len;
112
113 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
114 if (!elem) {
115 break;
116 }
117
118 len = iov_from_buf(elem->in_sg, elem->in_num, 0,
119 buf + offset, size - offset);
120 offset += len;
121
122 virtqueue_push(vq, elem, len);
123 g_free(elem);
124 }
125
126 virtio_notify(VIRTIO_DEVICE(port->vser), vq);
127 return offset;
128}
129
130static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
131{
132 VirtQueueElement *elem;
133
134 if (!virtio_queue_ready(vq)) {
135 return;
136 }
137 for (;;) {
138 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
139 if (!elem) {
140 break;
141 }
142 virtqueue_push(vq, elem, 0);
143 g_free(elem);
144 }
145 virtio_notify(vdev, vq);
146}
147
148static void discard_throttle_data(VirtIOSerialPort *port)
149{
150 if (port->elem) {
151 virtqueue_detach_element(port->ovq, port->elem, 0);
152 g_free(port->elem);
153 port->elem = NULL;
154 }
155}
156
157static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
158 VirtIODevice *vdev)
159{
160 VirtIOSerialPortClass *vsc;
161
162 assert(port);
163 assert(virtio_queue_ready(vq));
164
165 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
166
167 while (!port->throttled) {
168 unsigned int i;
169
170
171 if (!port->elem) {
172 port->elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
173 if (!port->elem) {
174 break;
175 }
176 port->iov_idx = 0;
177 port->iov_offset = 0;
178 }
179
180 for (i = port->iov_idx; i < port->elem->out_num; i++) {
181 size_t buf_size;
182 ssize_t ret;
183
184 buf_size = port->elem->out_sg[i].iov_len - port->iov_offset;
185 ret = vsc->have_data(port,
186 port->elem->out_sg[i].iov_base
187 + port->iov_offset,
188 buf_size);
189 if (!port->elem) {
190 return;
191 }
192 if (port->throttled) {
193 port->iov_idx = i;
194 if (ret > 0) {
195 port->iov_offset += ret;
196 }
197 break;
198 }
199 port->iov_offset = 0;
200 }
201 if (port->throttled) {
202 break;
203 }
204 virtqueue_push(vq, port->elem, 0);
205 g_free(port->elem);
206 port->elem = NULL;
207 }
208 virtio_notify(vdev, vq);
209}
210
211static void flush_queued_data(VirtIOSerialPort *port)
212{
213 assert(port);
214
215 if (!virtio_queue_ready(port->ovq)) {
216 return;
217 }
218 do_flush_queued_data(port, port->ovq, VIRTIO_DEVICE(port->vser));
219}
220
221static size_t send_control_msg(VirtIOSerial *vser, void *buf, size_t len)
222{
223 VirtQueueElement *elem;
224 VirtQueue *vq;
225
226 vq = vser->c_ivq;
227 if (!virtio_queue_ready(vq)) {
228 return 0;
229 }
230
231 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
232 if (!elem) {
233 return 0;
234 }
235
236
237 iov_from_buf(elem->in_sg, elem->in_num, 0, buf, len);
238
239 virtqueue_push(vq, elem, len);
240 virtio_notify(VIRTIO_DEVICE(vser), vq);
241 g_free(elem);
242
243 return len;
244}
245
246static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id,
247 uint16_t event, uint16_t value)
248{
249 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
250 struct virtio_console_control cpkt;
251
252 virtio_stl_p(vdev, &cpkt.id, port_id);
253 virtio_stw_p(vdev, &cpkt.event, event);
254 virtio_stw_p(vdev, &cpkt.value, value);
255
256 trace_virtio_serial_send_control_event(port_id, event, value);
257 return send_control_msg(vser, &cpkt, sizeof(cpkt));
258}
259
260
261int virtio_serial_open(VirtIOSerialPort *port)
262{
263
264 if (port->host_connected) {
265 return 0;
266 }
267
268 port->host_connected = true;
269 send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
270
271 return 0;
272}
273
274int virtio_serial_close(VirtIOSerialPort *port)
275{
276 port->host_connected = false;
277
278
279
280
281 port->throttled = false;
282 discard_throttle_data(port);
283 discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
284
285 send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 0);
286
287 return 0;
288}
289
290
291ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
292 size_t size)
293{
294 if (!port || !port->host_connected || !port->guest_connected) {
295 return 0;
296 }
297 return write_to_port(port, buf, size);
298}
299
300
301
302
303
304size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
305{
306 VirtIODevice *vdev = VIRTIO_DEVICE(port->vser);
307 VirtQueue *vq = port->ivq;
308 unsigned int bytes;
309
310 if (!virtio_queue_ready(vq) ||
311 !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) ||
312 virtio_queue_empty(vq)) {
313 return 0;
314 }
315 if (use_multiport(port->vser) && !port->guest_connected) {
316 return 0;
317 }
318 virtqueue_get_avail_bytes(vq, &bytes, NULL, 4096, 0);
319 return bytes;
320}
321
322static void flush_queued_data_bh(void *opaque)
323{
324 VirtIOSerialPort *port = opaque;
325
326 flush_queued_data(port);
327}
328
329void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
330{
331 if (!port) {
332 return;
333 }
334
335 trace_virtio_serial_throttle_port(port->id, throttle);
336 port->throttled = throttle;
337 if (throttle) {
338 return;
339 }
340 qemu_bh_schedule(port->bh);
341}
342
343
344static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
345{
346 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
347 struct VirtIOSerialPort *port;
348 VirtIOSerialPortClass *vsc;
349 struct virtio_console_control cpkt, *gcpkt;
350 uint8_t *buffer;
351 size_t buffer_len;
352
353 gcpkt = buf;
354
355 if (len < sizeof(cpkt)) {
356
357 return;
358 }
359
360 cpkt.event = virtio_lduw_p(vdev, &gcpkt->event);
361 cpkt.value = virtio_lduw_p(vdev, &gcpkt->value);
362
363 trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
364
365 if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
366 if (!cpkt.value) {
367 error_report("virtio-serial-bus: Guest failure in adding device %s",
368 vser->bus.qbus.name);
369 return;
370 }
371
372
373
374
375 QTAILQ_FOREACH(port, &vser->ports, next) {
376 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_ADD, 1);
377 }
378 return;
379 }
380
381 port = find_port_by_id(vser, virtio_ldl_p(vdev, &gcpkt->id));
382 if (!port) {
383 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
384 virtio_ldl_p(vdev, &gcpkt->id), vser->bus.qbus.name);
385 return;
386 }
387
388 trace_virtio_serial_handle_control_message_port(port->id);
389
390 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
391
392 switch(cpkt.event) {
393 case VIRTIO_CONSOLE_PORT_READY:
394 if (!cpkt.value) {
395 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
396 port->id, vser->bus.qbus.name);
397 break;
398 }
399
400
401
402
403
404
405
406 if (vsc->is_console) {
407 send_control_event(vser, port->id, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
408 }
409
410 if (port->name) {
411 virtio_stl_p(vdev, &cpkt.id, port->id);
412 virtio_stw_p(vdev, &cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
413 virtio_stw_p(vdev, &cpkt.value, 1);
414
415 buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
416 buffer = g_malloc(buffer_len);
417
418 memcpy(buffer, &cpkt, sizeof(cpkt));
419 memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
420 buffer[buffer_len - 1] = 0;
421
422 send_control_msg(vser, buffer, buffer_len);
423 g_free(buffer);
424 }
425
426 if (port->host_connected) {
427 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
428 }
429
430
431
432
433
434
435
436 if (vsc->guest_ready) {
437 vsc->guest_ready(port);
438 }
439 break;
440
441 case VIRTIO_CONSOLE_PORT_OPEN:
442 port->guest_connected = cpkt.value;
443 if (vsc->set_guest_connected) {
444
445 vsc->set_guest_connected(port, cpkt.value);
446 }
447 break;
448 }
449}
450
451static void control_in(VirtIODevice *vdev, VirtQueue *vq)
452{
453}
454
455static void control_out(VirtIODevice *vdev, VirtQueue *vq)
456{
457 VirtQueueElement *elem;
458 VirtIOSerial *vser;
459 uint8_t *buf;
460 size_t len;
461
462 vser = VIRTIO_SERIAL(vdev);
463
464 len = 0;
465 buf = NULL;
466 for (;;) {
467 size_t cur_len;
468
469 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
470 if (!elem) {
471 break;
472 }
473
474 cur_len = iov_size(elem->out_sg, elem->out_num);
475
476
477
478
479 if (cur_len > len) {
480 g_free(buf);
481
482 buf = g_malloc(cur_len);
483 len = cur_len;
484 }
485 iov_to_buf(elem->out_sg, elem->out_num, 0, buf, cur_len);
486
487 handle_control_message(vser, buf, cur_len);
488 virtqueue_push(vq, elem, 0);
489 g_free(elem);
490 }
491 g_free(buf);
492 virtio_notify(vdev, vq);
493}
494
495
496static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
497{
498 VirtIOSerial *vser;
499 VirtIOSerialPort *port;
500
501 vser = VIRTIO_SERIAL(vdev);
502 port = find_port_by_vq(vser, vq);
503
504 if (!port || !port->host_connected) {
505 discard_vq_data(vq, vdev);
506 return;
507 }
508
509 if (!port->throttled) {
510 do_flush_queued_data(port, vq, vdev);
511 return;
512 }
513}
514
515static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
516{
517
518
519
520
521
522
523
524
525
526
527
528 VirtIOSerial *vser;
529 VirtIOSerialPort *port;
530 VirtIOSerialPortClass *vsc;
531
532 vser = VIRTIO_SERIAL(vdev);
533 port = find_port_by_vq(vser, vq);
534
535 if (!port) {
536 return;
537 }
538 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
539
540
541
542
543
544
545 if (port->guest_connected && port->host_connected && vsc->guest_writable) {
546 vsc->guest_writable(port);
547 }
548}
549
550static uint64_t get_features(VirtIODevice *vdev, uint64_t features,
551 Error **errp)
552{
553 VirtIOSerial *vser;
554
555 vser = VIRTIO_SERIAL(vdev);
556
557 features |= vser->host_features;
558 if (vser->bus.max_nr_ports > 1) {
559 virtio_add_feature(&features, VIRTIO_CONSOLE_F_MULTIPORT);
560 }
561 return features;
562}
563
564
565static void get_config(VirtIODevice *vdev, uint8_t *config_data)
566{
567 VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
568 struct virtio_console_config *config =
569 (struct virtio_console_config *)config_data;
570
571 config->cols = 0;
572 config->rows = 0;
573 config->max_nr_ports = virtio_tswap32(vdev,
574 vser->serial.max_virtserial_ports);
575}
576
577
578static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
579{
580 VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
581 struct virtio_console_config *config =
582 (struct virtio_console_config *)config_data;
583 uint8_t emerg_wr_lo = le32_to_cpu(config->emerg_wr);
584 VirtIOSerialPort *port = find_first_connected_console(vser);
585 VirtIOSerialPortClass *vsc;
586
587 if (!config->emerg_wr) {
588 return;
589 }
590
591
592 config->emerg_wr = 0;
593 if (!port) {
594 return;
595 }
596 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
597 (void)vsc->have_data(port, &emerg_wr_lo, 1);
598}
599
600static void guest_reset(VirtIOSerial *vser)
601{
602 VirtIOSerialPort *port;
603 VirtIOSerialPortClass *vsc;
604
605 QTAILQ_FOREACH(port, &vser->ports, next) {
606 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
607
608 discard_throttle_data(port);
609
610 if (port->guest_connected) {
611 port->guest_connected = false;
612 if (vsc->set_guest_connected) {
613 vsc->set_guest_connected(port, false);
614 }
615 }
616 }
617}
618
619static void set_status(VirtIODevice *vdev, uint8_t status)
620{
621 VirtIOSerial *vser;
622 VirtIOSerialPort *port;
623
624 vser = VIRTIO_SERIAL(vdev);
625 port = find_port_by_id(vser, 0);
626
627 if (port && !use_multiport(port->vser)
628 && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
629
630
631
632
633
634
635 port->guest_connected = true;
636 }
637 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
638 guest_reset(vser);
639 }
640
641 QTAILQ_FOREACH(port, &vser->ports, next) {
642 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
643 if (vsc->enable_backend) {
644 vsc->enable_backend(port, vdev->vm_running);
645 }
646 }
647}
648
649static void vser_reset(VirtIODevice *vdev)
650{
651 VirtIOSerial *vser;
652
653 vser = VIRTIO_SERIAL(vdev);
654 guest_reset(vser);
655}
656
657static void virtio_serial_save_device(VirtIODevice *vdev, QEMUFile *f)
658{
659 VirtIOSerial *s = VIRTIO_SERIAL(vdev);
660 VirtIOSerialPort *port;
661 uint32_t nr_active_ports;
662 unsigned int i, max_nr_ports;
663 struct virtio_console_config config;
664
665
666 get_config(vdev, (uint8_t *)&config);
667 qemu_put_be16s(f, &config.cols);
668 qemu_put_be16s(f, &config.rows);
669 qemu_put_be32s(f, &config.max_nr_ports);
670
671
672 max_nr_ports = s->serial.max_virtserial_ports;
673 for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
674 qemu_put_be32s(f, &s->ports_map[i]);
675 }
676
677
678
679 nr_active_ports = 0;
680 QTAILQ_FOREACH(port, &s->ports, next) {
681 nr_active_ports++;
682 }
683
684 qemu_put_be32s(f, &nr_active_ports);
685
686
687
688
689 QTAILQ_FOREACH(port, &s->ports, next) {
690 uint32_t elem_popped;
691
692 qemu_put_be32s(f, &port->id);
693 qemu_put_byte(f, port->guest_connected);
694 qemu_put_byte(f, port->host_connected);
695
696 elem_popped = 0;
697 if (port->elem) {
698 elem_popped = 1;
699 }
700 qemu_put_be32s(f, &elem_popped);
701 if (elem_popped) {
702 qemu_put_be32s(f, &port->iov_idx);
703 qemu_put_be64s(f, &port->iov_offset);
704 qemu_put_virtqueue_element(f, port->elem);
705 }
706 }
707}
708
709static void virtio_serial_post_load_timer_cb(void *opaque)
710{
711 uint32_t i;
712 VirtIOSerial *s = VIRTIO_SERIAL(opaque);
713 VirtIOSerialPort *port;
714 uint8_t host_connected;
715 VirtIOSerialPortClass *vsc;
716
717 if (!s->post_load) {
718 return;
719 }
720 for (i = 0 ; i < s->post_load->nr_active_ports; ++i) {
721 port = s->post_load->connected[i].port;
722 host_connected = s->post_load->connected[i].host_connected;
723 if (host_connected != port->host_connected) {
724
725
726
727
728 send_control_event(s, port->id, VIRTIO_CONSOLE_PORT_OPEN,
729 port->host_connected);
730 }
731 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
732 if (vsc->set_guest_connected) {
733 vsc->set_guest_connected(port, port->guest_connected);
734 }
735 }
736 g_free(s->post_load->connected);
737 timer_del(s->post_load->timer);
738 timer_free(s->post_load->timer);
739 g_free(s->post_load);
740 s->post_load = NULL;
741}
742
743static int fetch_active_ports_list(QEMUFile *f,
744 VirtIOSerial *s, uint32_t nr_active_ports)
745{
746 VirtIODevice *vdev = VIRTIO_DEVICE(s);
747 uint32_t i;
748
749 s->post_load = g_malloc0(sizeof(*s->post_load));
750 s->post_load->nr_active_ports = nr_active_ports;
751 s->post_load->connected =
752 g_malloc0(sizeof(*s->post_load->connected) * nr_active_ports);
753
754 s->post_load->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
755 virtio_serial_post_load_timer_cb,
756 s);
757
758
759 for (i = 0; i < nr_active_ports; i++) {
760 VirtIOSerialPort *port;
761 uint32_t elem_popped;
762 uint32_t id;
763
764 id = qemu_get_be32(f);
765 port = find_port_by_id(s, id);
766 if (!port) {
767 return -EINVAL;
768 }
769
770 port->guest_connected = qemu_get_byte(f);
771 s->post_load->connected[i].port = port;
772 s->post_load->connected[i].host_connected = qemu_get_byte(f);
773
774 qemu_get_be32s(f, &elem_popped);
775 if (elem_popped) {
776 qemu_get_be32s(f, &port->iov_idx);
777 qemu_get_be64s(f, &port->iov_offset);
778
779 port->elem =
780 qemu_get_virtqueue_element(vdev, f, sizeof(VirtQueueElement));
781
782
783
784
785
786 virtio_serial_throttle_port(port, false);
787 }
788 }
789 timer_mod(s->post_load->timer, 1);
790 return 0;
791}
792
793static int virtio_serial_load_device(VirtIODevice *vdev, QEMUFile *f,
794 int version_id)
795{
796 VirtIOSerial *s = VIRTIO_SERIAL(vdev);
797 uint32_t max_nr_ports, nr_active_ports, ports_map;
798 unsigned int i;
799 int ret;
800 uint32_t tmp;
801
802
803 qemu_get_be16s(f, (uint16_t *) &tmp);
804 qemu_get_be16s(f, (uint16_t *) &tmp);
805 qemu_get_be32s(f, &tmp);
806
807 max_nr_ports = s->serial.max_virtserial_ports;
808 for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
809 qemu_get_be32s(f, &ports_map);
810
811 if (ports_map != s->ports_map[i]) {
812
813
814
815
816 return -EINVAL;
817 }
818 }
819
820 qemu_get_be32s(f, &nr_active_ports);
821
822 if (nr_active_ports) {
823 ret = fetch_active_ports_list(f, s, nr_active_ports);
824 if (ret) {
825 return ret;
826 }
827 }
828 return 0;
829}
830
831static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
832
833static Property virtser_props[] = {
834 DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
835 DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
836 DEFINE_PROP_END_OF_LIST()
837};
838
839#define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
840#define VIRTIO_SERIAL_BUS(obj) \
841 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
842
843static void virtser_bus_class_init(ObjectClass *klass, void *data)
844{
845 BusClass *k = BUS_CLASS(klass);
846 k->print_dev = virtser_bus_dev_print;
847}
848
849static const TypeInfo virtser_bus_info = {
850 .name = TYPE_VIRTIO_SERIAL_BUS,
851 .parent = TYPE_BUS,
852 .instance_size = sizeof(VirtIOSerialBus),
853 .class_init = virtser_bus_class_init,
854};
855
856static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
857{
858 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(qdev);
859
860 monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
861 indent, "", port->id,
862 port->guest_connected ? "on" : "off",
863 port->host_connected ? "on" : "off",
864 port->throttled ? "on" : "off");
865}
866
867
868static uint32_t find_free_port_id(VirtIOSerial *vser)
869{
870 unsigned int i, max_nr_ports;
871
872 max_nr_ports = vser->serial.max_virtserial_ports;
873 for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
874 uint32_t map, zeroes;
875
876 map = vser->ports_map[i];
877 zeroes = ctz32(~map);
878 if (zeroes != 32) {
879 return zeroes + i * 32;
880 }
881 }
882 return VIRTIO_CONSOLE_BAD_ID;
883}
884
885static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
886{
887 unsigned int i;
888
889 i = port_id / 32;
890 vser->ports_map[i] |= 1U << (port_id % 32);
891}
892
893static void add_port(VirtIOSerial *vser, uint32_t port_id)
894{
895 mark_port_added(vser, port_id);
896 send_control_event(vser, port_id, VIRTIO_CONSOLE_PORT_ADD, 1);
897}
898
899static void remove_port(VirtIOSerial *vser, uint32_t port_id)
900{
901 VirtIOSerialPort *port;
902
903
904
905
906
907
908 if (port_id) {
909 unsigned int i;
910
911 i = port_id / 32;
912 vser->ports_map[i] &= ~(1U << (port_id % 32));
913 }
914
915 port = find_port_by_id(vser, port_id);
916
917
918
919
920 assert(port);
921
922
923 discard_throttle_data(port);
924 discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
925
926 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_REMOVE, 1);
927}
928
929static void virtser_port_device_realize(DeviceState *dev, Error **errp)
930{
931 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
932 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
933 VirtIOSerialBus *bus = VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev));
934 int max_nr_ports;
935 bool plugging_port0;
936 Error *err = NULL;
937
938 port->vser = bus->vser;
939 port->bh = qemu_bh_new(flush_queued_data_bh, port);
940
941 assert(vsc->have_data);
942
943
944
945
946
947
948 plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
949
950 if (find_port_by_id(port->vser, port->id)) {
951 error_setg(errp, "virtio-serial-bus: A port already exists at id %u",
952 port->id);
953 return;
954 }
955
956 if (port->name != NULL && find_port_by_name(port->name)) {
957 error_setg(errp, "virtio-serial-bus: A port already exists by name %s",
958 port->name);
959 return;
960 }
961
962 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
963 if (plugging_port0) {
964 port->id = 0;
965 } else {
966 port->id = find_free_port_id(port->vser);
967 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
968 error_setg(errp, "virtio-serial-bus: Maximum port limit for "
969 "this device reached");
970 return;
971 }
972 }
973 }
974
975 max_nr_ports = port->vser->serial.max_virtserial_ports;
976 if (port->id >= max_nr_ports) {
977 error_setg(errp, "virtio-serial-bus: Out-of-range port id specified, "
978 "max. allowed: %u", max_nr_ports - 1);
979 return;
980 }
981
982 vsc->realize(dev, &err);
983 if (err != NULL) {
984 error_propagate(errp, err);
985 return;
986 }
987
988 port->elem = NULL;
989}
990
991static void virtser_port_device_plug(HotplugHandler *hotplug_dev,
992 DeviceState *dev, Error **errp)
993{
994 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
995
996 QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
997 port->ivq = port->vser->ivqs[port->id];
998 port->ovq = port->vser->ovqs[port->id];
999
1000 add_port(port->vser, port->id);
1001
1002
1003 virtio_notify_config(VIRTIO_DEVICE(hotplug_dev));
1004}
1005
1006static void virtser_port_device_unrealize(DeviceState *dev, Error **errp)
1007{
1008 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
1009 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
1010 VirtIOSerial *vser = port->vser;
1011
1012 qemu_bh_delete(port->bh);
1013 remove_port(port->vser, port->id);
1014
1015 QTAILQ_REMOVE(&vser->ports, port, next);
1016
1017 if (vsc->unrealize) {
1018 vsc->unrealize(dev, errp);
1019 }
1020}
1021
1022static void virtio_serial_device_realize(DeviceState *dev, Error **errp)
1023{
1024 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1025 VirtIOSerial *vser = VIRTIO_SERIAL(dev);
1026 uint32_t i, max_supported_ports;
1027 size_t config_size = sizeof(struct virtio_console_config);
1028
1029 if (!vser->serial.max_virtserial_ports) {
1030 error_setg(errp, "Maximum number of serial ports not specified");
1031 return;
1032 }
1033
1034
1035 max_supported_ports = VIRTIO_QUEUE_MAX / 2 - 1;
1036
1037 if (vser->serial.max_virtserial_ports > max_supported_ports) {
1038 error_setg(errp, "maximum ports supported: %u", max_supported_ports);
1039 return;
1040 }
1041
1042 if (!virtio_has_feature(vser->host_features,
1043 VIRTIO_CONSOLE_F_EMERG_WRITE)) {
1044 config_size = offsetof(struct virtio_console_config, emerg_wr);
1045 }
1046 virtio_init(vdev, "virtio-serial", VIRTIO_ID_CONSOLE,
1047 config_size);
1048
1049
1050 qbus_create_inplace(&vser->bus, sizeof(vser->bus), TYPE_VIRTIO_SERIAL_BUS,
1051 dev, vdev->bus_name);
1052 qbus_set_hotplug_handler(BUS(&vser->bus), DEVICE(vser), errp);
1053 vser->bus.vser = vser;
1054 QTAILQ_INIT(&vser->ports);
1055
1056 vser->bus.max_nr_ports = vser->serial.max_virtserial_ports;
1057 vser->ivqs = g_malloc(vser->serial.max_virtserial_ports
1058 * sizeof(VirtQueue *));
1059 vser->ovqs = g_malloc(vser->serial.max_virtserial_ports
1060 * sizeof(VirtQueue *));
1061
1062
1063 vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
1064
1065 vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
1066
1067
1068
1069
1070
1071
1072
1073
1074 vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
1075
1076 vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
1077
1078 for (i = 1; i < vser->bus.max_nr_ports; i++) {
1079
1080 vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
1081
1082 vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
1083 }
1084
1085 vser->ports_map = g_malloc0((DIV_ROUND_UP(vser->serial.max_virtserial_ports, 32))
1086 * sizeof(vser->ports_map[0]));
1087
1088
1089
1090
1091 mark_port_added(vser, 0);
1092
1093 vser->post_load = NULL;
1094
1095 QLIST_INSERT_HEAD(&vserdevices.devices, vser, next);
1096}
1097
1098static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
1099{
1100 DeviceClass *k = DEVICE_CLASS(klass);
1101
1102 set_bit(DEVICE_CATEGORY_INPUT, k->categories);
1103 k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
1104 k->realize = virtser_port_device_realize;
1105 k->unrealize = virtser_port_device_unrealize;
1106 k->props = virtser_props;
1107}
1108
1109static const TypeInfo virtio_serial_port_type_info = {
1110 .name = TYPE_VIRTIO_SERIAL_PORT,
1111 .parent = TYPE_DEVICE,
1112 .instance_size = sizeof(VirtIOSerialPort),
1113 .abstract = true,
1114 .class_size = sizeof(VirtIOSerialPortClass),
1115 .class_init = virtio_serial_port_class_init,
1116};
1117
1118static void virtio_serial_device_unrealize(DeviceState *dev, Error **errp)
1119{
1120 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1121 VirtIOSerial *vser = VIRTIO_SERIAL(dev);
1122
1123 QLIST_REMOVE(vser, next);
1124
1125 g_free(vser->ivqs);
1126 g_free(vser->ovqs);
1127 g_free(vser->ports_map);
1128 if (vser->post_load) {
1129 g_free(vser->post_load->connected);
1130 timer_del(vser->post_load->timer);
1131 timer_free(vser->post_load->timer);
1132 g_free(vser->post_load);
1133 }
1134
1135 qbus_set_hotplug_handler(BUS(&vser->bus), NULL, errp);
1136
1137 virtio_cleanup(vdev);
1138}
1139
1140
1141static const VMStateDescription vmstate_virtio_console = {
1142 .name = "virtio-console",
1143 .minimum_version_id = 3,
1144 .version_id = 3,
1145 .fields = (VMStateField[]) {
1146 VMSTATE_VIRTIO_DEVICE,
1147 VMSTATE_END_OF_LIST()
1148 },
1149};
1150
1151static Property virtio_serial_properties[] = {
1152 DEFINE_PROP_UINT32("max_ports", VirtIOSerial, serial.max_virtserial_ports,
1153 31),
1154 DEFINE_PROP_BIT64("emergency-write", VirtIOSerial, host_features,
1155 VIRTIO_CONSOLE_F_EMERG_WRITE, true),
1156 DEFINE_PROP_END_OF_LIST(),
1157};
1158
1159static void virtio_serial_class_init(ObjectClass *klass, void *data)
1160{
1161 DeviceClass *dc = DEVICE_CLASS(klass);
1162 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1163 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1164
1165 QLIST_INIT(&vserdevices.devices);
1166
1167 dc->props = virtio_serial_properties;
1168 dc->vmsd = &vmstate_virtio_console;
1169 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1170 vdc->realize = virtio_serial_device_realize;
1171 vdc->unrealize = virtio_serial_device_unrealize;
1172 vdc->get_features = get_features;
1173 vdc->get_config = get_config;
1174 vdc->set_config = set_config;
1175 vdc->set_status = set_status;
1176 vdc->reset = vser_reset;
1177 vdc->save = virtio_serial_save_device;
1178 vdc->load = virtio_serial_load_device;
1179 hc->plug = virtser_port_device_plug;
1180 hc->unplug = qdev_simple_device_unplug_cb;
1181}
1182
1183static const TypeInfo virtio_device_info = {
1184 .name = TYPE_VIRTIO_SERIAL,
1185 .parent = TYPE_VIRTIO_DEVICE,
1186 .instance_size = sizeof(VirtIOSerial),
1187 .class_init = virtio_serial_class_init,
1188 .interfaces = (InterfaceInfo[]) {
1189 { TYPE_HOTPLUG_HANDLER },
1190 { }
1191 }
1192};
1193
1194static void virtio_serial_register_types(void)
1195{
1196 type_register_static(&virtser_bus_info);
1197 type_register_static(&virtio_serial_port_type_info);
1198 type_register_static(&virtio_device_info);
1199}
1200
1201type_init(virtio_serial_register_types)
1202