]>
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. | |
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 | ||
42 | VirtIOSerialBus *bus; | |
43 | ||
44 | QTAILQ_HEAD(, VirtIOSerialPort) ports; | |
055b889f AS |
45 | |
46 | /* bitmap for identifying active ports */ | |
47 | uint32_t *ports_map; | |
48 | ||
98b19252 AS |
49 | struct virtio_console_config config; |
50 | }; | |
51 | ||
52 | static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id) | |
53 | { | |
54 | VirtIOSerialPort *port; | |
55 | ||
055b889f AS |
56 | if (id == VIRTIO_CONSOLE_BAD_ID) { |
57 | return NULL; | |
58 | } | |
59 | ||
98b19252 AS |
60 | QTAILQ_FOREACH(port, &vser->ports, next) { |
61 | if (port->id == id) | |
62 | return port; | |
63 | } | |
64 | return NULL; | |
65 | } | |
66 | ||
67 | static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq) | |
68 | { | |
69 | VirtIOSerialPort *port; | |
70 | ||
71 | QTAILQ_FOREACH(port, &vser->ports, next) { | |
72 | if (port->ivq == vq || port->ovq == vq) | |
73 | return port; | |
74 | } | |
75 | return NULL; | |
76 | } | |
77 | ||
6663a195 AS |
78 | static bool use_multiport(VirtIOSerial *vser) |
79 | { | |
80 | return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT); | |
81 | } | |
82 | ||
98b19252 AS |
83 | static size_t write_to_port(VirtIOSerialPort *port, |
84 | const uint8_t *buf, size_t size) | |
85 | { | |
86 | VirtQueueElement elem; | |
87 | VirtQueue *vq; | |
e4d5639d | 88 | size_t offset; |
98b19252 AS |
89 | |
90 | vq = port->ivq; | |
91 | if (!virtio_queue_ready(vq)) { | |
92 | return 0; | |
93 | } | |
98b19252 | 94 | |
e4d5639d | 95 | offset = 0; |
98b19252 | 96 | while (offset < size) { |
e4d5639d | 97 | size_t len; |
98b19252 AS |
98 | |
99 | if (!virtqueue_pop(vq, &elem)) { | |
100 | break; | |
101 | } | |
102 | ||
e4d5639d AS |
103 | len = iov_from_buf(elem.in_sg, elem.in_num, |
104 | buf + offset, size - offset); | |
105 | offset += len; | |
98b19252 | 106 | |
98b19252 AS |
107 | virtqueue_push(vq, &elem, len); |
108 | } | |
109 | ||
110 | virtio_notify(&port->vser->vdev, vq); | |
111 | return offset; | |
112 | } | |
113 | ||
114 | static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len) | |
115 | { | |
116 | VirtQueueElement elem; | |
117 | VirtQueue *vq; | |
118 | struct virtio_console_control *cpkt; | |
119 | ||
120 | vq = port->vser->c_ivq; | |
121 | if (!virtio_queue_ready(vq)) { | |
122 | return 0; | |
123 | } | |
124 | if (!virtqueue_pop(vq, &elem)) { | |
125 | return 0; | |
126 | } | |
127 | ||
128 | cpkt = (struct virtio_console_control *)buf; | |
129 | stl_p(&cpkt->id, port->id); | |
130 | memcpy(elem.in_sg[0].iov_base, buf, len); | |
131 | ||
132 | virtqueue_push(vq, &elem, len); | |
133 | virtio_notify(&port->vser->vdev, vq); | |
134 | return len; | |
135 | } | |
136 | ||
137 | static size_t send_control_event(VirtIOSerialPort *port, uint16_t event, | |
138 | uint16_t value) | |
139 | { | |
140 | struct virtio_console_control cpkt; | |
141 | ||
142 | stw_p(&cpkt.event, event); | |
143 | stw_p(&cpkt.value, value); | |
144 | ||
145 | return send_control_msg(port, &cpkt, sizeof(cpkt)); | |
146 | } | |
147 | ||
148 | /* Functions for use inside qemu to open and read from/write to ports */ | |
149 | int virtio_serial_open(VirtIOSerialPort *port) | |
150 | { | |
6663a195 AS |
151 | /* Don't allow opening an already-open port */ |
152 | if (port->host_connected) { | |
153 | return 0; | |
154 | } | |
155 | /* Send port open notification to the guest */ | |
156 | port->host_connected = true; | |
157 | send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1); | |
158 | ||
98b19252 AS |
159 | return 0; |
160 | } | |
161 | ||
162 | int virtio_serial_close(VirtIOSerialPort *port) | |
163 | { | |
6663a195 AS |
164 | port->host_connected = false; |
165 | send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0); | |
166 | ||
98b19252 AS |
167 | return 0; |
168 | } | |
169 | ||
170 | /* Individual ports/apps call this function to write to the guest. */ | |
171 | ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf, | |
172 | size_t size) | |
173 | { | |
6663a195 AS |
174 | if (!port || !port->host_connected || !port->guest_connected) { |
175 | return 0; | |
176 | } | |
98b19252 AS |
177 | return write_to_port(port, buf, size); |
178 | } | |
179 | ||
180 | /* | |
181 | * Readiness of the guest to accept data on a port. | |
182 | * Returns max. data the guest can receive | |
183 | */ | |
184 | size_t virtio_serial_guest_ready(VirtIOSerialPort *port) | |
185 | { | |
186 | VirtQueue *vq = port->ivq; | |
187 | ||
188 | if (!virtio_queue_ready(vq) || | |
189 | !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) || | |
190 | virtio_queue_empty(vq)) { | |
191 | return 0; | |
192 | } | |
6663a195 AS |
193 | if (use_multiport(port->vser) && !port->guest_connected) { |
194 | return 0; | |
195 | } | |
98b19252 AS |
196 | |
197 | if (virtqueue_avail_bytes(vq, 4096, 0)) { | |
198 | return 4096; | |
199 | } | |
200 | if (virtqueue_avail_bytes(vq, 1, 0)) { | |
201 | return 1; | |
202 | } | |
203 | return 0; | |
204 | } | |
205 | ||
206 | /* Guest wants to notify us of some event */ | |
207 | static void handle_control_message(VirtIOSerial *vser, void *buf) | |
208 | { | |
209 | struct VirtIOSerialPort *port; | |
210 | struct virtio_console_control cpkt, *gcpkt; | |
160600fd AS |
211 | uint8_t *buffer; |
212 | size_t buffer_len; | |
98b19252 AS |
213 | |
214 | gcpkt = buf; | |
98b19252 AS |
215 | |
216 | cpkt.event = lduw_p(&gcpkt->event); | |
217 | cpkt.value = lduw_p(&gcpkt->value); | |
218 | ||
055b889f AS |
219 | port = find_port_by_id(vser, ldl_p(&gcpkt->id)); |
220 | if (!port && cpkt.event != VIRTIO_CONSOLE_DEVICE_READY) | |
221 | return; | |
222 | ||
98b19252 | 223 | switch(cpkt.event) { |
055b889f | 224 | case VIRTIO_CONSOLE_DEVICE_READY: |
4048c7c3 AS |
225 | if (!cpkt.value) { |
226 | error_report("virtio-serial-bus: Guest failure in adding device %s\n", | |
227 | vser->bus->qbus.name); | |
228 | break; | |
229 | } | |
055b889f AS |
230 | /* |
231 | * The device is up, we can now tell the device about all the | |
232 | * ports we have here. | |
233 | */ | |
234 | QTAILQ_FOREACH(port, &vser->ports, next) { | |
235 | send_control_event(port, VIRTIO_CONSOLE_PORT_ADD, 1); | |
236 | } | |
237 | break; | |
238 | ||
98b19252 | 239 | case VIRTIO_CONSOLE_PORT_READY: |
4048c7c3 AS |
240 | if (!cpkt.value) { |
241 | error_report("virtio-serial-bus: Guest failure in adding port %u for device %s\n", | |
242 | port->id, vser->bus->qbus.name); | |
243 | break; | |
244 | } | |
98b19252 AS |
245 | /* |
246 | * Now that we know the guest asked for the port name, we're | |
247 | * sure the guest has initialised whatever state is necessary | |
248 | * for this port. Now's a good time to let the guest know if | |
249 | * this port is a console port so that the guest can hook it | |
250 | * up to hvc. | |
251 | */ | |
252 | if (port->is_console) { | |
253 | send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1); | |
254 | } | |
6663a195 | 255 | |
160600fd AS |
256 | if (port->name) { |
257 | stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME); | |
258 | stw_p(&cpkt.value, 1); | |
259 | ||
260 | buffer_len = sizeof(cpkt) + strlen(port->name) + 1; | |
261 | buffer = qemu_malloc(buffer_len); | |
262 | ||
263 | memcpy(buffer, &cpkt, sizeof(cpkt)); | |
264 | memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name)); | |
265 | buffer[buffer_len - 1] = 0; | |
266 | ||
267 | send_control_msg(port, buffer, buffer_len); | |
268 | qemu_free(buffer); | |
269 | } | |
270 | ||
6663a195 AS |
271 | if (port->host_connected) { |
272 | send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1); | |
273 | } | |
274 | ||
98b19252 AS |
275 | /* |
276 | * When the guest has asked us for this information it means | |
277 | * the guest is all setup and has its virtqueues | |
278 | * initialised. If some app is interested in knowing about | |
279 | * this event, let it know. | |
280 | */ | |
281 | if (port->info->guest_ready) { | |
282 | port->info->guest_ready(port); | |
283 | } | |
284 | break; | |
6663a195 AS |
285 | |
286 | case VIRTIO_CONSOLE_PORT_OPEN: | |
287 | port->guest_connected = cpkt.value; | |
288 | if (cpkt.value && port->info->guest_open) { | |
289 | /* Send the guest opened notification if an app is interested */ | |
290 | port->info->guest_open(port); | |
291 | } | |
292 | ||
293 | if (!cpkt.value && port->info->guest_close) { | |
294 | /* Send the guest closed notification if an app is interested */ | |
295 | port->info->guest_close(port); | |
296 | } | |
297 | break; | |
98b19252 AS |
298 | } |
299 | } | |
300 | ||
301 | static void control_in(VirtIODevice *vdev, VirtQueue *vq) | |
302 | { | |
303 | } | |
304 | ||
305 | static void control_out(VirtIODevice *vdev, VirtQueue *vq) | |
306 | { | |
307 | VirtQueueElement elem; | |
308 | VirtIOSerial *vser; | |
309 | ||
310 | vser = DO_UPCAST(VirtIOSerial, vdev, vdev); | |
311 | ||
312 | while (virtqueue_pop(vq, &elem)) { | |
313 | handle_control_message(vser, elem.out_sg[0].iov_base); | |
314 | virtqueue_push(vq, &elem, elem.out_sg[0].iov_len); | |
315 | } | |
316 | virtio_notify(vdev, vq); | |
317 | } | |
318 | ||
319 | /* Guest wrote something to some port. */ | |
320 | static void handle_output(VirtIODevice *vdev, VirtQueue *vq) | |
321 | { | |
322 | VirtIOSerial *vser; | |
323 | VirtQueueElement elem; | |
324 | ||
325 | vser = DO_UPCAST(VirtIOSerial, vdev, vdev); | |
326 | ||
327 | while (virtqueue_pop(vq, &elem)) { | |
328 | VirtIOSerialPort *port; | |
329 | size_t ret; | |
330 | ||
331 | port = find_port_by_vq(vser, vq); | |
332 | if (!port) { | |
333 | ret = 0; | |
334 | goto next_buf; | |
3ecb45f8 AS |
335 | } |
336 | ||
337 | if (!port->host_connected) { | |
338 | ret = 0; | |
339 | goto next_buf; | |
98b19252 AS |
340 | } |
341 | ||
342 | /* | |
343 | * A port may not have any handler registered for consuming the | |
344 | * data that the guest sends or it may not have a chardev associated | |
345 | * with it. Just ignore the data in that case. | |
346 | */ | |
347 | if (!port->info->have_data) { | |
348 | ret = 0; | |
349 | goto next_buf; | |
350 | } | |
351 | ||
352 | /* The guest always sends only one sg */ | |
353 | ret = port->info->have_data(port, elem.out_sg[0].iov_base, | |
354 | elem.out_sg[0].iov_len); | |
355 | ||
356 | next_buf: | |
357 | virtqueue_push(vq, &elem, ret); | |
358 | } | |
359 | virtio_notify(vdev, vq); | |
360 | } | |
361 | ||
362 | static void handle_input(VirtIODevice *vdev, VirtQueue *vq) | |
363 | { | |
364 | } | |
365 | ||
366 | static uint32_t get_features(VirtIODevice *vdev, uint32_t features) | |
367 | { | |
306eb457 AS |
368 | VirtIOSerial *vser; |
369 | ||
370 | vser = DO_UPCAST(VirtIOSerial, vdev, vdev); | |
371 | ||
ee4d45be MT |
372 | if (vser->bus->max_nr_ports > 1) { |
373 | features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT); | |
374 | } | |
98b19252 AS |
375 | return features; |
376 | } | |
377 | ||
378 | /* Guest requested config info */ | |
379 | static void get_config(VirtIODevice *vdev, uint8_t *config_data) | |
380 | { | |
381 | VirtIOSerial *vser; | |
382 | ||
383 | vser = DO_UPCAST(VirtIOSerial, vdev, vdev); | |
384 | memcpy(config_data, &vser->config, sizeof(struct virtio_console_config)); | |
385 | } | |
386 | ||
387 | static void set_config(VirtIODevice *vdev, const uint8_t *config_data) | |
388 | { | |
389 | struct virtio_console_config config; | |
390 | ||
391 | memcpy(&config, config_data, sizeof(config)); | |
392 | } | |
393 | ||
394 | static void virtio_serial_save(QEMUFile *f, void *opaque) | |
395 | { | |
396 | VirtIOSerial *s = opaque; | |
6663a195 AS |
397 | VirtIOSerialPort *port; |
398 | uint32_t nr_active_ports; | |
055b889f | 399 | unsigned int i; |
98b19252 AS |
400 | |
401 | /* The virtio device */ | |
402 | virtio_save(&s->vdev, f); | |
403 | ||
404 | /* The config space */ | |
405 | qemu_put_be16s(f, &s->config.cols); | |
406 | qemu_put_be16s(f, &s->config.rows); | |
6663a195 | 407 | |
055b889f AS |
408 | qemu_put_be32s(f, &s->config.max_nr_ports); |
409 | ||
410 | /* The ports map */ | |
411 | ||
412 | for (i = 0; i < (s->config.max_nr_ports + 31) / 32; i++) { | |
413 | qemu_put_be32s(f, &s->ports_map[i]); | |
414 | } | |
6663a195 | 415 | |
055b889f | 416 | /* Ports */ |
e245795b | 417 | |
6663a195 | 418 | nr_active_ports = 0; |
e245795b | 419 | QTAILQ_FOREACH(port, &s->ports, next) { |
6663a195 | 420 | nr_active_ports++; |
e245795b | 421 | } |
6663a195 AS |
422 | |
423 | qemu_put_be32s(f, &nr_active_ports); | |
424 | ||
425 | /* | |
426 | * Items in struct VirtIOSerialPort. | |
427 | */ | |
428 | QTAILQ_FOREACH(port, &s->ports, next) { | |
6663a195 AS |
429 | qemu_put_be32s(f, &port->id); |
430 | qemu_put_byte(f, port->guest_connected); | |
31abe21f | 431 | qemu_put_byte(f, port->host_connected); |
6663a195 | 432 | } |
98b19252 AS |
433 | } |
434 | ||
435 | static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id) | |
436 | { | |
437 | VirtIOSerial *s = opaque; | |
6663a195 | 438 | VirtIOSerialPort *port; |
055b889f AS |
439 | size_t ports_map_size; |
440 | uint32_t max_nr_ports, nr_active_ports, *ports_map; | |
6663a195 | 441 | unsigned int i; |
98b19252 AS |
442 | |
443 | if (version_id > 2) { | |
444 | return -EINVAL; | |
445 | } | |
6663a195 | 446 | |
98b19252 AS |
447 | /* The virtio device */ |
448 | virtio_load(&s->vdev, f); | |
449 | ||
450 | if (version_id < 2) { | |
451 | return 0; | |
452 | } | |
453 | ||
454 | /* The config space */ | |
455 | qemu_get_be16s(f, &s->config.cols); | |
456 | qemu_get_be16s(f, &s->config.rows); | |
295587f7 | 457 | |
055b889f AS |
458 | qemu_get_be32s(f, &max_nr_ports); |
459 | if (max_nr_ports > s->config.max_nr_ports) { | |
460 | /* Source could have had more ports than us. Fail migration. */ | |
295587f7 AS |
461 | return -EINVAL; |
462 | } | |
98b19252 | 463 | |
055b889f AS |
464 | ports_map_size = sizeof(uint32_t) * (max_nr_ports + 31) / 32; |
465 | ports_map = qemu_malloc(ports_map_size); | |
6663a195 | 466 | |
055b889f AS |
467 | for (i = 0; i < (max_nr_ports + 31) / 32; i++) { |
468 | qemu_get_be32s(f, &ports_map[i]); | |
469 | ||
470 | if (ports_map[i] != s->ports_map[i]) { | |
471 | /* | |
472 | * Ports active on source and destination don't | |
473 | * match. Fail migration. | |
474 | */ | |
475 | qemu_free(ports_map); | |
476 | return -EINVAL; | |
477 | } | |
e245795b | 478 | } |
055b889f | 479 | qemu_free(ports_map); |
e245795b | 480 | |
6663a195 AS |
481 | qemu_get_be32s(f, &nr_active_ports); |
482 | ||
483 | /* Items in struct VirtIOSerialPort */ | |
484 | for (i = 0; i < nr_active_ports; i++) { | |
485 | uint32_t id; | |
31abe21f | 486 | bool host_connected; |
6663a195 AS |
487 | |
488 | id = qemu_get_be32(f); | |
489 | port = find_port_by_id(s, id); | |
490 | ||
491 | port->guest_connected = qemu_get_byte(f); | |
31abe21f AS |
492 | host_connected = qemu_get_byte(f); |
493 | if (host_connected != port->host_connected) { | |
494 | /* | |
495 | * We have to let the guest know of the host connection | |
496 | * status change | |
497 | */ | |
498 | send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, | |
499 | port->host_connected); | |
500 | } | |
6663a195 | 501 | } |
98b19252 AS |
502 | return 0; |
503 | } | |
504 | ||
505 | static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent); | |
506 | ||
507 | static struct BusInfo virtser_bus_info = { | |
508 | .name = "virtio-serial-bus", | |
509 | .size = sizeof(VirtIOSerialBus), | |
510 | .print_dev = virtser_bus_dev_print, | |
511 | }; | |
512 | ||
513 | static VirtIOSerialBus *virtser_bus_new(DeviceState *dev) | |
514 | { | |
515 | VirtIOSerialBus *bus; | |
516 | ||
9ae84f0a | 517 | bus = FROM_QBUS(VirtIOSerialBus, qbus_create(&virtser_bus_info, dev, NULL)); |
98b19252 AS |
518 | bus->qbus.allow_hotplug = 1; |
519 | ||
520 | return bus; | |
521 | } | |
522 | ||
523 | static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) | |
524 | { | |
525 | VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev); | |
526 | VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev); | |
527 | ||
528 | monitor_printf(mon, "%*s dev-prop-int: id: %u\n", | |
529 | indent, "", port->id); | |
6663a195 AS |
530 | monitor_printf(mon, "%*s dev-prop-int: guest_connected: %d\n", |
531 | indent, "", port->guest_connected); | |
532 | monitor_printf(mon, "%*s dev-prop-int: host_connected: %d\n", | |
533 | indent, "", port->host_connected); | |
98b19252 AS |
534 | } |
535 | ||
055b889f AS |
536 | /* This function is only used if a port id is not provided by the user */ |
537 | static uint32_t find_free_port_id(VirtIOSerial *vser) | |
538 | { | |
539 | unsigned int i; | |
540 | ||
541 | for (i = 0; i < (vser->config.max_nr_ports + 31) / 32; i++) { | |
542 | uint32_t map, bit; | |
543 | ||
544 | map = vser->ports_map[i]; | |
545 | bit = ffs(~map); | |
546 | if (bit) { | |
547 | return (bit - 1) + i * 32; | |
548 | } | |
549 | } | |
550 | return VIRTIO_CONSOLE_BAD_ID; | |
551 | } | |
552 | ||
553 | static void mark_port_added(VirtIOSerial *vser, uint32_t port_id) | |
554 | { | |
555 | unsigned int i; | |
556 | ||
557 | i = port_id / 32; | |
558 | vser->ports_map[i] |= 1U << (port_id % 32); | |
559 | } | |
560 | ||
561 | static void add_port(VirtIOSerial *vser, uint32_t port_id) | |
562 | { | |
563 | mark_port_added(vser, port_id); | |
564 | ||
565 | send_control_event(find_port_by_id(vser, port_id), | |
566 | VIRTIO_CONSOLE_PORT_ADD, 1); | |
567 | } | |
568 | ||
569 | static void remove_port(VirtIOSerial *vser, uint32_t port_id) | |
570 | { | |
571 | unsigned int i; | |
572 | ||
573 | i = port_id / 32; | |
574 | vser->ports_map[i] &= ~(1U << (port_id % 32)); | |
575 | ||
576 | send_control_event(find_port_by_id(vser, port_id), | |
577 | VIRTIO_CONSOLE_PORT_REMOVE, 1); | |
578 | } | |
579 | ||
98b19252 AS |
580 | static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base) |
581 | { | |
582 | VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev); | |
583 | VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev, base); | |
584 | VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev); | |
585 | VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus); | |
586 | int ret; | |
587 | bool plugging_port0; | |
588 | ||
589 | port->vser = bus->vser; | |
590 | ||
591 | /* | |
592 | * Is the first console port we're seeing? If so, put it up at | |
593 | * location 0. This is done for backward compatibility (old | |
594 | * kernel, new qemu). | |
595 | */ | |
596 | plugging_port0 = port->is_console && !find_port_by_id(port->vser, 0); | |
597 | ||
055b889f AS |
598 | if (find_port_by_id(port->vser, port->id)) { |
599 | error_report("virtio-serial-bus: A port already exists at id %u\n", | |
600 | port->id); | |
98b19252 AS |
601 | return -1; |
602 | } | |
98b19252 | 603 | |
055b889f AS |
604 | if (port->id == VIRTIO_CONSOLE_BAD_ID) { |
605 | if (plugging_port0) { | |
606 | port->id = 0; | |
607 | } else { | |
608 | port->id = find_free_port_id(port->vser); | |
609 | if (port->id == VIRTIO_CONSOLE_BAD_ID) { | |
610 | error_report("virtio-serial-bus: Maximum port limit for this device reached\n"); | |
611 | return -1; | |
612 | } | |
613 | } | |
614 | } | |
615 | ||
616 | if (port->id >= port->vser->config.max_nr_ports) { | |
617 | error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u\n", | |
618 | port->vser->config.max_nr_ports - 1); | |
619 | return -1; | |
620 | } | |
621 | ||
622 | dev->info = info; | |
98b19252 AS |
623 | ret = info->init(dev); |
624 | if (ret) { | |
625 | return ret; | |
626 | } | |
627 | ||
6663a195 AS |
628 | if (!use_multiport(port->vser)) { |
629 | /* | |
630 | * Allow writes to guest in this case; we have no way of | |
631 | * knowing if a guest port is connected. | |
632 | */ | |
633 | port->guest_connected = true; | |
634 | } | |
635 | ||
98b19252 AS |
636 | QTAILQ_INSERT_TAIL(&port->vser->ports, port, next); |
637 | port->ivq = port->vser->ivqs[port->id]; | |
638 | port->ovq = port->vser->ovqs[port->id]; | |
639 | ||
055b889f AS |
640 | add_port(port->vser, port->id); |
641 | ||
98b19252 AS |
642 | /* Send an update to the guest about this new port added */ |
643 | virtio_notify_config(&port->vser->vdev); | |
644 | ||
645 | return ret; | |
646 | } | |
647 | ||
648 | static int virtser_port_qdev_exit(DeviceState *qdev) | |
649 | { | |
650 | VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev); | |
651 | VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev); | |
652 | VirtIOSerial *vser = port->vser; | |
653 | ||
055b889f | 654 | remove_port(port->vser, port->id); |
f146ec9a | 655 | |
98b19252 AS |
656 | QTAILQ_REMOVE(&vser->ports, port, next); |
657 | ||
658 | if (port->info->exit) | |
659 | port->info->exit(dev); | |
660 | ||
661 | return 0; | |
662 | } | |
663 | ||
664 | void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info) | |
665 | { | |
666 | info->qdev.init = virtser_port_qdev_init; | |
667 | info->qdev.bus_info = &virtser_bus_info; | |
668 | info->qdev.exit = virtser_port_qdev_exit; | |
669 | info->qdev.unplug = qdev_simple_unplug_cb; | |
670 | qdev_register(&info->qdev); | |
671 | } | |
672 | ||
673 | VirtIODevice *virtio_serial_init(DeviceState *dev, uint32_t max_nr_ports) | |
674 | { | |
675 | VirtIOSerial *vser; | |
676 | VirtIODevice *vdev; | |
677 | uint32_t i; | |
678 | ||
679 | if (!max_nr_ports) | |
680 | return NULL; | |
681 | ||
682 | vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE, | |
683 | sizeof(struct virtio_console_config), | |
684 | sizeof(VirtIOSerial)); | |
685 | ||
686 | vser = DO_UPCAST(VirtIOSerial, vdev, vdev); | |
687 | ||
688 | /* Spawn a new virtio-serial bus on which the ports will ride as devices */ | |
689 | vser->bus = virtser_bus_new(dev); | |
690 | vser->bus->vser = vser; | |
691 | QTAILQ_INIT(&vser->ports); | |
692 | ||
693 | vser->bus->max_nr_ports = max_nr_ports; | |
694 | vser->ivqs = qemu_malloc(max_nr_ports * sizeof(VirtQueue *)); | |
695 | vser->ovqs = qemu_malloc(max_nr_ports * sizeof(VirtQueue *)); | |
696 | ||
697 | /* Add a queue for host to guest transfers for port 0 (backward compat) */ | |
698 | vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input); | |
699 | /* Add a queue for guest to host transfers for port 0 (backward compat) */ | |
700 | vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output); | |
701 | ||
702 | /* control queue: host to guest */ | |
703 | vser->c_ivq = virtio_add_queue(vdev, 16, control_in); | |
704 | /* control queue: guest to host */ | |
705 | vser->c_ovq = virtio_add_queue(vdev, 16, control_out); | |
706 | ||
707 | for (i = 1; i < vser->bus->max_nr_ports; i++) { | |
708 | /* Add a per-port queue for host to guest transfers */ | |
709 | vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input); | |
710 | /* Add a per-per queue for guest to host transfers */ | |
711 | vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output); | |
712 | } | |
713 | ||
714 | vser->config.max_nr_ports = max_nr_ports; | |
055b889f | 715 | vser->ports_map = qemu_mallocz((max_nr_ports + 31) / 32); |
98b19252 AS |
716 | /* |
717 | * Reserve location 0 for a console port for backward compat | |
718 | * (old kernel, new qemu) | |
719 | */ | |
055b889f | 720 | mark_port_added(vser, 0); |
98b19252 AS |
721 | |
722 | vser->vdev.get_features = get_features; | |
723 | vser->vdev.get_config = get_config; | |
724 | vser->vdev.set_config = set_config; | |
725 | ||
726 | /* | |
727 | * Register for the savevm section with the virtio-console name | |
728 | * to preserve backward compat | |
729 | */ | |
730 | register_savevm("virtio-console", -1, 2, virtio_serial_save, | |
731 | virtio_serial_load, vser); | |
732 | ||
733 | return vdev; | |
734 | } |