virtio-serial-bus.c 25 KB

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