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