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 = malloc(sizeof(struct vtcon_softc_port) * max,
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 int i, idx, portidx, nvqs, error;
504 nvqs = sc->vtcon_max_ports * 2;
505 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
508 info = malloc(sizeof(struct vq_alloc_info) * nvqs, M_TEMP, M_NOWAIT);
512 for (i = 0, idx = 0, portidx = 0; i < nvqs / 2; i++, idx += 2) {
515 /* The control virtqueues are after the first port. */
516 VQ_ALLOC_INFO_INIT(&info[idx], 0,
517 vtcon_ctrl_event_intr, sc, &sc->vtcon_ctrl_rxvq,
518 "%s-control rx", device_get_nameunit(dev));
519 VQ_ALLOC_INFO_INIT(&info[idx+1], 0,
520 NULL, sc, &sc->vtcon_ctrl_txvq,
521 "%s-control tx", device_get_nameunit(dev));
525 scport = &sc->vtcon_ports[portidx];
527 VQ_ALLOC_INFO_INIT(&info[idx], 0, vtcon_port_intr,
528 scport, &scport->vcsp_invq, "%s-port%d in",
529 device_get_nameunit(dev), i);
530 VQ_ALLOC_INFO_INIT(&info[idx+1], 0, NULL,
531 NULL, &scport->vcsp_outvq, "%s-port%d out",
532 device_get_nameunit(dev), i);
537 error = virtio_alloc_virtqueues(dev, 0, nvqs, info);
544 vtcon_determine_max_ports(struct vtcon_softc *sc,
545 struct virtio_console_config *concfg)
548 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
549 sc->vtcon_max_ports =
550 min(concfg->max_nr_ports, VTCON_MAX_PORTS);
551 if (sc->vtcon_max_ports == 0)
552 sc->vtcon_max_ports = 1;
554 sc->vtcon_max_ports = 1;
558 vtcon_destroy_ports(struct vtcon_softc *sc)
560 struct vtcon_softc_port *scport;
561 struct vtcon_port *port;
562 struct virtqueue *vq;
565 if (sc->vtcon_ports == NULL)
569 for (i = 0; i < sc->vtcon_max_ports; i++) {
570 scport = &sc->vtcon_ports[i];
572 port = scport->vcsp_port;
574 scport->vcsp_port = NULL;
575 VTCON_PORT_LOCK(port);
577 vtcon_port_teardown(port);
581 vq = scport->vcsp_invq;
583 vtcon_port_drain_bufs(vq);
587 free(sc->vtcon_ports, M_DEVBUF);
588 sc->vtcon_ports = NULL;
592 vtcon_stop(struct vtcon_softc *sc)
595 vtcon_disable_interrupts(sc);
596 virtio_stop(sc->vtcon_dev);
600 vtcon_ctrl_event_enqueue(struct vtcon_softc *sc,
601 struct virtio_console_control *control)
603 struct sglist_seg segs[2];
605 struct virtqueue *vq;
608 vq = sc->vtcon_ctrl_rxvq;
610 sglist_init(&sg, 2, segs);
611 error = sglist_append(&sg, control, VTCON_CTRL_BUFSZ);
612 KASSERT(error == 0, ("%s: error %d adding control to sglist",
615 return (virtqueue_enqueue(vq, control, &sg, 0, sg.sg_nseg));
619 vtcon_ctrl_event_create(struct vtcon_softc *sc)
621 struct virtio_console_control *control;
624 control = malloc(VTCON_CTRL_BUFSZ, M_DEVBUF, M_ZERO | M_NOWAIT);
628 error = vtcon_ctrl_event_enqueue(sc, control);
630 free(control, M_DEVBUF);
636 vtcon_ctrl_event_requeue(struct vtcon_softc *sc,
637 struct virtio_console_control *control)
641 bzero(control, VTCON_CTRL_BUFSZ);
643 error = vtcon_ctrl_event_enqueue(sc, control);
645 ("%s: cannot requeue control buffer %d", __func__, error));
649 vtcon_ctrl_event_populate(struct vtcon_softc *sc)
651 struct virtqueue *vq;
654 vq = sc->vtcon_ctrl_rxvq;
657 for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
658 error = vtcon_ctrl_event_create(sc);
664 virtqueue_notify(vq);
672 vtcon_ctrl_event_drain(struct vtcon_softc *sc)
674 struct virtio_console_control *control;
675 struct virtqueue *vq;
678 vq = sc->vtcon_ctrl_rxvq;
685 while ((control = virtqueue_drain(vq, &last)) != NULL)
686 free(control, M_DEVBUF);
691 vtcon_ctrl_init(struct vtcon_softc *sc)
695 error = vtcon_ctrl_event_populate(sc);
701 vtcon_ctrl_deinit(struct vtcon_softc *sc)
704 vtcon_ctrl_event_drain(sc);
708 vtcon_ctrl_port_add_event(struct vtcon_softc *sc, int id)
715 /* This single thread only way for ports to be created. */
716 if (sc->vtcon_ports[id].vcsp_port != NULL) {
717 device_printf(dev, "%s: adding port %d, but already exists\n",
722 error = vtcon_port_create(sc, id);
724 device_printf(dev, "%s: cannot create port %d: %d\n",
725 __func__, id, error);
726 vtcon_ctrl_send_control(sc, id, VIRTIO_CONSOLE_PORT_READY, 0);
732 vtcon_ctrl_port_remove_event(struct vtcon_softc *sc, int id)
735 struct vtcon_softc_port *scport;
736 struct vtcon_port *port;
739 scport = &sc->vtcon_ports[id];
742 port = scport->vcsp_port;
745 device_printf(dev, "%s: remove port %d, but does not exist\n",
750 scport->vcsp_port = NULL;
751 VTCON_PORT_LOCK(port);
753 vtcon_port_teardown(port);
757 vtcon_ctrl_port_console_event(struct vtcon_softc *sc, int id)
760 struct vtcon_softc_port *scport;
761 struct vtcon_port *port;
764 scport = &sc->vtcon_ports[id];
767 port = scport->vcsp_port;
770 device_printf(dev, "%s: console port %d, but does not exist\n",
775 VTCON_PORT_LOCK(port);
777 port->vtcport_flags |= VTCON_PORT_FLAG_CONSOLE;
778 vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
779 VTCON_PORT_UNLOCK(port);
783 vtcon_ctrl_port_open_event(struct vtcon_softc *sc, int id)
786 struct vtcon_softc_port *scport;
787 struct vtcon_port *port;
790 scport = &sc->vtcon_ports[id];
793 port = scport->vcsp_port;
796 device_printf(dev, "%s: open port %d, but does not exist\n",
801 VTCON_PORT_LOCK(port);
803 vtcon_port_enable_intr(port);
804 VTCON_PORT_UNLOCK(port);
808 vtcon_ctrl_port_name_event(struct vtcon_softc *sc, int id, const char *name,
812 struct vtcon_softc_port *scport;
813 struct vtcon_port *port;
816 scport = &sc->vtcon_ports[id];
819 * The VirtIO specification says the NUL terminator is not included in
820 * the length, but QEMU includes it. Adjust the length if needed.
822 if (name == NULL || len == 0)
824 if (name[len - 1] == '\0') {
831 port = scport->vcsp_port;
834 device_printf(dev, "%s: name port %d, but does not exist\n",
839 VTCON_PORT_LOCK(port);
841 vtcon_port_dev_alias(port, name, len);
842 VTCON_PORT_UNLOCK(port);
846 vtcon_ctrl_process_event(struct vtcon_softc *sc,
847 struct virtio_console_control *control, void *data, size_t data_len)
855 if (id < 0 || id >= sc->vtcon_max_ports) {
856 device_printf(dev, "%s: invalid port ID %d\n", __func__, id);
860 switch (control->event) {
861 case VIRTIO_CONSOLE_PORT_ADD:
862 vtcon_ctrl_port_add_event(sc, id);
865 case VIRTIO_CONSOLE_PORT_REMOVE:
866 vtcon_ctrl_port_remove_event(sc, id);
869 case VIRTIO_CONSOLE_CONSOLE_PORT:
870 vtcon_ctrl_port_console_event(sc, id);
873 case VIRTIO_CONSOLE_RESIZE:
876 case VIRTIO_CONSOLE_PORT_OPEN:
877 vtcon_ctrl_port_open_event(sc, id);
880 case VIRTIO_CONSOLE_PORT_NAME:
881 vtcon_ctrl_port_name_event(sc, id, (const char *)data, data_len);
887 vtcon_ctrl_task_cb(void *xsc, int pending)
889 struct vtcon_softc *sc;
890 struct virtqueue *vq;
891 struct virtio_console_control *control;
898 vq = sc->vtcon_ctrl_rxvq;
902 while ((detached = (sc->vtcon_flags & VTCON_FLAG_DETACHED)) == 0) {
903 control = virtqueue_dequeue(vq, &len);
907 if (len > sizeof(struct virtio_console_control)) {
908 data = (void *) &control[1];
909 data_len = len - sizeof(struct virtio_console_control);
916 vtcon_ctrl_process_event(sc, control, data, data_len);
918 vtcon_ctrl_event_requeue(sc, control);
922 virtqueue_notify(vq);
923 if (virtqueue_enable_intr(vq) != 0)
924 taskqueue_enqueue(taskqueue_thread,
925 &sc->vtcon_ctrl_task);
932 vtcon_ctrl_event_intr(void *xsc)
934 struct vtcon_softc *sc;
939 * Only some events require us to potentially block, but it
940 * easier to just defer all event handling to the taskqueue.
942 taskqueue_enqueue(taskqueue_thread, &sc->vtcon_ctrl_task);
946 vtcon_ctrl_poll(struct vtcon_softc *sc,
947 struct virtio_console_control *control)
949 struct sglist_seg segs[2];
951 struct virtqueue *vq;
954 vq = sc->vtcon_ctrl_txvq;
956 sglist_init(&sg, 2, segs);
957 error = sglist_append(&sg, control,
958 sizeof(struct virtio_console_control));
959 KASSERT(error == 0, ("%s: error %d adding control to sglist",
963 * We cannot use the softc lock to serialize access to this
964 * virtqueue since this is called from the tty layer with the
965 * port lock held. Acquiring the softc would violate our lock
968 VTCON_CTRL_TX_LOCK(sc);
969 KASSERT(virtqueue_empty(vq),
970 ("%s: virtqueue is not emtpy", __func__));
971 error = virtqueue_enqueue(vq, control, &sg, sg.sg_nseg, 0);
973 virtqueue_notify(vq);
974 virtqueue_poll(vq, NULL);
976 VTCON_CTRL_TX_UNLOCK(sc);
980 vtcon_ctrl_send_control(struct vtcon_softc *sc, uint32_t portid,
981 uint16_t event, uint16_t value)
983 struct virtio_console_control control;
985 if ((sc->vtcon_flags & VTCON_FLAG_MULTIPORT) == 0)
989 control.event = event;
990 control.value = value;
992 vtcon_ctrl_poll(sc, &control);
996 vtcon_port_enqueue_buf(struct vtcon_port *port, void *buf, size_t len)
998 struct sglist_seg segs[2];
1000 struct virtqueue *vq;
1003 vq = port->vtcport_invq;
1005 sglist_init(&sg, 2, segs);
1006 error = sglist_append(&sg, buf, len);
1008 ("%s: error %d adding buffer to sglist", __func__, error));
1010 error = virtqueue_enqueue(vq, buf, &sg, 0, sg.sg_nseg);
1016 vtcon_port_create_buf(struct vtcon_port *port)
1021 buf = malloc(VTCON_BULK_BUFSZ, M_DEVBUF, M_ZERO | M_NOWAIT);
1025 error = vtcon_port_enqueue_buf(port, buf, VTCON_BULK_BUFSZ);
1027 free(buf, M_DEVBUF);
1033 vtcon_port_requeue_buf(struct vtcon_port *port, void *buf)
1037 error = vtcon_port_enqueue_buf(port, buf, VTCON_BULK_BUFSZ);
1039 ("%s: cannot requeue input buffer %d", __func__, error));
1043 vtcon_port_populate(struct vtcon_port *port)
1045 struct virtqueue *vq;
1048 vq = port->vtcport_invq;
1051 for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
1052 error = vtcon_port_create_buf(port);
1058 virtqueue_notify(vq);
1066 vtcon_port_destroy(struct vtcon_port *port)
1069 port->vtcport_sc = NULL;
1070 port->vtcport_scport = NULL;
1071 port->vtcport_invq = NULL;
1072 port->vtcport_outvq = NULL;
1073 port->vtcport_id = -1;
1074 mtx_destroy(&port->vtcport_mtx);
1075 free(port, M_DEVBUF);
1079 vtcon_port_init_vqs(struct vtcon_port *port)
1081 struct vtcon_softc_port *scport;
1084 scport = port->vtcport_scport;
1086 port->vtcport_invq = scport->vcsp_invq;
1087 port->vtcport_outvq = scport->vcsp_outvq;
1090 * Free any data left over from when this virtqueue was in use by a
1091 * prior port. We have not yet notified the host that the port is
1092 * ready, so assume nothing in the virtqueue can be for us.
1094 vtcon_port_drain(port);
1096 KASSERT(virtqueue_empty(port->vtcport_invq),
1097 ("%s: in virtqueue is not empty", __func__));
1098 KASSERT(virtqueue_empty(port->vtcport_outvq),
1099 ("%s: out virtqueue is not empty", __func__));
1101 error = vtcon_port_populate(port);
1109 vtcon_port_create(struct vtcon_softc *sc, int id)
1112 struct vtcon_softc_port *scport;
1113 struct vtcon_port *port;
1116 dev = sc->vtcon_dev;
1117 scport = &sc->vtcon_ports[id];
1119 VTCON_ASSERT_VALID_PORTID(sc, id);
1120 MPASS(scport->vcsp_port == NULL);
1122 port = malloc(sizeof(struct vtcon_port), M_DEVBUF, M_NOWAIT | M_ZERO);
1126 port->vtcport_sc = sc;
1127 port->vtcport_scport = scport;
1128 port->vtcport_id = id;
1129 mtx_init(&port->vtcport_mtx, "vtcpmtx", NULL, MTX_DEF);
1130 port->vtcport_tty = tty_alloc_mutex(&vtcon_tty_class, port,
1131 &port->vtcport_mtx);
1133 error = vtcon_port_init_vqs(port);
1135 VTCON_PORT_LOCK(port);
1136 vtcon_port_teardown(port);
1141 VTCON_PORT_LOCK(port);
1142 scport->vcsp_port = port;
1143 vtcon_port_enable_intr(port);
1144 vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_READY, 1);
1145 VTCON_PORT_UNLOCK(port);
1148 tty_makedev(port->vtcport_tty, NULL, "%s%r.%r", VTCON_TTY_PREFIX,
1149 device_get_unit(dev), id);
1155 vtcon_port_dev_alias(struct vtcon_port *port, const char *name, size_t len)
1157 struct vtcon_softc *sc;
1162 sc = port->vtcport_sc;
1163 tp = port->vtcport_tty;
1165 if (port->vtcport_flags & VTCON_PORT_FLAG_ALIAS)
1168 /* Port name is UTF-8, but we can only handle ASCII. */
1169 for (i = 0; i < len; i++) {
1170 if (!isascii(name[i]))
1175 * Port name may not conform to the devfs requirements so we cannot use
1176 * tty_makealias() because the MAKEDEV_CHECKNAME flag must be specified.
1178 error = make_dev_alias_p(MAKEDEV_NOWAIT | MAKEDEV_CHECKNAME, &pdev,
1179 tp->t_dev, "%s/%*s", VTCON_TTY_ALIAS_PREFIX, (int)len, name);
1181 device_printf(sc->vtcon_dev,
1182 "%s: cannot make dev alias (%s/%*s) error %d\n", __func__,
1183 VTCON_TTY_ALIAS_PREFIX, (int)len, name, error);
1185 port->vtcport_flags |= VTCON_PORT_FLAG_ALIAS;
1189 vtcon_port_drain_bufs(struct virtqueue *vq)
1196 while ((buf = virtqueue_drain(vq, &last)) != NULL)
1197 free(buf, M_DEVBUF);
1201 vtcon_port_drain(struct vtcon_port *port)
1204 vtcon_port_drain_bufs(port->vtcport_invq);
1208 vtcon_port_teardown(struct vtcon_port *port)
1212 tp = port->vtcport_tty;
1214 port->vtcport_flags |= VTCON_PORT_FLAG_GONE;
1217 atomic_add_int(&vtcon_pending_free, 1);
1220 vtcon_port_destroy(port);
1224 vtcon_port_change_size(struct vtcon_port *port, uint16_t cols, uint16_t rows)
1229 tp = port->vtcport_tty;
1234 bzero(&sz, sizeof(struct winsize));
1238 tty_set_winsize(tp, &sz);
1242 vtcon_port_update_console_size(struct vtcon_softc *sc)
1244 struct vtcon_port *port;
1245 struct vtcon_softc_port *scport;
1246 uint16_t cols, rows;
1248 vtcon_get_console_size(sc, &cols, &rows);
1251 * For now, assume the first (only) port is the console. Note
1252 * QEMU does not implement this feature yet.
1254 scport = &sc->vtcon_ports[0];
1257 port = scport->vcsp_port;
1260 VTCON_PORT_LOCK(port);
1262 vtcon_port_change_size(port, cols, rows);
1263 VTCON_PORT_UNLOCK(port);
1269 vtcon_port_enable_intr(struct vtcon_port *port)
1273 * NOTE: The out virtqueue is always polled, so its interrupt
1276 virtqueue_enable_intr(port->vtcport_invq);
1280 vtcon_port_disable_intr(struct vtcon_port *port)
1283 if (port->vtcport_invq != NULL)
1284 virtqueue_disable_intr(port->vtcport_invq);
1285 if (port->vtcport_outvq != NULL)
1286 virtqueue_disable_intr(port->vtcport_outvq);
1290 vtcon_port_in(struct vtcon_port *port)
1292 struct virtqueue *vq;
1298 tp = port->vtcport_tty;
1299 vq = port->vtcport_invq;
1304 while ((buf = virtqueue_dequeue(vq, &len)) != NULL) {
1305 for (i = 0; i < len; i++) {
1307 if (port->vtcport_flags & VTCON_PORT_FLAG_CONSOLE)
1308 kdb_alt_break(buf[i],
1309 &port->vtcport_alt_break_state);
1311 ttydisc_rint(tp, buf[i], 0);
1313 vtcon_port_requeue_buf(port, buf);
1316 ttydisc_rint_done(tp);
1319 virtqueue_notify(vq);
1321 if (virtqueue_enable_intr(vq) != 0)
1326 vtcon_port_intr(void *scportx)
1328 struct vtcon_softc_port *scport;
1329 struct vtcon_softc *sc;
1330 struct vtcon_port *port;
1333 sc = scport->vcsp_sc;
1336 port = scport->vcsp_port;
1341 VTCON_PORT_LOCK(port);
1343 if ((port->vtcport_flags & VTCON_PORT_FLAG_GONE) == 0)
1344 vtcon_port_in(port);
1345 VTCON_PORT_UNLOCK(port);
1349 vtcon_port_out(struct vtcon_port *port, void *buf, int bufsize)
1351 struct sglist_seg segs[2];
1353 struct virtqueue *vq;
1356 vq = port->vtcport_outvq;
1357 KASSERT(virtqueue_empty(vq),
1358 ("%s: port %p out virtqueue not emtpy", __func__, port));
1360 sglist_init(&sg, 2, segs);
1361 error = sglist_append(&sg, buf, bufsize);
1362 KASSERT(error == 0, ("%s: error %d adding buffer to sglist",
1365 error = virtqueue_enqueue(vq, buf, &sg, sg.sg_nseg, 0);
1367 virtqueue_notify(vq);
1368 virtqueue_poll(vq, NULL);
1373 vtcon_port_submit_event(struct vtcon_port *port, uint16_t event,
1376 struct vtcon_softc *sc;
1378 sc = port->vtcport_sc;
1380 vtcon_ctrl_send_control(sc, port->vtcport_id, event, value);
1384 vtcon_tty_open(struct tty *tp)
1386 struct vtcon_port *port;
1388 port = tty_softc(tp);
1390 if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1393 vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1399 vtcon_tty_close(struct tty *tp)
1401 struct vtcon_port *port;
1403 port = tty_softc(tp);
1405 if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1408 vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1412 vtcon_tty_outwakeup(struct tty *tp)
1414 struct vtcon_port *port;
1415 char buf[VTCON_BULK_BUFSZ];
1418 port = tty_softc(tp);
1420 if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1423 while ((len = ttydisc_getc(tp, buf, sizeof(buf))) != 0)
1424 vtcon_port_out(port, buf, len);
1428 vtcon_tty_free(void *xport)
1430 struct vtcon_port *port;
1434 vtcon_port_destroy(port);
1435 atomic_subtract_int(&vtcon_pending_free, 1);
1439 vtcon_get_console_size(struct vtcon_softc *sc, uint16_t *cols, uint16_t *rows)
1441 struct virtio_console_config concfg;
1443 KASSERT(sc->vtcon_flags & VTCON_FLAG_SIZE,
1444 ("%s: size feature not negotiated", __func__));
1446 vtcon_read_config(sc, &concfg);
1448 *cols = concfg.cols;
1449 *rows = concfg.rows;
1453 vtcon_enable_interrupts(struct vtcon_softc *sc)
1455 struct vtcon_softc_port *scport;
1456 struct vtcon_port *port;
1461 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
1462 virtqueue_enable_intr(sc->vtcon_ctrl_rxvq);
1464 for (i = 0; i < sc->vtcon_max_ports; i++) {
1465 scport = &sc->vtcon_ports[i];
1467 port = scport->vcsp_port;
1471 VTCON_PORT_LOCK(port);
1472 vtcon_port_enable_intr(port);
1473 VTCON_PORT_UNLOCK(port);
1480 vtcon_disable_interrupts(struct vtcon_softc *sc)
1482 struct vtcon_softc_port *scport;
1483 struct vtcon_port *port;
1486 VTCON_LOCK_ASSERT(sc);
1488 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
1489 virtqueue_disable_intr(sc->vtcon_ctrl_rxvq);
1491 for (i = 0; i < sc->vtcon_max_ports; i++) {
1492 scport = &sc->vtcon_ports[i];
1494 port = scport->vcsp_port;
1498 VTCON_PORT_LOCK(port);
1499 vtcon_port_disable_intr(port);
1500 VTCON_PORT_UNLOCK(port);