]>
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 <[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. | |
6b620ca3 PB |
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. | |
98b19252 AS |
19 | */ |
20 | ||
e4d5639d | 21 | #include "iov.h" |
98b19252 AS |
22 | #include "monitor.h" |
23 | #include "qemu-queue.h" | |
24 | #include "sysbus.h" | |
49e3fdd7 | 25 | #include "trace.h" |
98b19252 AS |
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 | ||
bdb917bf AS |
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 | ||
98b19252 AS |
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 | ||
5e52e5f9 | 55 | VirtIOSerialBus bus; |
98b19252 | 56 | |
8b53a865 AS |
57 | DeviceState *qdev; |
58 | ||
98b19252 | 59 | QTAILQ_HEAD(, VirtIOSerialPort) ports; |
055b889f AS |
60 | |
61 | /* bitmap for identifying active ports */ | |
62 | uint32_t *ports_map; | |
63 | ||
98b19252 | 64 | struct virtio_console_config config; |
80dcfb85 | 65 | |
bdb917bf | 66 | struct VirtIOSerialPostLoad *post_load; |
98b19252 AS |
67 | }; |
68 | ||
69 | static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id) | |
70 | { | |
71 | VirtIOSerialPort *port; | |
72 | ||
055b889f AS |
73 | if (id == VIRTIO_CONSOLE_BAD_ID) { |
74 | return NULL; | |
75 | } | |
76 | ||
98b19252 AS |
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 | ||
6663a195 AS |
95 | static bool use_multiport(VirtIOSerial *vser) |
96 | { | |
97 | return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT); | |
98 | } | |
99 | ||
98b19252 AS |
100 | static size_t write_to_port(VirtIOSerialPort *port, |
101 | const uint8_t *buf, size_t size) | |
102 | { | |
103 | VirtQueueElement elem; | |
104 | VirtQueue *vq; | |
e4d5639d | 105 | size_t offset; |
98b19252 AS |
106 | |
107 | vq = port->ivq; | |
108 | if (!virtio_queue_ready(vq)) { | |
109 | return 0; | |
110 | } | |
98b19252 | 111 | |
e4d5639d | 112 | offset = 0; |
98b19252 | 113 | while (offset < size) { |
e4d5639d | 114 | size_t len; |
98b19252 AS |
115 | |
116 | if (!virtqueue_pop(vq, &elem)) { | |
117 | break; | |
118 | } | |
119 | ||
dcf6f5e1 MT |
120 | len = iov_from_buf(elem.in_sg, elem.in_num, 0, |
121 | buf + offset, size - offset); | |
e4d5639d | 122 | offset += len; |
98b19252 | 123 | |
98b19252 AS |
124 | virtqueue_push(vq, &elem, len); |
125 | } | |
126 | ||
127 | virtio_notify(&port->vser->vdev, vq); | |
128 | return offset; | |
129 | } | |
130 | ||
6bff8656 | 131 | static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev) |
a69c7600 AS |
132 | { |
133 | VirtQueueElement elem; | |
134 | ||
7185f931 AS |
135 | if (!virtio_queue_ready(vq)) { |
136 | return; | |
137 | } | |
6bff8656 AS |
138 | while (virtqueue_pop(vq, &elem)) { |
139 | virtqueue_push(vq, &elem, 0); | |
140 | } | |
141 | virtio_notify(vdev, vq); | |
142 | } | |
143 | ||
9ed7b059 | 144 | static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq, |
6bff8656 | 145 | VirtIODevice *vdev) |
a69c7600 | 146 | { |
f82e35e3 | 147 | VirtIOSerialPortClass *vsc; |
a15bb0d6 | 148 | |
6bff8656 | 149 | assert(port); |
fd11a78b | 150 | assert(virtio_queue_ready(vq)); |
a69c7600 | 151 | |
f82e35e3 | 152 | vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
a15bb0d6 | 153 | |
f1925dff | 154 | while (!port->throttled) { |
471344db | 155 | unsigned int i; |
a69c7600 | 156 | |
f1925dff AS |
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 | } | |
a69c7600 | 165 | |
f1925dff AS |
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; | |
f82e35e3 | 171 | ret = vsc->have_data(port, |
a15bb0d6 MA |
172 | port->elem.out_sg[i].iov_base |
173 | + port->iov_offset, | |
174 | buf_size); | |
f1925dff AS |
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)) { | |
ed8e5a85 CB |
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 | */ | |
f82e35e3 | 190 | if (!vsc->is_console) { |
ed8e5a85 CB |
191 | virtio_serial_throttle_port(port, true); |
192 | } | |
f1925dff AS |
193 | port->iov_idx = i; |
194 | if (ret > 0) { | |
195 | port->iov_offset += ret; | |
196 | } | |
197 | break; | |
198 | } | |
199 | port->iov_offset = 0; | |
a69c7600 | 200 | } |
f1925dff AS |
201 | if (port->throttled) { |
202 | break; | |
203 | } | |
204 | virtqueue_push(vq, &port->elem, 0); | |
205 | port->elem.out_num = 0; | |
a69c7600 AS |
206 | } |
207 | virtio_notify(vdev, vq); | |
208 | } | |
209 | ||
6bff8656 | 210 | static void flush_queued_data(VirtIOSerialPort *port) |
9ed7b059 | 211 | { |
a1c59752 | 212 | assert(port); |
9ed7b059 | 213 | |
6b611d3a AS |
214 | if (!virtio_queue_ready(port->ovq)) { |
215 | return; | |
216 | } | |
6bff8656 | 217 | do_flush_queued_data(port, port->ovq, &port->vser->vdev); |
9ed7b059 AS |
218 | } |
219 | ||
4e28976e | 220 | static size_t send_control_msg(VirtIOSerial *vser, void *buf, size_t len) |
98b19252 AS |
221 | { |
222 | VirtQueueElement elem; | |
223 | VirtQueue *vq; | |
98b19252 | 224 | |
4e28976e | 225 | vq = vser->c_ivq; |
98b19252 AS |
226 | if (!virtio_queue_ready(vq)) { |
227 | return 0; | |
228 | } | |
229 | if (!virtqueue_pop(vq, &elem)) { | |
230 | return 0; | |
231 | } | |
232 | ||
98b19252 AS |
233 | memcpy(elem.in_sg[0].iov_base, buf, len); |
234 | ||
235 | virtqueue_push(vq, &elem, len); | |
4e28976e | 236 | virtio_notify(&vser->vdev, vq); |
98b19252 AS |
237 | return len; |
238 | } | |
239 | ||
4e28976e AS |
240 | static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id, |
241 | uint16_t event, uint16_t value) | |
98b19252 AS |
242 | { |
243 | struct virtio_console_control cpkt; | |
244 | ||
4e28976e | 245 | stl_p(&cpkt.id, port_id); |
98b19252 AS |
246 | stw_p(&cpkt.event, event); |
247 | stw_p(&cpkt.value, value); | |
248 | ||
4e28976e AS |
249 | trace_virtio_serial_send_control_event(port_id, event, value); |
250 | return send_control_msg(vser, &cpkt, sizeof(cpkt)); | |
98b19252 AS |
251 | } |
252 | ||
253 | /* Functions for use inside qemu to open and read from/write to ports */ | |
254 | int virtio_serial_open(VirtIOSerialPort *port) | |
255 | { | |
6663a195 AS |
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; | |
4e28976e | 262 | send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1); |
6663a195 | 263 | |
98b19252 AS |
264 | return 0; |
265 | } | |
266 | ||
267 | int virtio_serial_close(VirtIOSerialPort *port) | |
268 | { | |
6663a195 | 269 | port->host_connected = false; |
9ed7b059 AS |
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; | |
6bff8656 | 275 | discard_vq_data(port->ovq, &port->vser->vdev); |
9ed7b059 | 276 | |
4e28976e | 277 | send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 0); |
6663a195 | 278 | |
98b19252 AS |
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 | { | |
6663a195 AS |
286 | if (!port || !port->host_connected || !port->guest_connected) { |
287 | return 0; | |
288 | } | |
98b19252 AS |
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; | |
ad3005ad | 299 | unsigned int bytes; |
98b19252 AS |
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 | } | |
6663a195 AS |
306 | if (use_multiport(port->vser) && !port->guest_connected) { |
307 | return 0; | |
308 | } | |
e1f7b481 | 309 | virtqueue_get_avail_bytes(vq, &bytes, NULL, 4096, 0); |
ad3005ad | 310 | return bytes; |
98b19252 AS |
311 | } |
312 | ||
199646d8 AL |
313 | static void flush_queued_data_bh(void *opaque) |
314 | { | |
315 | VirtIOSerialPort *port = opaque; | |
316 | ||
317 | flush_queued_data(port); | |
318 | } | |
319 | ||
9ed7b059 AS |
320 | void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle) |
321 | { | |
322 | if (!port) { | |
323 | return; | |
324 | } | |
325 | ||
49e3fdd7 | 326 | trace_virtio_serial_throttle_port(port->id, throttle); |
9ed7b059 AS |
327 | port->throttled = throttle; |
328 | if (throttle) { | |
329 | return; | |
330 | } | |
199646d8 | 331 | qemu_bh_schedule(port->bh); |
9ed7b059 AS |
332 | } |
333 | ||
98b19252 | 334 | /* Guest wants to notify us of some event */ |
e61da14d | 335 | static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len) |
98b19252 AS |
336 | { |
337 | struct VirtIOSerialPort *port; | |
f82e35e3 | 338 | VirtIOSerialPortClass *vsc; |
98b19252 | 339 | struct virtio_console_control cpkt, *gcpkt; |
160600fd AS |
340 | uint8_t *buffer; |
341 | size_t buffer_len; | |
98b19252 AS |
342 | |
343 | gcpkt = buf; | |
98b19252 | 344 | |
e61da14d AS |
345 | if (len < sizeof(cpkt)) { |
346 | /* The guest sent an invalid control packet */ | |
347 | return; | |
348 | } | |
349 | ||
98b19252 AS |
350 | cpkt.event = lduw_p(&gcpkt->event); |
351 | cpkt.value = lduw_p(&gcpkt->value); | |
352 | ||
49e3fdd7 AS |
353 | trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value); |
354 | ||
d2e4d08b | 355 | if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) { |
4048c7c3 | 356 | if (!cpkt.value) { |
6daf194d | 357 | error_report("virtio-serial-bus: Guest failure in adding device %s", |
5e52e5f9 | 358 | vser->bus.qbus.name); |
d2e4d08b | 359 | return; |
4048c7c3 | 360 | } |
055b889f AS |
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) { | |
4e28976e | 366 | send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_ADD, 1); |
055b889f | 367 | } |
d2e4d08b LC |
368 | return; |
369 | } | |
055b889f | 370 | |
d2e4d08b LC |
371 | port = find_port_by_id(vser, ldl_p(&gcpkt->id)); |
372 | if (!port) { | |
95c9cde2 | 373 | error_report("virtio-serial-bus: Unexpected port id %u for device %s", |
d2e4d08b LC |
374 | ldl_p(&gcpkt->id), vser->bus.qbus.name); |
375 | return; | |
376 | } | |
377 | ||
49e3fdd7 AS |
378 | trace_virtio_serial_handle_control_message_port(port->id); |
379 | ||
f82e35e3 | 380 | vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
d2e4d08b LC |
381 | |
382 | switch(cpkt.event) { | |
98b19252 | 383 | case VIRTIO_CONSOLE_PORT_READY: |
4048c7c3 | 384 | if (!cpkt.value) { |
6daf194d | 385 | error_report("virtio-serial-bus: Guest failure in adding port %u for device %s", |
5e52e5f9 | 386 | port->id, vser->bus.qbus.name); |
4048c7c3 AS |
387 | break; |
388 | } | |
98b19252 AS |
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 | */ | |
f82e35e3 | 396 | if (vsc->is_console) { |
4e28976e | 397 | send_control_event(vser, port->id, VIRTIO_CONSOLE_CONSOLE_PORT, 1); |
98b19252 | 398 | } |
6663a195 | 399 | |
160600fd | 400 | if (port->name) { |
4e28976e | 401 | stl_p(&cpkt.id, port->id); |
160600fd AS |
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; | |
7267c094 | 406 | buffer = g_malloc(buffer_len); |
160600fd AS |
407 | |
408 | memcpy(buffer, &cpkt, sizeof(cpkt)); | |
409 | memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name)); | |
410 | buffer[buffer_len - 1] = 0; | |
411 | ||
4e28976e | 412 | send_control_msg(vser, buffer, buffer_len); |
7267c094 | 413 | g_free(buffer); |
160600fd AS |
414 | } |
415 | ||
6663a195 | 416 | if (port->host_connected) { |
4e28976e | 417 | send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1); |
6663a195 AS |
418 | } |
419 | ||
98b19252 AS |
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 | */ | |
f82e35e3 AL |
426 | if (vsc->guest_ready) { |
427 | vsc->guest_ready(port); | |
98b19252 AS |
428 | } |
429 | break; | |
6663a195 AS |
430 | |
431 | case VIRTIO_CONSOLE_PORT_OPEN: | |
432 | port->guest_connected = cpkt.value; | |
f82e35e3 | 433 | if (cpkt.value && vsc->guest_open) { |
6663a195 | 434 | /* Send the guest opened notification if an app is interested */ |
f82e35e3 | 435 | vsc->guest_open(port); |
6663a195 AS |
436 | } |
437 | ||
f82e35e3 | 438 | if (!cpkt.value && vsc->guest_close) { |
6663a195 | 439 | /* Send the guest closed notification if an app is interested */ |
f82e35e3 | 440 | vsc->guest_close(port); |
6663a195 AS |
441 | } |
442 | break; | |
98b19252 AS |
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; | |
e61da14d AS |
454 | uint8_t *buf; |
455 | size_t len; | |
98b19252 AS |
456 | |
457 | vser = DO_UPCAST(VirtIOSerial, vdev, vdev); | |
458 | ||
e61da14d AS |
459 | len = 0; |
460 | buf = NULL; | |
98b19252 | 461 | while (virtqueue_pop(vq, &elem)) { |
45270ad8 | 462 | size_t cur_len; |
e61da14d AS |
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) { | |
7267c094 | 470 | g_free(buf); |
e61da14d | 471 | |
7267c094 | 472 | buf = g_malloc(cur_len); |
e61da14d AS |
473 | len = cur_len; |
474 | } | |
dcf6f5e1 | 475 | iov_to_buf(elem.out_sg, elem.out_num, 0, buf, cur_len); |
e61da14d | 476 | |
45270ad8 | 477 | handle_control_message(vser, buf, cur_len); |
1e4476aa | 478 | virtqueue_push(vq, &elem, 0); |
98b19252 | 479 | } |
7267c094 | 480 | g_free(buf); |
98b19252 AS |
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; | |
a69c7600 | 488 | VirtIOSerialPort *port; |
98b19252 AS |
489 | |
490 | vser = DO_UPCAST(VirtIOSerial, vdev, vdev); | |
a69c7600 | 491 | port = find_port_by_vq(vser, vq); |
98b19252 | 492 | |
03ecd2c8 | 493 | if (!port || !port->host_connected) { |
6bff8656 AS |
494 | discard_vq_data(vq, vdev); |
495 | return; | |
496 | } | |
e9b382b0 AS |
497 | |
498 | if (!port->throttled) { | |
499 | do_flush_queued_data(port, vq, vdev); | |
9ed7b059 AS |
500 | return; |
501 | } | |
98b19252 AS |
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 | { | |
306eb457 AS |
510 | VirtIOSerial *vser; |
511 | ||
512 | vser = DO_UPCAST(VirtIOSerial, vdev, vdev); | |
513 | ||
5e52e5f9 | 514 | if (vser->bus.max_nr_ports > 1) { |
ee4d45be MT |
515 | features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT); |
516 | } | |
98b19252 AS |
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 | ||
43997225 AS |
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 | ||
62a9fbf7 AL |
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 | } | |
43997225 AS |
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); | |
62a9fbf7 AL |
581 | } |
582 | ||
98b19252 AS |
583 | static void virtio_serial_save(QEMUFile *f, void *opaque) |
584 | { | |
585 | VirtIOSerial *s = opaque; | |
6663a195 AS |
586 | VirtIOSerialPort *port; |
587 | uint32_t nr_active_ports; | |
5c1c9bb2 | 588 | unsigned int i, max_nr_ports; |
98b19252 AS |
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); | |
6663a195 | 596 | |
055b889f AS |
597 | qemu_put_be32s(f, &s->config.max_nr_ports); |
598 | ||
599 | /* The ports map */ | |
5c1c9bb2 AK |
600 | max_nr_ports = tswap32(s->config.max_nr_ports); |
601 | for (i = 0; i < (max_nr_ports + 31) / 32; i++) { | |
055b889f AS |
602 | qemu_put_be32s(f, &s->ports_map[i]); |
603 | } | |
6663a195 | 604 | |
055b889f | 605 | /* Ports */ |
e245795b | 606 | |
6663a195 | 607 | nr_active_ports = 0; |
e245795b | 608 | QTAILQ_FOREACH(port, &s->ports, next) { |
6663a195 | 609 | nr_active_ports++; |
e245795b | 610 | } |
6663a195 AS |
611 | |
612 | qemu_put_be32s(f, &nr_active_ports); | |
613 | ||
614 | /* | |
615 | * Items in struct VirtIOSerialPort. | |
616 | */ | |
617 | QTAILQ_FOREACH(port, &s->ports, next) { | |
37f95bf3 AS |
618 | uint32_t elem_popped; |
619 | ||
6663a195 AS |
620 | qemu_put_be32s(f, &port->id); |
621 | qemu_put_byte(f, port->guest_connected); | |
31abe21f | 622 | qemu_put_byte(f, port->host_connected); |
37f95bf3 AS |
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 | } | |
6663a195 | 636 | } |
98b19252 AS |
637 | } |
638 | ||
80dcfb85 AL |
639 | static void virtio_serial_post_load_timer_cb(void *opaque) |
640 | { | |
c3587ca1 | 641 | uint32_t i; |
80dcfb85 AL |
642 | VirtIOSerial *s = opaque; |
643 | VirtIOSerialPort *port; | |
644 | uint8_t host_connected; | |
645 | ||
bdb917bf AS |
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; | |
80dcfb85 AL |
652 | if (host_connected != port->host_connected) { |
653 | /* | |
654 | * We have to let the guest know of the host connection | |
655 | * status change | |
656 | */ | |
4e28976e | 657 | send_control_event(s, port->id, VIRTIO_CONSOLE_PORT_OPEN, |
80dcfb85 AL |
658 | port->host_connected); |
659 | } | |
660 | } | |
bdb917bf AS |
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; | |
80dcfb85 AL |
665 | } |
666 | ||
2e575a86 AS |
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 | ||
bdb917bf AS |
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); | |
2e575a86 AS |
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); | |
bdb917bf AS |
693 | s->post_load->connected[i].port = port; |
694 | s->post_load->connected[i].host_connected = qemu_get_byte(f); | |
2e575a86 AS |
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 | } | |
bdb917bf | 719 | qemu_mod_timer(s->post_load->timer, 1); |
2e575a86 AS |
720 | return 0; |
721 | } | |
722 | ||
98b19252 AS |
723 | static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id) |
724 | { | |
725 | VirtIOSerial *s = opaque; | |
f83ccb3e | 726 | uint32_t max_nr_ports, nr_active_ports, ports_map; |
6663a195 | 727 | unsigned int i; |
2a633c46 | 728 | int ret; |
98b19252 | 729 | |
37f95bf3 | 730 | if (version_id > 3) { |
98b19252 AS |
731 | return -EINVAL; |
732 | } | |
6663a195 | 733 | |
98b19252 | 734 | /* The virtio device */ |
2a633c46 OW |
735 | ret = virtio_load(&s->vdev, f); |
736 | if (ret) { | |
737 | return ret; | |
738 | } | |
98b19252 AS |
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); | |
295587f7 | 747 | |
055b889f | 748 | qemu_get_be32s(f, &max_nr_ports); |
5c1c9bb2 AK |
749 | tswap32s(&max_nr_ports); |
750 | if (max_nr_ports > tswap32(s->config.max_nr_ports)) { | |
055b889f | 751 | /* Source could have had more ports than us. Fail migration. */ |
295587f7 AS |
752 | return -EINVAL; |
753 | } | |
98b19252 | 754 | |
055b889f | 755 | for (i = 0; i < (max_nr_ports + 31) / 32; i++) { |
f83ccb3e | 756 | qemu_get_be32s(f, &ports_map); |
055b889f | 757 | |
f83ccb3e | 758 | if (ports_map != s->ports_map[i]) { |
055b889f AS |
759 | /* |
760 | * Ports active on source and destination don't | |
761 | * match. Fail migration. | |
762 | */ | |
055b889f AS |
763 | return -EINVAL; |
764 | } | |
e245795b AS |
765 | } |
766 | ||
6663a195 AS |
767 | qemu_get_be32s(f, &nr_active_ports); |
768 | ||
2e575a86 AS |
769 | if (nr_active_ports) { |
770 | ret = fetch_active_ports_list(f, version_id, s, nr_active_ports); | |
771 | if (ret) { | |
772 | return ret; | |
37f95bf3 | 773 | } |
6663a195 | 774 | } |
98b19252 AS |
775 | return 0; |
776 | } | |
777 | ||
778 | static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent); | |
779 | ||
3cb75a7c PB |
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 | ||
0d936928 AL |
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, | |
98b19252 AS |
801 | }; |
802 | ||
98b19252 AS |
803 | static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) |
804 | { | |
a43f9c90 | 805 | VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev); |
98b19252 | 806 | |
021a1318 MA |
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"); | |
98b19252 AS |
812 | } |
813 | ||
055b889f AS |
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 | { | |
5c1c9bb2 | 817 | unsigned int i, max_nr_ports; |
055b889f | 818 | |
5c1c9bb2 AK |
819 | max_nr_ports = tswap32(vser->config.max_nr_ports); |
820 | for (i = 0; i < (max_nr_ports + 31) / 32; i++) { | |
055b889f AS |
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); | |
4e28976e | 843 | send_control_event(vser, port_id, VIRTIO_CONSOLE_PORT_ADD, 1); |
055b889f AS |
844 | } |
845 | ||
846 | static void remove_port(VirtIOSerial *vser, uint32_t port_id) | |
847 | { | |
9ed7b059 | 848 | VirtIOSerialPort *port; |
055b889f AS |
849 | unsigned int i; |
850 | ||
851 | i = port_id / 32; | |
852 | vser->ports_map[i] &= ~(1U << (port_id % 32)); | |
853 | ||
9ed7b059 | 854 | port = find_port_by_id(vser, port_id); |
91bdd1cf AS |
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 | ||
9ed7b059 | 861 | /* Flush out any unconsumed buffers first */ |
6bff8656 | 862 | discard_vq_data(port->ovq, &port->vser->vdev); |
9ed7b059 | 863 | |
4e28976e | 864 | send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_REMOVE, 1); |
055b889f AS |
865 | } |
866 | ||
d307af79 | 867 | static int virtser_port_qdev_init(DeviceState *qdev) |
98b19252 | 868 | { |
a43f9c90 | 869 | VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev); |
f82e35e3 | 870 | VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
98b19252 | 871 | VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus); |
5c1c9bb2 | 872 | int ret, max_nr_ports; |
98b19252 AS |
873 | bool plugging_port0; |
874 | ||
875 | port->vser = bus->vser; | |
199646d8 | 876 | port->bh = qemu_bh_new(flush_queued_data_bh, port); |
98b19252 | 877 | |
f82e35e3 | 878 | assert(vsc->have_data); |
03ecd2c8 | 879 | |
98b19252 AS |
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 | */ | |
f82e35e3 | 885 | plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0); |
98b19252 | 886 | |
055b889f | 887 | if (find_port_by_id(port->vser, port->id)) { |
6daf194d | 888 | error_report("virtio-serial-bus: A port already exists at id %u", |
055b889f | 889 | port->id); |
98b19252 AS |
890 | return -1; |
891 | } | |
98b19252 | 892 | |
055b889f AS |
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) { | |
6daf194d | 899 | error_report("virtio-serial-bus: Maximum port limit for this device reached"); |
055b889f AS |
900 | return -1; |
901 | } | |
902 | } | |
903 | } | |
904 | ||
5c1c9bb2 AK |
905 | max_nr_ports = tswap32(port->vser->config.max_nr_ports); |
906 | if (port->id >= max_nr_ports) { | |
6daf194d | 907 | error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u", |
5c1c9bb2 | 908 | max_nr_ports - 1); |
055b889f AS |
909 | return -1; |
910 | } | |
911 | ||
f82e35e3 | 912 | ret = vsc->init(port); |
98b19252 AS |
913 | if (ret) { |
914 | return ret; | |
915 | } | |
916 | ||
f1925dff AS |
917 | port->elem.out_num = 0; |
918 | ||
98b19252 AS |
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 | ||
055b889f AS |
923 | add_port(port->vser, port->id); |
924 | ||
98b19252 AS |
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 | { | |
a43f9c90 | 933 | VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev); |
f82e35e3 | 934 | VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
98b19252 AS |
935 | VirtIOSerial *vser = port->vser; |
936 | ||
199646d8 | 937 | qemu_bh_delete(port->bh); |
055b889f | 938 | remove_port(port->vser, port->id); |
f146ec9a | 939 | |
98b19252 AS |
940 | QTAILQ_REMOVE(&vser->ports, port, next); |
941 | ||
f82e35e3 AL |
942 | if (vsc->exit) { |
943 | vsc->exit(port); | |
a15bb0d6 | 944 | } |
98b19252 AS |
945 | return 0; |
946 | } | |
947 | ||
6b331efb | 948 | VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf) |
98b19252 AS |
949 | { |
950 | VirtIOSerial *vser; | |
951 | VirtIODevice *vdev; | |
5ab4bb59 | 952 | uint32_t i, max_supported_ports; |
98b19252 | 953 | |
6b331efb | 954 | if (!conf->max_virtserial_ports) |
98b19252 AS |
955 | return NULL; |
956 | ||
5ab4bb59 AS |
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 | ||
6b331efb | 960 | if (conf->max_virtserial_ports > max_supported_ports) { |
5ab4bb59 AS |
961 | error_report("maximum ports supported: %u", max_supported_ports); |
962 | return NULL; | |
963 | } | |
964 | ||
98b19252 AS |
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 */ | |
0d936928 | 972 | qbus_create_inplace(&vser->bus.qbus, TYPE_VIRTIO_SERIAL_BUS, dev, NULL); |
5e52e5f9 MA |
973 | vser->bus.qbus.allow_hotplug = 1; |
974 | vser->bus.vser = vser; | |
98b19252 AS |
975 | QTAILQ_INIT(&vser->ports); |
976 | ||
5e52e5f9 | 977 | vser->bus.max_nr_ports = conf->max_virtserial_ports; |
7267c094 AL |
978 | vser->ivqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *)); |
979 | vser->ovqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *)); | |
98b19252 AS |
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 | ||
a01a9cb8 AS |
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 | ||
98b19252 | 992 | /* control queue: host to guest */ |
a01a9cb8 | 993 | vser->c_ivq = virtio_add_queue(vdev, 32, control_in); |
98b19252 | 994 | /* control queue: guest to host */ |
a01a9cb8 | 995 | vser->c_ovq = virtio_add_queue(vdev, 32, control_out); |
98b19252 | 996 | |
5e52e5f9 | 997 | for (i = 1; i < vser->bus.max_nr_ports; i++) { |
98b19252 AS |
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 | ||
5c1c9bb2 | 1004 | vser->config.max_nr_ports = tswap32(conf->max_virtserial_ports); |
7267c094 | 1005 | vser->ports_map = g_malloc0(((conf->max_virtserial_ports + 31) / 32) |
a132a679 | 1006 | * sizeof(vser->ports_map[0])); |
98b19252 AS |
1007 | /* |
1008 | * Reserve location 0 for a console port for backward compat | |
1009 | * (old kernel, new qemu) | |
1010 | */ | |
055b889f | 1011 | mark_port_added(vser, 0); |
98b19252 AS |
1012 | |
1013 | vser->vdev.get_features = get_features; | |
1014 | vser->vdev.get_config = get_config; | |
1015 | vser->vdev.set_config = set_config; | |
62a9fbf7 | 1016 | vser->vdev.set_status = set_status; |
43997225 | 1017 | vser->vdev.reset = vser_reset; |
98b19252 | 1018 | |
8b53a865 AS |
1019 | vser->qdev = dev; |
1020 | ||
bdb917bf AS |
1021 | vser->post_load = NULL; |
1022 | ||
98b19252 AS |
1023 | /* |
1024 | * Register for the savevm section with the virtio-console name | |
1025 | * to preserve backward compat | |
1026 | */ | |
37f95bf3 | 1027 | register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save, |
98b19252 AS |
1028 | virtio_serial_load, vser); |
1029 | ||
1030 | return vdev; | |
1031 | } | |
8b53a865 AS |
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 | ||
7267c094 AL |
1039 | g_free(vser->ivqs); |
1040 | g_free(vser->ovqs); | |
1041 | g_free(vser->ports_map); | |
bdb917bf AS |
1042 | if (vser->post_load) { |
1043 | g_free(vser->post_load->connected); | |
a75bf146 | 1044 | qemu_del_timer(vser->post_load->timer); |
bdb917bf AS |
1045 | qemu_free_timer(vser->post_load->timer); |
1046 | g_free(vser->post_load); | |
1047 | } | |
8b53a865 AS |
1048 | virtio_cleanup(vdev); |
1049 | } | |
f82e35e3 | 1050 | |
39bffca2 AL |
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; | |
0d936928 | 1055 | k->bus_type = TYPE_VIRTIO_SERIAL_BUS; |
39bffca2 AL |
1056 | k->exit = virtser_port_qdev_exit; |
1057 | k->unplug = qdev_simple_unplug_cb; | |
bce54474 | 1058 | k->props = virtser_props; |
39bffca2 AL |
1059 | } |
1060 | ||
f82e35e3 AL |
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), | |
39bffca2 | 1067 | .class_init = virtio_serial_port_class_init, |
f82e35e3 AL |
1068 | }; |
1069 | ||
83f7d43a | 1070 | static void virtio_serial_register_types(void) |
f82e35e3 | 1071 | { |
0d936928 | 1072 | type_register_static(&virtser_bus_info); |
f82e35e3 AL |
1073 | type_register_static(&virtio_serial_port_type_info); |
1074 | } | |
1075 | ||
83f7d43a | 1076 | type_init(virtio_serial_register_types) |