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