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