]> Git Repo - qemu.git/blob - hw/virtio-serial-bus.c
net: reorganize headers
[qemu.git] / hw / 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 "iov.h"
22 #include "monitor.h"
23 #include "qemu-queue.h"
24 #include "sysbus.h"
25 #include "trace.h"
26 #include "virtio-serial.h"
27
28 /* The virtio-serial bus on top of which the ports will ride as devices */
29 struct VirtIOSerialBus {
30     BusState qbus;
31
32     /* This is the parent device that provides the bus for ports. */
33     VirtIOSerial *vser;
34
35     /* The maximum number of ports that can ride on top of this bus */
36     uint32_t max_nr_ports;
37 };
38
39 struct VirtIOSerial {
40     VirtIODevice vdev;
41
42     VirtQueue *c_ivq, *c_ovq;
43     /* Arrays of ivqs and ovqs: one per port */
44     VirtQueue **ivqs, **ovqs;
45
46     VirtIOSerialBus bus;
47
48     DeviceState *qdev;
49
50     QTAILQ_HEAD(, VirtIOSerialPort) ports;
51
52     /* bitmap for identifying active ports */
53     uint32_t *ports_map;
54
55     struct virtio_console_config config;
56
57     struct {
58         QEMUTimer *timer;
59         int nr_active_ports;
60         struct {
61             VirtIOSerialPort *port;
62             uint8_t host_connected;
63         } *connected;
64     } post_load;
65 };
66
67 static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
68 {
69     VirtIOSerialPort *port;
70
71     if (id == VIRTIO_CONSOLE_BAD_ID) {
72         return NULL;
73     }
74
75     QTAILQ_FOREACH(port, &vser->ports, next) {
76         if (port->id == id)
77             return port;
78     }
79     return NULL;
80 }
81
82 static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
83 {
84     VirtIOSerialPort *port;
85
86     QTAILQ_FOREACH(port, &vser->ports, next) {
87         if (port->ivq == vq || port->ovq == vq)
88             return port;
89     }
90     return NULL;
91 }
92
93 static bool use_multiport(VirtIOSerial *vser)
94 {
95     return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
96 }
97
98 static size_t write_to_port(VirtIOSerialPort *port,
99                             const uint8_t *buf, size_t size)
100 {
101     VirtQueueElement elem;
102     VirtQueue *vq;
103     size_t offset;
104
105     vq = port->ivq;
106     if (!virtio_queue_ready(vq)) {
107         return 0;
108     }
109
110     offset = 0;
111     while (offset < size) {
112         size_t len;
113
114         if (!virtqueue_pop(vq, &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     }
124
125     virtio_notify(&port->vser->vdev, vq);
126     return offset;
127 }
128
129 static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
130 {
131     VirtQueueElement elem;
132
133     if (!virtio_queue_ready(vq)) {
134         return;
135     }
136     while (virtqueue_pop(vq, &elem)) {
137         virtqueue_push(vq, &elem, 0);
138     }
139     virtio_notify(vdev, vq);
140 }
141
142 static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
143                                  VirtIODevice *vdev)
144 {
145     VirtIOSerialPortClass *vsc;
146
147     assert(port);
148     assert(virtio_queue_ready(vq));
149
150     vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
151
152     while (!port->throttled) {
153         unsigned int i;
154
155         /* Pop an elem only if we haven't left off a previous one mid-way */
156         if (!port->elem.out_num) {
157             if (!virtqueue_pop(vq, &port->elem)) {
158                 break;
159             }
160             port->iov_idx = 0;
161             port->iov_offset = 0;
162         }
163
164         for (i = port->iov_idx; i < port->elem.out_num; i++) {
165             size_t buf_size;
166             ssize_t ret;
167
168             buf_size = port->elem.out_sg[i].iov_len - port->iov_offset;
169             ret = vsc->have_data(port,
170                                   port->elem.out_sg[i].iov_base
171                                   + port->iov_offset,
172                                   buf_size);
173             if (ret < 0 && ret != -EAGAIN) {
174                 /* We don't handle any other type of errors here */
175                 abort();
176             }
177             if (ret == -EAGAIN || (ret >= 0 && ret < buf_size)) {
178                 /*
179                  * this is a temporary check until chardevs can signal to
180                  * frontends that they are writable again. This prevents
181                  * the console from going into throttled mode (forever)
182                  * if virtio-console is connected to a pty without a
183                  * listener. Otherwise the guest spins forever.
184                  * We can revert this if
185                  * 1: chardevs can notify frondends
186                  * 2: the guest driver does not spin in these cases
187                  */
188                 if (!vsc->is_console) {
189                     virtio_serial_throttle_port(port, true);
190                 }
191                 port->iov_idx = i;
192                 if (ret > 0) {
193                     port->iov_offset += ret;
194                 }
195                 break;
196             }
197             port->iov_offset = 0;
198         }
199         if (port->throttled) {
200             break;
201         }
202         virtqueue_push(vq, &port->elem, 0);
203         port->elem.out_num = 0;
204     }
205     virtio_notify(vdev, vq);
206 }
207
208 static void flush_queued_data(VirtIOSerialPort *port)
209 {
210     assert(port);
211
212     if (!virtio_queue_ready(port->ovq)) {
213         return;
214     }
215     do_flush_queued_data(port, port->ovq, &port->vser->vdev);
216 }
217
218 static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
219 {
220     VirtQueueElement elem;
221     VirtQueue *vq;
222     struct virtio_console_control *cpkt;
223
224     vq = port->vser->c_ivq;
225     if (!virtio_queue_ready(vq)) {
226         return 0;
227     }
228     if (!virtqueue_pop(vq, &elem)) {
229         return 0;
230     }
231
232     cpkt = (struct virtio_console_control *)buf;
233     stl_p(&cpkt->id, port->id);
234     memcpy(elem.in_sg[0].iov_base, buf, len);
235
236     virtqueue_push(vq, &elem, len);
237     virtio_notify(&port->vser->vdev, vq);
238     return len;
239 }
240
241 static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
242                                  uint16_t value)
243 {
244     struct virtio_console_control cpkt;
245
246     stw_p(&cpkt.event, event);
247     stw_p(&cpkt.value, value);
248
249     trace_virtio_serial_send_control_event(port->id, event, value);
250     return send_control_msg(port, &cpkt, sizeof(cpkt));
251 }
252
253 /* Functions for use inside qemu to open and read from/write to ports */
254 int virtio_serial_open(VirtIOSerialPort *port)
255 {
256     /* Don't allow opening an already-open port */
257     if (port->host_connected) {
258         return 0;
259     }
260     /* Send port open notification to the guest */
261     port->host_connected = true;
262     send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
263
264     return 0;
265 }
266
267 int virtio_serial_close(VirtIOSerialPort *port)
268 {
269     port->host_connected = false;
270     /*
271      * If there's any data the guest sent which the app didn't
272      * consume, reset the throttling flag and discard the data.
273      */
274     port->throttled = false;
275     discard_vq_data(port->ovq, &port->vser->vdev);
276
277     send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
278
279     return 0;
280 }
281
282 /* Individual ports/apps call this function to write to the guest. */
283 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
284                             size_t size)
285 {
286     if (!port || !port->host_connected || !port->guest_connected) {
287         return 0;
288     }
289     return write_to_port(port, buf, size);
290 }
291
292 /*
293  * Readiness of the guest to accept data on a port.
294  * Returns max. data the guest can receive
295  */
296 size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
297 {
298     VirtQueue *vq = port->ivq;
299     unsigned int bytes;
300
301     if (!virtio_queue_ready(vq) ||
302         !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
303         virtio_queue_empty(vq)) {
304         return 0;
305     }
306     if (use_multiport(port->vser) && !port->guest_connected) {
307         return 0;
308     }
309     virtqueue_get_avail_bytes(vq, &bytes, NULL, 4096, 0);
310     return bytes;
311 }
312
313 static void flush_queued_data_bh(void *opaque)
314 {
315     VirtIOSerialPort *port = opaque;
316
317     flush_queued_data(port);
318 }
319
320 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
321 {
322     if (!port) {
323         return;
324     }
325
326     trace_virtio_serial_throttle_port(port->id, throttle);
327     port->throttled = throttle;
328     if (throttle) {
329         return;
330     }
331     qemu_bh_schedule(port->bh);
332 }
333
334 /* Guest wants to notify us of some event */
335 static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
336 {
337     struct VirtIOSerialPort *port;
338     VirtIOSerialPortClass *vsc;
339     struct virtio_console_control cpkt, *gcpkt;
340     uint8_t *buffer;
341     size_t buffer_len;
342
343     gcpkt = buf;
344
345     if (len < sizeof(cpkt)) {
346         /* The guest sent an invalid control packet */
347         return;
348     }
349
350     cpkt.event = lduw_p(&gcpkt->event);
351     cpkt.value = lduw_p(&gcpkt->value);
352
353     trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
354
355     if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
356         if (!cpkt.value) {
357             error_report("virtio-serial-bus: Guest failure in adding device %s",
358                          vser->bus.qbus.name);
359             return;
360         }
361         /*
362          * The device is up, we can now tell the device about all the
363          * ports we have here.
364          */
365         QTAILQ_FOREACH(port, &vser->ports, next) {
366             send_control_event(port, VIRTIO_CONSOLE_PORT_ADD, 1);
367         }
368         return;
369     }
370
371     port = find_port_by_id(vser, ldl_p(&gcpkt->id));
372     if (!port) {
373         error_report("virtio-serial-bus: Unexpected port id %u for device %s",
374                      ldl_p(&gcpkt->id), vser->bus.qbus.name);
375         return;
376     }
377
378     trace_virtio_serial_handle_control_message_port(port->id);
379
380     vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
381
382     switch(cpkt.event) {
383     case VIRTIO_CONSOLE_PORT_READY:
384         if (!cpkt.value) {
385             error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
386                          port->id, vser->bus.qbus.name);
387             break;
388         }
389         /*
390          * Now that we know the guest asked for the port name, we're
391          * sure the guest has initialised whatever state is necessary
392          * for this port. Now's a good time to let the guest know if
393          * this port is a console port so that the guest can hook it
394          * up to hvc.
395          */
396         if (vsc->is_console) {
397             send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
398         }
399
400         if (port->name) {
401             stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
402             stw_p(&cpkt.value, 1);
403
404             buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
405             buffer = g_malloc(buffer_len);
406
407             memcpy(buffer, &cpkt, sizeof(cpkt));
408             memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
409             buffer[buffer_len - 1] = 0;
410
411             send_control_msg(port, buffer, buffer_len);
412             g_free(buffer);
413         }
414
415         if (port->host_connected) {
416             send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
417         }
418
419         /*
420          * When the guest has asked us for this information it means
421          * the guest is all setup and has its virtqueues
422          * initialised. If some app is interested in knowing about
423          * this event, let it know.
424          */
425         if (vsc->guest_ready) {
426             vsc->guest_ready(port);
427         }
428         break;
429
430     case VIRTIO_CONSOLE_PORT_OPEN:
431         port->guest_connected = cpkt.value;
432         if (cpkt.value && vsc->guest_open) {
433             /* Send the guest opened notification if an app is interested */
434             vsc->guest_open(port);
435         }
436
437         if (!cpkt.value && vsc->guest_close) {
438             /* Send the guest closed notification if an app is interested */
439             vsc->guest_close(port);
440         }
441         break;
442     }
443 }
444
445 static void control_in(VirtIODevice *vdev, VirtQueue *vq)
446 {
447 }
448
449 static void control_out(VirtIODevice *vdev, VirtQueue *vq)
450 {
451     VirtQueueElement elem;
452     VirtIOSerial *vser;
453     uint8_t *buf;
454     size_t len;
455
456     vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
457
458     len = 0;
459     buf = NULL;
460     while (virtqueue_pop(vq, &elem)) {
461         size_t cur_len;
462
463         cur_len = iov_size(elem.out_sg, elem.out_num);
464         /*
465          * Allocate a new buf only if we didn't have one previously or
466          * if the size of the buf differs
467          */
468         if (cur_len > len) {
469             g_free(buf);
470
471             buf = g_malloc(cur_len);
472             len = cur_len;
473         }
474         iov_to_buf(elem.out_sg, elem.out_num, 0, buf, cur_len);
475
476         handle_control_message(vser, buf, cur_len);
477         virtqueue_push(vq, &elem, 0);
478     }
479     g_free(buf);
480     virtio_notify(vdev, vq);
481 }
482
483 /* Guest wrote something to some port. */
484 static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
485 {
486     VirtIOSerial *vser;
487     VirtIOSerialPort *port;
488
489     vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
490     port = find_port_by_vq(vser, vq);
491
492     if (!port || !port->host_connected) {
493         discard_vq_data(vq, vdev);
494         return;
495     }
496
497     if (!port->throttled) {
498         do_flush_queued_data(port, vq, vdev);
499         return;
500     }
501 }
502
503 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
504 {
505 }
506
507 static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
508 {
509     VirtIOSerial *vser;
510
511     vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
512
513     if (vser->bus.max_nr_ports > 1) {
514         features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
515     }
516     return features;
517 }
518
519 /* Guest requested config info */
520 static void get_config(VirtIODevice *vdev, uint8_t *config_data)
521 {
522     VirtIOSerial *vser;
523
524     vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
525     memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
526 }
527
528 static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
529 {
530     struct virtio_console_config config;
531
532     memcpy(&config, config_data, sizeof(config));
533 }
534
535 static void guest_reset(VirtIOSerial *vser)
536 {
537     VirtIOSerialPort *port;
538     VirtIOSerialPortClass *vsc;
539
540     QTAILQ_FOREACH(port, &vser->ports, next) {
541         vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
542         if (port->guest_connected) {
543             port->guest_connected = false;
544
545             if (vsc->guest_close)
546                 vsc->guest_close(port);
547         }
548     }
549 }
550
551 static void set_status(VirtIODevice *vdev, uint8_t status)
552 {
553     VirtIOSerial *vser;
554     VirtIOSerialPort *port;
555
556     vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
557     port = find_port_by_id(vser, 0);
558
559     if (port && !use_multiport(port->vser)
560         && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
561         /*
562          * Non-multiport guests won't be able to tell us guest
563          * open/close status.  Such guests can only have a port at id
564          * 0, so set guest_connected for such ports as soon as guest
565          * is up.
566          */
567         port->guest_connected = true;
568     }
569     if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
570         guest_reset(vser);
571     }
572 }
573
574 static void vser_reset(VirtIODevice *vdev)
575 {
576     VirtIOSerial *vser;
577
578     vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
579     guest_reset(vser);
580 }
581
582 static void virtio_serial_save(QEMUFile *f, void *opaque)
583 {
584     VirtIOSerial *s = opaque;
585     VirtIOSerialPort *port;
586     uint32_t nr_active_ports;
587     unsigned int i, max_nr_ports;
588
589     /* The virtio device */
590     virtio_save(&s->vdev, f);
591
592     /* The config space */
593     qemu_put_be16s(f, &s->config.cols);
594     qemu_put_be16s(f, &s->config.rows);
595
596     qemu_put_be32s(f, &s->config.max_nr_ports);
597
598     /* The ports map */
599     max_nr_ports = tswap32(s->config.max_nr_ports);
600     for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
601         qemu_put_be32s(f, &s->ports_map[i]);
602     }
603
604     /* Ports */
605
606     nr_active_ports = 0;
607     QTAILQ_FOREACH(port, &s->ports, next) {
608         nr_active_ports++;
609     }
610
611     qemu_put_be32s(f, &nr_active_ports);
612
613     /*
614      * Items in struct VirtIOSerialPort.
615      */
616     QTAILQ_FOREACH(port, &s->ports, next) {
617         uint32_t elem_popped;
618
619         qemu_put_be32s(f, &port->id);
620         qemu_put_byte(f, port->guest_connected);
621         qemu_put_byte(f, port->host_connected);
622
623         elem_popped = 0;
624         if (port->elem.out_num) {
625             elem_popped = 1;
626         }
627         qemu_put_be32s(f, &elem_popped);
628         if (elem_popped) {
629             qemu_put_be32s(f, &port->iov_idx);
630             qemu_put_be64s(f, &port->iov_offset);
631
632             qemu_put_buffer(f, (unsigned char *)&port->elem,
633                             sizeof(port->elem));
634         }
635     }
636 }
637
638 static void virtio_serial_post_load_timer_cb(void *opaque)
639 {
640     int i;
641     VirtIOSerial *s = opaque;
642     VirtIOSerialPort *port;
643     uint8_t host_connected;
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;
648         if (host_connected != port->host_connected) {
649             /*
650              * We have to let the guest know of the host connection
651              * status change
652              */
653             send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
654                                port->host_connected);
655         }
656     }
657     g_free(s->post_load.connected);
658     s->post_load.connected = NULL;
659 }
660
661 static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
662 {
663     VirtIOSerial *s = opaque;
664     VirtIOSerialPort *port;
665     uint32_t max_nr_ports, nr_active_ports, ports_map;
666     unsigned int i;
667     int ret;
668
669     if (version_id > 3) {
670         return -EINVAL;
671     }
672
673     /* The virtio device */
674     ret = virtio_load(&s->vdev, f);
675     if (ret) {
676         return ret;
677     }
678
679     if (version_id < 2) {
680         return 0;
681     }
682
683     /* The config space */
684     qemu_get_be16s(f, &s->config.cols);
685     qemu_get_be16s(f, &s->config.rows);
686
687     qemu_get_be32s(f, &max_nr_ports);
688     tswap32s(&max_nr_ports);
689     if (max_nr_ports > tswap32(s->config.max_nr_ports)) {
690         /* Source could have had more ports than us. Fail migration. */
691         return -EINVAL;
692     }
693
694     for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
695         qemu_get_be32s(f, &ports_map);
696
697         if (ports_map != s->ports_map[i]) {
698             /*
699              * Ports active on source and destination don't
700              * match. Fail migration.
701              */
702             return -EINVAL;
703         }
704     }
705
706     qemu_get_be32s(f, &nr_active_ports);
707
708     s->post_load.nr_active_ports = nr_active_ports;
709     s->post_load.connected =
710         g_malloc0(sizeof(*s->post_load.connected) * nr_active_ports);
711
712     /* Items in struct VirtIOSerialPort */
713     for (i = 0; i < nr_active_ports; i++) {
714         uint32_t id;
715
716         id = qemu_get_be32(f);
717         port = find_port_by_id(s, id);
718         if (!port) {
719             return -EINVAL;
720         }
721
722         port->guest_connected = qemu_get_byte(f);
723         s->post_load.connected[i].port = port;
724         s->post_load.connected[i].host_connected = qemu_get_byte(f);
725
726         if (version_id > 2) {
727             uint32_t elem_popped;
728
729             qemu_get_be32s(f, &elem_popped);
730             if (elem_popped) {
731                 qemu_get_be32s(f, &port->iov_idx);
732                 qemu_get_be64s(f, &port->iov_offset);
733
734                 qemu_get_buffer(f, (unsigned char *)&port->elem,
735                                 sizeof(port->elem));
736                 virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
737                                  port->elem.in_num, 1);
738                 virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
739                                  port->elem.out_num, 1);
740
741                 /*
742                  *  Port was throttled on source machine.  Let's
743                  *  unthrottle it here so data starts flowing again.
744                  */
745                 virtio_serial_throttle_port(port, false);
746             }
747         }
748     }
749     qemu_mod_timer(s->post_load.timer, 1);
750     return 0;
751 }
752
753 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
754
755 static Property virtser_props[] = {
756     DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
757     DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
758     DEFINE_PROP_END_OF_LIST()
759 };
760
761 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
762 #define VIRTIO_SERIAL_BUS(obj) \
763       OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
764
765 static void virtser_bus_class_init(ObjectClass *klass, void *data)
766 {
767     BusClass *k = BUS_CLASS(klass);
768     k->print_dev = virtser_bus_dev_print;
769 }
770
771 static const TypeInfo virtser_bus_info = {
772     .name = TYPE_VIRTIO_SERIAL_BUS,
773     .parent = TYPE_BUS,
774     .instance_size = sizeof(VirtIOSerialBus),
775     .class_init = virtser_bus_class_init,
776 };
777
778 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
779 {
780     VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
781
782     monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
783                    indent, "", port->id,
784                    port->guest_connected ? "on" : "off",
785                    port->host_connected ? "on" : "off",
786                    port->throttled ? "on" : "off");
787 }
788
789 /* This function is only used if a port id is not provided by the user */
790 static uint32_t find_free_port_id(VirtIOSerial *vser)
791 {
792     unsigned int i, max_nr_ports;
793
794     max_nr_ports = tswap32(vser->config.max_nr_ports);
795     for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
796         uint32_t map, bit;
797
798         map = vser->ports_map[i];
799         bit = ffs(~map);
800         if (bit) {
801             return (bit - 1) + i * 32;
802         }
803     }
804     return VIRTIO_CONSOLE_BAD_ID;
805 }
806
807 static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
808 {
809     unsigned int i;
810
811     i = port_id / 32;
812     vser->ports_map[i] |= 1U << (port_id % 32);
813 }
814
815 static void add_port(VirtIOSerial *vser, uint32_t port_id)
816 {
817     mark_port_added(vser, port_id);
818
819     send_control_event(find_port_by_id(vser, port_id),
820                        VIRTIO_CONSOLE_PORT_ADD, 1);
821 }
822
823 static void remove_port(VirtIOSerial *vser, uint32_t port_id)
824 {
825     VirtIOSerialPort *port;
826     unsigned int i;
827
828     i = port_id / 32;
829     vser->ports_map[i] &= ~(1U << (port_id % 32));
830
831     port = find_port_by_id(vser, port_id);
832     /* Flush out any unconsumed buffers first */
833     discard_vq_data(port->ovq, &port->vser->vdev);
834
835     send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
836 }
837
838 static int virtser_port_qdev_init(DeviceState *qdev)
839 {
840     VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
841     VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
842     VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
843     int ret, max_nr_ports;
844     bool plugging_port0;
845
846     port->vser = bus->vser;
847     port->bh = qemu_bh_new(flush_queued_data_bh, port);
848
849     assert(vsc->have_data);
850
851     /*
852      * Is the first console port we're seeing? If so, put it up at
853      * location 0. This is done for backward compatibility (old
854      * kernel, new qemu).
855      */
856     plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
857
858     if (find_port_by_id(port->vser, port->id)) {
859         error_report("virtio-serial-bus: A port already exists at id %u",
860                      port->id);
861         return -1;
862     }
863
864     if (port->id == VIRTIO_CONSOLE_BAD_ID) {
865         if (plugging_port0) {
866             port->id = 0;
867         } else {
868             port->id = find_free_port_id(port->vser);
869             if (port->id == VIRTIO_CONSOLE_BAD_ID) {
870                 error_report("virtio-serial-bus: Maximum port limit for this device reached");
871                 return -1;
872             }
873         }
874     }
875
876     max_nr_ports = tswap32(port->vser->config.max_nr_ports);
877     if (port->id >= max_nr_ports) {
878         error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
879                      max_nr_ports - 1);
880         return -1;
881     }
882
883     ret = vsc->init(port);
884     if (ret) {
885         return ret;
886     }
887
888     port->elem.out_num = 0;
889
890     QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
891     port->ivq = port->vser->ivqs[port->id];
892     port->ovq = port->vser->ovqs[port->id];
893
894     add_port(port->vser, port->id);
895
896     /* Send an update to the guest about this new port added */
897     virtio_notify_config(&port->vser->vdev);
898
899     return ret;
900 }
901
902 static int virtser_port_qdev_exit(DeviceState *qdev)
903 {
904     VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
905     VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
906     VirtIOSerial *vser = port->vser;
907
908     qemu_bh_delete(port->bh);
909     remove_port(port->vser, port->id);
910
911     QTAILQ_REMOVE(&vser->ports, port, next);
912
913     if (vsc->exit) {
914         vsc->exit(port);
915     }
916     return 0;
917 }
918
919 VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
920 {
921     VirtIOSerial *vser;
922     VirtIODevice *vdev;
923     uint32_t i, max_supported_ports;
924
925     if (!conf->max_virtserial_ports)
926         return NULL;
927
928     /* Each port takes 2 queues, and one pair is for the control queue */
929     max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1;
930
931     if (conf->max_virtserial_ports > max_supported_ports) {
932         error_report("maximum ports supported: %u", max_supported_ports);
933         return NULL;
934     }
935
936     vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
937                               sizeof(struct virtio_console_config),
938                               sizeof(VirtIOSerial));
939
940     vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
941
942     /* Spawn a new virtio-serial bus on which the ports will ride as devices */
943     qbus_create_inplace(&vser->bus.qbus, TYPE_VIRTIO_SERIAL_BUS, dev, NULL);
944     vser->bus.qbus.allow_hotplug = 1;
945     vser->bus.vser = vser;
946     QTAILQ_INIT(&vser->ports);
947
948     vser->bus.max_nr_ports = conf->max_virtserial_ports;
949     vser->ivqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
950     vser->ovqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
951
952     /* Add a queue for host to guest transfers for port 0 (backward compat) */
953     vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
954     /* Add a queue for guest to host transfers for port 0 (backward compat) */
955     vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
956
957     /* TODO: host to guest notifications can get dropped
958      * if the queue fills up. Implement queueing in host,
959      * this might also make it possible to reduce the control
960      * queue size: as guest preposts buffers there,
961      * this will save 4Kbyte of guest memory per entry. */
962
963     /* control queue: host to guest */
964     vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
965     /* control queue: guest to host */
966     vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
967
968     for (i = 1; i < vser->bus.max_nr_ports; i++) {
969         /* Add a per-port queue for host to guest transfers */
970         vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
971         /* Add a per-per queue for guest to host transfers */
972         vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
973     }
974
975     vser->config.max_nr_ports = tswap32(conf->max_virtserial_ports);
976     vser->ports_map = g_malloc0(((conf->max_virtserial_ports + 31) / 32)
977         * sizeof(vser->ports_map[0]));
978     /*
979      * Reserve location 0 for a console port for backward compat
980      * (old kernel, new qemu)
981      */
982     mark_port_added(vser, 0);
983
984     vser->vdev.get_features = get_features;
985     vser->vdev.get_config = get_config;
986     vser->vdev.set_config = set_config;
987     vser->vdev.set_status = set_status;
988     vser->vdev.reset = vser_reset;
989
990     vser->qdev = dev;
991
992     /*
993      * Register for the savevm section with the virtio-console name
994      * to preserve backward compat
995      */
996     register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
997                     virtio_serial_load, vser);
998
999     vser->post_load.timer = qemu_new_timer_ns(vm_clock,
1000             virtio_serial_post_load_timer_cb, vser);
1001
1002     return vdev;
1003 }
1004
1005 void virtio_serial_exit(VirtIODevice *vdev)
1006 {
1007     VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
1008
1009     unregister_savevm(vser->qdev, "virtio-console", vser);
1010
1011     g_free(vser->ivqs);
1012     g_free(vser->ovqs);
1013     g_free(vser->ports_map);
1014     g_free(vser->post_load.connected);
1015     qemu_free_timer(vser->post_load.timer);
1016
1017     virtio_cleanup(vdev);
1018 }
1019
1020 static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
1021 {
1022     DeviceClass *k = DEVICE_CLASS(klass);
1023     k->init = virtser_port_qdev_init;
1024     k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
1025     k->exit = virtser_port_qdev_exit;
1026     k->unplug = qdev_simple_unplug_cb;
1027     k->props = virtser_props;
1028 }
1029
1030 static TypeInfo virtio_serial_port_type_info = {
1031     .name = TYPE_VIRTIO_SERIAL_PORT,
1032     .parent = TYPE_DEVICE,
1033     .instance_size = sizeof(VirtIOSerialPort),
1034     .abstract = true,
1035     .class_size = sizeof(VirtIOSerialPortClass),
1036     .class_init = virtio_serial_port_class_init,
1037 };
1038
1039 static void virtio_serial_register_types(void)
1040 {
1041     type_register_static(&virtser_bus_info);
1042     type_register_static(&virtio_serial_port_type_info);
1043 }
1044
1045 type_init(virtio_serial_register_types)
This page took 0.081778 seconds and 4 git commands to generate.