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