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