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