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