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