summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Fritsch <sf@cvs.openbsd.org>2015-12-21 22:15:54 +0000
committerStefan Fritsch <sf@cvs.openbsd.org>2015-12-21 22:15:54 +0000
commit4a5b9623ccbe4632ccd0f7c8f65419dd08900017 (patch)
treebdb55f5b41f934aca0d91d2acebebf6d1de94cd6
parent8c5eba112685438d66036673fc3a015feb88fb92 (diff)
add virtio-console driver
This patch adds a new driver for use of virtio-console devices as ttys. It's still in an early state and not compiled by default, yet. Currently it is only wired into amd64. i386 is still missing. Discussed with uebayasi@, deraadt@
-rw-r--r--etc/MAKEDEV.common4
-rw-r--r--etc/etc.amd64/MAKEDEV.md4
-rw-r--r--share/man/man4/Makefile4
-rw-r--r--share/man/man4/viocon.457
-rw-r--r--sys/arch/amd64/amd64/conf.c5
-rw-r--r--sys/arch/amd64/conf/GENERIC3
-rw-r--r--sys/dev/pci/files.pci6
-rw-r--r--sys/dev/pci/viocon.c618
8 files changed, 694 insertions, 7 deletions
diff --git a/etc/MAKEDEV.common b/etc/MAKEDEV.common
index 1f03b7636c0..92cce531a1a 100644
--- a/etc/MAKEDEV.common
+++ b/etc/MAKEDEV.common
@@ -1,4 +1,4 @@
-vers(a, {-$OpenBSD: MAKEDEV.common,v 1.82 2015/12/09 04:11:58 deraadt Exp $-})dnl
+vers(a, {-$OpenBSD: MAKEDEV.common,v 1.83 2015/12/21 22:15:53 sf Exp $-})dnl
dnl
dnl Copyright (c) 2001-2006 Todd T. Fries <todd@OpenBSD.org>
dnl
@@ -383,6 +383,8 @@ _mkdev(com, {-tty[0-7][0-9a-f]-}, {-U=${i#tty*}
M tty$U c major_com_c $o 660 dialer uucp
M cua$U c major_com_c Add($o, 128) 660 dialer uucp-})dnl
__devitem(ttyc, ttyc*, Cyclades serial ports,cy)dnl
+__devitem(ttyVI, ttyVI*, Virtio serial ports,viocon)dnl
+_mkdev(ttyVI, ttyVI*, {-M ttyVI$U c major_ttyVI_c $U 660 dialer uucp-})dnl
__devitem(tzs, tty[a-z]*, Zilog 8530 Serial Port,zs)dnl
_mkdev(tzs, {-tty[a-z]-}, {-u=${i#tty*}
case $u in
diff --git a/etc/etc.amd64/MAKEDEV.md b/etc/etc.amd64/MAKEDEV.md
index 390867ce853..104a91bc449 100644
--- a/etc/etc.amd64/MAKEDEV.md
+++ b/etc/etc.amd64/MAKEDEV.md
@@ -1,6 +1,6 @@
define(MACHINE,amd64)dnl
vers(__file__,
- {-$OpenBSD: MAKEDEV.md,v 1.61 2015/11/10 21:42:28 sthen Exp $-},
+ {-$OpenBSD: MAKEDEV.md,v 1.62 2015/12/21 22:15:53 sf Exp $-},
etc.MACHINE)dnl
dnl
dnl Copyright (c) 2001-2006 Todd T. Fries <todd@OpenBSD.org>
@@ -39,6 +39,7 @@ _DEV(st, 14, 5)
_TITLE(term)
_DEV(com, 8)
_DEV(ttyc, 38)
+_DEV(ttyVI, 94)
_TITLE(pty)
_DEV(ptm, 81)
_DEV(pty, 6)
@@ -130,5 +131,6 @@ target(all, bktr, 0)dnl
target(all, gpio, 0, 1, 2)dnl
target(all, nvram)dnl
target(all, drm, 0, 1, 2, 3)dnl
+target(all, ttyVI, 00, 10, 20, 30, 40)dnl
twrget(ramd, wsdisp, ttyC, 0)dnl
target(ramd, fd, 0)dnl
diff --git a/share/man/man4/Makefile b/share/man/man4/Makefile
index 19f64b98456..821de462c70 100644
--- a/share/man/man4/Makefile
+++ b/share/man/man4/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.608 2015/12/09 00:34:09 mikeb Exp $
+# $OpenBSD: Makefile,v 1.609 2015/12/21 22:15:53 sf Exp $
MAN= aac.4 ac97.4 acphy.4 \
acpi.4 acpiac.4 acpiasus.4 acpibat.4 acpibtn.4 acpicpu.4 acpidock.4 \
@@ -68,7 +68,7 @@ MAN= aac.4 ac97.4 acphy.4 \
uvscom.4 \
vether.4 vga.4 vgafb.4 vge.4 \
viapm.4 viasio.4 vic.4 video.4 vio.4 \
- vioblk.4 viomb.4 viornd.4 vioscsi.4 virtio.4 vlan.4 \
+ vioblk.4 viocon.4 viomb.4 viornd.4 vioscsi.4 virtio.4 vlan.4 \
vmt.4 vmwpvs.4 vmx.4 vnd.4 vr.4 \
vscsi.4 vte.4 vxlan.4 \
watchdog.4 wb.4 wbenv.4 wbng.4 wbsd.4 wbsio.4 wd.4 wdc.4 wdsc.4 we.4 \
diff --git a/share/man/man4/viocon.4 b/share/man/man4/viocon.4
new file mode 100644
index 00000000000..959ec255ae9
--- /dev/null
+++ b/share/man/man4/viocon.4
@@ -0,0 +1,57 @@
+.\" $OpenBSD: viocon.4,v 1.1 2015/12/21 22:15:53 sf Exp $
+.\"
+.\" Copyright (c) 2015 Stefan Fritsch <sf@sfritsch.de>
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd $Mdocdate: December 21 2015 $
+.Dt VIOCON 4
+.Os
+.Sh NAME
+.Nm viocon
+.Nd VirtIO console device
+.Sh SYNOPSIS
+.Cd "viocon* at virtio?"
+.Sh DESCRIPTION
+The
+.Nm
+driver provides support for the
+.Xr virtio 4
+console interface provided by KVM, QEMU, and others.
+.Pp
+It provides serial ports that are attached as ttys.
+Use as a kernel console for
+.Ox
+is not yet supported.
+The multiport feature is not yet supported.
+.Pp
+.Sh FILES
+.Bl -tag -width Pa -compact
+.It Pa /dev/ttyVI00
+.It Pa /dev/ttyVI10
+.El
+.Sh SEE ALSO
+.Xr intro 4 ,
+.Xr virtio 4 ,
+.Xr tty 4
+.Sh HISTORY
+The
+.Nm
+driver first appeared in
+.Ox 5.9 .
+.Sh AUTHORS
+.An -nosplit
+The
+.Nm
+driver was written by
+.An Stefan Fritsch Aq Mt sf@sfritsch.de .
diff --git a/sys/arch/amd64/amd64/conf.c b/sys/arch/amd64/amd64/conf.c
index 0c9c20d898e..2289de94790 100644
--- a/sys/arch/amd64/amd64/conf.c
+++ b/sys/arch/amd64/amd64/conf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: conf.c,v 1.52 2015/11/13 07:52:20 mlarkin Exp $ */
+/* $OpenBSD: conf.c,v 1.53 2015/12/21 22:15:53 sf Exp $ */
/*
* Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
@@ -165,6 +165,8 @@ cdev_decl(nvram);
cdev_decl(drm);
#include "vmm.h"
cdev_decl(vmm);
+#include "viocon.h"
+cdev_decl(viocon);
#include "wsdisplay.h"
#include "wskbd.h"
@@ -291,6 +293,7 @@ struct cdevsw cdevsw[] =
cdev_pppx_init(NPPPX,pppx), /* 91: pppx */
cdev_fuse_init(NFUSE,fuse), /* 92: fuse */
cdev_tun_init(NTUN,tap), /* 93: Ethernet network tunnel */
+ cdev_tty_init(NVIOCON,viocon), /* 94: virtio console */
};
int nchrdev = nitems(cdevsw);
diff --git a/sys/arch/amd64/conf/GENERIC b/sys/arch/amd64/conf/GENERIC
index 072a4e8fc63..3fe8927d3f9 100644
--- a/sys/arch/amd64/conf/GENERIC
+++ b/sys/arch/amd64/conf/GENERIC
@@ -1,4 +1,4 @@
-# $OpenBSD: GENERIC,v 1.404 2015/12/08 22:05:31 mikeb Exp $
+# $OpenBSD: GENERIC,v 1.405 2015/12/21 22:15:53 sf Exp $
#
# For further information on compiling OpenBSD kernels, see the config(8)
# man page.
@@ -624,3 +624,4 @@ vio* at virtio? # Virtio network device
viomb* at virtio? # Virtio memory ballooning device
viornd* at virtio? # Virtio entropy device
vioscsi* at virtio? # Virtio SCSI device
+#viocon* at virtio? # Virtio console device
diff --git a/sys/dev/pci/files.pci b/sys/dev/pci/files.pci
index a29c3955dbf..0629f6bdb95 100644
--- a/sys/dev/pci/files.pci
+++ b/sys/dev/pci/files.pci
@@ -1,4 +1,4 @@
-# $OpenBSD: files.pci,v 1.320 2015/12/08 22:04:07 mikeb Exp $
+# $OpenBSD: files.pci,v 1.321 2015/12/21 22:15:53 sf Exp $
# $NetBSD: files.pci,v 1.20 1996/09/24 17:47:15 christos Exp $
#
# Config file and device description for machine-independent PCI code.
@@ -822,6 +822,10 @@ device vioblk: scsi
attach vioblk at virtio
file dev/pci/vioblk.c vioblk
+device viocon
+attach viocon at virtio
+file dev/pci/viocon.c viocon needs-flag
+
device viomb
attach viomb at virtio
file dev/pci/viomb.c viomb
diff --git a/sys/dev/pci/viocon.c b/sys/dev/pci/viocon.c
new file mode 100644
index 00000000000..cb8a3228a24
--- /dev/null
+++ b/sys/dev/pci/viocon.c
@@ -0,0 +1,618 @@
+/* $OpenBSD: viocon.c,v 1.1 2015/12/21 22:15:53 sf Exp $ */
+
+/*
+ * Copyright (c) 2013-2015 Stefan Fritsch <sf@sfritsch.de>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/timeout.h>
+#include <machine/bus.h>
+#include <sys/device.h>
+#include <sys/conf.h>
+#include <sys/tty.h>
+#include <dev/pci/pcivar.h>
+#include <dev/pci/virtioreg.h>
+#include <dev/pci/virtiovar.h>
+
+
+/* features */
+#define VIRTIO_CONSOLE_F_SIZE (1<<0)
+#define VIRTIO_CONSOLE_F_MULTIPORT (1<<1)
+#define VIRTIO_CONSOLE_F_EMERG_WRITE (1<<2)
+
+/* config space */
+#define VIRTIO_CONSOLE_COLS 0 /* 16 bits */
+#define VIRTIO_CONSOLE_ROWS 2 /* 16 bits */
+#define VIRTIO_CONSOLE_MAX_NR_PORTS 4 /* 32 bits */
+#define VIRTIO_CONSOLE_EMERG_WR 8 /* 32 bits */
+
+#define VIOCON_DEBUG 0
+
+#if VIOCON_DEBUG
+#define DBGPRINT(fmt, args...) printf("%s: " fmt "\n", __func__, ## args)
+#else
+#define DBGPRINT(fmt, args...)
+#endif
+
+struct virtio_feature_name viocon_feature_names[] = {
+ { VIRTIO_CONSOLE_F_SIZE, "Size" },
+ { VIRTIO_CONSOLE_F_MULTIPORT, "MultiPort" },
+ { VIRTIO_CONSOLE_F_EMERG_WRITE, "EmergWrite" },
+ { 0, NULL },
+};
+
+struct virtio_console_control {
+ uint32_t id; /* Port number */
+
+#define VIRTIO_CONSOLE_DEVICE_READY 0
+#define VIRTIO_CONSOLE_PORT_ADD 1
+#define VIRTIO_CONSOLE_PORT_REMOVE 2
+#define VIRTIO_CONSOLE_PORT_READY 3
+#define VIRTIO_CONSOLE_CONSOLE_PORT 4
+#define VIRTIO_CONSOLE_RESIZE 5
+#define VIRTIO_CONSOLE_PORT_OPEN 6
+#define VIRTIO_CONSOLE_PORT_NAME 7
+ uint16_t event;
+
+ uint16_t value;
+};
+
+struct virtio_console_control_resize {
+ /* yes, the order is different than in config space */
+ uint16_t rows;
+ uint16_t cols;
+};
+
+#define BUFSIZE 128
+CTASSERT(BUFSIZE < TTHIWATMINSPACE);
+
+#define VIOCONUNIT(x) (minor(x) >> 4)
+#define VIOCONPORT(x) (minor(x) & 0x0f)
+
+struct viocon_port {
+ struct viocon_softc *vp_sc;
+ struct virtqueue *vp_rx;
+ struct virtqueue *vp_tx;
+ void *vp_si;
+ struct tty *vp_tty;
+ const char *vp_name;
+ bus_dma_segment_t vp_dmaseg;
+ bus_dmamap_t vp_dmamap;
+#ifdef NOTYET
+ unsigned int vp_host_open:1; /* XXX needs F_MULTIPORT */
+ unsigned int vp_guest_open:1; /* XXX needs F_MULTIPORT */
+ unsigned int vp_is_console:1; /* XXX needs F_MULTIPORT */
+#endif
+ unsigned int vp_iflow:1; /* rx flow control */
+ uint16_t vp_rows;
+ uint16_t vp_cols;
+ u_char *vp_rx_buf;
+ u_char *vp_tx_buf;
+};
+
+struct viocon_softc {
+ struct device sc_dev;
+ struct virtio_softc *sc_virtio;
+
+ struct virtqueue *sc_c_vq_rx;
+ struct virtqueue *sc_c_vq_tx;
+
+ unsigned int sc_max_ports;
+ struct viocon_port **sc_ports;
+
+ bus_dmamap_t sc_dmamap;
+};
+
+int viocon_match(struct device *, void *, void *);
+void viocon_attach(struct device *, struct device *, void *);
+int viocon_tx_intr(struct virtqueue *);
+int viocon_tx_drain(struct viocon_port *, struct virtqueue *vq);
+int viocon_rx_intr(struct virtqueue *);
+void viocon_rx_soft(void *);
+void viocon_rx_fill(struct viocon_port *);
+int viocon_port_create(struct viocon_softc *, int);
+void vioconstart(struct tty *);
+int vioconhwiflow(struct tty *, int);
+int vioconparam(struct tty *, struct termios *);
+int vioconopen(dev_t, int, int, struct proc *);
+int vioconclose(dev_t, int, int, struct proc *);
+int vioconread(dev_t, struct uio *, int);
+int vioconwrite(dev_t, struct uio *, int);
+int vioconstop(struct tty *, int);
+int vioconioctl(dev_t, u_long, caddr_t, int, struct proc *);
+struct tty *viocontty(dev_t dev);
+
+struct cfattach viocon_ca = {
+ sizeof(struct viocon_softc),
+ viocon_match,
+ viocon_attach,
+ NULL
+};
+
+struct cfdriver viocon_cd = {
+ NULL, "viocon", DV_TTY
+};
+
+static inline struct viocon_softc *
+dev2sc(dev_t dev)
+{
+ return viocon_cd.cd_devs[VIOCONUNIT(dev)];
+}
+
+static inline struct viocon_port *
+dev2port(dev_t dev)
+{
+ return dev2sc(dev)->sc_ports[VIOCONPORT(dev)];
+}
+
+int viocon_match(struct device *parent, void *match, void *aux)
+{
+ struct virtio_softc *va = aux;
+ if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_CONSOLE)
+ return 1;
+ return 0;
+}
+
+void
+viocon_attach(struct device *parent, struct device *self, void *aux)
+{
+ struct viocon_softc *sc = (struct viocon_softc *)self;
+ struct virtio_softc *vsc = (struct virtio_softc *)parent;
+ int maxports = 1;
+
+ if (vsc->sc_child)
+ panic("already attached to something else");
+ vsc->sc_child = self;
+ vsc->sc_ipl = IPL_TTY;
+ vsc->sc_intrhand = virtio_vq_intr;
+ vsc->sc_config_change = 0;
+ sc->sc_virtio = vsc;
+ sc->sc_max_ports = maxports;
+
+ vsc->sc_vqs = malloc(2 * (maxports + 1) * sizeof(struct virtqueue), M_DEVBUF,
+ M_WAITOK|M_CANFAIL|M_ZERO);
+ sc->sc_ports = malloc(maxports * sizeof(sc->sc_ports[0]), M_DEVBUF,
+ M_WAITOK|M_CANFAIL|M_ZERO);
+ if (vsc->sc_vqs == NULL || sc->sc_ports == NULL) {
+ printf("\n%s: Cannot allocate memory\n", __func__);
+ goto err;
+ }
+
+ virtio_negotiate_features(vsc, VIRTIO_CONSOLE_F_SIZE,
+ viocon_feature_names);
+
+ printf("\n");
+ DBGPRINT("softc: %p\n", sc);
+ if (viocon_port_create(sc, 0) != 0) {
+ printf("\n%s: viocon_port_create failed\n", __func__);
+ goto err;
+ }
+ viocon_rx_fill(sc->sc_ports[0]);
+
+ return;
+err:
+ vsc->sc_child = VIRTIO_CHILD_ERROR;
+ if (vsc->sc_vqs)
+ free(vsc->sc_vqs, M_DEVBUF, 2 * (maxports + 1) * sizeof(struct virtqueue));
+ if (sc->sc_ports)
+ free(sc->sc_ports, M_DEVBUF, maxports * sizeof(sc->sc_ports[0]));
+}
+
+int
+viocon_port_create(struct viocon_softc *sc, int portidx)
+{
+ struct virtio_softc *vsc = sc->sc_virtio;
+ int rxidx, txidx, allocsize, nsegs;
+ char name[6];
+ struct viocon_port *vp;
+ caddr_t kva;
+ struct tty *tp;
+
+ vp = malloc(sizeof(*vp), M_DEVBUF, M_WAITOK|M_CANFAIL|M_ZERO);
+ if (vp == NULL)
+ return ENOMEM;
+ sc->sc_ports[portidx] = vp;
+ vp->vp_sc = sc;
+ DBGPRINT("vp: %p\n", vp);
+
+ if (portidx == 0)
+ rxidx = 0;
+ else
+ rxidx = 2 * (portidx + 1);
+ txidx = rxidx + 1;
+
+ snprintf(name, sizeof(name), "p%drx", portidx);
+ if (virtio_alloc_vq(vsc, &vsc->sc_vqs[rxidx], rxidx, BUFSIZE, 1,
+ name) != 0) {
+ printf("\nCan't alloc %s virtqueue\n", name);
+ goto err;
+ }
+ vp->vp_rx = &vsc->sc_vqs[rxidx];
+ vp->vp_rx->vq_done = viocon_rx_intr;
+ vp->vp_si = softintr_establish(IPL_TTY, viocon_rx_soft, vp);
+ DBGPRINT("rx: %p\n", vp->vp_rx);
+
+ snprintf(name, sizeof(name), "p%dtx", portidx);
+ if (virtio_alloc_vq(vsc, &vsc->sc_vqs[txidx], txidx, BUFSIZE, 1,
+ name) != 0) {
+ printf("\nCan't alloc %s virtqueue\n", name);
+ goto err;
+ }
+ vp->vp_tx = &vsc->sc_vqs[txidx];
+ vp->vp_tx->vq_done = viocon_tx_intr;
+ DBGPRINT("tx: %p\n", vp->vp_tx);
+
+ vsc->sc_nvqs += 2;
+
+ allocsize = (vp->vp_rx->vq_num + vp->vp_tx->vq_num) * BUFSIZE;
+
+ if (bus_dmamap_create(vsc->sc_dmat, allocsize, 1, allocsize, 0,
+ BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &vp->vp_dmamap) != 0)
+ goto err;
+ if (bus_dmamem_alloc(vsc->sc_dmat, allocsize, 8, 0, &vp->vp_dmaseg,
+ 1, &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO) != 0)
+ goto err;
+ if (bus_dmamem_map(vsc->sc_dmat, &vp->vp_dmaseg, nsegs,
+ allocsize, &kva, BUS_DMA_NOWAIT) != 0)
+ goto err;
+ if (bus_dmamap_load(vsc->sc_dmat, vp->vp_dmamap, kva,
+ allocsize, NULL, BUS_DMA_NOWAIT) != 0)
+ goto err;
+ vp->vp_rx_buf = (unsigned char *)kva;
+ /*
+ * XXX use only a small circular tx buffer instead of many BUFSIZE buffers?
+ */
+ vp->vp_tx_buf = vp->vp_rx_buf + vp->vp_rx->vq_num * BUFSIZE;
+
+ if (vsc->sc_features & VIRTIO_CONSOLE_F_SIZE) {
+ vp->vp_cols = virtio_read_device_config_2(vsc,
+ VIRTIO_CONSOLE_COLS);
+ vp->vp_rows = virtio_read_device_config_2(vsc,
+ VIRTIO_CONSOLE_ROWS);
+ }
+
+ tp = ttymalloc(1000000);
+ tp->t_oproc = vioconstart;
+ tp->t_param = vioconparam;
+ tp->t_hwiflow = vioconhwiflow;
+ tp->t_dev = (sc->sc_dev.dv_unit << 4) | portidx;
+ vp->vp_tty = tp;
+ DBGPRINT("tty: %p\n", tp);
+
+ virtio_start_vq_intr(vsc, vp->vp_rx);
+ virtio_start_vq_intr(vsc, vp->vp_tx);
+
+ return 0;
+err:
+ panic("%s failed", __func__);
+ return -1;
+}
+
+int
+viocon_tx_drain(struct viocon_port *vp, struct virtqueue *vq)
+{
+ struct virtio_softc *vsc = vq->vq_owner;
+ int ndone = 0, len, slot;
+
+ splassert(IPL_TTY);
+ while (virtio_dequeue(vsc, vq, &slot, &len) == 0) {
+ bus_dmamap_sync(vsc->sc_dmat, vp->vp_dmamap,
+ vp->vp_tx_buf - vp->vp_rx_buf + slot * BUFSIZE, BUFSIZE,
+ BUS_DMASYNC_POSTREAD);
+ virtio_dequeue_commit(vq, slot);
+ ndone++;
+ }
+ return ndone;
+}
+
+int
+viocon_tx_intr(struct virtqueue *vq)
+{
+ struct virtio_softc *vsc = vq->vq_owner;
+ struct viocon_softc *sc = (struct viocon_softc *)vsc->sc_child;
+ int ndone = 0;
+ int portidx = (vq->vq_index - 1) / 2;
+ struct viocon_port *vp = sc->sc_ports[portidx];
+ struct tty *tp = vp->vp_tty;
+
+ splassert(IPL_TTY);
+ ndone = viocon_tx_drain(vp, vq);
+ if (ndone && ISSET(tp->t_state, TS_BUSY)) {
+ CLR(tp->t_state, TS_BUSY);
+ linesw[tp->t_line].l_start(tp);
+ }
+
+ return 1;
+}
+
+void
+viocon_rx_fill(struct viocon_port *vp)
+{
+ struct virtqueue *vq = vp->vp_rx;
+ struct virtio_softc *vsc = vp->vp_sc->sc_virtio;
+ int r, slot, ndone = 0;
+
+ while ((r = virtio_enqueue_prep(vq, &slot)) == 0) {
+ if (virtio_enqueue_reserve(vq, slot, 1) != 0)
+ break;
+ bus_dmamap_sync(vsc->sc_dmat, vp->vp_dmamap, slot * BUFSIZE,
+ BUFSIZE, BUS_DMASYNC_PREREAD);
+ virtio_enqueue_p(vq, slot, vp->vp_dmamap, slot * BUFSIZE,
+ BUFSIZE, 0);
+ virtio_enqueue_commit(vsc, vq, slot, 0);
+ ndone++;
+ }
+ KASSERT(r == 0 || r == EAGAIN);
+ if (ndone > 0)
+ virtio_notify(vsc, vq);
+}
+
+int
+viocon_rx_intr(struct virtqueue *vq)
+{
+ struct virtio_softc *vsc = vq->vq_owner;
+ struct viocon_softc *sc = (struct viocon_softc *)vsc->sc_child;
+ int portidx = (vq->vq_index - 1) / 2;
+ struct viocon_port *vp = sc->sc_ports[portidx];
+
+ softintr_schedule(vp->vp_si);
+ return 1;
+}
+
+void
+viocon_rx_soft(void *arg)
+{
+ struct viocon_port *vp = arg;
+ struct virtqueue *vq = vp->vp_rx;
+ struct virtio_softc *vsc = vq->vq_owner;
+ struct tty *tp = vp->vp_tty;
+ int slot, len, i;
+ u_char *p;
+
+ while (!vp->vp_iflow && virtio_dequeue(vsc, vq, &slot, &len) == 0) {
+ bus_dmamap_sync(vsc->sc_dmat, vp->vp_dmamap,
+ slot * BUFSIZE, BUFSIZE, BUS_DMASYNC_POSTREAD);
+ p = vp->vp_rx_buf + slot * BUFSIZE;
+ for (i = 0; i < len; i++)
+ (*linesw[tp->t_line].l_rint)(*p++, tp);
+ virtio_dequeue_commit(vq, slot);
+ }
+
+ viocon_rx_fill(vp);
+
+ return;
+}
+
+void
+vioconstart(struct tty *tp)
+{
+ struct viocon_softc *sc = dev2sc(tp->t_dev);
+ struct virtio_softc *vsc;
+ struct viocon_port *vp = dev2port(tp->t_dev);
+ struct virtqueue *vq;
+ u_char *buf;
+ int s, cnt, slot, ret, ndone;
+
+ vsc = sc->sc_virtio;
+ vq = vp->vp_tx;
+
+ s = spltty();
+
+ ndone = viocon_tx_drain(vp, vq);
+ if (ISSET(tp->t_state, TS_BUSY)) {
+ if (ndone > 0)
+ CLR(tp->t_state, TS_BUSY);
+ else
+ goto out;
+ }
+ if (ISSET(tp->t_state, TS_TIMEOUT | TS_TTSTOP))
+ goto out;
+
+ if (tp->t_outq.c_cc == 0)
+ goto out;
+ ndone = 0;
+
+ while (tp->t_outq.c_cc > 0) {
+ ret = virtio_enqueue_prep(vq, &slot);
+ if (ret == EAGAIN)
+ break;
+ KASSERT(ret == 0);
+ ret = virtio_enqueue_reserve(vq, slot, 1);
+ KASSERT(ret == 0);
+ buf = vp->vp_tx_buf + slot * BUFSIZE;
+ cnt = q_to_b(&tp->t_outq, buf, BUFSIZE);
+ bus_dmamap_sync(vsc->sc_dmat, vp->vp_dmamap,
+ vp->vp_tx_buf - vp->vp_rx_buf + slot * BUFSIZE, cnt,
+ BUS_DMASYNC_PREWRITE);
+ virtio_enqueue_p(vq, slot, vp->vp_dmamap,
+ vp->vp_tx_buf - vp->vp_rx_buf + slot * BUFSIZE, cnt, 1);
+ virtio_enqueue_commit(vsc, vq, slot, 0);
+ ndone++;
+ }
+ if (ret == EAGAIN)
+ SET(tp->t_state, TS_BUSY);
+ if (ndone > 0)
+ virtio_notify(vsc, vq);
+ ttwakeupwr(tp);
+out:
+ splx(s);
+}
+
+int
+vioconhwiflow(struct tty *tp, int stop)
+{
+ struct viocon_port *vp = dev2port(tp->t_dev);
+ int s;
+
+ s = spltty();
+ vp->vp_iflow = stop;
+ if (stop) {
+ virtio_stop_vq_intr(vp->vp_sc->sc_virtio, vp->vp_rx);
+ } else {
+ virtio_start_vq_intr(vp->vp_sc->sc_virtio, vp->vp_rx);
+ softintr_schedule(vp->vp_si);
+ }
+ splx(s);
+ return 1;
+}
+
+int
+vioconparam(struct tty *tp, struct termios *t)
+{
+ tp->t_ispeed = t->c_ispeed;
+ tp->t_ospeed = t->c_ospeed;
+ tp->t_cflag = t->c_cflag;
+
+ vioconstart(tp);
+ return 0;
+}
+
+int
+vioconopen(dev_t dev, int flag, int mode, struct proc *p)
+{
+ int unit = VIOCONUNIT(dev);
+ int port = VIOCONPORT(dev);
+ struct viocon_softc *sc;
+ struct viocon_port *vp;
+ struct tty *tp;
+ int s, error;
+
+ if (unit >= viocon_cd.cd_ndevs)
+ return (ENXIO);
+ sc = viocon_cd.cd_devs[unit];
+ if (sc == NULL)
+ return (ENXIO);
+ if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
+ return (ENXIO);
+
+ s = spltty();
+ if (port >= sc->sc_max_ports) {
+ splx(s);
+ return (ENXIO);
+ }
+ vp = sc->sc_ports[port];
+ tp = vp->vp_tty;
+#ifdef NOTYET
+ vp->vp_guest_open = 1;
+#endif
+ splx(s);
+
+ if (!ISSET(tp->t_state, TS_ISOPEN)) {
+ SET(tp->t_state, TS_WOPEN);
+ ttychars(tp);
+ tp->t_ispeed = 1000000;
+ tp->t_ospeed = 1000000;
+ tp->t_cflag = TTYDEF_CFLAG|CLOCAL|CRTSCTS;
+ tp->t_iflag = TTYDEF_IFLAG;
+ tp->t_oflag = TTYDEF_OFLAG;
+ tp->t_lflag = TTYDEF_LFLAG;
+ if (vp->vp_cols != 0) {
+ tp->t_winsize.ws_col = vp->vp_cols;
+ tp->t_winsize.ws_row = vp->vp_rows;
+ }
+
+ s = spltty();
+ vioconparam(tp, &tp->t_termios);
+ ttsetwater(tp);
+ splx(s);
+ }
+ else if (ISSET(tp->t_state, TS_XCLUDE) && suser(p, 0) != 0) {
+ return (EBUSY);
+ }
+
+ error = linesw[tp->t_line].l_open(dev, tp, p);
+ return error;
+}
+
+int
+vioconclose(dev_t dev, int flag, int mode, struct proc *p)
+{
+ struct viocon_port *vp = dev2port(dev);
+ struct tty *tp = vp->vp_tty;
+ int s;
+
+ if (!ISSET(tp->t_state, TS_ISOPEN))
+ return 0;
+
+ linesw[tp->t_line].l_close(tp, flag, p);
+ s = spltty();
+#ifdef NOTYET
+ vp->vp_guest_open = 0;
+#endif
+ CLR(tp->t_state, TS_BUSY | TS_FLUSH);
+ ttyclose(tp);
+ splx(s);
+
+ return 0;
+}
+
+int
+vioconread(dev_t dev, struct uio *uio, int flag)
+{
+ struct viocon_port *vp = dev2port(dev);
+ struct tty *tp = vp->vp_tty;
+
+ return linesw[tp->t_line].l_read(tp, uio, flag);
+}
+
+int
+vioconwrite(dev_t dev, struct uio *uio, int flag)
+{
+ struct viocon_port *vp = dev2port(dev);
+ struct tty *tp = vp->vp_tty;
+
+ return linesw[tp->t_line].l_write(tp, uio, flag);
+}
+
+struct tty *
+viocontty(dev_t dev)
+{
+ struct viocon_port *vp = dev2port(dev);
+
+ return vp->vp_tty;
+}
+
+int
+vioconstop(struct tty *tp, int flag)
+{
+ int s;
+
+ s = spltty();
+ if (ISSET(tp->t_state, TS_BUSY))
+ if (!ISSET(tp->t_state, TS_TTSTOP))
+ SET(tp->t_state, TS_FLUSH);
+ splx(s);
+ return 0;
+}
+
+int
+vioconioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
+{
+ struct viocon_port *vp = dev2port(dev);
+ struct tty *tp;
+ int error1, error2;
+
+ tp = vp->vp_tty;
+
+ error1 = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
+ if (error1 >= 0)
+ return error1;
+ error2 = ttioctl(tp, cmd, data, flag, p);
+ if (error2 >= 0)
+ return error2;
+ return ENOTTY;
+}