Home | History | Annotate | Line # | Download | only in virtio
viocon.c revision 1.6
      1  1.6  yamaguch /*	$NetBSD: viocon.c,v 1.6 2023/03/23 03:27:48 yamaguchi Exp $	*/
      2  1.1  riastrad /*	$OpenBSD: viocon.c,v 1.8 2021/11/05 11:38:29 mpi Exp $	*/
      3  1.1  riastrad 
      4  1.1  riastrad /*
      5  1.1  riastrad  * Copyright (c) 2013-2015 Stefan Fritsch <sf (at) sfritsch.de>
      6  1.1  riastrad  *
      7  1.1  riastrad  * Permission to use, copy, modify, and distribute this software for any
      8  1.1  riastrad  * purpose with or without fee is hereby granted, provided that the above
      9  1.1  riastrad  * copyright notice and this permission notice appear in all copies.
     10  1.1  riastrad  *
     11  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  1.1  riastrad  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  1.1  riastrad  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  1.1  riastrad  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  1.1  riastrad  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  1.1  riastrad  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  1.1  riastrad  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  1.1  riastrad  */
     19  1.1  riastrad 
     20  1.1  riastrad #include <sys/cdefs.h>
     21  1.6  yamaguch __KERNEL_RCSID(0, "$NetBSD: viocon.c,v 1.6 2023/03/23 03:27:48 yamaguchi Exp $");
     22  1.1  riastrad 
     23  1.1  riastrad #include <sys/param.h>
     24  1.1  riastrad #include <sys/types.h>
     25  1.1  riastrad 
     26  1.1  riastrad #include <sys/bus.h>
     27  1.1  riastrad #include <sys/conf.h>
     28  1.1  riastrad #include <sys/device.h>
     29  1.1  riastrad #include <sys/kauth.h>
     30  1.1  riastrad #include <sys/kernel.h>
     31  1.1  riastrad #include <sys/kmem.h>
     32  1.1  riastrad #include <sys/lwp.h>
     33  1.1  riastrad #include <sys/systm.h>
     34  1.1  riastrad #include <sys/tty.h>
     35  1.1  riastrad 
     36  1.1  riastrad #include <dev/pci/virtioreg.h>
     37  1.1  riastrad #include <dev/pci/virtiovar.h>
     38  1.1  riastrad 
     39  1.1  riastrad #include "ioconf.h"
     40  1.1  riastrad 
     41  1.1  riastrad /* OpenBSD compat shims */
     42  1.1  riastrad #define	ttymalloc(speed)	tty_alloc()
     43  1.1  riastrad #define	splassert(ipl)		__nothing
     44  1.1  riastrad #define	virtio_notify(vsc, vq)	virtio_enqueue_commit(vsc, vq, -1, true)
     45  1.1  riastrad #define	ttwakeupwr(tp)		__nothing
     46  1.1  riastrad 
     47  1.1  riastrad /* features */
     48  1.1  riastrad #define	VIRTIO_CONSOLE_F_SIZE		(1ULL<<0)
     49  1.1  riastrad #define	VIRTIO_CONSOLE_F_MULTIPORT	(1ULL<<1)
     50  1.1  riastrad #define	VIRTIO_CONSOLE_F_EMERG_WRITE 	(1ULL<<2)
     51  1.1  riastrad 
     52  1.1  riastrad /* config space */
     53  1.1  riastrad #define VIRTIO_CONSOLE_COLS		0	/* 16 bits */
     54  1.1  riastrad #define VIRTIO_CONSOLE_ROWS		2	/* 16 bits */
     55  1.1  riastrad #define VIRTIO_CONSOLE_MAX_NR_PORTS	4	/* 32 bits */
     56  1.1  riastrad #define VIRTIO_CONSOLE_EMERG_WR		8	/* 32 bits */
     57  1.1  riastrad 
     58  1.1  riastrad #define VIOCON_DEBUG	0
     59  1.1  riastrad 
     60  1.1  riastrad #if VIOCON_DEBUG
     61  1.1  riastrad #define DPRINTF(x...) printf(x)
     62  1.1  riastrad #else
     63  1.1  riastrad #define DPRINTF(x...)
     64  1.1  riastrad #endif
     65  1.1  riastrad 
     66  1.1  riastrad #define	VIRTIO_CONSOLE_FLAG_BITS					      \
     67  1.1  riastrad 	VIRTIO_COMMON_FLAG_BITS						      \
     68  1.1  riastrad 	"b\x00" "SIZE\0"						      \
     69  1.1  riastrad 	"b\x01" "MULTIPORT\0"						      \
     70  1.1  riastrad 	"b\x02" "EMERG_WRITE\0"
     71  1.1  riastrad 
     72  1.1  riastrad struct virtio_console_control {
     73  1.1  riastrad 	uint32_t id;	/* Port number */
     74  1.1  riastrad 
     75  1.1  riastrad #define	VIRTIO_CONSOLE_DEVICE_READY	0
     76  1.1  riastrad #define	VIRTIO_CONSOLE_PORT_ADD		1
     77  1.1  riastrad #define	VIRTIO_CONSOLE_PORT_REMOVE	2
     78  1.1  riastrad #define	VIRTIO_CONSOLE_PORT_READY	3
     79  1.1  riastrad #define	VIRTIO_CONSOLE_CONSOLE_PORT	4
     80  1.1  riastrad #define	VIRTIO_CONSOLE_RESIZE		5
     81  1.1  riastrad #define	VIRTIO_CONSOLE_PORT_OPEN	6
     82  1.1  riastrad #define	VIRTIO_CONSOLE_PORT_NAME	7
     83  1.1  riastrad 	uint16_t event;
     84  1.1  riastrad 
     85  1.1  riastrad 	uint16_t value;
     86  1.1  riastrad };
     87  1.1  riastrad 
     88  1.1  riastrad struct virtio_console_control_resize {
     89  1.1  riastrad 	/* yes, the order is different than in config space */
     90  1.1  riastrad 	uint16_t rows;
     91  1.1  riastrad 	uint16_t cols;
     92  1.1  riastrad };
     93  1.1  riastrad 
     94  1.1  riastrad #define	BUFSIZE		128
     95  1.1  riastrad 
     96  1.2  riastrad #define	VIOCONDEV(u,p)	makedev(cdevsw_lookup_major(&viocon_cdevsw),	      \
     97  1.2  riastrad 			    ((u) << 4) | (p))
     98  1.1  riastrad #define VIOCONUNIT(x)	(minor(x) >> 4)
     99  1.1  riastrad #define VIOCONPORT(x)	(minor(x) & 0x0f)
    100  1.1  riastrad 
    101  1.1  riastrad struct viocon_port {
    102  1.1  riastrad 	struct viocon_softc	*vp_sc;
    103  1.1  riastrad 	struct virtqueue	*vp_rx;
    104  1.1  riastrad 	struct virtqueue	*vp_tx;
    105  1.1  riastrad 	void			*vp_si;
    106  1.1  riastrad 	struct tty		*vp_tty;
    107  1.1  riastrad 	const char 		*vp_name;
    108  1.1  riastrad 	bus_dma_segment_t	 vp_dmaseg;
    109  1.1  riastrad 	bus_dmamap_t		 vp_dmamap;
    110  1.1  riastrad #ifdef NOTYET
    111  1.1  riastrad 	unsigned int		 vp_host_open:1;	/* XXX needs F_MULTIPORT */
    112  1.1  riastrad 	unsigned int		 vp_guest_open:1;	/* XXX needs F_MULTIPORT */
    113  1.1  riastrad 	unsigned int		 vp_is_console:1;	/* XXX needs F_MULTIPORT */
    114  1.1  riastrad #endif
    115  1.1  riastrad 	unsigned int		 vp_iflow:1;		/* rx flow control */
    116  1.1  riastrad 	uint16_t		 vp_rows;
    117  1.1  riastrad 	uint16_t		 vp_cols;
    118  1.1  riastrad 	u_char			*vp_rx_buf;
    119  1.1  riastrad 	u_char			*vp_tx_buf;
    120  1.1  riastrad };
    121  1.1  riastrad 
    122  1.1  riastrad struct viocon_softc {
    123  1.1  riastrad 	struct device		*sc_dev;
    124  1.1  riastrad 	struct virtio_softc	*sc_virtio;
    125  1.1  riastrad 	struct virtqueue	*sc_vqs;
    126  1.1  riastrad 
    127  1.1  riastrad 	struct virtqueue        *sc_c_vq_rx;
    128  1.1  riastrad 	struct virtqueue        *sc_c_vq_tx;
    129  1.1  riastrad 
    130  1.1  riastrad 	unsigned int		 sc_max_ports;
    131  1.1  riastrad 	struct viocon_port	**sc_ports;
    132  1.1  riastrad };
    133  1.1  riastrad 
    134  1.1  riastrad int	viocon_match(struct device *, struct cfdata *, void *);
    135  1.1  riastrad void	viocon_attach(struct device *, struct device *, void *);
    136  1.1  riastrad int	viocon_tx_intr(struct virtqueue *);
    137  1.1  riastrad int	viocon_tx_drain(struct viocon_port *, struct virtqueue *vq);
    138  1.1  riastrad int	viocon_rx_intr(struct virtqueue *);
    139  1.1  riastrad void	viocon_rx_soft(void *);
    140  1.1  riastrad void	viocon_rx_fill(struct viocon_port *);
    141  1.1  riastrad int	viocon_port_create(struct viocon_softc *, int);
    142  1.1  riastrad void	vioconstart(struct tty *);
    143  1.1  riastrad int	vioconhwiflow(struct tty *, int);
    144  1.1  riastrad int	vioconparam(struct tty *, struct termios *);
    145  1.1  riastrad int	vioconopen(dev_t, int, int, struct lwp *);
    146  1.1  riastrad int	vioconclose(dev_t, int, int, struct lwp *);
    147  1.1  riastrad int	vioconread(dev_t, struct uio *, int);
    148  1.1  riastrad int	vioconwrite(dev_t, struct uio *, int);
    149  1.1  riastrad void	vioconstop(struct tty *, int);
    150  1.1  riastrad int	vioconioctl(dev_t, u_long, void *, int, struct lwp *);
    151  1.1  riastrad struct tty	*viocontty(dev_t dev);
    152  1.1  riastrad 
    153  1.1  riastrad CFATTACH_DECL_NEW(viocon, sizeof(struct viocon_softc),
    154  1.1  riastrad     viocon_match, viocon_attach, /*detach*/NULL, /*activate*/NULL);
    155  1.1  riastrad 
    156  1.1  riastrad const struct cdevsw viocon_cdevsw = {
    157  1.1  riastrad 	.d_open = vioconopen,
    158  1.1  riastrad 	.d_close = vioconclose,
    159  1.1  riastrad 	.d_read = vioconread,
    160  1.1  riastrad 	.d_write = vioconwrite,
    161  1.1  riastrad 	.d_ioctl = vioconioctl,
    162  1.1  riastrad 	.d_stop = vioconstop,
    163  1.1  riastrad 	.d_tty = viocontty,
    164  1.1  riastrad 	.d_poll = nopoll,	/* XXX */
    165  1.1  riastrad 	.d_mmap = nommap,
    166  1.1  riastrad 	.d_kqfilter = ttykqfilter,
    167  1.1  riastrad 	.d_discard = nodiscard,
    168  1.1  riastrad 	.d_flag = D_TTY,
    169  1.1  riastrad };
    170  1.1  riastrad 
    171  1.1  riastrad static inline struct viocon_softc *
    172  1.1  riastrad dev2sc(dev_t dev)
    173  1.1  riastrad {
    174  1.1  riastrad 	return device_lookup_private(&viocon_cd, VIOCONUNIT(dev));
    175  1.1  riastrad }
    176  1.1  riastrad 
    177  1.1  riastrad static inline struct viocon_port *
    178  1.1  riastrad dev2port(dev_t dev)
    179  1.1  riastrad {
    180  1.1  riastrad 	return dev2sc(dev)->sc_ports[VIOCONPORT(dev)];
    181  1.1  riastrad }
    182  1.1  riastrad 
    183  1.1  riastrad int viocon_match(struct device *parent, struct cfdata *match, void *aux)
    184  1.1  riastrad {
    185  1.1  riastrad 	struct virtio_attach_args *va = aux;
    186  1.1  riastrad 	if (va->sc_childdevid == VIRTIO_DEVICE_ID_CONSOLE)
    187  1.1  riastrad 		return 1;
    188  1.1  riastrad 	return 0;
    189  1.1  riastrad }
    190  1.1  riastrad 
    191  1.1  riastrad void
    192  1.1  riastrad viocon_attach(struct device *parent, struct device *self, void *aux)
    193  1.1  riastrad {
    194  1.1  riastrad 	struct viocon_softc *sc = device_private(self);
    195  1.1  riastrad 	struct virtio_softc *vsc = device_private(parent);
    196  1.1  riastrad 	int maxports = 1;
    197  1.1  riastrad 
    198  1.1  riastrad 	sc->sc_dev = self;
    199  1.1  riastrad 	if (virtio_child(vsc) != NULL) {
    200  1.1  riastrad 		aprint_error(": parent %s already has a child\n",
    201  1.1  riastrad 		    device_xname(parent));
    202  1.1  riastrad 		return;
    203  1.1  riastrad 	}
    204  1.1  riastrad 	sc->sc_virtio = vsc;
    205  1.1  riastrad 	sc->sc_max_ports = maxports;
    206  1.1  riastrad 
    207  1.1  riastrad 	sc->sc_vqs = kmem_zalloc(2 * (maxports + 1) * sizeof(sc->sc_vqs[0]),
    208  1.1  riastrad 	    KM_SLEEP);
    209  1.1  riastrad 	sc->sc_ports = kmem_zalloc(maxports * sizeof(sc->sc_ports[0]),
    210  1.1  riastrad 	    KM_SLEEP);
    211  1.1  riastrad 
    212  1.6  yamaguch 	virtio_child_attach_start(vsc, self, IPL_TTY,
    213  1.6  yamaguch 	    /*req_features*/VIRTIO_CONSOLE_F_SIZE, VIRTIO_CONSOLE_FLAG_BITS);
    214  1.1  riastrad 
    215  1.1  riastrad 	DPRINTF("%s: softc: %p\n", __func__, sc);
    216  1.1  riastrad 	if (viocon_port_create(sc, 0) != 0) {
    217  1.1  riastrad 		printf("\n%s: viocon_port_create failed\n", __func__);
    218  1.1  riastrad 		goto err;
    219  1.1  riastrad 	}
    220  1.1  riastrad 	viocon_rx_fill(sc->sc_ports[0]);
    221  1.1  riastrad 
    222  1.6  yamaguch 	if (virtio_child_attach_finish(vsc, sc->sc_vqs, sc->sc_max_ports * 2,
    223  1.6  yamaguch 	    /*config_change*/NULL, virtio_vq_intr, /*req_flags*/0) != 0)
    224  1.1  riastrad 		goto err;
    225  1.1  riastrad 
    226  1.1  riastrad 	return;
    227  1.1  riastrad err:
    228  1.1  riastrad 	kmem_free(sc->sc_vqs, 2 * (maxports + 1) * sizeof(sc->sc_vqs[0]));
    229  1.1  riastrad 	kmem_free(sc->sc_ports, maxports * sizeof(sc->sc_ports[0]));
    230  1.1  riastrad 	virtio_child_attach_failed(vsc);
    231  1.1  riastrad }
    232  1.1  riastrad 
    233  1.1  riastrad int
    234  1.1  riastrad viocon_port_create(struct viocon_softc *sc, int portidx)
    235  1.1  riastrad {
    236  1.1  riastrad 	struct virtio_softc *vsc = sc->sc_virtio;
    237  1.1  riastrad 	int rxidx, txidx, allocsize, nsegs;
    238  1.1  riastrad 	char name[6];
    239  1.1  riastrad 	struct viocon_port *vp;
    240  1.1  riastrad 	void *kva;
    241  1.1  riastrad 	struct tty *tp;
    242  1.1  riastrad 
    243  1.1  riastrad 	vp = kmem_zalloc(sizeof(*vp), KM_SLEEP);
    244  1.1  riastrad 	if (vp == NULL)
    245  1.1  riastrad 		return ENOMEM;
    246  1.1  riastrad 	sc->sc_ports[portidx] = vp;
    247  1.1  riastrad 	vp->vp_sc = sc;
    248  1.1  riastrad 	DPRINTF("%s: vp: %p\n", __func__, vp);
    249  1.1  riastrad 
    250  1.1  riastrad 	if (portidx == 0)
    251  1.1  riastrad 		rxidx = 0;
    252  1.1  riastrad 	else
    253  1.1  riastrad 		rxidx = 2 * (portidx + 1);
    254  1.1  riastrad 	txidx = rxidx + 1;
    255  1.1  riastrad 
    256  1.1  riastrad 	snprintf(name, sizeof(name), "p%drx", portidx);
    257  1.1  riastrad 	if (virtio_alloc_vq(vsc, &sc->sc_vqs[rxidx], rxidx, BUFSIZE, 1,
    258  1.1  riastrad 	    name) != 0) {
    259  1.1  riastrad 		printf("\nCan't alloc %s virtqueue\n", name);
    260  1.1  riastrad 		goto err;
    261  1.1  riastrad 	}
    262  1.1  riastrad 	vp->vp_rx = &sc->sc_vqs[rxidx];
    263  1.1  riastrad 	vp->vp_rx->vq_done = viocon_rx_intr;
    264  1.1  riastrad 	vp->vp_si = softint_establish(SOFTINT_SERIAL, viocon_rx_soft, vp);
    265  1.1  riastrad 	DPRINTF("%s: rx: %p\n", __func__, vp->vp_rx);
    266  1.1  riastrad 
    267  1.1  riastrad 	snprintf(name, sizeof(name), "p%dtx", portidx);
    268  1.1  riastrad 	if (virtio_alloc_vq(vsc, &sc->sc_vqs[txidx], txidx, BUFSIZE, 1,
    269  1.1  riastrad 	    name) != 0) {
    270  1.1  riastrad 		printf("\nCan't alloc %s virtqueue\n", name);
    271  1.1  riastrad 		goto err;
    272  1.1  riastrad 	}
    273  1.1  riastrad 	vp->vp_tx = &sc->sc_vqs[txidx];
    274  1.1  riastrad 	vp->vp_tx->vq_done = viocon_tx_intr;
    275  1.1  riastrad 	DPRINTF("%s: tx: %p\n", __func__, vp->vp_tx);
    276  1.1  riastrad 
    277  1.1  riastrad 	allocsize = (vp->vp_rx->vq_num + vp->vp_tx->vq_num) * BUFSIZE;
    278  1.1  riastrad 
    279  1.1  riastrad 	if (bus_dmamap_create(virtio_dmat(vsc), allocsize, 1, allocsize, 0,
    280  1.1  riastrad 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &vp->vp_dmamap) != 0)
    281  1.1  riastrad 		goto err;
    282  1.1  riastrad 	if (bus_dmamem_alloc(virtio_dmat(vsc), allocsize, 8, 0, &vp->vp_dmaseg,
    283  1.1  riastrad 	    1, &nsegs, BUS_DMA_NOWAIT) != 0)
    284  1.1  riastrad 		goto err;
    285  1.1  riastrad 	if (bus_dmamem_map(virtio_dmat(vsc), &vp->vp_dmaseg, nsegs,
    286  1.1  riastrad 	    allocsize, &kva, BUS_DMA_NOWAIT) != 0)
    287  1.1  riastrad 		goto err;
    288  1.1  riastrad 	memset(kva, 0, allocsize);
    289  1.1  riastrad 	if (bus_dmamap_load(virtio_dmat(vsc), vp->vp_dmamap, kva,
    290  1.1  riastrad 	    allocsize, NULL, BUS_DMA_NOWAIT) != 0)
    291  1.1  riastrad 		goto err;
    292  1.1  riastrad 	vp->vp_rx_buf = (unsigned char *)kva;
    293  1.1  riastrad 	/*
    294  1.1  riastrad 	 * XXX use only a small circular tx buffer instead of many BUFSIZE buffers?
    295  1.1  riastrad 	 */
    296  1.1  riastrad 	vp->vp_tx_buf = vp->vp_rx_buf + vp->vp_rx->vq_num * BUFSIZE;
    297  1.1  riastrad 
    298  1.1  riastrad 	if (virtio_features(vsc) & VIRTIO_CONSOLE_F_SIZE) {
    299  1.1  riastrad 		vp->vp_cols = virtio_read_device_config_2(vsc,
    300  1.1  riastrad 		    VIRTIO_CONSOLE_COLS);
    301  1.1  riastrad 		vp->vp_rows = virtio_read_device_config_2(vsc,
    302  1.1  riastrad 		    VIRTIO_CONSOLE_ROWS);
    303  1.1  riastrad 	}
    304  1.1  riastrad 
    305  1.1  riastrad 	tp = ttymalloc(1000000);
    306  1.1  riastrad 	tp->t_oproc = vioconstart;
    307  1.1  riastrad 	tp->t_param = vioconparam;
    308  1.1  riastrad 	tp->t_hwiflow = vioconhwiflow;
    309  1.2  riastrad 	tp->t_dev = VIOCONDEV(device_unit(sc->sc_dev), portidx);
    310  1.1  riastrad 	vp->vp_tty = tp;
    311  1.1  riastrad 	DPRINTF("%s: tty: %p\n", __func__, tp);
    312  1.1  riastrad 
    313  1.1  riastrad 	virtio_start_vq_intr(vsc, vp->vp_rx);
    314  1.1  riastrad 	virtio_start_vq_intr(vsc, vp->vp_tx);
    315  1.1  riastrad 
    316  1.1  riastrad 	return 0;
    317  1.1  riastrad err:
    318  1.1  riastrad 	panic("%s failed", __func__);
    319  1.1  riastrad 	return -1;
    320  1.1  riastrad }
    321  1.1  riastrad 
    322  1.1  riastrad int
    323  1.1  riastrad viocon_tx_drain(struct viocon_port *vp, struct virtqueue *vq)
    324  1.1  riastrad {
    325  1.1  riastrad 	struct virtio_softc *vsc = vq->vq_owner;
    326  1.1  riastrad 	int ndone = 0, len, slot;
    327  1.1  riastrad 
    328  1.1  riastrad 	splassert(IPL_TTY);
    329  1.1  riastrad 	while (virtio_dequeue(vsc, vq, &slot, &len) == 0) {
    330  1.1  riastrad 		bus_dmamap_sync(virtio_dmat(vsc), vp->vp_dmamap,
    331  1.1  riastrad 		    vp->vp_tx_buf - vp->vp_rx_buf + slot * BUFSIZE, BUFSIZE,
    332  1.4  riastrad 		    BUS_DMASYNC_POSTWRITE);
    333  1.1  riastrad 		virtio_dequeue_commit(vsc, vq, slot);
    334  1.1  riastrad 		ndone++;
    335  1.1  riastrad 	}
    336  1.1  riastrad 	return ndone;
    337  1.1  riastrad }
    338  1.1  riastrad 
    339  1.1  riastrad int
    340  1.1  riastrad viocon_tx_intr(struct virtqueue *vq)
    341  1.1  riastrad {
    342  1.1  riastrad 	struct virtio_softc *vsc = vq->vq_owner;
    343  1.1  riastrad 	struct viocon_softc *sc = device_private(virtio_child(vsc));
    344  1.1  riastrad 	int ndone = 0;
    345  1.1  riastrad 	int portidx = (vq->vq_index - 1) / 2;
    346  1.1  riastrad 	struct viocon_port *vp = sc->sc_ports[portidx];
    347  1.1  riastrad 	struct tty *tp = vp->vp_tty;
    348  1.1  riastrad 
    349  1.1  riastrad 	splassert(IPL_TTY);
    350  1.1  riastrad 	ndone = viocon_tx_drain(vp, vq);
    351  1.1  riastrad 	if (ndone && ISSET(tp->t_state, TS_BUSY)) {
    352  1.1  riastrad 		CLR(tp->t_state, TS_BUSY);
    353  1.1  riastrad 		(*tp->t_linesw->l_start)(tp);
    354  1.1  riastrad 	}
    355  1.1  riastrad 
    356  1.1  riastrad 	return 1;
    357  1.1  riastrad }
    358  1.1  riastrad 
    359  1.1  riastrad void
    360  1.1  riastrad viocon_rx_fill(struct viocon_port *vp)
    361  1.1  riastrad {
    362  1.1  riastrad 	struct virtqueue *vq = vp->vp_rx;
    363  1.1  riastrad 	struct virtio_softc *vsc = vp->vp_sc->sc_virtio;
    364  1.1  riastrad 	int r, slot, ndone = 0;
    365  1.1  riastrad 
    366  1.1  riastrad 	while ((r = virtio_enqueue_prep(vsc, vq, &slot)) == 0) {
    367  1.1  riastrad 		if (virtio_enqueue_reserve(vsc, vq, slot, 1) != 0)
    368  1.1  riastrad 			break;
    369  1.1  riastrad 		bus_dmamap_sync(virtio_dmat(vsc), vp->vp_dmamap, slot * BUFSIZE,
    370  1.1  riastrad 		    BUFSIZE, BUS_DMASYNC_PREREAD);
    371  1.1  riastrad 		virtio_enqueue_p(vsc, vq, slot, vp->vp_dmamap, slot * BUFSIZE,
    372  1.1  riastrad 		    BUFSIZE, 0);
    373  1.1  riastrad 		virtio_enqueue_commit(vsc, vq, slot, 0);
    374  1.1  riastrad 		ndone++;
    375  1.1  riastrad 	}
    376  1.1  riastrad 	KASSERT(r == 0 || r == EAGAIN);
    377  1.1  riastrad 	if (ndone > 0)
    378  1.1  riastrad 		virtio_notify(vsc, vq);
    379  1.1  riastrad }
    380  1.1  riastrad 
    381  1.1  riastrad int
    382  1.1  riastrad viocon_rx_intr(struct virtqueue *vq)
    383  1.1  riastrad {
    384  1.1  riastrad 	struct virtio_softc *vsc = vq->vq_owner;
    385  1.1  riastrad 	struct viocon_softc *sc = device_private(virtio_child(vsc));
    386  1.1  riastrad 	int portidx = (vq->vq_index - 1) / 2;
    387  1.1  riastrad 	struct viocon_port *vp = sc->sc_ports[portidx];
    388  1.1  riastrad 
    389  1.1  riastrad 	softint_schedule(vp->vp_si);
    390  1.1  riastrad 	return 1;
    391  1.1  riastrad }
    392  1.1  riastrad 
    393  1.1  riastrad void
    394  1.1  riastrad viocon_rx_soft(void *arg)
    395  1.1  riastrad {
    396  1.1  riastrad 	struct viocon_port *vp = arg;
    397  1.1  riastrad 	struct virtqueue *vq = vp->vp_rx;
    398  1.1  riastrad 	struct virtio_softc *vsc = vq->vq_owner;
    399  1.1  riastrad 	struct tty *tp = vp->vp_tty;
    400  1.1  riastrad 	int slot, len, i;
    401  1.1  riastrad 	u_char *p;
    402  1.1  riastrad 
    403  1.1  riastrad 	while (!vp->vp_iflow && virtio_dequeue(vsc, vq, &slot, &len) == 0) {
    404  1.1  riastrad 		bus_dmamap_sync(virtio_dmat(vsc), vp->vp_dmamap,
    405  1.1  riastrad 		    slot * BUFSIZE, BUFSIZE, BUS_DMASYNC_POSTREAD);
    406  1.1  riastrad 		p = vp->vp_rx_buf + slot * BUFSIZE;
    407  1.1  riastrad 		for (i = 0; i < len; i++)
    408  1.1  riastrad 			(*tp->t_linesw->l_rint)(*p++, tp);
    409  1.1  riastrad 		virtio_dequeue_commit(vsc, vq, slot);
    410  1.1  riastrad 	}
    411  1.1  riastrad 
    412  1.1  riastrad 	viocon_rx_fill(vp);
    413  1.1  riastrad 
    414  1.1  riastrad 	return;
    415  1.1  riastrad }
    416  1.1  riastrad 
    417  1.1  riastrad void
    418  1.1  riastrad vioconstart(struct tty *tp)
    419  1.1  riastrad {
    420  1.1  riastrad 	struct viocon_softc *sc = dev2sc(tp->t_dev);
    421  1.1  riastrad 	struct virtio_softc *vsc;
    422  1.1  riastrad 	struct viocon_port *vp = dev2port(tp->t_dev);
    423  1.1  riastrad 	struct virtqueue *vq;
    424  1.1  riastrad 	u_char *buf;
    425  1.1  riastrad 	int s, cnt, slot, ret, ndone;
    426  1.1  riastrad 
    427  1.1  riastrad 	vsc = sc->sc_virtio;
    428  1.1  riastrad 	vq = vp->vp_tx;
    429  1.1  riastrad 
    430  1.1  riastrad 	s = spltty();
    431  1.1  riastrad 
    432  1.1  riastrad 	ndone = viocon_tx_drain(vp, vq);
    433  1.1  riastrad 	if (ISSET(tp->t_state, TS_BUSY)) {
    434  1.1  riastrad 		if (ndone > 0)
    435  1.1  riastrad 			CLR(tp->t_state, TS_BUSY);
    436  1.1  riastrad 		else
    437  1.1  riastrad 			goto out;
    438  1.1  riastrad 	}
    439  1.1  riastrad 	if (ISSET(tp->t_state, TS_TIMEOUT | TS_TTSTOP))
    440  1.1  riastrad 		goto out;
    441  1.1  riastrad 
    442  1.1  riastrad 	if (tp->t_outq.c_cc == 0)
    443  1.1  riastrad 		goto out;
    444  1.1  riastrad 	ndone = 0;
    445  1.1  riastrad 
    446  1.1  riastrad 	while (tp->t_outq.c_cc > 0) {
    447  1.1  riastrad 		ret = virtio_enqueue_prep(vsc, vq, &slot);
    448  1.1  riastrad 		if (ret == EAGAIN) {
    449  1.1  riastrad 			SET(tp->t_state, TS_BUSY);
    450  1.1  riastrad 			break;
    451  1.1  riastrad 		}
    452  1.1  riastrad 		KASSERT(ret == 0);
    453  1.1  riastrad 		ret = virtio_enqueue_reserve(vsc, vq, slot, 1);
    454  1.1  riastrad 		KASSERT(ret == 0);
    455  1.1  riastrad 		buf = vp->vp_tx_buf + slot * BUFSIZE;
    456  1.1  riastrad 		cnt = q_to_b(&tp->t_outq, buf, BUFSIZE);
    457  1.1  riastrad 		bus_dmamap_sync(virtio_dmat(vsc), vp->vp_dmamap,
    458  1.1  riastrad 		    vp->vp_tx_buf - vp->vp_rx_buf + slot * BUFSIZE, cnt,
    459  1.1  riastrad 		    BUS_DMASYNC_PREWRITE);
    460  1.1  riastrad 		virtio_enqueue_p(vsc, vq, slot, vp->vp_dmamap,
    461  1.1  riastrad 		    vp->vp_tx_buf - vp->vp_rx_buf + slot * BUFSIZE, cnt, 1);
    462  1.1  riastrad 		virtio_enqueue_commit(vsc, vq, slot, 0);
    463  1.1  riastrad 		ndone++;
    464  1.1  riastrad 	}
    465  1.1  riastrad 	if (ndone > 0)
    466  1.1  riastrad 		virtio_notify(vsc, vq);
    467  1.1  riastrad 	ttwakeupwr(tp);
    468  1.1  riastrad out:
    469  1.1  riastrad 	splx(s);
    470  1.1  riastrad }
    471  1.1  riastrad 
    472  1.1  riastrad int
    473  1.1  riastrad vioconhwiflow(struct tty *tp, int stop)
    474  1.1  riastrad {
    475  1.1  riastrad 	struct viocon_port *vp = dev2port(tp->t_dev);
    476  1.1  riastrad 	int s;
    477  1.1  riastrad 
    478  1.1  riastrad 	s = spltty();
    479  1.1  riastrad 	vp->vp_iflow = stop;
    480  1.1  riastrad 	if (stop) {
    481  1.1  riastrad 		virtio_stop_vq_intr(vp->vp_sc->sc_virtio, vp->vp_rx);
    482  1.1  riastrad 	} else {
    483  1.1  riastrad 		virtio_start_vq_intr(vp->vp_sc->sc_virtio, vp->vp_rx);
    484  1.1  riastrad 		softint_schedule(vp->vp_si);
    485  1.1  riastrad 	}
    486  1.1  riastrad 	splx(s);
    487  1.1  riastrad 	return 1;
    488  1.1  riastrad }
    489  1.1  riastrad 
    490  1.1  riastrad int
    491  1.1  riastrad vioconparam(struct tty *tp, struct termios *t)
    492  1.1  riastrad {
    493  1.1  riastrad 	tp->t_ispeed = t->c_ispeed;
    494  1.1  riastrad 	tp->t_ospeed = t->c_ospeed;
    495  1.1  riastrad 	tp->t_cflag = t->c_cflag;
    496  1.1  riastrad 
    497  1.1  riastrad 	vioconstart(tp);
    498  1.1  riastrad 	return 0;
    499  1.1  riastrad }
    500  1.1  riastrad 
    501  1.1  riastrad int
    502  1.1  riastrad vioconopen(dev_t dev, int flag, int mode, struct lwp *l)
    503  1.1  riastrad {
    504  1.1  riastrad 	int unit = VIOCONUNIT(dev);
    505  1.1  riastrad 	int port = VIOCONPORT(dev);
    506  1.1  riastrad 	struct viocon_softc *sc;
    507  1.1  riastrad 	struct viocon_port *vp;
    508  1.1  riastrad 	struct tty *tp;
    509  1.1  riastrad 	int s, error;
    510  1.1  riastrad 
    511  1.1  riastrad 	sc = device_lookup_private(&viocon_cd, unit);
    512  1.1  riastrad 	if (sc == NULL)
    513  1.1  riastrad 		return (ENXIO);
    514  1.1  riastrad 	if (!device_is_active(sc->sc_dev))
    515  1.1  riastrad 		return (ENXIO);
    516  1.1  riastrad 
    517  1.1  riastrad 	s = spltty();
    518  1.1  riastrad 	if (port >= sc->sc_max_ports) {
    519  1.1  riastrad 		splx(s);
    520  1.1  riastrad 		return (ENXIO);
    521  1.1  riastrad 	}
    522  1.1  riastrad 	vp = sc->sc_ports[port];
    523  1.1  riastrad 	tp = vp->vp_tty;
    524  1.1  riastrad #ifdef NOTYET
    525  1.1  riastrad 	vp->vp_guest_open = 1;
    526  1.1  riastrad #endif
    527  1.1  riastrad 	splx(s);
    528  1.1  riastrad 
    529  1.1  riastrad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    530  1.1  riastrad 		return (EBUSY);
    531  1.1  riastrad 
    532  1.1  riastrad 	s = spltty();
    533  1.1  riastrad 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    534  1.1  riastrad 		ttychars(tp);
    535  1.1  riastrad 		tp->t_ispeed = 1000000;
    536  1.1  riastrad 		tp->t_ospeed = 1000000;
    537  1.1  riastrad 		tp->t_cflag = TTYDEF_CFLAG|CLOCAL|CRTSCTS;
    538  1.1  riastrad 		tp->t_iflag = TTYDEF_IFLAG;
    539  1.1  riastrad 		tp->t_oflag = TTYDEF_OFLAG;
    540  1.1  riastrad 		tp->t_lflag = TTYDEF_LFLAG;
    541  1.1  riastrad 		if (vp->vp_cols != 0) {
    542  1.1  riastrad 			tp->t_winsize.ws_col = vp->vp_cols;
    543  1.1  riastrad 			tp->t_winsize.ws_row = vp->vp_rows;
    544  1.1  riastrad 		}
    545  1.1  riastrad 
    546  1.1  riastrad 		vioconparam(tp, &tp->t_termios);
    547  1.1  riastrad 		ttsetwater(tp);
    548  1.1  riastrad 	}
    549  1.1  riastrad 	splx(s);
    550  1.1  riastrad 
    551  1.1  riastrad 	error = (*tp->t_linesw->l_open)(dev, tp);
    552  1.1  riastrad 	return error;
    553  1.1  riastrad }
    554  1.1  riastrad 
    555  1.1  riastrad int
    556  1.1  riastrad vioconclose(dev_t dev, int flag, int mode, struct lwp *l)
    557  1.1  riastrad {
    558  1.1  riastrad 	struct viocon_port *vp = dev2port(dev);
    559  1.1  riastrad 	struct tty *tp = vp->vp_tty;
    560  1.1  riastrad 	int s;
    561  1.1  riastrad 
    562  1.1  riastrad 	if (!ISSET(tp->t_state, TS_ISOPEN))
    563  1.1  riastrad 		return 0;
    564  1.1  riastrad 
    565  1.1  riastrad 	(*tp->t_linesw->l_close)(tp, flag);
    566  1.1  riastrad 	s = spltty();
    567  1.1  riastrad #ifdef NOTYET
    568  1.1  riastrad 	vp->vp_guest_open = 0;
    569  1.1  riastrad #endif
    570  1.1  riastrad 	CLR(tp->t_state, TS_BUSY | TS_FLUSH);
    571  1.1  riastrad 	ttyclose(tp);
    572  1.1  riastrad 	splx(s);
    573  1.1  riastrad 
    574  1.1  riastrad 	return 0;
    575  1.1  riastrad }
    576  1.1  riastrad 
    577  1.1  riastrad int
    578  1.1  riastrad vioconread(dev_t dev, struct uio *uio, int flag)
    579  1.1  riastrad {
    580  1.1  riastrad 	struct viocon_port *vp = dev2port(dev);
    581  1.1  riastrad 	struct tty *tp = vp->vp_tty;
    582  1.1  riastrad 
    583  1.1  riastrad 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    584  1.1  riastrad }
    585  1.1  riastrad 
    586  1.1  riastrad int
    587  1.1  riastrad vioconwrite(dev_t dev, struct uio *uio, int flag)
    588  1.1  riastrad {
    589  1.1  riastrad 	struct viocon_port *vp = dev2port(dev);
    590  1.1  riastrad 	struct tty *tp = vp->vp_tty;
    591  1.1  riastrad 
    592  1.1  riastrad 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    593  1.1  riastrad }
    594  1.1  riastrad 
    595  1.1  riastrad struct tty *
    596  1.1  riastrad viocontty(dev_t dev)
    597  1.1  riastrad {
    598  1.1  riastrad 	struct viocon_port *vp = dev2port(dev);
    599  1.1  riastrad 
    600  1.1  riastrad 	return vp->vp_tty;
    601  1.1  riastrad }
    602  1.1  riastrad 
    603  1.1  riastrad void
    604  1.1  riastrad vioconstop(struct tty *tp, int flag)
    605  1.1  riastrad {
    606  1.1  riastrad 	int s;
    607  1.1  riastrad 
    608  1.1  riastrad 	s = spltty();
    609  1.1  riastrad 	if (ISSET(tp->t_state, TS_BUSY))
    610  1.1  riastrad 		if (!ISSET(tp->t_state, TS_TTSTOP))
    611  1.1  riastrad 			SET(tp->t_state, TS_FLUSH);
    612  1.1  riastrad 	splx(s);
    613  1.1  riastrad }
    614  1.1  riastrad 
    615  1.1  riastrad int
    616  1.1  riastrad vioconioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    617  1.1  riastrad {
    618  1.1  riastrad 	struct viocon_port *vp = dev2port(dev);
    619  1.1  riastrad 	struct tty *tp;
    620  1.1  riastrad 	int error1, error2;
    621  1.1  riastrad 
    622  1.1  riastrad 	tp = vp->vp_tty;
    623  1.1  riastrad 
    624  1.1  riastrad 	error1 = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    625  1.1  riastrad 	if (error1 >= 0)
    626  1.1  riastrad 		return error1;
    627  1.1  riastrad 	error2 = ttioctl(tp, cmd, data, flag, l);
    628  1.1  riastrad 	if (error2 >= 0)
    629  1.1  riastrad 		return error2;
    630  1.1  riastrad 	return ENOTTY;
    631  1.1  riastrad }
    632