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/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/sglist.h>
41 #include <sys/sysctl.h>
42 #include <sys/taskqueue.h>
43 #include <sys/queue.h>
49 #include <machine/bus.h>
50 #include <machine/resource.h>
53 #include <dev/virtio/virtio.h>
54 #include <dev/virtio/virtqueue.h>
55 #include <dev/virtio/console/virtio_console.h>
57 #include "virtio_if.h"
59 #define VTCON_MAX_PORTS 32
60 #define VTCON_TTY_PREFIX "V"
61 #define VTCON_BULK_BUFSZ 128
64 * The buffer cannot cross more than one page boundary due to the
65 * size of the sglist segment array used.
67 CTASSERT(VTCON_BULK_BUFSZ <= PAGE_SIZE);
70 struct vtcon_softc_port;
73 struct mtx vtcport_mtx;
74 struct vtcon_softc *vtcport_sc;
75 struct vtcon_softc_port *vtcport_scport;
76 struct tty *vtcport_tty;
77 struct virtqueue *vtcport_invq;
78 struct virtqueue *vtcport_outvq;
81 #define VTCON_PORT_FLAG_GONE 0x01
82 #define VTCON_PORT_FLAG_CONSOLE 0x02
85 int vtcport_alt_break_state;
89 #define VTCON_PORT_LOCK(_port) mtx_lock(&(_port)->vtcport_mtx)
90 #define VTCON_PORT_UNLOCK(_port) mtx_unlock(&(_port)->vtcport_mtx)
92 struct vtcon_softc_port {
93 struct vtcon_softc *vcsp_sc;
94 struct vtcon_port *vcsp_port;
95 struct virtqueue *vcsp_invq;
96 struct virtqueue *vcsp_outvq;
101 struct mtx vtcon_mtx;
102 uint64_t vtcon_features;
103 uint32_t vtcon_max_ports;
104 uint32_t vtcon_flags;
105 #define VTCON_FLAG_DETACHED 0x01
106 #define VTCON_FLAG_SIZE 0x02
107 #define VTCON_FLAG_MULTIPORT 0x04
110 * Ports can be added and removed during runtime, but we have
111 * to allocate all the virtqueues during attach. This array is
112 * indexed by the port ID.
114 struct vtcon_softc_port *vtcon_ports;
116 struct task vtcon_ctrl_task;
117 struct virtqueue *vtcon_ctrl_rxvq;
118 struct virtqueue *vtcon_ctrl_txvq;
119 struct mtx vtcon_ctrl_tx_mtx;
122 #define VTCON_LOCK(_sc) mtx_lock(&(_sc)->vtcon_mtx)
123 #define VTCON_UNLOCK(_sc) mtx_unlock(&(_sc)->vtcon_mtx)
124 #define VTCON_LOCK_ASSERT(_sc) \
125 mtx_assert(&(_sc)->vtcon_mtx, MA_OWNED)
126 #define VTCON_LOCK_ASSERT_NOTOWNED(_sc) \
127 mtx_assert(&(_sc)->vtcon_mtx, MA_NOTOWNED)
129 #define VTCON_CTRL_TX_LOCK(_sc) mtx_lock(&(_sc)->vtcon_ctrl_tx_mtx)
130 #define VTCON_CTRL_TX_UNLOCK(_sc) mtx_unlock(&(_sc)->vtcon_ctrl_tx_mtx)
132 #define VTCON_ASSERT_VALID_PORTID(_sc, _id) \
133 KASSERT((_id) >= 0 && (_id) < (_sc)->vtcon_max_ports, \
134 ("%s: port ID %d out of range", __func__, _id))
136 #define VTCON_FEATURES VIRTIO_CONSOLE_F_MULTIPORT
138 static struct virtio_feature_desc vtcon_feature_desc[] = {
139 { VIRTIO_CONSOLE_F_SIZE, "ConsoleSize" },
140 { VIRTIO_CONSOLE_F_MULTIPORT, "MultiplePorts" },
141 { VIRTIO_CONSOLE_F_EMERG_WRITE, "EmergencyWrite" },
146 static int vtcon_modevent(module_t, int, void *);
147 static void vtcon_drain_all(void);
149 static int vtcon_probe(device_t);
150 static int vtcon_attach(device_t);
151 static int vtcon_detach(device_t);
152 static int vtcon_config_change(device_t);
154 static void vtcon_setup_features(struct vtcon_softc *);
155 static void vtcon_negotiate_features(struct vtcon_softc *);
156 static int vtcon_alloc_scports(struct vtcon_softc *);
157 static int vtcon_alloc_virtqueues(struct vtcon_softc *);
158 static void vtcon_read_config(struct vtcon_softc *,
159 struct virtio_console_config *);
161 static void vtcon_determine_max_ports(struct vtcon_softc *,
162 struct virtio_console_config *);
163 static void vtcon_destroy_ports(struct vtcon_softc *);
164 static void vtcon_stop(struct vtcon_softc *);
166 static int vtcon_ctrl_event_enqueue(struct vtcon_softc *,
167 struct virtio_console_control *);
168 static int vtcon_ctrl_event_create(struct vtcon_softc *);
169 static void vtcon_ctrl_event_requeue(struct vtcon_softc *,
170 struct virtio_console_control *);
171 static int vtcon_ctrl_event_populate(struct vtcon_softc *);
172 static void vtcon_ctrl_event_drain(struct vtcon_softc *);
173 static int vtcon_ctrl_init(struct vtcon_softc *);
174 static void vtcon_ctrl_deinit(struct vtcon_softc *);
175 static void vtcon_ctrl_port_add_event(struct vtcon_softc *, int);
176 static void vtcon_ctrl_port_remove_event(struct vtcon_softc *, int);
177 static void vtcon_ctrl_port_console_event(struct vtcon_softc *, int);
178 static void vtcon_ctrl_port_open_event(struct vtcon_softc *, int);
179 static void vtcon_ctrl_process_event(struct vtcon_softc *,
180 struct virtio_console_control *);
181 static void vtcon_ctrl_task_cb(void *, int);
182 static void vtcon_ctrl_event_intr(void *);
183 static void vtcon_ctrl_poll(struct vtcon_softc *,
184 struct virtio_console_control *control);
185 static void vtcon_ctrl_send_control(struct vtcon_softc *, uint32_t,
188 static int vtcon_port_enqueue_buf(struct vtcon_port *, void *, size_t);
189 static int vtcon_port_create_buf(struct vtcon_port *);
190 static void vtcon_port_requeue_buf(struct vtcon_port *, void *);
191 static int vtcon_port_populate(struct vtcon_port *);
192 static void vtcon_port_destroy(struct vtcon_port *);
193 static int vtcon_port_create(struct vtcon_softc *, int);
194 static void vtcon_port_drain_bufs(struct virtqueue *);
195 static void vtcon_port_drain(struct vtcon_port *);
196 static void vtcon_port_teardown(struct vtcon_port *);
197 static void vtcon_port_change_size(struct vtcon_port *, uint16_t,
199 static void vtcon_port_update_console_size(struct vtcon_softc *);
200 static void vtcon_port_enable_intr(struct vtcon_port *);
201 static void vtcon_port_disable_intr(struct vtcon_port *);
202 static void vtcon_port_in(struct vtcon_port *);
203 static void vtcon_port_intr(void *);
204 static void vtcon_port_out(struct vtcon_port *, void *, int);
205 static void vtcon_port_submit_event(struct vtcon_port *, uint16_t,
208 static int vtcon_tty_open(struct tty *);
209 static void vtcon_tty_close(struct tty *);
210 static void vtcon_tty_outwakeup(struct tty *);
211 static void vtcon_tty_free(void *);
213 static void vtcon_get_console_size(struct vtcon_softc *, uint16_t *,
216 static void vtcon_enable_interrupts(struct vtcon_softc *);
217 static void vtcon_disable_interrupts(struct vtcon_softc *);
219 static int vtcon_pending_free;
221 static struct ttydevsw vtcon_tty_class = {
223 .tsw_open = vtcon_tty_open,
224 .tsw_close = vtcon_tty_close,
225 .tsw_outwakeup = vtcon_tty_outwakeup,
226 .tsw_free = vtcon_tty_free,
229 static device_method_t vtcon_methods[] = {
230 /* Device methods. */
231 DEVMETHOD(device_probe, vtcon_probe),
232 DEVMETHOD(device_attach, vtcon_attach),
233 DEVMETHOD(device_detach, vtcon_detach),
235 /* VirtIO methods. */
236 DEVMETHOD(virtio_config_change, vtcon_config_change),
241 static driver_t vtcon_driver = {
244 sizeof(struct vtcon_softc)
246 static devclass_t vtcon_devclass;
248 DRIVER_MODULE(virtio_console, virtio_pci, vtcon_driver, vtcon_devclass,
250 MODULE_VERSION(virtio_console, 1);
251 MODULE_DEPEND(virtio_console, virtio, 1, 1, 1);
254 vtcon_modevent(module_t mod, int type, void *unused)
281 vtcon_drain_all(void)
285 for (first = 1; vtcon_pending_free != 0; first = 0) {
287 printf("virtio_console: Waiting for all detached TTY "
288 "devices to have open fds closed.\n");
290 pause("vtcondra", hz);
295 vtcon_probe(device_t dev)
298 if (virtio_get_device_type(dev) != VIRTIO_ID_CONSOLE)
301 device_set_desc(dev, "VirtIO Console Adapter");
303 return (BUS_PROBE_DEFAULT);
307 vtcon_attach(device_t dev)
309 struct vtcon_softc *sc;
310 struct virtio_console_config concfg;
313 sc = device_get_softc(dev);
316 mtx_init(&sc->vtcon_mtx, "vtconmtx", NULL, MTX_DEF);
317 mtx_init(&sc->vtcon_ctrl_tx_mtx, "vtconctrlmtx", NULL, MTX_DEF);
319 virtio_set_feature_desc(dev, vtcon_feature_desc);
320 vtcon_setup_features(sc);
322 vtcon_read_config(sc, &concfg);
323 vtcon_determine_max_ports(sc, &concfg);
325 error = vtcon_alloc_scports(sc);
327 device_printf(dev, "cannot allocate softc port structures\n");
331 error = vtcon_alloc_virtqueues(sc);
333 device_printf(dev, "cannot allocate virtqueues\n");
337 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
338 TASK_INIT(&sc->vtcon_ctrl_task, 0, vtcon_ctrl_task_cb, sc);
339 error = vtcon_ctrl_init(sc);
343 error = vtcon_port_create(sc, 0);
346 if (sc->vtcon_flags & VTCON_FLAG_SIZE)
347 vtcon_port_update_console_size(sc);
350 error = virtio_setup_intr(dev, INTR_TYPE_TTY);
352 device_printf(dev, "cannot setup virtqueue interrupts\n");
356 vtcon_enable_interrupts(sc);
358 vtcon_ctrl_send_control(sc, VIRTIO_CONSOLE_BAD_ID,
359 VIRTIO_CONSOLE_DEVICE_READY, 1);
369 vtcon_detach(device_t dev)
371 struct vtcon_softc *sc;
373 sc = device_get_softc(dev);
376 sc->vtcon_flags |= VTCON_FLAG_DETACHED;
377 if (device_is_attached(dev))
381 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
382 taskqueue_drain(taskqueue_thread, &sc->vtcon_ctrl_task);
383 vtcon_ctrl_deinit(sc);
386 vtcon_destroy_ports(sc);
387 mtx_destroy(&sc->vtcon_mtx);
388 mtx_destroy(&sc->vtcon_ctrl_tx_mtx);
394 vtcon_config_change(device_t dev)
396 struct vtcon_softc *sc;
398 sc = device_get_softc(dev);
401 * When the multiport feature is negotiated, all configuration
402 * changes are done through control virtqueue events.
404 if ((sc->vtcon_flags & VTCON_FLAG_MULTIPORT) == 0) {
405 if (sc->vtcon_flags & VTCON_FLAG_SIZE)
406 vtcon_port_update_console_size(sc);
413 vtcon_negotiate_features(struct vtcon_softc *sc)
419 features = VTCON_FEATURES;
421 sc->vtcon_features = virtio_negotiate_features(dev, features);
425 vtcon_setup_features(struct vtcon_softc *sc)
431 vtcon_negotiate_features(sc);
433 if (virtio_with_feature(dev, VIRTIO_CONSOLE_F_SIZE))
434 sc->vtcon_flags |= VTCON_FLAG_SIZE;
435 if (virtio_with_feature(dev, VIRTIO_CONSOLE_F_MULTIPORT))
436 sc->vtcon_flags |= VTCON_FLAG_MULTIPORT;
439 #define VTCON_GET_CONFIG(_dev, _feature, _field, _cfg) \
440 if (virtio_with_feature(_dev, _feature)) { \
441 virtio_read_device_config(_dev, \
442 offsetof(struct virtio_console_config, _field), \
443 &(_cfg)->_field, sizeof((_cfg)->_field)); \
447 vtcon_read_config(struct vtcon_softc *sc, struct virtio_console_config *concfg)
453 bzero(concfg, sizeof(struct virtio_console_config));
455 VTCON_GET_CONFIG(dev, VIRTIO_CONSOLE_F_SIZE, cols, concfg);
456 VTCON_GET_CONFIG(dev, VIRTIO_CONSOLE_F_SIZE, rows, concfg);
457 VTCON_GET_CONFIG(dev, VIRTIO_CONSOLE_F_MULTIPORT, max_nr_ports, concfg);
460 #undef VTCON_GET_CONFIG
463 vtcon_alloc_scports(struct vtcon_softc *sc)
465 struct vtcon_softc_port *scport;
468 max = sc->vtcon_max_ports;
470 sc->vtcon_ports = malloc(sizeof(struct vtcon_softc_port) * max,
471 M_DEVBUF, M_NOWAIT | M_ZERO);
472 if (sc->vtcon_ports == NULL)
475 for (i = 0; i < max; i++) {
476 scport = &sc->vtcon_ports[i];
477 scport->vcsp_sc = sc;
484 vtcon_alloc_virtqueues(struct vtcon_softc *sc)
487 struct vq_alloc_info *info;
488 struct vtcon_softc_port *scport;
489 int i, idx, portidx, nvqs, error;
493 nvqs = sc->vtcon_max_ports * 2;
494 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
497 info = malloc(sizeof(struct vq_alloc_info) * nvqs, M_TEMP, M_NOWAIT);
501 for (i = 0, idx = 0, portidx = 0; i < nvqs / 2; i++, idx += 2) {
504 /* The control virtqueues are after the first port. */
505 VQ_ALLOC_INFO_INIT(&info[idx], 0,
506 vtcon_ctrl_event_intr, sc, &sc->vtcon_ctrl_rxvq,
507 "%s-control rx", device_get_nameunit(dev));
508 VQ_ALLOC_INFO_INIT(&info[idx+1], 0,
509 NULL, sc, &sc->vtcon_ctrl_txvq,
510 "%s-control tx", device_get_nameunit(dev));
514 scport = &sc->vtcon_ports[portidx];
516 VQ_ALLOC_INFO_INIT(&info[idx], 0, vtcon_port_intr,
517 scport, &scport->vcsp_invq, "%s-port%d in",
518 device_get_nameunit(dev), i);
519 VQ_ALLOC_INFO_INIT(&info[idx+1], 0, NULL,
520 NULL, &scport->vcsp_outvq, "%s-port%d out",
521 device_get_nameunit(dev), i);
526 error = virtio_alloc_virtqueues(dev, 0, nvqs, info);
533 vtcon_determine_max_ports(struct vtcon_softc *sc,
534 struct virtio_console_config *concfg)
537 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT) {
538 sc->vtcon_max_ports =
539 min(concfg->max_nr_ports, VTCON_MAX_PORTS);
540 if (sc->vtcon_max_ports == 0)
541 sc->vtcon_max_ports = 1;
543 sc->vtcon_max_ports = 1;
547 vtcon_destroy_ports(struct vtcon_softc *sc)
549 struct vtcon_softc_port *scport;
550 struct vtcon_port *port;
551 struct virtqueue *vq;
554 if (sc->vtcon_ports == NULL)
558 for (i = 0; i < sc->vtcon_max_ports; i++) {
559 scport = &sc->vtcon_ports[i];
561 port = scport->vcsp_port;
563 scport->vcsp_port = NULL;
564 VTCON_PORT_LOCK(port);
566 vtcon_port_teardown(port);
570 vq = scport->vcsp_invq;
572 vtcon_port_drain_bufs(vq);
576 free(sc->vtcon_ports, M_DEVBUF);
577 sc->vtcon_ports = NULL;
581 vtcon_stop(struct vtcon_softc *sc)
584 vtcon_disable_interrupts(sc);
585 virtio_stop(sc->vtcon_dev);
589 vtcon_ctrl_event_enqueue(struct vtcon_softc *sc,
590 struct virtio_console_control *control)
592 struct sglist_seg segs[2];
594 struct virtqueue *vq;
597 vq = sc->vtcon_ctrl_rxvq;
599 sglist_init(&sg, 2, segs);
600 error = sglist_append(&sg, control,
601 sizeof(struct virtio_console_control));
602 KASSERT(error == 0, ("%s: error %d adding control to sglist",
605 return (virtqueue_enqueue(vq, control, &sg, 0, sg.sg_nseg));
609 vtcon_ctrl_event_create(struct vtcon_softc *sc)
611 struct virtio_console_control *control;
614 control = malloc(sizeof(struct virtio_console_control), M_DEVBUF,
619 error = vtcon_ctrl_event_enqueue(sc, control);
621 free(control, M_DEVBUF);
627 vtcon_ctrl_event_requeue(struct vtcon_softc *sc,
628 struct virtio_console_control *control)
632 bzero(control, sizeof(struct virtio_console_control));
634 error = vtcon_ctrl_event_enqueue(sc, control);
636 ("%s: cannot requeue control buffer %d", __func__, error));
640 vtcon_ctrl_event_populate(struct vtcon_softc *sc)
642 struct virtqueue *vq;
645 vq = sc->vtcon_ctrl_rxvq;
648 for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
649 error = vtcon_ctrl_event_create(sc);
655 virtqueue_notify(vq);
663 vtcon_ctrl_event_drain(struct vtcon_softc *sc)
665 struct virtio_console_control *control;
666 struct virtqueue *vq;
669 vq = sc->vtcon_ctrl_rxvq;
676 while ((control = virtqueue_drain(vq, &last)) != NULL)
677 free(control, M_DEVBUF);
682 vtcon_ctrl_init(struct vtcon_softc *sc)
686 error = vtcon_ctrl_event_populate(sc);
692 vtcon_ctrl_deinit(struct vtcon_softc *sc)
695 vtcon_ctrl_event_drain(sc);
699 vtcon_ctrl_port_add_event(struct vtcon_softc *sc, int id)
706 /* This single thread only way for ports to be created. */
707 if (sc->vtcon_ports[id].vcsp_port != NULL) {
708 device_printf(dev, "%s: adding port %d, but already exists\n",
713 error = vtcon_port_create(sc, id);
715 device_printf(dev, "%s: cannot create port %d: %d\n",
716 __func__, id, error);
717 vtcon_ctrl_send_control(sc, id, VIRTIO_CONSOLE_PORT_READY, 0);
723 vtcon_ctrl_port_remove_event(struct vtcon_softc *sc, int id)
726 struct vtcon_softc_port *scport;
727 struct vtcon_port *port;
730 scport = &sc->vtcon_ports[id];
733 port = scport->vcsp_port;
736 device_printf(dev, "%s: remove port %d, but does not exist\n",
741 scport->vcsp_port = NULL;
742 VTCON_PORT_LOCK(port);
744 vtcon_port_teardown(port);
748 vtcon_ctrl_port_console_event(struct vtcon_softc *sc, int id)
751 struct vtcon_softc_port *scport;
752 struct vtcon_port *port;
755 scport = &sc->vtcon_ports[id];
758 port = scport->vcsp_port;
761 device_printf(dev, "%s: console port %d, but does not exist\n",
766 VTCON_PORT_LOCK(port);
768 port->vtcport_flags |= VTCON_PORT_FLAG_CONSOLE;
769 vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
770 VTCON_PORT_UNLOCK(port);
774 vtcon_ctrl_port_open_event(struct vtcon_softc *sc, int id)
777 struct vtcon_softc_port *scport;
778 struct vtcon_port *port;
781 scport = &sc->vtcon_ports[id];
784 port = scport->vcsp_port;
787 device_printf(dev, "%s: open port %d, but does not exist\n",
792 VTCON_PORT_LOCK(port);
794 vtcon_port_enable_intr(port);
795 VTCON_PORT_UNLOCK(port);
799 vtcon_ctrl_process_event(struct vtcon_softc *sc,
800 struct virtio_console_control *control)
808 if (id < 0 || id >= sc->vtcon_max_ports) {
809 device_printf(dev, "%s: invalid port ID %d\n", __func__, id);
813 switch (control->event) {
814 case VIRTIO_CONSOLE_PORT_ADD:
815 vtcon_ctrl_port_add_event(sc, id);
818 case VIRTIO_CONSOLE_PORT_REMOVE:
819 vtcon_ctrl_port_remove_event(sc, id);
822 case VIRTIO_CONSOLE_CONSOLE_PORT:
823 vtcon_ctrl_port_console_event(sc, id);
826 case VIRTIO_CONSOLE_RESIZE:
829 case VIRTIO_CONSOLE_PORT_OPEN:
830 vtcon_ctrl_port_open_event(sc, id);
833 case VIRTIO_CONSOLE_PORT_NAME:
839 vtcon_ctrl_task_cb(void *xsc, int pending)
841 struct vtcon_softc *sc;
842 struct virtqueue *vq;
843 struct virtio_console_control *control;
847 vq = sc->vtcon_ctrl_rxvq;
851 while ((detached = (sc->vtcon_flags & VTCON_FLAG_DETACHED)) == 0) {
852 control = virtqueue_dequeue(vq, NULL);
857 vtcon_ctrl_process_event(sc, control);
859 vtcon_ctrl_event_requeue(sc, control);
863 virtqueue_notify(vq);
864 if (virtqueue_enable_intr(vq) != 0)
865 taskqueue_enqueue(taskqueue_thread,
866 &sc->vtcon_ctrl_task);
873 vtcon_ctrl_event_intr(void *xsc)
875 struct vtcon_softc *sc;
880 * Only some events require us to potentially block, but it
881 * easier to just defer all event handling to the taskqueue.
883 taskqueue_enqueue(taskqueue_thread, &sc->vtcon_ctrl_task);
887 vtcon_ctrl_poll(struct vtcon_softc *sc,
888 struct virtio_console_control *control)
890 struct sglist_seg segs[2];
892 struct virtqueue *vq;
895 vq = sc->vtcon_ctrl_txvq;
897 sglist_init(&sg, 2, segs);
898 error = sglist_append(&sg, control,
899 sizeof(struct virtio_console_control));
900 KASSERT(error == 0, ("%s: error %d adding control to sglist",
904 * We cannot use the softc lock to serialize access to this
905 * virtqueue since this is called from the tty layer with the
906 * port lock held. Acquiring the softc would violate our lock
909 VTCON_CTRL_TX_LOCK(sc);
910 KASSERT(virtqueue_empty(vq),
911 ("%s: virtqueue is not emtpy", __func__));
912 error = virtqueue_enqueue(vq, control, &sg, sg.sg_nseg, 0);
914 virtqueue_notify(vq);
915 virtqueue_poll(vq, NULL);
917 VTCON_CTRL_TX_UNLOCK(sc);
921 vtcon_ctrl_send_control(struct vtcon_softc *sc, uint32_t portid,
922 uint16_t event, uint16_t value)
924 struct virtio_console_control control;
926 if ((sc->vtcon_flags & VTCON_FLAG_MULTIPORT) == 0)
930 control.event = event;
931 control.value = value;
933 vtcon_ctrl_poll(sc, &control);
937 vtcon_port_enqueue_buf(struct vtcon_port *port, void *buf, size_t len)
939 struct sglist_seg segs[2];
941 struct virtqueue *vq;
944 vq = port->vtcport_invq;
946 sglist_init(&sg, 2, segs);
947 error = sglist_append(&sg, buf, len);
949 ("%s: error %d adding buffer to sglist", __func__, error));
951 error = virtqueue_enqueue(vq, buf, &sg, 0, sg.sg_nseg);
957 vtcon_port_create_buf(struct vtcon_port *port)
962 buf = malloc(VTCON_BULK_BUFSZ, M_DEVBUF, M_ZERO | M_NOWAIT);
966 error = vtcon_port_enqueue_buf(port, buf, VTCON_BULK_BUFSZ);
974 vtcon_port_requeue_buf(struct vtcon_port *port, void *buf)
978 error = vtcon_port_enqueue_buf(port, buf, VTCON_BULK_BUFSZ);
980 ("%s: cannot requeue input buffer %d", __func__, error));
984 vtcon_port_populate(struct vtcon_port *port)
986 struct virtqueue *vq;
989 vq = port->vtcport_invq;
992 for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
993 error = vtcon_port_create_buf(port);
999 virtqueue_notify(vq);
1007 vtcon_port_destroy(struct vtcon_port *port)
1010 port->vtcport_sc = NULL;
1011 port->vtcport_scport = NULL;
1012 port->vtcport_invq = NULL;
1013 port->vtcport_outvq = NULL;
1014 port->vtcport_id = -1;
1015 mtx_destroy(&port->vtcport_mtx);
1016 free(port, M_DEVBUF);
1020 vtcon_port_init_vqs(struct vtcon_port *port)
1022 struct vtcon_softc_port *scport;
1025 scport = port->vtcport_scport;
1027 port->vtcport_invq = scport->vcsp_invq;
1028 port->vtcport_outvq = scport->vcsp_outvq;
1031 * Free any data left over from when this virtqueue was in use by a
1032 * prior port. We have not yet notified the host that the port is
1033 * ready, so assume nothing in the virtqueue can be for us.
1035 vtcon_port_drain(port);
1037 KASSERT(virtqueue_empty(port->vtcport_invq),
1038 ("%s: in virtqueue is not empty", __func__));
1039 KASSERT(virtqueue_empty(port->vtcport_outvq),
1040 ("%s: out virtqueue is not empty", __func__));
1042 error = vtcon_port_populate(port);
1050 vtcon_port_create(struct vtcon_softc *sc, int id)
1053 struct vtcon_softc_port *scport;
1054 struct vtcon_port *port;
1057 dev = sc->vtcon_dev;
1058 scport = &sc->vtcon_ports[id];
1060 VTCON_ASSERT_VALID_PORTID(sc, id);
1061 MPASS(scport->vcsp_port == NULL);
1063 port = malloc(sizeof(struct vtcon_port), M_DEVBUF, M_NOWAIT | M_ZERO);
1067 port->vtcport_sc = sc;
1068 port->vtcport_scport = scport;
1069 port->vtcport_id = id;
1070 mtx_init(&port->vtcport_mtx, "vtcpmtx", NULL, MTX_DEF);
1071 port->vtcport_tty = tty_alloc_mutex(&vtcon_tty_class, port,
1072 &port->vtcport_mtx);
1074 error = vtcon_port_init_vqs(port);
1076 VTCON_PORT_LOCK(port);
1077 vtcon_port_teardown(port);
1082 VTCON_PORT_LOCK(port);
1083 scport->vcsp_port = port;
1084 vtcon_port_enable_intr(port);
1085 vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_READY, 1);
1086 VTCON_PORT_UNLOCK(port);
1089 tty_makedev(port->vtcport_tty, NULL, "%s%r.%r", VTCON_TTY_PREFIX,
1090 device_get_unit(dev), id);
1096 vtcon_port_drain_bufs(struct virtqueue *vq)
1103 while ((buf = virtqueue_drain(vq, &last)) != NULL)
1104 free(buf, M_DEVBUF);
1108 vtcon_port_drain(struct vtcon_port *port)
1111 vtcon_port_drain_bufs(port->vtcport_invq);
1115 vtcon_port_teardown(struct vtcon_port *port)
1119 tp = port->vtcport_tty;
1121 port->vtcport_flags |= VTCON_PORT_FLAG_GONE;
1124 atomic_add_int(&vtcon_pending_free, 1);
1127 vtcon_port_destroy(port);
1131 vtcon_port_change_size(struct vtcon_port *port, uint16_t cols, uint16_t rows)
1136 tp = port->vtcport_tty;
1141 bzero(&sz, sizeof(struct winsize));
1145 tty_set_winsize(tp, &sz);
1149 vtcon_port_update_console_size(struct vtcon_softc *sc)
1151 struct vtcon_port *port;
1152 struct vtcon_softc_port *scport;
1153 uint16_t cols, rows;
1155 vtcon_get_console_size(sc, &cols, &rows);
1158 * For now, assume the first (only) port is the console. Note
1159 * QEMU does not implement this feature yet.
1161 scport = &sc->vtcon_ports[0];
1164 port = scport->vcsp_port;
1167 VTCON_PORT_LOCK(port);
1169 vtcon_port_change_size(port, cols, rows);
1170 VTCON_PORT_UNLOCK(port);
1176 vtcon_port_enable_intr(struct vtcon_port *port)
1180 * NOTE: The out virtqueue is always polled, so its interupt
1183 virtqueue_enable_intr(port->vtcport_invq);
1187 vtcon_port_disable_intr(struct vtcon_port *port)
1190 if (port->vtcport_invq != NULL)
1191 virtqueue_disable_intr(port->vtcport_invq);
1192 if (port->vtcport_outvq != NULL)
1193 virtqueue_disable_intr(port->vtcport_outvq);
1197 vtcon_port_in(struct vtcon_port *port)
1199 struct virtqueue *vq;
1205 tp = port->vtcport_tty;
1206 vq = port->vtcport_invq;
1211 while ((buf = virtqueue_dequeue(vq, &len)) != NULL) {
1212 for (i = 0; i < len; i++) {
1214 if (port->vtcport_flags & VTCON_PORT_FLAG_CONSOLE)
1215 kdb_alt_break(buf[i],
1216 &port->vtcport_alt_break_state);
1218 ttydisc_rint(tp, buf[i], 0);
1220 vtcon_port_requeue_buf(port, buf);
1223 ttydisc_rint_done(tp);
1226 virtqueue_notify(vq);
1228 if (virtqueue_enable_intr(vq) != 0)
1233 vtcon_port_intr(void *scportx)
1235 struct vtcon_softc_port *scport;
1236 struct vtcon_softc *sc;
1237 struct vtcon_port *port;
1240 sc = scport->vcsp_sc;
1243 port = scport->vcsp_port;
1248 VTCON_PORT_LOCK(port);
1250 if ((port->vtcport_flags & VTCON_PORT_FLAG_GONE) == 0)
1251 vtcon_port_in(port);
1252 VTCON_PORT_UNLOCK(port);
1256 vtcon_port_out(struct vtcon_port *port, void *buf, int bufsize)
1258 struct sglist_seg segs[2];
1260 struct virtqueue *vq;
1263 vq = port->vtcport_outvq;
1264 KASSERT(virtqueue_empty(vq),
1265 ("%s: port %p out virtqueue not emtpy", __func__, port));
1267 sglist_init(&sg, 2, segs);
1268 error = sglist_append(&sg, buf, bufsize);
1269 KASSERT(error == 0, ("%s: error %d adding buffer to sglist",
1272 error = virtqueue_enqueue(vq, buf, &sg, sg.sg_nseg, 0);
1274 virtqueue_notify(vq);
1275 virtqueue_poll(vq, NULL);
1280 vtcon_port_submit_event(struct vtcon_port *port, uint16_t event,
1283 struct vtcon_softc *sc;
1285 sc = port->vtcport_sc;
1287 vtcon_ctrl_send_control(sc, port->vtcport_id, event, value);
1291 vtcon_tty_open(struct tty *tp)
1293 struct vtcon_port *port;
1295 port = tty_softc(tp);
1297 if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1300 vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1306 vtcon_tty_close(struct tty *tp)
1308 struct vtcon_port *port;
1310 port = tty_softc(tp);
1312 if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1315 vtcon_port_submit_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1319 vtcon_tty_outwakeup(struct tty *tp)
1321 struct vtcon_port *port;
1322 char buf[VTCON_BULK_BUFSZ];
1325 port = tty_softc(tp);
1327 if (port->vtcport_flags & VTCON_PORT_FLAG_GONE)
1330 while ((len = ttydisc_getc(tp, buf, sizeof(buf))) != 0)
1331 vtcon_port_out(port, buf, len);
1335 vtcon_tty_free(void *xport)
1337 struct vtcon_port *port;
1341 vtcon_port_destroy(port);
1342 atomic_subtract_int(&vtcon_pending_free, 1);
1346 vtcon_get_console_size(struct vtcon_softc *sc, uint16_t *cols, uint16_t *rows)
1348 struct virtio_console_config concfg;
1350 KASSERT(sc->vtcon_flags & VTCON_FLAG_SIZE,
1351 ("%s: size feature not negotiated", __func__));
1353 vtcon_read_config(sc, &concfg);
1355 *cols = concfg.cols;
1356 *rows = concfg.rows;
1360 vtcon_enable_interrupts(struct vtcon_softc *sc)
1362 struct vtcon_softc_port *scport;
1363 struct vtcon_port *port;
1368 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
1369 virtqueue_enable_intr(sc->vtcon_ctrl_rxvq);
1371 for (i = 0; i < sc->vtcon_max_ports; i++) {
1372 scport = &sc->vtcon_ports[i];
1374 port = scport->vcsp_port;
1378 VTCON_PORT_LOCK(port);
1379 vtcon_port_enable_intr(port);
1380 VTCON_PORT_UNLOCK(port);
1387 vtcon_disable_interrupts(struct vtcon_softc *sc)
1389 struct vtcon_softc_port *scport;
1390 struct vtcon_port *port;
1393 VTCON_LOCK_ASSERT(sc);
1395 if (sc->vtcon_flags & VTCON_FLAG_MULTIPORT)
1396 virtqueue_disable_intr(sc->vtcon_ctrl_rxvq);
1398 for (i = 0; i < sc->vtcon_max_ports; i++) {
1399 scport = &sc->vtcon_ports[i];
1401 port = scport->vcsp_port;
1405 VTCON_PORT_LOCK(port);
1406 vtcon_port_disable_intr(port);
1407 VTCON_PORT_UNLOCK(port);