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