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