]>
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 | ||
1de7afc9 | 21 | #include "qemu/iov.h" |
83c9089e | 22 | #include "monitor/monitor.h" |
1de7afc9 | 23 | #include "qemu/queue.h" |
83c9f4ca | 24 | #include "hw/sysbus.h" |
49e3fdd7 | 25 | #include "trace.h" |
0d09e41a | 26 | #include "hw/virtio/virtio-serial.h" |
e0ab7fac | 27 | #include "hw/virtio/virtio-access.h" |
98b19252 | 28 | |
a1857ad1 AS |
29 | struct VirtIOSerialDevices { |
30 | QLIST_HEAD(, VirtIOSerial) devices; | |
31 | } vserdevices; | |
32 | ||
98b19252 AS |
33 | static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id) |
34 | { | |
35 | VirtIOSerialPort *port; | |
36 | ||
055b889f AS |
37 | if (id == VIRTIO_CONSOLE_BAD_ID) { |
38 | return NULL; | |
39 | } | |
40 | ||
98b19252 AS |
41 | QTAILQ_FOREACH(port, &vser->ports, next) { |
42 | if (port->id == id) | |
43 | return port; | |
44 | } | |
45 | return NULL; | |
46 | } | |
47 | ||
48 | static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq) | |
49 | { | |
50 | VirtIOSerialPort *port; | |
51 | ||
52 | QTAILQ_FOREACH(port, &vser->ports, next) { | |
53 | if (port->ivq == vq || port->ovq == vq) | |
54 | return port; | |
55 | } | |
56 | return NULL; | |
57 | } | |
58 | ||
d0a0bfe6 AS |
59 | static VirtIOSerialPort *find_port_by_name(char *name) |
60 | { | |
61 | VirtIOSerial *vser; | |
62 | ||
63 | QLIST_FOREACH(vser, &vserdevices.devices, next) { | |
64 | VirtIOSerialPort *port; | |
65 | ||
66 | QTAILQ_FOREACH(port, &vser->ports, next) { | |
67 | if (!strcmp(port->name, name)) { | |
68 | return port; | |
69 | } | |
70 | } | |
71 | } | |
72 | return NULL; | |
73 | } | |
74 | ||
6663a195 AS |
75 | static bool use_multiport(VirtIOSerial *vser) |
76 | { | |
76017fd2 FK |
77 | VirtIODevice *vdev = VIRTIO_DEVICE(vser); |
78 | return vdev->guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT); | |
6663a195 AS |
79 | } |
80 | ||
98b19252 AS |
81 | static size_t write_to_port(VirtIOSerialPort *port, |
82 | const uint8_t *buf, size_t size) | |
83 | { | |
84 | VirtQueueElement elem; | |
85 | VirtQueue *vq; | |
e4d5639d | 86 | size_t offset; |
98b19252 AS |
87 | |
88 | vq = port->ivq; | |
89 | if (!virtio_queue_ready(vq)) { | |
90 | return 0; | |
91 | } | |
98b19252 | 92 | |
e4d5639d | 93 | offset = 0; |
98b19252 | 94 | while (offset < size) { |
e4d5639d | 95 | size_t len; |
98b19252 AS |
96 | |
97 | if (!virtqueue_pop(vq, &elem)) { | |
98 | break; | |
99 | } | |
100 | ||
dcf6f5e1 MT |
101 | len = iov_from_buf(elem.in_sg, elem.in_num, 0, |
102 | buf + offset, size - offset); | |
e4d5639d | 103 | offset += len; |
98b19252 | 104 | |
98b19252 AS |
105 | virtqueue_push(vq, &elem, len); |
106 | } | |
107 | ||
76017fd2 | 108 | virtio_notify(VIRTIO_DEVICE(port->vser), vq); |
98b19252 AS |
109 | return offset; |
110 | } | |
111 | ||
6bff8656 | 112 | static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev) |
a69c7600 AS |
113 | { |
114 | VirtQueueElement elem; | |
115 | ||
7185f931 AS |
116 | if (!virtio_queue_ready(vq)) { |
117 | return; | |
118 | } | |
6bff8656 AS |
119 | while (virtqueue_pop(vq, &elem)) { |
120 | virtqueue_push(vq, &elem, 0); | |
121 | } | |
122 | virtio_notify(vdev, vq); | |
123 | } | |
124 | ||
9ed7b059 | 125 | static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq, |
6bff8656 | 126 | VirtIODevice *vdev) |
a69c7600 | 127 | { |
f82e35e3 | 128 | VirtIOSerialPortClass *vsc; |
a15bb0d6 | 129 | |
6bff8656 | 130 | assert(port); |
fd11a78b | 131 | assert(virtio_queue_ready(vq)); |
a69c7600 | 132 | |
f82e35e3 | 133 | vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
a15bb0d6 | 134 | |
f1925dff | 135 | while (!port->throttled) { |
471344db | 136 | unsigned int i; |
a69c7600 | 137 | |
f1925dff AS |
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 | } | |
a69c7600 | 146 | |
f1925dff AS |
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; | |
f82e35e3 | 152 | ret = vsc->have_data(port, |
a15bb0d6 MA |
153 | port->elem.out_sg[i].iov_base |
154 | + port->iov_offset, | |
155 | buf_size); | |
d6258c93 | 156 | if (port->throttled) { |
f1925dff AS |
157 | port->iov_idx = i; |
158 | if (ret > 0) { | |
159 | port->iov_offset += ret; | |
160 | } | |
161 | break; | |
162 | } | |
163 | port->iov_offset = 0; | |
a69c7600 | 164 | } |
f1925dff AS |
165 | if (port->throttled) { |
166 | break; | |
167 | } | |
168 | virtqueue_push(vq, &port->elem, 0); | |
169 | port->elem.out_num = 0; | |
a69c7600 AS |
170 | } |
171 | virtio_notify(vdev, vq); | |
172 | } | |
173 | ||
6bff8656 | 174 | static void flush_queued_data(VirtIOSerialPort *port) |
9ed7b059 | 175 | { |
a1c59752 | 176 | assert(port); |
9ed7b059 | 177 | |
6b611d3a AS |
178 | if (!virtio_queue_ready(port->ovq)) { |
179 | return; | |
180 | } | |
76017fd2 | 181 | do_flush_queued_data(port, port->ovq, VIRTIO_DEVICE(port->vser)); |
9ed7b059 AS |
182 | } |
183 | ||
4e28976e | 184 | static size_t send_control_msg(VirtIOSerial *vser, void *buf, size_t len) |
98b19252 AS |
185 | { |
186 | VirtQueueElement elem; | |
187 | VirtQueue *vq; | |
98b19252 | 188 | |
4e28976e | 189 | vq = vser->c_ivq; |
98b19252 AS |
190 | if (!virtio_queue_ready(vq)) { |
191 | return 0; | |
192 | } | |
193 | if (!virtqueue_pop(vq, &elem)) { | |
194 | return 0; | |
195 | } | |
196 | ||
98b19252 AS |
197 | memcpy(elem.in_sg[0].iov_base, buf, len); |
198 | ||
199 | virtqueue_push(vq, &elem, len); | |
76017fd2 | 200 | virtio_notify(VIRTIO_DEVICE(vser), vq); |
98b19252 AS |
201 | return len; |
202 | } | |
203 | ||
4e28976e AS |
204 | static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id, |
205 | uint16_t event, uint16_t value) | |
98b19252 | 206 | { |
e0ab7fac | 207 | VirtIODevice *vdev = VIRTIO_DEVICE(vser); |
98b19252 AS |
208 | struct virtio_console_control cpkt; |
209 | ||
e0ab7fac RR |
210 | virtio_stl_p(vdev, &cpkt.id, port_id); |
211 | virtio_stw_p(vdev, &cpkt.event, event); | |
212 | virtio_stw_p(vdev, &cpkt.value, value); | |
98b19252 | 213 | |
4e28976e AS |
214 | trace_virtio_serial_send_control_event(port_id, event, value); |
215 | return send_control_msg(vser, &cpkt, sizeof(cpkt)); | |
98b19252 AS |
216 | } |
217 | ||
218 | /* Functions for use inside qemu to open and read from/write to ports */ | |
219 | int virtio_serial_open(VirtIOSerialPort *port) | |
220 | { | |
6663a195 AS |
221 | /* Don't allow opening an already-open port */ |
222 | if (port->host_connected) { | |
223 | return 0; | |
224 | } | |
225 | /* Send port open notification to the guest */ | |
226 | port->host_connected = true; | |
4e28976e | 227 | send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1); |
6663a195 | 228 | |
98b19252 AS |
229 | return 0; |
230 | } | |
231 | ||
232 | int virtio_serial_close(VirtIOSerialPort *port) | |
233 | { | |
6663a195 | 234 | port->host_connected = false; |
9ed7b059 AS |
235 | /* |
236 | * If there's any data the guest sent which the app didn't | |
237 | * consume, reset the throttling flag and discard the data. | |
238 | */ | |
239 | port->throttled = false; | |
76017fd2 | 240 | discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser)); |
9ed7b059 | 241 | |
4e28976e | 242 | send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 0); |
6663a195 | 243 | |
98b19252 AS |
244 | return 0; |
245 | } | |
246 | ||
247 | /* Individual ports/apps call this function to write to the guest. */ | |
248 | ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf, | |
249 | size_t size) | |
250 | { | |
6663a195 AS |
251 | if (!port || !port->host_connected || !port->guest_connected) { |
252 | return 0; | |
253 | } | |
98b19252 AS |
254 | return write_to_port(port, buf, size); |
255 | } | |
256 | ||
257 | /* | |
258 | * Readiness of the guest to accept data on a port. | |
259 | * Returns max. data the guest can receive | |
260 | */ | |
261 | size_t virtio_serial_guest_ready(VirtIOSerialPort *port) | |
262 | { | |
76017fd2 | 263 | VirtIODevice *vdev = VIRTIO_DEVICE(port->vser); |
98b19252 | 264 | VirtQueue *vq = port->ivq; |
ad3005ad | 265 | unsigned int bytes; |
98b19252 AS |
266 | |
267 | if (!virtio_queue_ready(vq) || | |
76017fd2 | 268 | !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) || |
98b19252 AS |
269 | virtio_queue_empty(vq)) { |
270 | return 0; | |
271 | } | |
6663a195 AS |
272 | if (use_multiport(port->vser) && !port->guest_connected) { |
273 | return 0; | |
274 | } | |
e1f7b481 | 275 | virtqueue_get_avail_bytes(vq, &bytes, NULL, 4096, 0); |
ad3005ad | 276 | return bytes; |
98b19252 AS |
277 | } |
278 | ||
199646d8 AL |
279 | static void flush_queued_data_bh(void *opaque) |
280 | { | |
281 | VirtIOSerialPort *port = opaque; | |
282 | ||
283 | flush_queued_data(port); | |
284 | } | |
285 | ||
9ed7b059 AS |
286 | void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle) |
287 | { | |
288 | if (!port) { | |
289 | return; | |
290 | } | |
291 | ||
49e3fdd7 | 292 | trace_virtio_serial_throttle_port(port->id, throttle); |
9ed7b059 AS |
293 | port->throttled = throttle; |
294 | if (throttle) { | |
295 | return; | |
296 | } | |
199646d8 | 297 | qemu_bh_schedule(port->bh); |
9ed7b059 AS |
298 | } |
299 | ||
98b19252 | 300 | /* Guest wants to notify us of some event */ |
e61da14d | 301 | static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len) |
98b19252 | 302 | { |
e0ab7fac | 303 | VirtIODevice *vdev = VIRTIO_DEVICE(vser); |
98b19252 | 304 | struct VirtIOSerialPort *port; |
f82e35e3 | 305 | VirtIOSerialPortClass *vsc; |
98b19252 | 306 | struct virtio_console_control cpkt, *gcpkt; |
160600fd AS |
307 | uint8_t *buffer; |
308 | size_t buffer_len; | |
98b19252 AS |
309 | |
310 | gcpkt = buf; | |
98b19252 | 311 | |
e61da14d AS |
312 | if (len < sizeof(cpkt)) { |
313 | /* The guest sent an invalid control packet */ | |
314 | return; | |
315 | } | |
316 | ||
e0ab7fac RR |
317 | cpkt.event = virtio_lduw_p(vdev, &gcpkt->event); |
318 | cpkt.value = virtio_lduw_p(vdev, &gcpkt->value); | |
98b19252 | 319 | |
49e3fdd7 AS |
320 | trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value); |
321 | ||
d2e4d08b | 322 | if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) { |
4048c7c3 | 323 | if (!cpkt.value) { |
6daf194d | 324 | error_report("virtio-serial-bus: Guest failure in adding device %s", |
5e52e5f9 | 325 | vser->bus.qbus.name); |
d2e4d08b | 326 | return; |
4048c7c3 | 327 | } |
055b889f AS |
328 | /* |
329 | * The device is up, we can now tell the device about all the | |
330 | * ports we have here. | |
331 | */ | |
332 | QTAILQ_FOREACH(port, &vser->ports, next) { | |
4e28976e | 333 | send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_ADD, 1); |
055b889f | 334 | } |
d2e4d08b LC |
335 | return; |
336 | } | |
055b889f | 337 | |
e0ab7fac | 338 | port = find_port_by_id(vser, virtio_ldl_p(vdev, &gcpkt->id)); |
d2e4d08b | 339 | if (!port) { |
95c9cde2 | 340 | error_report("virtio-serial-bus: Unexpected port id %u for device %s", |
e0ab7fac | 341 | virtio_ldl_p(vdev, &gcpkt->id), vser->bus.qbus.name); |
d2e4d08b LC |
342 | return; |
343 | } | |
344 | ||
49e3fdd7 AS |
345 | trace_virtio_serial_handle_control_message_port(port->id); |
346 | ||
f82e35e3 | 347 | vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
d2e4d08b LC |
348 | |
349 | switch(cpkt.event) { | |
98b19252 | 350 | case VIRTIO_CONSOLE_PORT_READY: |
4048c7c3 | 351 | if (!cpkt.value) { |
6daf194d | 352 | error_report("virtio-serial-bus: Guest failure in adding port %u for device %s", |
5e52e5f9 | 353 | port->id, vser->bus.qbus.name); |
4048c7c3 AS |
354 | break; |
355 | } | |
98b19252 AS |
356 | /* |
357 | * Now that we know the guest asked for the port name, we're | |
358 | * sure the guest has initialised whatever state is necessary | |
359 | * for this port. Now's a good time to let the guest know if | |
360 | * this port is a console port so that the guest can hook it | |
361 | * up to hvc. | |
362 | */ | |
f82e35e3 | 363 | if (vsc->is_console) { |
4e28976e | 364 | send_control_event(vser, port->id, VIRTIO_CONSOLE_CONSOLE_PORT, 1); |
98b19252 | 365 | } |
6663a195 | 366 | |
160600fd | 367 | if (port->name) { |
e0ab7fac RR |
368 | virtio_stl_p(vdev, &cpkt.id, port->id); |
369 | virtio_stw_p(vdev, &cpkt.event, VIRTIO_CONSOLE_PORT_NAME); | |
370 | virtio_stw_p(vdev, &cpkt.value, 1); | |
160600fd AS |
371 | |
372 | buffer_len = sizeof(cpkt) + strlen(port->name) + 1; | |
7267c094 | 373 | buffer = g_malloc(buffer_len); |
160600fd AS |
374 | |
375 | memcpy(buffer, &cpkt, sizeof(cpkt)); | |
376 | memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name)); | |
377 | buffer[buffer_len - 1] = 0; | |
378 | ||
4e28976e | 379 | send_control_msg(vser, buffer, buffer_len); |
7267c094 | 380 | g_free(buffer); |
160600fd AS |
381 | } |
382 | ||
6663a195 | 383 | if (port->host_connected) { |
4e28976e | 384 | send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1); |
6663a195 AS |
385 | } |
386 | ||
98b19252 AS |
387 | /* |
388 | * When the guest has asked us for this information it means | |
389 | * the guest is all setup and has its virtqueues | |
390 | * initialised. If some app is interested in knowing about | |
391 | * this event, let it know. | |
392 | */ | |
f82e35e3 AL |
393 | if (vsc->guest_ready) { |
394 | vsc->guest_ready(port); | |
98b19252 AS |
395 | } |
396 | break; | |
6663a195 AS |
397 | |
398 | case VIRTIO_CONSOLE_PORT_OPEN: | |
399 | port->guest_connected = cpkt.value; | |
b2c1394a | 400 | if (vsc->set_guest_connected) { |
6663a195 | 401 | /* Send the guest opened notification if an app is interested */ |
b2c1394a | 402 | vsc->set_guest_connected(port, cpkt.value); |
6663a195 AS |
403 | } |
404 | break; | |
98b19252 AS |
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; | |
e61da14d AS |
416 | uint8_t *buf; |
417 | size_t len; | |
98b19252 | 418 | |
76017fd2 | 419 | vser = VIRTIO_SERIAL(vdev); |
98b19252 | 420 | |
e61da14d AS |
421 | len = 0; |
422 | buf = NULL; | |
98b19252 | 423 | while (virtqueue_pop(vq, &elem)) { |
45270ad8 | 424 | size_t cur_len; |
e61da14d AS |
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) { | |
7267c094 | 432 | g_free(buf); |
e61da14d | 433 | |
7267c094 | 434 | buf = g_malloc(cur_len); |
e61da14d AS |
435 | len = cur_len; |
436 | } | |
dcf6f5e1 | 437 | iov_to_buf(elem.out_sg, elem.out_num, 0, buf, cur_len); |
e61da14d | 438 | |
45270ad8 | 439 | handle_control_message(vser, buf, cur_len); |
1e4476aa | 440 | virtqueue_push(vq, &elem, 0); |
98b19252 | 441 | } |
7267c094 | 442 | g_free(buf); |
98b19252 AS |
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; | |
a69c7600 | 450 | VirtIOSerialPort *port; |
98b19252 | 451 | |
76017fd2 | 452 | vser = VIRTIO_SERIAL(vdev); |
a69c7600 | 453 | port = find_port_by_vq(vser, vq); |
98b19252 | 454 | |
03ecd2c8 | 455 | if (!port || !port->host_connected) { |
6bff8656 AS |
456 | discard_vq_data(vq, vdev); |
457 | return; | |
458 | } | |
e9b382b0 AS |
459 | |
460 | if (!port->throttled) { | |
461 | do_flush_queued_data(port, vq, vdev); | |
9ed7b059 AS |
462 | return; |
463 | } | |
98b19252 AS |
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 | { | |
306eb457 AS |
472 | VirtIOSerial *vser; |
473 | ||
76017fd2 | 474 | vser = VIRTIO_SERIAL(vdev); |
306eb457 | 475 | |
5e52e5f9 | 476 | if (vser->bus.max_nr_ports > 1) { |
ee4d45be MT |
477 | features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT); |
478 | } | |
98b19252 AS |
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 | ||
76017fd2 | 487 | vser = VIRTIO_SERIAL(vdev); |
98b19252 AS |
488 | memcpy(config_data, &vser->config, sizeof(struct virtio_console_config)); |
489 | } | |
490 | ||
43997225 AS |
491 | static void guest_reset(VirtIOSerial *vser) |
492 | { | |
493 | VirtIOSerialPort *port; | |
494 | VirtIOSerialPortClass *vsc; | |
495 | ||
496 | QTAILQ_FOREACH(port, &vser->ports, next) { | |
497 | vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port); | |
498 | if (port->guest_connected) { | |
499 | port->guest_connected = false; | |
b2c1394a HG |
500 | if (vsc->set_guest_connected) { |
501 | vsc->set_guest_connected(port, false); | |
502 | } | |
43997225 AS |
503 | } |
504 | } | |
505 | } | |
506 | ||
62a9fbf7 AL |
507 | static void set_status(VirtIODevice *vdev, uint8_t status) |
508 | { | |
509 | VirtIOSerial *vser; | |
510 | VirtIOSerialPort *port; | |
511 | ||
76017fd2 | 512 | vser = VIRTIO_SERIAL(vdev); |
62a9fbf7 AL |
513 | port = find_port_by_id(vser, 0); |
514 | ||
515 | if (port && !use_multiport(port->vser) | |
516 | && (status & VIRTIO_CONFIG_S_DRIVER_OK)) { | |
517 | /* | |
518 | * Non-multiport guests won't be able to tell us guest | |
519 | * open/close status. Such guests can only have a port at id | |
520 | * 0, so set guest_connected for such ports as soon as guest | |
521 | * is up. | |
522 | */ | |
523 | port->guest_connected = true; | |
524 | } | |
43997225 AS |
525 | if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
526 | guest_reset(vser); | |
527 | } | |
528 | } | |
529 | ||
530 | static void vser_reset(VirtIODevice *vdev) | |
531 | { | |
532 | VirtIOSerial *vser; | |
533 | ||
76017fd2 | 534 | vser = VIRTIO_SERIAL(vdev); |
43997225 | 535 | guest_reset(vser); |
e0ab7fac RR |
536 | |
537 | /* In case we have switched endianness */ | |
538 | vser->config.max_nr_ports = | |
539 | virtio_tswap32(vdev, vser->serial.max_virtserial_ports); | |
62a9fbf7 AL |
540 | } |
541 | ||
98b19252 AS |
542 | static void virtio_serial_save(QEMUFile *f, void *opaque) |
543 | { | |
13c6855a GK |
544 | /* The virtio device */ |
545 | virtio_save(VIRTIO_DEVICE(opaque), f); | |
546 | } | |
547 | ||
548 | static void virtio_serial_save_device(VirtIODevice *vdev, QEMUFile *f) | |
549 | { | |
550 | VirtIOSerial *s = VIRTIO_SERIAL(vdev); | |
6663a195 AS |
551 | VirtIOSerialPort *port; |
552 | uint32_t nr_active_ports; | |
5c1c9bb2 | 553 | unsigned int i, max_nr_ports; |
98b19252 | 554 | |
98b19252 AS |
555 | /* The config space */ |
556 | qemu_put_be16s(f, &s->config.cols); | |
557 | qemu_put_be16s(f, &s->config.rows); | |
6663a195 | 558 | |
055b889f AS |
559 | qemu_put_be32s(f, &s->config.max_nr_ports); |
560 | ||
561 | /* The ports map */ | |
f2f6e00b | 562 | max_nr_ports = s->serial.max_virtserial_ports; |
5c1c9bb2 | 563 | for (i = 0; i < (max_nr_ports + 31) / 32; i++) { |
055b889f AS |
564 | qemu_put_be32s(f, &s->ports_map[i]); |
565 | } | |
6663a195 | 566 | |
055b889f | 567 | /* Ports */ |
e245795b | 568 | |
6663a195 | 569 | nr_active_ports = 0; |
e245795b | 570 | QTAILQ_FOREACH(port, &s->ports, next) { |
6663a195 | 571 | nr_active_ports++; |
e245795b | 572 | } |
6663a195 AS |
573 | |
574 | qemu_put_be32s(f, &nr_active_ports); | |
575 | ||
576 | /* | |
577 | * Items in struct VirtIOSerialPort. | |
578 | */ | |
579 | QTAILQ_FOREACH(port, &s->ports, next) { | |
37f95bf3 AS |
580 | uint32_t elem_popped; |
581 | ||
6663a195 AS |
582 | qemu_put_be32s(f, &port->id); |
583 | qemu_put_byte(f, port->guest_connected); | |
31abe21f | 584 | qemu_put_byte(f, port->host_connected); |
37f95bf3 AS |
585 | |
586 | elem_popped = 0; | |
587 | if (port->elem.out_num) { | |
588 | elem_popped = 1; | |
589 | } | |
590 | qemu_put_be32s(f, &elem_popped); | |
591 | if (elem_popped) { | |
592 | qemu_put_be32s(f, &port->iov_idx); | |
593 | qemu_put_be64s(f, &port->iov_offset); | |
594 | ||
595 | qemu_put_buffer(f, (unsigned char *)&port->elem, | |
596 | sizeof(port->elem)); | |
597 | } | |
6663a195 | 598 | } |
98b19252 AS |
599 | } |
600 | ||
80dcfb85 AL |
601 | static void virtio_serial_post_load_timer_cb(void *opaque) |
602 | { | |
c3587ca1 | 603 | uint32_t i; |
76017fd2 | 604 | VirtIOSerial *s = VIRTIO_SERIAL(opaque); |
80dcfb85 AL |
605 | VirtIOSerialPort *port; |
606 | uint8_t host_connected; | |
bc6b815d | 607 | VirtIOSerialPortClass *vsc; |
80dcfb85 | 608 | |
bdb917bf AS |
609 | if (!s->post_load) { |
610 | return; | |
611 | } | |
612 | for (i = 0 ; i < s->post_load->nr_active_ports; ++i) { | |
613 | port = s->post_load->connected[i].port; | |
614 | host_connected = s->post_load->connected[i].host_connected; | |
80dcfb85 AL |
615 | if (host_connected != port->host_connected) { |
616 | /* | |
617 | * We have to let the guest know of the host connection | |
618 | * status change | |
619 | */ | |
4e28976e | 620 | send_control_event(s, port->id, VIRTIO_CONSOLE_PORT_OPEN, |
80dcfb85 AL |
621 | port->host_connected); |
622 | } | |
bc6b815d AL |
623 | vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
624 | if (vsc->set_guest_connected) { | |
625 | vsc->set_guest_connected(port, port->guest_connected); | |
626 | } | |
80dcfb85 | 627 | } |
bdb917bf | 628 | g_free(s->post_load->connected); |
bc72ad67 | 629 | timer_free(s->post_load->timer); |
bdb917bf AS |
630 | g_free(s->post_load); |
631 | s->post_load = NULL; | |
80dcfb85 AL |
632 | } |
633 | ||
2e575a86 AS |
634 | static int fetch_active_ports_list(QEMUFile *f, int version_id, |
635 | VirtIOSerial *s, uint32_t nr_active_ports) | |
636 | { | |
637 | uint32_t i; | |
638 | ||
bdb917bf AS |
639 | s->post_load = g_malloc0(sizeof(*s->post_load)); |
640 | s->post_load->nr_active_ports = nr_active_ports; | |
641 | s->post_load->connected = | |
642 | g_malloc0(sizeof(*s->post_load->connected) * nr_active_ports); | |
643 | ||
bc72ad67 | 644 | s->post_load->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, |
bdb917bf AS |
645 | virtio_serial_post_load_timer_cb, |
646 | s); | |
2e575a86 AS |
647 | |
648 | /* Items in struct VirtIOSerialPort */ | |
649 | for (i = 0; i < nr_active_ports; i++) { | |
650 | VirtIOSerialPort *port; | |
651 | uint32_t id; | |
652 | ||
653 | id = qemu_get_be32(f); | |
654 | port = find_port_by_id(s, id); | |
655 | if (!port) { | |
656 | return -EINVAL; | |
657 | } | |
658 | ||
659 | port->guest_connected = qemu_get_byte(f); | |
bdb917bf AS |
660 | s->post_load->connected[i].port = port; |
661 | s->post_load->connected[i].host_connected = qemu_get_byte(f); | |
2e575a86 AS |
662 | |
663 | if (version_id > 2) { | |
664 | uint32_t elem_popped; | |
665 | ||
666 | qemu_get_be32s(f, &elem_popped); | |
667 | if (elem_popped) { | |
668 | qemu_get_be32s(f, &port->iov_idx); | |
669 | qemu_get_be64s(f, &port->iov_offset); | |
670 | ||
671 | qemu_get_buffer(f, (unsigned char *)&port->elem, | |
672 | sizeof(port->elem)); | |
673 | virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr, | |
674 | port->elem.in_num, 1); | |
675 | virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr, | |
676 | port->elem.out_num, 1); | |
677 | ||
678 | /* | |
679 | * Port was throttled on source machine. Let's | |
680 | * unthrottle it here so data starts flowing again. | |
681 | */ | |
682 | virtio_serial_throttle_port(port, false); | |
683 | } | |
684 | } | |
685 | } | |
bc72ad67 | 686 | timer_mod(s->post_load->timer, 1); |
2e575a86 AS |
687 | return 0; |
688 | } | |
689 | ||
98b19252 AS |
690 | static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id) |
691 | { | |
37f95bf3 | 692 | if (version_id > 3) { |
98b19252 AS |
693 | return -EINVAL; |
694 | } | |
6663a195 | 695 | |
98b19252 | 696 | /* The virtio device */ |
13c6855a GK |
697 | return virtio_load(VIRTIO_DEVICE(opaque), f, version_id); |
698 | } | |
699 | ||
700 | static int virtio_serial_load_device(VirtIODevice *vdev, QEMUFile *f, | |
701 | int version_id) | |
702 | { | |
703 | VirtIOSerial *s = VIRTIO_SERIAL(vdev); | |
704 | uint32_t max_nr_ports, nr_active_ports, ports_map; | |
705 | unsigned int i; | |
706 | int ret; | |
707 | uint32_t tmp; | |
98b19252 AS |
708 | |
709 | if (version_id < 2) { | |
710 | return 0; | |
711 | } | |
712 | ||
e38e943a AG |
713 | /* Unused */ |
714 | qemu_get_be16s(f, (uint16_t *) &tmp); | |
715 | qemu_get_be16s(f, (uint16_t *) &tmp); | |
716 | qemu_get_be32s(f, &tmp); | |
98b19252 | 717 | |
f2f6e00b | 718 | max_nr_ports = s->serial.max_virtserial_ports; |
055b889f | 719 | for (i = 0; i < (max_nr_ports + 31) / 32; i++) { |
f83ccb3e | 720 | qemu_get_be32s(f, &ports_map); |
055b889f | 721 | |
f83ccb3e | 722 | if (ports_map != s->ports_map[i]) { |
055b889f AS |
723 | /* |
724 | * Ports active on source and destination don't | |
725 | * match. Fail migration. | |
726 | */ | |
055b889f AS |
727 | return -EINVAL; |
728 | } | |
e245795b AS |
729 | } |
730 | ||
6663a195 AS |
731 | qemu_get_be32s(f, &nr_active_ports); |
732 | ||
2e575a86 AS |
733 | if (nr_active_ports) { |
734 | ret = fetch_active_ports_list(f, version_id, s, nr_active_ports); | |
735 | if (ret) { | |
736 | return ret; | |
37f95bf3 | 737 | } |
6663a195 | 738 | } |
98b19252 AS |
739 | return 0; |
740 | } | |
741 | ||
742 | static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent); | |
743 | ||
3cb75a7c PB |
744 | static Property virtser_props[] = { |
745 | DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID), | |
746 | DEFINE_PROP_STRING("name", VirtIOSerialPort, name), | |
747 | DEFINE_PROP_END_OF_LIST() | |
748 | }; | |
749 | ||
0d936928 AL |
750 | #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus" |
751 | #define VIRTIO_SERIAL_BUS(obj) \ | |
752 | OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS) | |
753 | ||
754 | static void virtser_bus_class_init(ObjectClass *klass, void *data) | |
755 | { | |
756 | BusClass *k = BUS_CLASS(klass); | |
757 | k->print_dev = virtser_bus_dev_print; | |
758 | } | |
759 | ||
760 | static const TypeInfo virtser_bus_info = { | |
761 | .name = TYPE_VIRTIO_SERIAL_BUS, | |
762 | .parent = TYPE_BUS, | |
763 | .instance_size = sizeof(VirtIOSerialBus), | |
764 | .class_init = virtser_bus_class_init, | |
98b19252 AS |
765 | }; |
766 | ||
98b19252 AS |
767 | static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) |
768 | { | |
a43f9c90 | 769 | VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev); |
98b19252 | 770 | |
021a1318 MA |
771 | monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n", |
772 | indent, "", port->id, | |
773 | port->guest_connected ? "on" : "off", | |
774 | port->host_connected ? "on" : "off", | |
775 | port->throttled ? "on" : "off"); | |
98b19252 AS |
776 | } |
777 | ||
055b889f AS |
778 | /* This function is only used if a port id is not provided by the user */ |
779 | static uint32_t find_free_port_id(VirtIOSerial *vser) | |
780 | { | |
5c1c9bb2 | 781 | unsigned int i, max_nr_ports; |
055b889f | 782 | |
f2f6e00b | 783 | max_nr_ports = vser->serial.max_virtserial_ports; |
5c1c9bb2 | 784 | for (i = 0; i < (max_nr_ports + 31) / 32; i++) { |
055b889f AS |
785 | uint32_t map, bit; |
786 | ||
787 | map = vser->ports_map[i]; | |
788 | bit = ffs(~map); | |
789 | if (bit) { | |
790 | return (bit - 1) + i * 32; | |
791 | } | |
792 | } | |
793 | return VIRTIO_CONSOLE_BAD_ID; | |
794 | } | |
795 | ||
796 | static void mark_port_added(VirtIOSerial *vser, uint32_t port_id) | |
797 | { | |
798 | unsigned int i; | |
799 | ||
800 | i = port_id / 32; | |
801 | vser->ports_map[i] |= 1U << (port_id % 32); | |
802 | } | |
803 | ||
804 | static void add_port(VirtIOSerial *vser, uint32_t port_id) | |
805 | { | |
806 | mark_port_added(vser, port_id); | |
4e28976e | 807 | send_control_event(vser, port_id, VIRTIO_CONSOLE_PORT_ADD, 1); |
055b889f AS |
808 | } |
809 | ||
810 | static void remove_port(VirtIOSerial *vser, uint32_t port_id) | |
811 | { | |
9ed7b059 | 812 | VirtIOSerialPort *port; |
055b889f | 813 | |
57d84cf3 AS |
814 | /* |
815 | * Don't mark port 0 removed -- we explicitly reserve it for | |
816 | * backward compat with older guests, ensure a virtconsole device | |
817 | * unplug retains the reservation. | |
818 | */ | |
819 | if (port_id) { | |
820 | unsigned int i; | |
821 | ||
822 | i = port_id / 32; | |
823 | vser->ports_map[i] &= ~(1U << (port_id % 32)); | |
824 | } | |
055b889f | 825 | |
9ed7b059 | 826 | port = find_port_by_id(vser, port_id); |
91bdd1cf AS |
827 | /* |
828 | * This function is only called from qdev's unplug callback; if we | |
829 | * get a NULL port here, we're in trouble. | |
830 | */ | |
831 | assert(port); | |
832 | ||
9ed7b059 | 833 | /* Flush out any unconsumed buffers first */ |
76017fd2 | 834 | discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser)); |
9ed7b059 | 835 | |
4e28976e | 836 | send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_REMOVE, 1); |
055b889f AS |
837 | } |
838 | ||
2ef66625 | 839 | static void virtser_port_device_realize(DeviceState *dev, Error **errp) |
98b19252 | 840 | { |
2ef66625 | 841 | VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev); |
f82e35e3 | 842 | VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port); |
2ef66625 AF |
843 | VirtIOSerialBus *bus = VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev)); |
844 | int max_nr_ports; | |
98b19252 | 845 | bool plugging_port0; |
2ef66625 | 846 | Error *err = NULL; |
98b19252 AS |
847 | |
848 | port->vser = bus->vser; | |
199646d8 | 849 | port->bh = qemu_bh_new(flush_queued_data_bh, port); |
98b19252 | 850 | |
f82e35e3 | 851 | assert(vsc->have_data); |
03ecd2c8 | 852 | |
98b19252 AS |
853 | /* |
854 | * Is the first console port we're seeing? If so, put it up at | |
855 | * location 0. This is done for backward compatibility (old | |
856 | * kernel, new qemu). | |
857 | */ | |
f82e35e3 | 858 | plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0); |
98b19252 | 859 | |
055b889f | 860 | if (find_port_by_id(port->vser, port->id)) { |
2ef66625 AF |
861 | error_setg(errp, "virtio-serial-bus: A port already exists at id %u", |
862 | port->id); | |
863 | return; | |
98b19252 | 864 | } |
98b19252 | 865 | |
7eb73114 | 866 | if (port->name != NULL && find_port_by_name(port->name)) { |
d0a0bfe6 AS |
867 | error_setg(errp, "virtio-serial-bus: A port already exists by name %s", |
868 | port->name); | |
869 | return; | |
870 | } | |
871 | ||
055b889f AS |
872 | if (port->id == VIRTIO_CONSOLE_BAD_ID) { |
873 | if (plugging_port0) { | |
874 | port->id = 0; | |
875 | } else { | |
876 | port->id = find_free_port_id(port->vser); | |
877 | if (port->id == VIRTIO_CONSOLE_BAD_ID) { | |
2ef66625 AF |
878 | error_setg(errp, "virtio-serial-bus: Maximum port limit for " |
879 | "this device reached"); | |
880 | return; | |
055b889f AS |
881 | } |
882 | } | |
883 | } | |
884 | ||
f2f6e00b | 885 | max_nr_ports = port->vser->serial.max_virtserial_ports; |
5c1c9bb2 | 886 | if (port->id >= max_nr_ports) { |
2ef66625 AF |
887 | error_setg(errp, "virtio-serial-bus: Out-of-range port id specified, " |
888 | "max. allowed: %u", max_nr_ports - 1); | |
889 | return; | |
055b889f AS |
890 | } |
891 | ||
2ef66625 AF |
892 | vsc->realize(dev, &err); |
893 | if (err != NULL) { | |
894 | error_propagate(errp, err); | |
895 | return; | |
98b19252 AS |
896 | } |
897 | ||
f1925dff | 898 | port->elem.out_num = 0; |
0ddef15b IM |
899 | } |
900 | ||
901 | static void virtser_port_device_plug(HotplugHandler *hotplug_dev, | |
902 | DeviceState *dev, Error **errp) | |
903 | { | |
904 | VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev); | |
f1925dff | 905 | |
98b19252 AS |
906 | QTAILQ_INSERT_TAIL(&port->vser->ports, port, next); |
907 | port->ivq = port->vser->ivqs[port->id]; | |
908 | port->ovq = port->vser->ovqs[port->id]; | |
909 | ||
055b889f AS |
910 | add_port(port->vser, port->id); |
911 | ||
98b19252 | 912 | /* Send an update to the guest about this new port added */ |
0ddef15b | 913 | virtio_notify_config(VIRTIO_DEVICE(hotplug_dev)); |
98b19252 AS |
914 | } |
915 | ||
2ef66625 | 916 | static void virtser_port_device_unrealize(DeviceState *dev, Error **errp) |
98b19252 | 917 | { |
2ef66625 AF |
918 | VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev); |
919 | VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(dev); | |
98b19252 AS |
920 | VirtIOSerial *vser = port->vser; |
921 | ||
199646d8 | 922 | qemu_bh_delete(port->bh); |
055b889f | 923 | remove_port(port->vser, port->id); |
f146ec9a | 924 | |
98b19252 AS |
925 | QTAILQ_REMOVE(&vser->ports, port, next); |
926 | ||
2ef66625 AF |
927 | if (vsc->unrealize) { |
928 | vsc->unrealize(dev, errp); | |
a15bb0d6 | 929 | } |
98b19252 AS |
930 | } |
931 | ||
86346244 | 932 | static void virtio_serial_device_realize(DeviceState *dev, Error **errp) |
98b19252 | 933 | { |
86346244 | 934 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
b1a20c3f | 935 | VirtIOSerial *vser = VIRTIO_SERIAL(dev); |
5ab4bb59 | 936 | uint32_t i, max_supported_ports; |
98b19252 | 937 | |
34b95b2c | 938 | if (!vser->serial.max_virtserial_ports) { |
86346244 AF |
939 | error_setg(errp, "Maximum number of serial ports not specified"); |
940 | return; | |
34b95b2c | 941 | } |
98b19252 | 942 | |
5ab4bb59 AS |
943 | /* Each port takes 2 queues, and one pair is for the control queue */ |
944 | max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1; | |
945 | ||
34b95b2c | 946 | if (vser->serial.max_virtserial_ports > max_supported_ports) { |
86346244 AF |
947 | error_setg(errp, "maximum ports supported: %u", max_supported_ports); |
948 | return; | |
5ab4bb59 AS |
949 | } |
950 | ||
34b95b2c FK |
951 | virtio_init(vdev, "virtio-serial", VIRTIO_ID_CONSOLE, |
952 | sizeof(struct virtio_console_config)); | |
98b19252 AS |
953 | |
954 | /* Spawn a new virtio-serial bus on which the ports will ride as devices */ | |
fb17dfe0 | 955 | qbus_create_inplace(&vser->bus, sizeof(vser->bus), TYPE_VIRTIO_SERIAL_BUS, |
b1a20c3f | 956 | dev, vdev->bus_name); |
0ddef15b | 957 | qbus_set_hotplug_handler(BUS(&vser->bus), DEVICE(vser), errp); |
5e52e5f9 | 958 | vser->bus.vser = vser; |
98b19252 AS |
959 | QTAILQ_INIT(&vser->ports); |
960 | ||
34b95b2c FK |
961 | vser->bus.max_nr_ports = vser->serial.max_virtserial_ports; |
962 | vser->ivqs = g_malloc(vser->serial.max_virtserial_ports | |
963 | * sizeof(VirtQueue *)); | |
964 | vser->ovqs = g_malloc(vser->serial.max_virtserial_ports | |
965 | * sizeof(VirtQueue *)); | |
98b19252 AS |
966 | |
967 | /* Add a queue for host to guest transfers for port 0 (backward compat) */ | |
968 | vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input); | |
969 | /* Add a queue for guest to host transfers for port 0 (backward compat) */ | |
970 | vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output); | |
971 | ||
a01a9cb8 AS |
972 | /* TODO: host to guest notifications can get dropped |
973 | * if the queue fills up. Implement queueing in host, | |
974 | * this might also make it possible to reduce the control | |
975 | * queue size: as guest preposts buffers there, | |
976 | * this will save 4Kbyte of guest memory per entry. */ | |
977 | ||
98b19252 | 978 | /* control queue: host to guest */ |
a01a9cb8 | 979 | vser->c_ivq = virtio_add_queue(vdev, 32, control_in); |
98b19252 | 980 | /* control queue: guest to host */ |
a01a9cb8 | 981 | vser->c_ovq = virtio_add_queue(vdev, 32, control_out); |
98b19252 | 982 | |
5e52e5f9 | 983 | for (i = 1; i < vser->bus.max_nr_ports; i++) { |
98b19252 AS |
984 | /* Add a per-port queue for host to guest transfers */ |
985 | vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input); | |
986 | /* Add a per-per queue for guest to host transfers */ | |
987 | vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output); | |
988 | } | |
989 | ||
e0ab7fac RR |
990 | vser->config.max_nr_ports = |
991 | virtio_tswap32(vdev, vser->serial.max_virtserial_ports); | |
34b95b2c | 992 | vser->ports_map = g_malloc0(((vser->serial.max_virtserial_ports + 31) / 32) |
a132a679 | 993 | * sizeof(vser->ports_map[0])); |
98b19252 AS |
994 | /* |
995 | * Reserve location 0 for a console port for backward compat | |
996 | * (old kernel, new qemu) | |
997 | */ | |
055b889f | 998 | mark_port_added(vser, 0); |
98b19252 | 999 | |
bdb917bf AS |
1000 | vser->post_load = NULL; |
1001 | ||
98b19252 AS |
1002 | /* |
1003 | * Register for the savevm section with the virtio-console name | |
1004 | * to preserve backward compat | |
1005 | */ | |
b1a20c3f | 1006 | register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save, |
98b19252 | 1007 | virtio_serial_load, vser); |
a1857ad1 AS |
1008 | |
1009 | QLIST_INSERT_HEAD(&vserdevices.devices, vser, next); | |
8b53a865 | 1010 | } |
f82e35e3 | 1011 | |
39bffca2 AL |
1012 | static void virtio_serial_port_class_init(ObjectClass *klass, void *data) |
1013 | { | |
1014 | DeviceClass *k = DEVICE_CLASS(klass); | |
2ef66625 | 1015 | |
125ee0ed | 1016 | set_bit(DEVICE_CATEGORY_INPUT, k->categories); |
0d936928 | 1017 | k->bus_type = TYPE_VIRTIO_SERIAL_BUS; |
2ef66625 AF |
1018 | k->realize = virtser_port_device_realize; |
1019 | k->unrealize = virtser_port_device_unrealize; | |
bce54474 | 1020 | k->props = virtser_props; |
39bffca2 AL |
1021 | } |
1022 | ||
8c43a6f0 | 1023 | static const TypeInfo virtio_serial_port_type_info = { |
f82e35e3 AL |
1024 | .name = TYPE_VIRTIO_SERIAL_PORT, |
1025 | .parent = TYPE_DEVICE, | |
1026 | .instance_size = sizeof(VirtIOSerialPort), | |
1027 | .abstract = true, | |
1028 | .class_size = sizeof(VirtIOSerialPortClass), | |
39bffca2 | 1029 | .class_init = virtio_serial_port_class_init, |
f82e35e3 AL |
1030 | }; |
1031 | ||
306ec6c3 | 1032 | static void virtio_serial_device_unrealize(DeviceState *dev, Error **errp) |
2cd2b016 | 1033 | { |
306ec6c3 AF |
1034 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
1035 | VirtIOSerial *vser = VIRTIO_SERIAL(dev); | |
2cd2b016 | 1036 | |
a1857ad1 AS |
1037 | QLIST_REMOVE(vser, next); |
1038 | ||
306ec6c3 | 1039 | unregister_savevm(dev, "virtio-console", vser); |
2cd2b016 FK |
1040 | |
1041 | g_free(vser->ivqs); | |
1042 | g_free(vser->ovqs); | |
1043 | g_free(vser->ports_map); | |
1044 | if (vser->post_load) { | |
1045 | g_free(vser->post_load->connected); | |
bc72ad67 AB |
1046 | timer_del(vser->post_load->timer); |
1047 | timer_free(vser->post_load->timer); | |
2cd2b016 FK |
1048 | g_free(vser->post_load); |
1049 | } | |
6a1a8cc7 | 1050 | virtio_cleanup(vdev); |
2cd2b016 FK |
1051 | } |
1052 | ||
1053 | static Property virtio_serial_properties[] = { | |
1054 | DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerial, serial), | |
1055 | DEFINE_PROP_END_OF_LIST(), | |
1056 | }; | |
1057 | ||
1058 | static void virtio_serial_class_init(ObjectClass *klass, void *data) | |
1059 | { | |
1060 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1061 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
0ddef15b | 1062 | HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); |
86346244 | 1063 | |
a1857ad1 AS |
1064 | QLIST_INIT(&vserdevices.devices); |
1065 | ||
2cd2b016 | 1066 | dc->props = virtio_serial_properties; |
125ee0ed | 1067 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
86346244 | 1068 | vdc->realize = virtio_serial_device_realize; |
306ec6c3 | 1069 | vdc->unrealize = virtio_serial_device_unrealize; |
2cd2b016 FK |
1070 | vdc->get_features = get_features; |
1071 | vdc->get_config = get_config; | |
2cd2b016 FK |
1072 | vdc->set_status = set_status; |
1073 | vdc->reset = vser_reset; | |
13c6855a GK |
1074 | vdc->save = virtio_serial_save_device; |
1075 | vdc->load = virtio_serial_load_device; | |
0ddef15b IM |
1076 | hc->plug = virtser_port_device_plug; |
1077 | hc->unplug = qdev_simple_device_unplug_cb; | |
2cd2b016 FK |
1078 | } |
1079 | ||
1080 | static const TypeInfo virtio_device_info = { | |
1081 | .name = TYPE_VIRTIO_SERIAL, | |
1082 | .parent = TYPE_VIRTIO_DEVICE, | |
1083 | .instance_size = sizeof(VirtIOSerial), | |
1084 | .class_init = virtio_serial_class_init, | |
0ddef15b IM |
1085 | .interfaces = (InterfaceInfo[]) { |
1086 | { TYPE_HOTPLUG_HANDLER }, | |
1087 | { } | |
1088 | } | |
2cd2b016 FK |
1089 | }; |
1090 | ||
83f7d43a | 1091 | static void virtio_serial_register_types(void) |
f82e35e3 | 1092 | { |
0d936928 | 1093 | type_register_static(&virtser_bus_info); |
f82e35e3 | 1094 | type_register_static(&virtio_serial_port_type_info); |
2cd2b016 | 1095 | type_register_static(&virtio_device_info); |
f82e35e3 AL |
1096 | } |
1097 | ||
83f7d43a | 1098 | type_init(virtio_serial_register_types) |