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