virtio-serial.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Virtio Serial / Console Support
  3. *
  4. * Copyright IBM, Corp. 2008
  5. * Copyright Red Hat, Inc. 2009, 2010
  6. *
  7. * Authors:
  8. * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
  9. * Amit Shah <amit.shah@redhat.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2. See
  12. * the COPYING file in the top-level directory.
  13. *
  14. */
  15. #ifndef _QEMU_VIRTIO_SERIAL_H
  16. #define _QEMU_VIRTIO_SERIAL_H
  17. #include "qdev.h"
  18. #include "virtio.h"
  19. /* == Interface shared between the guest kernel and qemu == */
  20. /* The Virtio ID for virtio console / serial ports */
  21. #define VIRTIO_ID_CONSOLE 3
  22. /* Features supported */
  23. #define VIRTIO_CONSOLE_F_MULTIPORT 1
  24. #define VIRTIO_CONSOLE_BAD_ID (~(uint32_t)0)
  25. struct virtio_console_config {
  26. /*
  27. * These two fields are used by VIRTIO_CONSOLE_F_SIZE which
  28. * isn't implemented here yet
  29. */
  30. uint16_t cols;
  31. uint16_t rows;
  32. uint32_t max_nr_ports;
  33. } QEMU_PACKED;
  34. struct virtio_console_control {
  35. uint32_t id; /* Port number */
  36. uint16_t event; /* The kind of control event (see below) */
  37. uint16_t value; /* Extra information for the key */
  38. };
  39. struct virtio_serial_conf {
  40. /* Max. number of ports we can have for a virtio-serial device */
  41. uint32_t max_virtserial_ports;
  42. };
  43. /* Some events for the internal messages (control packets) */
  44. #define VIRTIO_CONSOLE_DEVICE_READY 0
  45. #define VIRTIO_CONSOLE_PORT_ADD 1
  46. #define VIRTIO_CONSOLE_PORT_REMOVE 2
  47. #define VIRTIO_CONSOLE_PORT_READY 3
  48. #define VIRTIO_CONSOLE_CONSOLE_PORT 4
  49. #define VIRTIO_CONSOLE_RESIZE 5
  50. #define VIRTIO_CONSOLE_PORT_OPEN 6
  51. #define VIRTIO_CONSOLE_PORT_NAME 7
  52. /* == In-qemu interface == */
  53. typedef struct VirtIOSerial VirtIOSerial;
  54. typedef struct VirtIOSerialBus VirtIOSerialBus;
  55. typedef struct VirtIOSerialPort VirtIOSerialPort;
  56. typedef struct VirtIOSerialPortInfo VirtIOSerialPortInfo;
  57. /*
  58. * This is the state that's shared between all the ports. Some of the
  59. * state is configurable via command-line options. Some of it can be
  60. * set by individual devices in their initfn routines. Some of the
  61. * state is set by the generic qdev device init routine.
  62. */
  63. struct VirtIOSerialPort {
  64. DeviceState dev;
  65. QTAILQ_ENTRY(VirtIOSerialPort) next;
  66. /*
  67. * This field gives us the virtio device as well as the qdev bus
  68. * that we are associated with
  69. */
  70. VirtIOSerial *vser;
  71. VirtQueue *ivq, *ovq;
  72. /*
  73. * This name is sent to the guest and exported via sysfs.
  74. * The guest could create symlinks based on this information.
  75. * The name is in the reverse fqdn format, like org.qemu.console.0
  76. */
  77. char *name;
  78. /*
  79. * This id helps identify ports between the guest and the host.
  80. * The guest sends a "header" with this id with each data packet
  81. * that it sends and the host can then find out which associated
  82. * device to send out this data to
  83. */
  84. uint32_t id;
  85. /*
  86. * This is the elem that we pop from the virtqueue. A slow
  87. * backend that consumes guest data (e.g. the file backend for
  88. * qemu chardevs) can cause the guest to block till all the output
  89. * is flushed. This isn't desired, so we keep a note of the last
  90. * element popped and continue consuming it once the backend
  91. * becomes writable again.
  92. */
  93. VirtQueueElement elem;
  94. /*
  95. * The index and the offset into the iov buffer that was popped in
  96. * elem above.
  97. */
  98. uint32_t iov_idx;
  99. uint64_t iov_offset;
  100. /*
  101. * When unthrottling we use a bottom-half to call flush_queued_data.
  102. */
  103. QEMUBH *bh;
  104. /* Is the corresponding guest device open? */
  105. bool guest_connected;
  106. /* Is this device open for IO on the host? */
  107. bool host_connected;
  108. /* Do apps not want to receive data? */
  109. bool throttled;
  110. };
  111. struct VirtIOSerialPortInfo {
  112. DeviceInfo qdev;
  113. /* Is this a device that binds with hvc in the guest? */
  114. bool is_console;
  115. /*
  116. * The per-port (or per-app) init function that's called when a
  117. * new device is found on the bus.
  118. */
  119. int (*init)(VirtIOSerialPort *port);
  120. /*
  121. * Per-port exit function that's called when a port gets
  122. * hot-unplugged or removed.
  123. */
  124. int (*exit)(VirtIOSerialPort *port);
  125. /* Callbacks for guest events */
  126. /* Guest opened device. */
  127. void (*guest_open)(VirtIOSerialPort *port);
  128. /* Guest closed device. */
  129. void (*guest_close)(VirtIOSerialPort *port);
  130. /* Guest is now ready to accept data (virtqueues set up). */
  131. void (*guest_ready)(VirtIOSerialPort *port);
  132. /*
  133. * Guest wrote some data to the port. This data is handed over to
  134. * the app via this callback. The app can return a size less than
  135. * 'len'. In this case, throttling will be enabled for this port.
  136. */
  137. ssize_t (*have_data)(VirtIOSerialPort *port, const uint8_t *buf,
  138. size_t len);
  139. };
  140. /* Interface to the virtio-serial bus */
  141. /*
  142. * Individual ports/apps should call this function to register the port
  143. * with the virtio-serial bus
  144. */
  145. void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info);
  146. /*
  147. * Open a connection to the port
  148. * Returns 0 on success (always).
  149. */
  150. int virtio_serial_open(VirtIOSerialPort *port);
  151. /*
  152. * Close the connection to the port
  153. * Returns 0 on success (always).
  154. */
  155. int virtio_serial_close(VirtIOSerialPort *port);
  156. /*
  157. * Send data to Guest
  158. */
  159. ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
  160. size_t size);
  161. /*
  162. * Query whether a guest is ready to receive data.
  163. */
  164. size_t virtio_serial_guest_ready(VirtIOSerialPort *port);
  165. /*
  166. * Flow control: Ports can signal to the virtio-serial core to stop
  167. * sending data or re-start sending data, depending on the 'throttle'
  168. * value here.
  169. */
  170. void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle);
  171. #endif