2 * Copyright (c) 2014, Bryan Venteicher <bryanv@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /* Driver for VirtIO console devices. */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/ctype.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/sglist.h>
42 #include <sys/sysctl.h>
43 #include <sys/taskqueue.h>
44 #include <sys/queue.h>
50 #include <machine/bus.h>
51 #include <machine/resource.h>
54 #include <dev/virtio/virtio.h>
55 #include <dev/virtio/virtqueue.h>
56 #include <dev/virtio/console/virtio_console.h>
58 #include "virtio_if.h"
60 #define VTCON_MAX_PORTS 32
61 #define VTCON_TTY_PREFIX "V"
62 #define VTCON_TTY_ALIAS_PREFIX "vtcon"
63 #define VTCON_BULK_BUFSZ 128
64 #define VTCON_CTRL_BUFSZ 128
67 * The buffers cannot cross more than one page boundary due to the
68 * size of the sglist segment array used.
70 CTASSERT(VTCON_BULK_BUFSZ <= PAGE_SIZE);
71 CTASSERT(VTCON_CTRL_BUFSZ <= PAGE_SIZE);
73 CTASSERT(sizeof(struct virtio_console_config) <= VTCON_CTRL_BUFSZ);
76 struct vtcon_softc_port;
79 struct mtx vtcport_mtx;
80 struct vtcon_softc *vtcport_sc;
81 struct vtcon_softc_port *vtcport_scport;
82 struct tty *vtcport_tty;
83 struct virtqueue *vtcport_invq;
84 struct virtqueue *vtcport_outvq;
87 #define VTCON_PORT_FLAG_GONE 0x01
88 #define VTCON_PORT_FLAG_CONSOLE 0x02
89 #define VTCON_PORT_FLAG_ALIAS 0x04
92 int vtcport_alt_break_state;
96 #define VTCON_PORT_LOCK(_port) mtx_lock(&(_port)->vtcport_mtx)
97 #define VTCON_PORT_UNLOCK(_port) mtx_unlock(&(_port)->vtcport_mtx)
99 struct vtcon_softc_port {
100 struct vtcon_softc *vcsp_sc;
101 struct vtcon_port *vcsp_port;
102 struct virtqueue *vcsp_invq;
103 struct virtqueue *vcsp_outvq;
108 struct mtx vtcon_mtx;
109 uint64_t vtcon_features;
110 uint32_t vtcon_max_ports;
111 uint32_t vtcon_flags;
112 #define VTCON_FLAG_DETACHED 0x01
113 #define VTCON_FLAG_SIZE 0x02
114 #define VTCON_FLAG_MULTIPORT 0x04
117 * Ports can be added and removed during runtime, but we have
118 * to allocate all the virtqueues during attach. This array is
119 * indexed by the port ID.
121 struct vtcon_softc_port *vtcon_ports;
123 struct task vtcon_ctrl_task;
124 struct virtqueue *vtcon_ctrl_rxvq;
125 struct virtqueue *vtcon_ctrl_txvq;
126 struct mtx vtcon_ctrl_tx_mtx;
129 #define VTCON_LOCK(_sc) mtx_lock(&(_sc)->vtcon_mtx)
130 #define VTCON_UNLOCK(_sc) mtx_unlock(&(_sc)->vtcon_mtx)
131 #define VTCON_LOCK_ASSERT(_sc) \
132 mtx_assert(&(_sc)->vtcon_mtx, MA_OWNED)
133 #define VTCON_LOCK_ASSERT_NOTOWNED(_sc) \
134 mtx_assert(&(_sc)->vtcon_mtx, MA_NOTOWNED)
136 #define VTCON_CTRL_TX_LOCK(_sc) mtx_lock(&(_sc)->vtcon_ctrl_tx_mtx)
137 #define VTCON_CTRL_TX_UNLOCK(_sc) mtx_unlock(&(_sc)->vtcon_ctrl_tx_mtx)
139 #define VTCON_ASSERT_VALID_PORTID(_sc, _id) \
140 KASSERT((_id) >= 0 && (_id) < (_sc)->vtcon_max_ports, \
141 ("%s: port ID %d out of range", __func__, _id))
143 #define VTCON_FEATURES VIRTIO_CONSOLE_F_MULTIPORT
145 static struct virtio_feature_desc vtcon_feature_desc[] = {
146 { VIRTIO_CONSOLE_F_SIZE, "ConsoleSize" },
147 { VIRTIO_CONSOLE_F_MULTIPORT, "MultiplePorts" },
148 { VIRTIO_CONSOLE_F_EMERG_WRITE, "EmergencyWrite" },
153 static int vtcon_modevent(module_t, int, void *);
154 static void vtcon_drain_all(void);
156 static int vtcon_probe(device_t);
157 static int vtcon_attach(device_t);
158 static int vtcon_detach(device_t);
159 static int vtcon_config_change(device_t);
161 static void vtcon_setup_features(struct vtcon_softc *);
162 static void vtcon_negotiate_features(struct vtcon_softc *);
163 static int vtcon_alloc_scports(struct vtcon_softc *);
164 static int vtcon_alloc_virtqueues(struct vtcon_softc *);
165 static void vtcon_read_config(struct vtcon_softc *,
166 struct virtio_console_config *);
168 static void vtcon_determine_max_ports(struct vtcon_softc *,
169 struct virtio_console_config *);
170 static void vtcon_destroy_ports(struct vtcon_softc *);
171 static void vtcon_stop(struct vtcon_softc *);
173 static int vtcon_ctrl_event_enqueue(struct vtcon_softc *,
174 struct virtio_console_control *);
175 static int vtcon_ctrl_event_create(struct vtcon_softc *);
176 static void vtcon_ctrl_event_requeue(struct vtcon_softc *,
177 struct virtio_console_control *);
178 static int vtcon_ctrl_event_populate(struct vtcon_softc *);
179 static void vtcon_ctrl_event_drain(struct vtcon_softc *);
180 static int vtcon_ctrl_init(struct vtcon_softc *);
181 static void vtcon_ctrl_deinit(struct vtcon_softc *);
182 static void vtcon_ctrl_port_add_event(struct vtcon_softc *, int);
183 static void vtcon_ctrl_port_remove_event(struct vtcon_softc *, int);
184 static void vtcon_ctrl_port_console_event(struct vtcon_softc *, int);
185 static void vtcon_ctrl_port_open_event(struct vtcon_softc *, int);
186 static void vtcon_ctrl_port_name_event(struct vtcon_softc *, int,
187 const char *, size_t);
188 static void vtcon_ctrl_process_event(struct vtcon_softc *,
189 struct virtio_console_control *, void *, size_t);
190 static void vtcon_ctrl_task_cb(void *, int);
191 static void vtcon_ctrl_event_intr(void *);
192 static void vtcon_ctrl_poll(struct vtcon_softc *,
193 struct virtio_console_control *control);
194 static void vtcon_ctrl_send_control(struct vtcon_softc *, uint32_t,
197 static int vtcon_port_enqueue_buf(struct vtcon_port *, void *, size_t);
198 static int vtcon_port_create_buf(struct vtcon_port *);
199 static void vtcon_port_requeue_buf(struct vtcon_port *, void *);
200 static int vtcon_port_populate(struct vtcon_port *);
201 static void vtcon_port_destroy(struct vtcon_port *);
202 static int vtcon_port_create(struct vtcon_softc *, int);
203 static void vtcon_port_dev_alias(struct vtcon_port *, const char *,
205 static void vtcon_port_drain_bufs(struct virtqueue *);
206 static void vtcon_port_drain(struct vtcon_port *);
207 static void vtcon_port_teardown(struct vtcon_port *);
208 static void vtcon_port_change_size(struct vtcon_port *, uint16_t,
210 static void vtcon_port_update_console_size(struct vtcon_softc *);
211 static void vtcon_port_enable_intr(struct vtcon_port *);
212 static void vtcon_port_disable_intr(struct vtcon_port *);
213 static void vtcon_port_in(struct vtcon_port *);
214 static void vtcon_port_intr(void *);
215 static void vtcon_port_out(struct vtcon_port *, void *, int);
216 static void vtcon_port_submit_event(struct vtcon_port *, uint16_t,
219 static int vtcon_tty_open(struct tty *);
220 static void vtcon_tty_close(struct tty *);
221 static void vtcon_tty_outwakeup(struct tty *);
222 static void vtcon_tty_free(void *);
224 static void vtcon_get_console_size(struct vtcon_softc *, uint16_t *,
227 static void vtcon_enable_interrupts(struct vtcon_softc *);
228 static void vtcon_disable_interrupts(struct vtcon_softc *);
230 static int vtcon_pending_free;
232 static struct ttydevsw vtcon_tty_class = {
234 .tsw_open = vtcon_tty_open,
235 .tsw_close = vtcon_tty_close,
236 .tsw_outwakeup = vtcon_tty_outwakeup,
237 .tsw_free = vtcon_tty_free,
240 static device_method_t vtcon_methods[] = {
241 /* Device methods. */
242 DEVMETHOD(device_probe, vtcon_probe),
243 DEVMETHOD(device_attach, vtcon_attach),
244 DEVMETHOD(device_detach, vtcon_detach),
246 /* VirtIO methods. */
247 DEVMETHOD(virtio_config_change, vtcon_config_change),
252 static driver_t vtcon_driver = {
255 sizeof(struct vtcon_softc)
257 static devclass_t vtcon_devclass;
259 DRIVER_MODULE(virtio_console, virtio_pci, vtcon_driver, vtcon_devclass,
261 MODULE_VERSION(virtio_console, 1);
262 MODULE_DEPEND(virtio_console, virtio, 1, 1, 1);
265 vtcon_modevent(module_t mod, int type, void *unused)
292 vtcon_drain_all(void)
296 for (first = 1; vtcon_pending_free != 0; first = 0) {
298 printf("virtio_console: Waiting for all detached TTY "
299 "devices to have open fds closed.\n");
301 pause("vtcondra", hz);
306 vtcon_probe(device_t dev)
309 if (virtio_get_device_type(dev) != VIRTIO_ID_CONSOLE)
312 device_set_desc(dev, "VirtIO Console Adapter");
314 return (BUS_PROBE_DEFAULT);
318 vtcon_attach(device_t dev)
320 struct vtcon_softc *sc;
321 struct virtio_console_config concfg;
324 sc = device_get_softc(dev);
327 mtx_init(&sc->vtcon_mtx, "vtconmtx", NULL, MTX_DEF);
328 mtx_init(&sc->vtcon_ctrl_tx_mtx, "vtconctrlmtx", NULL, MTX_DEF);
330 virtio_set_feature_desc(dev, vtcon_feature_desc);
331 vtcon_setup_features(sc);
333 vtcon_read_config(sc, &concfg);
334 vtcon_determine_max_ports(sc, &concfg);
336 error = vtcon_alloc_scports(sc);
338 device_printf(dev, "cannot allocate softc port structures\n");
342 error = vtcon_alloc_virtqueues(sc);
344 device_printf(dev, "cannot allocate virtqueues\n");
348 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
349 TASK_INIT(&sc->vtcon_ctrl_task, 0, vtcon_ctrl_task_cb, sc);
350 error = vtcon_ctrl_init(sc);
354 error = vtcon_port_create(sc, 0);
357 if (sc->vtcon_flags & VTCON_FLAG_SIZE)
358 vtcon_port_update_console_size(sc);
361 error = virtio_setup_intr(dev, INTR_TYPE_TTY);
363 device_printf(dev, "cannot setup virtqueue interrupts\n");
367 vtcon_enable_interrupts(sc);
369 vtcon_ctrl_send_control(sc, VIRTIO_CONSOLE_BAD_ID,
370 VIRTIO_CONSOLE_DEVICE_READY, 1);
380 vtcon_detach(device_t dev)
382 struct vtcon_softc *sc;
384 sc = device_get_softc(dev);
387 sc->vtcon_flags |= VTCON_FLAG_DETACHED;
388 if (device_is_attached(dev))
392 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
393 taskqueue_drain(taskqueue_thread, &sc->vtcon_ctrl_task);
394 vtcon_ctrl_deinit(sc);
397 vtcon_destroy_ports(sc);
398 mtx_destroy(&sc->vtcon_mtx);
399 mtx_destroy(&sc->vtcon_ctrl_tx_mtx);
405 vtcon_config_change(device_t dev)
407 struct vtcon_softc *sc;
409 sc = device_get_softc(dev);
412 * When the multiport feature is negotiated, all configuration
413 * changes are done through control virtqueue events.
415 if ((sc->vtcon_flags & VTCON_FLAG_MULTIPORT) == 0) {
416 if (sc->vtcon_flags & VTCON_FLAG_SIZE)
417 vtcon_port_update_console_size(sc);
424 vtcon_negotiate_features(struct vtcon_softc *sc)
430 features = VTCON_FEATURES;
432 sc->vtcon_features = virtio_negotiate_features(dev, features);
436 vtcon_setup_features(struct vtcon_softc *sc)
442 vtcon_negotiate_features(sc);
444 if (virtio_with_feature(dev, VIRTIO_CONSOLE_F_SIZE))
445 sc->vtcon_flags |= VTCON_FLAG_SIZE;
446 if (virtio_with_feature(dev, VIRTIO_CONSOLE_F_MULTIPORT))
447 sc->vtcon_flags |= VTCON_FLAG_MULTIPORT;
450 #define VTCON_GET_CONFIG(_dev, _feature, _field, _cfg) \
451 if (virtio_with_feature(_dev, _feature)) { \
452 virtio_read_device_config(_dev, \
453 offsetof(struct virtio_console_config, _field), \
454 &(_cfg)->_field, sizeof((_cfg)->_field)); \
458 vtcon_read_config(struct vtcon_softc *sc, struct virtio_console_config *concfg)
464 bzero(concfg, sizeof(struct virtio_console_config));
466 VTCON_GET_CONFIG(dev, VIRTIO_CONSOLE_F_SIZE, cols, concfg);
467 VTCON_GET_CONFIG(dev, VIRTIO_CONSOLE_F_SIZE, rows, concfg);
468 VTCON_GET_CONFIG(dev, VIRTIO_CONSOLE_F_MULTIPORT, max_nr_ports, concfg);
471 #undef VTCON_GET_CONFIG
474 vtcon_alloc_scports(struct vtcon_softc *sc)
476 struct vtcon_softc_port *scport;
479 max = sc->vtcon_max_ports;
481 sc->vtcon_ports = mallocarray(max, sizeof(struct vtcon_softc_port),
482 M_DEVBUF, M_NOWAIT | M_ZERO);
483 if (sc->vtcon_ports == NULL)
486 for (i = 0; i < max; i++) {
487 scport = &sc->vtcon_ports[i];
488 scport->vcsp_sc = sc;
495 vtcon_alloc_virtqueues(struct vtcon_softc *sc)
498 struct vq_alloc_info *info;
499 struct vtcon_softc_port *scport;
500 u_int i, idx, portidx, nvqs;
505 nvqs = sc->vtcon_max_ports * 2;
506 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
509 info = mallocarray(nvqs, sizeof(struct vq_alloc_info), M_TEMP,
514 for (i = 0, idx = 0, portidx = 0; i < nvqs / 2; i++, idx += 2) {
517 /* The control virtqueues are after the first port. */
518 VQ_ALLOC_INFO_INIT(&info[idx], 0,
519 vtcon_ctrl_event_intr, sc, &sc->vtcon_ctrl_rxvq,
520 "%s-control rx", device_get_nameunit(dev));
521 VQ_ALLOC_INFO_INIT(&info[idx+1], 0,
522 NULL, sc, &sc->vtcon_ctrl_txvq,
523 "%s-control tx", device_get_nameunit(dev));
527 scport = &sc->vtcon_ports[portidx];
529 VQ_ALLOC_INFO_INIT(&info[idx], 0, vtcon_port_intr,
530 scport, &scport->vcsp_invq, "%s-port%d in",
531 device_get_nameunit(dev), i);
532 VQ_ALLOC_INFO_INIT(&info[idx+1], 0, NULL,
533 NULL, &scport->vcsp_outvq, "%s-port%d out",
534 device_get_nameunit(dev), i);
539 error = virtio_alloc_virtqueues(dev, 0, nvqs, info);
546 vtcon_determine_max_ports(struct vtcon_softc *sc,
547 struct virtio_console_config *concfg)
550 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
551 sc->vtcon_max_ports =
552 min(concfg->max_nr_ports, VTCON_MAX_PORTS);
553 if (sc->vtcon_max_ports == 0)
554 sc->vtcon_max_ports = 1;
556 sc->vtcon_max_ports = 1;
560 vtcon_destroy_ports(struct vtcon_softc *sc)
562 struct vtcon_softc_port *scport;
563 struct vtcon_port *port;
564 struct virtqueue *vq;
567 if (sc->vtcon_ports == NULL)
571 for (i = 0; i < sc->vtcon_max_ports; i++) {
572 scport = &sc->vtcon_ports[i];
574 port = scport->vcsp_port;
576 scport->vcsp_port = NULL;
577 VTCON_PORT_LOCK(port);
579 vtcon_port_teardown(port);
583 vq = scport->vcsp_invq;
585 vtcon_port_drain_bufs(vq);
589 free(sc->vtcon_ports, M_DEVBUF);
590 sc->vtcon_ports = NULL;
594 vtcon_stop(struct vtcon_softc *sc)
597 vtcon_disable_interrupts(sc);
598 virtio_stop(sc->vtcon_dev);
602 vtcon_ctrl_event_enqueue(struct vtcon_softc *sc,
603 struct virtio_console_control *control)
605 struct sglist_seg segs[2];
607 struct virtqueue *vq;
610 vq = sc->vtcon_ctrl_rxvq;
612 sglist_init(&sg, 2, segs);
613 error = sglist_append(&sg, control, VTCON_CTRL_BUFSZ);
614 KASSERT(error == 0, ("%s: error %d adding control to sglist",
617 return (virtqueue_enqueue(vq, control, &sg, 0, sg.sg_nseg));
621 vtcon_ctrl_event_create(struct vtcon_softc *sc)
623 struct virtio_console_control *control;
626 control = malloc(VTCON_CTRL_BUFSZ, M_DEVBUF, M_ZERO | M_NOWAIT);
630 error = vtcon_ctrl_event_enqueue(sc, control);
632 free(control, M_DEVBUF);
638 vtcon_ctrl_event_requeue(struct vtcon_softc *sc,
639 struct virtio_console_control *control)
643 bzero(control, VTCON_CTRL_BUFSZ);
645 error = vtcon_ctrl_event_enqueue(sc, control);
647 ("%s: cannot requeue control buffer %d", __func__, error));
651 vtcon_ctrl_event_populate(struct vtcon_softc *sc)
653 struct virtqueue *vq;
656 vq = sc->vtcon_ctrl_rxvq;
659 for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
660 error = vtcon_ctrl_event_create(sc);
666 virtqueue_notify(vq);
674 vtcon_ctrl_event_drain(struct vtcon_softc *sc)
676 struct virtio_console_control *control;
677 struct virtqueue *vq;
680 vq = sc->vtcon_ctrl_rxvq;
687 while ((control = virtqueue_drain(vq, &last)) != NULL)
688 free(control, M_DEVBUF);
693 vtcon_ctrl_init(struct vtcon_softc *sc)
697 error = vtcon_ctrl_event_populate(sc);
703 vtcon_ctrl_deinit(struct vtcon_softc *sc)
706 vtcon_ctrl_event_drain(sc);
710 vtcon_ctrl_port_add_event(struct vtcon_softc *sc, int id)
717 /* This single thread only way for ports to be created. */
718 if (sc->vtcon_ports[id].vcsp_port != NULL) {
719 device_printf(dev, "%s: adding port %d, but already exists\n",
724 error = vtcon_port_create(sc, id);
726 device_printf(dev, "%s: cannot create port %d: %d\n",
727 __func__, id, error);
728 vtcon_ctrl_send_control(sc, id, VIRTIO_CONSOLE_PORT_READY, 0);
734 vtcon_ctrl_port_remove_event(struct vtcon_softc *sc, int id)
737 struct vtcon_softc_port *scport;
738 struct vtcon_port *port;
741 scport = &sc->vtcon_ports[id];
744 port = scport->vcsp_port;
747 device_printf(dev, "%s: remove port %d, but does not exist\n",
752 scport->vcsp_port = NULL;
753 VTCON_PORT_LOCK(port);
755 vtcon_port_teardown(port);
759 vtcon_ctrl_port_console_event(struct vtcon_softc *sc, int id)
762 struct vtcon_softc_port *scport;
763 struct vtcon_port *port;
766 scport = &sc->vtcon_ports[id];
769 port = scport->vcsp_port;
772 device_printf(dev, "%s: console port %d, but does not exist\n",
777 VTCON_PORT_LOCK(port);
779 port->vtcport_flags |= VTCON_PORT_FLAG_CONSOLE;
780 vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
781 VTCON_PORT_UNLOCK(port);
785 vtcon_ctrl_port_open_event(struct vtcon_softc *sc, int id)
788 struct vtcon_softc_port *scport;
789 struct vtcon_port *port;
792 scport = &sc->vtcon_ports[id];
795 port = scport->vcsp_port;
798 device_printf(dev, "%s: open port %d, but does not exist\n",
803 VTCON_PORT_LOCK(port);
805 vtcon_port_enable_intr(port);
806 VTCON_PORT_UNLOCK(port);
810 vtcon_ctrl_port_name_event(struct vtcon_softc *sc, int id, const char *name,
814 struct vtcon_softc_port *scport;
815 struct vtcon_port *port;
818 scport = &sc->vtcon_ports[id];
821 * The VirtIO specification says the NUL terminator is not included in
822 * the length, but QEMU includes it. Adjust the length if needed.
824 if (name == NULL || len == 0)
826 if (name[len - 1] == '\0') {
833 port = scport->vcsp_port;
836 device_printf(dev, "%s: name port %d, but does not exist\n",
841 VTCON_PORT_LOCK(port);
843 vtcon_port_dev_alias(port, name, len);
844 VTCON_PORT_UNLOCK(port);
848 vtcon_ctrl_process_event(struct vtcon_softc *sc,
849 struct virtio_console_control *control, void *data, size_t data_len)
857 if (id < 0 || id >= sc->vtcon_max_ports) {
858 device_printf(dev, "%s: invalid port ID %d\n", __func__, id);
862 switch (control->event) {
863 case VIRTIO_CONSOLE_PORT_ADD:
864 vtcon_ctrl_port_add_event(sc, id);
867 case VIRTIO_CONSOLE_PORT_REMOVE:
868 vtcon_ctrl_port_remove_event(sc, id);
871 case VIRTIO_CONSOLE_CONSOLE_PORT:
872 vtcon_ctrl_port_console_event(sc, id);
875 case VIRTIO_CONSOLE_RESIZE:
878 case VIRTIO_CONSOLE_PORT_OPEN:
879 vtcon_ctrl_port_open_event(sc, id);
882 case VIRTIO_CONSOLE_PORT_NAME:
883 vtcon_ctrl_port_name_event(sc, id, (const char *)data, data_len);
889 vtcon_ctrl_task_cb(void *xsc, int pending)
891 struct vtcon_softc *sc;
892 struct virtqueue *vq;
893 struct virtio_console_control *control;
900 vq = sc->vtcon_ctrl_rxvq;
904 while ((detached = (sc->vtcon_flags & VTCON_FLAG_DETACHED)) == 0) {
905 control = virtqueue_dequeue(vq, &len);
909 if (len > sizeof(struct virtio_console_control)) {
910 data = (void *) &control[1];
911 data_len = len - sizeof(struct virtio_console_control);
918 vtcon_ctrl_process_event(sc, control, data, data_len);
920 vtcon_ctrl_event_requeue(sc, control);
924 virtqueue_notify(vq);
925 if (virtqueue_enable_intr(vq) != 0)
926 taskqueue_enqueue(taskqueue_thread,
927 &sc->vtcon_ctrl_task);
934 vtcon_ctrl_event_intr(void *xsc)
936 struct vtcon_softc *sc;
941 * Only some events require us to potentially block, but it
942 * easier to just defer all event handling to the taskqueue.
944 taskqueue_enqueue(taskqueue_thread, &sc->vtcon_ctrl_task);
948 vtcon_ctrl_poll(struct vtcon_softc *sc,
949 struct virtio_console_control *control)
951 struct sglist_seg segs[2];
953 struct virtqueue *vq;
956 vq = sc->vtcon_ctrl_txvq;
958 sglist_init(&sg, 2, segs);
959 error = sglist_append(&sg, control,
960 sizeof(struct virtio_console_control));
961 KASSERT(error == 0, ("%s: error %d adding control to sglist",
965 * We cannot use the softc lock to serialize access to this
966 * virtqueue since this is called from the tty layer with the
967 * port lock held. Acquiring the softc would violate our lock
970 VTCON_CTRL_TX_LOCK(sc);
971 KASSERT(virtqueue_empty(vq),
972 ("%s: virtqueue is not emtpy", __func__));
973 error = virtqueue_enqueue(vq, control, &sg, sg.sg_nseg, 0);
975 virtqueue_notify(vq);
976 virtqueue_poll(vq, NULL);
978 VTCON_CTRL_TX_UNLOCK(sc);
982 vtcon_ctrl_send_control(struct vtcon_softc *sc, uint32_t portid,
983 uint16_t event, uint16_t value)
985 struct virtio_console_control control;
987 if ((sc->vtcon_flags & VTCON_FLAG_MULTIPORT) == 0)
991 control.event = event;
992 control.value = value;
994 vtcon_ctrl_poll(sc, &control);
998 vtcon_port_enqueue_buf(struct vtcon_port *port, void *buf, size_t len)
1000 struct sglist_seg segs[2];
1002 struct virtqueue *vq;
1005 vq = port->vtcport_invq;
1007 sglist_init(&sg, 2, segs);
1008 error = sglist_append(&sg, buf, len);
1010 ("%s: error %d adding buffer to sglist", __func__, error));
1012 error = virtqueue_enqueue(vq, buf, &sg, 0, sg.sg_nseg);
1018 vtcon_port_create_buf(struct vtcon_port *port)
1023 buf = malloc(VTCON_BULK_BUFSZ, M_DEVBUF, M_ZERO | M_NOWAIT);
1027 error = vtcon_port_enqueue_buf(port, buf, VTCON_BULK_BUFSZ);
1029 free(buf, M_DEVBUF);
1035 vtcon_port_requeue_buf(struct vtcon_port *port, void *buf)
1039 error = vtcon_port_enqueue_buf(port, buf, VTCON_BULK_BUFSZ);
1041 ("%s: cannot requeue input buffer %d", __func__, error));
1045 vtcon_port_populate(struct vtcon_port *port)
1047 struct virtqueue *vq;
1050 vq = port->vtcport_invq;
1053 for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
1054 error = vtcon_port_create_buf(port);
1060 virtqueue_notify(vq);
1068 vtcon_port_destroy(struct vtcon_port *port)
1071 port->vtcport_sc = NULL;
1072 port->vtcport_scport = NULL;
1073 port->vtcport_invq = NULL;
1074 port->vtcport_outvq = NULL;
1075 port->vtcport_id = -1;
1076 mtx_destroy(&port->vtcport_mtx);
1077 free(port, M_DEVBUF);
1081 vtcon_port_init_vqs(struct vtcon_port *port)
1083 struct vtcon_softc_port *scport;
1086 scport = port->vtcport_scport;
1088 port->vtcport_invq = scport->vcsp_invq;
1089 port->vtcport_outvq = scport->vcsp_outvq;
1092 * Free any data left over from when this virtqueue was in use by a
1093 * prior port. We have not yet notified the host that the port is
1094 * ready, so assume nothing in the virtqueue can be for us.
1096 vtcon_port_drain(port);
1098 KASSERT(virtqueue_empty(port->vtcport_invq),
1099 ("%s: in virtqueue is not empty", __func__));
1100 KASSERT(virtqueue_empty(port->vtcport_outvq),
1101 ("%s: out virtqueue is not empty", __func__));
1103 error = vtcon_port_populate(port);
1111 vtcon_port_create(struct vtcon_softc *sc, int id)
1114 struct vtcon_softc_port *scport;
1115 struct vtcon_port *port;
1118 dev = sc->vtcon_dev;
1119 scport = &sc->vtcon_ports[id];
1121 VTCON_ASSERT_VALID_PORTID(sc, id);
1122 MPASS(scport->vcsp_port == NULL);
1124 port = malloc(sizeof(struct vtcon_port), M_DEVBUF, M_NOWAIT | M_ZERO);
1128 port->vtcport_sc = sc;
1129 port->vtcport_scport = scport;
1130 port->vtcport_id = id;
1131 mtx_init(&port->vtcport_mtx, "vtcpmtx", NULL, MTX_DEF);
1132 port->vtcport_tty = tty_alloc_mutex(&vtcon_tty_class, port,
1133 &port->vtcport_mtx);
1135 error = vtcon_port_init_vqs(port);
1137 VTCON_PORT_LOCK(port);
1138 vtcon_port_teardown(port);
1143 VTCON_PORT_LOCK(port);
1144 scport->vcsp_port = port;
1145 vtcon_port_enable_intr(port);
1146 vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_READY, 1);
1147 VTCON_PORT_UNLOCK(port);
1150 tty_makedev(port->vtcport_tty, NULL, "%s%r.%r", VTCON_TTY_PREFIX,
1151 device_get_unit(dev), id);
1157 vtcon_port_dev_alias(struct vtcon_port *port, const char *name, size_t len)
1159 struct vtcon_softc *sc;
1164 sc = port->vtcport_sc;
1165 tp = port->vtcport_tty;
1167 if (port->vtcport_flags & VTCON_PORT_FLAG_ALIAS)
1170 /* Port name is UTF-8, but we can only handle ASCII. */
1171 for (i = 0; i < len; i++) {
1172 if (!isascii(name[i]))
1177 * Port name may not conform to the devfs requirements so we cannot use
1178 * tty_makealias() because the MAKEDEV_CHECKNAME flag must be specified.
1180 error = make_dev_alias_p(MAKEDEV_NOWAIT | MAKEDEV_CHECKNAME, &pdev,
1181 tp->t_dev, "%s/%*s", VTCON_TTY_ALIAS_PREFIX, (int)len, name);
1183 device_printf(sc->vtcon_dev,
1184 "%s: cannot make dev alias (%s/%*s) error %d\n", __func__,
1185 VTCON_TTY_ALIAS_PREFIX, (int)len, name, error);
1187 port->vtcport_flags |= VTCON_PORT_FLAG_ALIAS;
1191 vtcon_port_drain_bufs(struct virtqueue *vq)
1198 while ((buf = virtqueue_drain(vq, &last)) != NULL)
1199 free(buf, M_DEVBUF);
1203 vtcon_port_drain(struct vtcon_port *port)
1206 vtcon_port_drain_bufs(port->vtcport_invq);
1210 vtcon_port_teardown(struct vtcon_port *port)
1214 tp = port->vtcport_tty;
1216 port->vtcport_flags |= VTCON_PORT_FLAG_GONE;
1219 atomic_add_int(&vtcon_pending_free, 1);
1222 vtcon_port_destroy(port);
1226 vtcon_port_change_size(struct vtcon_port *port, uint16_t cols, uint16_t rows)
1231 tp = port->vtcport_tty;
1236 bzero(&sz, sizeof(struct winsize));
1240 tty_set_winsize(tp, &sz);
1244 vtcon_port_update_console_size(struct vtcon_softc *sc)
1246 struct vtcon_port *port;
1247 struct vtcon_softc_port *scport;
1248 uint16_t cols, rows;
1250 vtcon_get_console_size(sc, &cols, &rows);
1253 * For now, assume the first (only) port is the console. Note
1254 * QEMU does not implement this feature yet.
1256 scport = &sc->vtcon_ports[0];
1259 port = scport->vcsp_port;
1262 VTCON_PORT_LOCK(port);
1264 vtcon_port_change_size(port, cols, rows);
1265 VTCON_PORT_UNLOCK(port);
1271 vtcon_port_enable_intr(struct vtcon_port *port)
1275 * NOTE: The out virtqueue is always polled, so its interrupt
1278 virtqueue_enable_intr(port->vtcport_invq);
1282 vtcon_port_disable_intr(struct vtcon_port *port)
1285 if (port->vtcport_invq != NULL)
1286 virtqueue_disable_intr(port->vtcport_invq);
1287 if (port->vtcport_outvq != NULL)
1288 virtqueue_disable_intr(port->vtcport_outvq);
1292 vtcon_port_in(struct vtcon_port *port)
1294 struct virtqueue *vq;
1300 tp = port->vtcport_tty;
1301 vq = port->vtcport_invq;
1306 while ((buf = virtqueue_dequeue(vq, &len)) != NULL) {
1307 for (i = 0; i < len; i++) {
1309 if (port->vtcport_flags & VTCON_PORT_FLAG_CONSOLE)
1310 kdb_alt_break(buf[i],
1311 &port->vtcport_alt_break_state);
1313 ttydisc_rint(tp, buf[i], 0);
1315 vtcon_port_requeue_buf(port, buf);
1318 ttydisc_rint_done(tp);
1321 virtqueue_notify(vq);
1323 if (virtqueue_enable_intr(vq) != 0)
1328 vtcon_port_intr(void *scportx)
1330 struct vtcon_softc_port *scport;
1331 struct vtcon_softc *sc;
1332 struct vtcon_port *port;
1335 sc = scport->vcsp_sc;
1338 port = scport->vcsp_port;
1343 VTCON_PORT_LOCK(port);
1345 if ((port->vtcport_flags & VTCON_PORT_FLAG_GONE) == 0)
1346 vtcon_port_in(port);
1347 VTCON_PORT_UNLOCK(port);
1351 vtcon_port_out(struct vtcon_port *port, void *buf, int bufsize)
1353 struct sglist_seg segs[2];
1355 struct virtqueue *vq;
1358 vq = port->vtcport_outvq;
1359 KASSERT(virtqueue_empty(vq),
1360 ("%s: port %p out virtqueue not emtpy", __func__, port));
1362 sglist_init(&sg, 2, segs);
1363 error = sglist_append(&sg, buf, bufsize);
1364 KASSERT(error == 0, ("%s: error %d adding buffer to sglist",
1367 error = virtqueue_enqueue(vq, buf, &sg, sg.sg_nseg, 0);
1369 virtqueue_notify(vq);
1370 virtqueue_poll(vq, NULL);
1375 vtcon_port_submit_event(struct vtcon_port *port, uint16_t event,
1378 struct vtcon_softc *sc;
1380 sc = port->vtcport_sc;
1382 vtcon_ctrl_send_control(sc, port->vtcport_id, event, value);
1386 vtcon_tty_open(struct tty *tp)
1388 struct vtcon_port *port;
1390 port = tty_softc(tp);
1392 if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1395 vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1401 vtcon_tty_close(struct tty *tp)
1403 struct vtcon_port *port;
1405 port = tty_softc(tp);
1407 if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1410 vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1414 vtcon_tty_outwakeup(struct tty *tp)
1416 struct vtcon_port *port;
1417 char buf[VTCON_BULK_BUFSZ];
1420 port = tty_softc(tp);
1422 if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1425 while ((len = ttydisc_getc(tp, buf, sizeof(buf))) != 0)
1426 vtcon_port_out(port, buf, len);
1430 vtcon_tty_free(void *xport)
1432 struct vtcon_port *port;
1436 vtcon_port_destroy(port);
1437 atomic_subtract_int(&vtcon_pending_free, 1);
1441 vtcon_get_console_size(struct vtcon_softc *sc, uint16_t *cols, uint16_t *rows)
1443 struct virtio_console_config concfg;
1445 KASSERT(sc->vtcon_flags & VTCON_FLAG_SIZE,
1446 ("%s: size feature not negotiated", __func__));
1448 vtcon_read_config(sc, &concfg);
1450 *cols = concfg.cols;
1451 *rows = concfg.rows;
1455 vtcon_enable_interrupts(struct vtcon_softc *sc)
1457 struct vtcon_softc_port *scport;
1458 struct vtcon_port *port;
1463 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
1464 virtqueue_enable_intr(sc->vtcon_ctrl_rxvq);
1466 for (i = 0; i < sc->vtcon_max_ports; i++) {
1467 scport = &sc->vtcon_ports[i];
1469 port = scport->vcsp_port;
1473 VTCON_PORT_LOCK(port);
1474 vtcon_port_enable_intr(port);
1475 VTCON_PORT_UNLOCK(port);
1482 vtcon_disable_interrupts(struct vtcon_softc *sc)
1484 struct vtcon_softc_port *scport;
1485 struct vtcon_port *port;
1488 VTCON_LOCK_ASSERT(sc);
1490 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
1491 virtqueue_disable_intr(sc->vtcon_ctrl_rxvq);
1493 for (i = 0; i < sc->vtcon_max_ports; i++) {
1494 scport = &sc->vtcon_ports[i];
1496 port = scport->vcsp_port;
1500 VTCON_PORT_LOCK(port);
1501 vtcon_port_disable_intr(port);
1502 VTCON_PORT_UNLOCK(port);