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