Source-Changes-D archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: CVS commit: src



Hi,

Thanks for your great work!

Could you add /dev/vio9p? creation rule to /dev/MAKEDEV?
I have no idea where is relevant in src/etc/MAKEDEV.tmpl.
My idea is as follows:

Index: etc/MAKEDEV.tmpl
===================================================================
RCS file: /cvsroot/src/etc/MAKEDEV.tmpl,v
retrieving revision 1.209
diff -u -r1.209 MAKEDEV.tmpl
--- etc/MAKEDEV.tmpl	27 Oct 2019 07:08:15 -0000	1.209
+++ etc/MAKEDEV.tmpl	28 Oct 2019 13:07:11 -0000
@@ -1762,6 +1762,11 @@
 	mkdev qemufwcfg$unit c %qemufwcfg_chr% $unit 660
 	;;
 
+vio9p[0-9]*)
+	unit=${i#vio9p}
+	mkdev vio9p$unit c %vio9p_chr% $unit 660
+	;;
+
 nvram)
 	mkdev nvram c %nvram_chr% 0 644
 	;;


"Ryota Ozaki" <ozaki-r%netbsd.org@localhost> writes:

> Module Name:	src
> Committed By:	ozaki-r
> Date:		Mon Oct 28 02:56:41 UTC 2019
>
> Modified Files:
> 	src/distrib/sets/lists/man: mi
> 	src/share/man/man4: Makefile
> 	src/sys/conf: majors
> 	src/sys/dev/pci: files.virtio
> 	src/sys/modules: Makefile
> Added Files:
> 	src/share/man/man4: vio9p.4
> 	src/sys/dev/pci: vio9p.c
> 	src/sys/modules/vio9p: Makefile vio9p.ioconf
>
> Log Message:
> Implement a front-end driver of virtio-9p called vio9p
>
> In conjunction with mount_9p, it enables a NetBSD system running as a VM guest
> to mount an exported filesystem by the host via virtio-9p.  It exports a 9p
> end-point of virtio-9p via a character device file for mount_9p.
>
> Reviewed by yamaguchi@
>
>
> To generate a diff of this commit:
> cvs rdiff -u -r1.1658 -r1.1659 src/distrib/sets/lists/man/mi
> cvs rdiff -u -r1.685 -r1.686 src/share/man/man4/Makefile
> cvs rdiff -u -r0 -r1.1 src/share/man/man4/vio9p.4
> cvs rdiff -u -r1.89 -r1.90 src/sys/conf/majors
> cvs rdiff -u -r1.1 -r1.2 src/sys/dev/pci/files.virtio
> cvs rdiff -u -r0 -r1.1 src/sys/dev/pci/vio9p.c
> cvs rdiff -u -r1.227 -r1.228 src/sys/modules/Makefile
> cvs rdiff -u -r0 -r1.1 src/sys/modules/vio9p/Makefile \
>     src/sys/modules/vio9p/vio9p.ioconf
>
> Please note that diffs are not public domain; they are subject to the
> copyright notices on the relevant files.
>
> Modified files:
>
> Index: src/distrib/sets/lists/man/mi
> diff -u src/distrib/sets/lists/man/mi:1.1658 src/distrib/sets/lists/man/mi:1.1659
> --- src/distrib/sets/lists/man/mi:1.1658	Fri Oct 25 17:39:56 2019
> +++ src/distrib/sets/lists/man/mi	Mon Oct 28 02:56:40 2019
> @@ -1,4 +1,4 @@
> -# $NetBSD: mi,v 1.1658 2019/10/25 17:39:56 martin Exp $
> +# $NetBSD: mi,v 1.1659 2019/10/28 02:56:40 ozaki-r Exp $
>  #
>  # Note: don't delete entries from here - mark them as "obsolete" instead.
>  #
> @@ -1990,6 +1990,7 @@
>  ./usr/share/man/cat4/viapm.0			man-obsolete		obsolete
>  ./usr/share/man/cat4/video.0			man-sys-catman		.cat
>  ./usr/share/man/cat4/vinum.0			man-obsolete		obsolete
> +./usr/share/man/cat4/vio9p.0			man-sys-catman		.cat
>  ./usr/share/man/cat4/vioif.0			man-sys-catman		.cat
>  ./usr/share/man/cat4/viomb.0			man-sys-catman		.cat
>  ./usr/share/man/cat4/viornd.0			man-sys-catman		.cat
> @@ -5085,6 +5086,7 @@
>  ./usr/share/man/html4/viaenv.html		man-sys-htmlman		html
>  ./usr/share/man/html4/viaide.html		man-sys-htmlman		html
>  ./usr/share/man/html4/video.html		man-sys-htmlman		html
> +./usr/share/man/html4/vio9p.html		man-sys-htmlman		html
>  ./usr/share/man/html4/vioif.html		man-sys-htmlman		html
>  ./usr/share/man/html4/viomb.html		man-sys-htmlman		html
>  ./usr/share/man/html4/viornd.html		man-sys-htmlman		html
> @@ -8112,6 +8114,7 @@
>  ./usr/share/man/man4/viapm.4			man-obsolete		obsolete
>  ./usr/share/man/man4/video.4			man-sys-man		.man
>  ./usr/share/man/man4/vinum.4			man-obsolete		obsolete
> +./usr/share/man/man4/vio9p.4			man-sys-man		.man
>  ./usr/share/man/man4/vioif.4			man-sys-man		.man
>  ./usr/share/man/man4/viomb.4			man-sys-man		.man
>  ./usr/share/man/man4/viornd.4			man-sys-man		.man
>
> Index: src/share/man/man4/Makefile
> diff -u src/share/man/man4/Makefile:1.685 src/share/man/man4/Makefile:1.686
> --- src/share/man/man4/Makefile:1.685	Fri Oct 25 17:39:57 2019
> +++ src/share/man/man4/Makefile	Mon Oct 28 02:56:40 2019
> @@ -1,4 +1,4 @@
> -#	$NetBSD: Makefile,v 1.685 2019/10/25 17:39:57 martin Exp $
> +#	$NetBSD: Makefile,v 1.686 2019/10/28 02:56:40 ozaki-r Exp $
>  #	@(#)Makefile	8.1 (Berkeley) 6/18/93
>  
>  MAN=	aac.4 ac97.4 acardide.4 aceride.4 acphy.4 \
> @@ -66,9 +66,9 @@ MAN=	aac.4 ac97.4 acardide.4 aceride.4 a
>  	trm.4 tsllux.4 tty.4 tun.4 tqphy.4 twa.4 twe.4 txp.4 \
>  	uark.4 ubsec.4 udp.4 uep.4 ug.4 uha.4 uk.4 ukphy.4 umb.4 \
>  	unix.4 userconf.4 \
> -	vald.4 valz.4 veriexec.4 vga.4 vge.4 viaide.4 video.4 vioif.4 viomb.4 \
> -	viornd.4 vioscsi.4 virt.4 \
> -	virtio.4 vlan.4 vmmon.4 vmnet.4 vnd.4 voodoofb.4 vr.4 vte.4 \
> +	vald.4 valz.4 veriexec.4 vga.4 vge.4 viaide.4 video.4 \
> +	vio9p.4 vioif.4 viomb.4 viornd.4 vioscsi.4 virt.4 virtio.4 \
> +	vlan.4 vmmon.4 vmnet.4 vnd.4 voodoofb.4 vr.4 vte.4 \
>  	wapbl.4 wb.4 wbsio.4 wd.4 wdc.4 wi.4 wm.4 wpi.4 \
>  	wsbell.4 wscons.4 wsdisplay.4 wsfont.4 wskbd.4 wsmouse.4 wsmux.4 \
>  	xbox.4 xge.4 \
>
> Index: src/sys/conf/majors
> diff -u src/sys/conf/majors:1.89 src/sys/conf/majors:1.90
> --- src/sys/conf/majors:1.89	Sun Sep 15 11:45:47 2019
> +++ src/sys/conf/majors	Mon Oct 28 02:56:40 2019
> @@ -1,4 +1,4 @@
> -# $NetBSD: majors,v 1.89 2019/09/15 11:45:47 maxv Exp $
> +# $NetBSD: majors,v 1.90 2019/10/28 02:56:40 ozaki-r Exp $
>  #
>  # Device majors for Machine-Independent drivers.
>  #
> @@ -87,3 +87,4 @@ device-major spi       char 347		   spi
>  # Major 353 is reserved for external/cddl/osnet/dev/sdt/sdt.c
>  device-major ipmi      char 354		   ipmi
>  device-major vhci      char 355            vhci
> +device-major vio9p     char 356		   vio9p
>
> Index: src/sys/dev/pci/files.virtio
> diff -u src/sys/dev/pci/files.virtio:1.1 src/sys/dev/pci/files.virtio:1.2
> --- src/sys/dev/pci/files.virtio:1.1	Wed Aug  2 08:39:14 2017
> +++ src/sys/dev/pci/files.virtio	Mon Oct 28 02:56:40 2019
> @@ -1,4 +1,4 @@
> -#	$NetBSD: files.virtio,v 1.1 2017/08/02 08:39:14 cherry Exp $
> +#	$NetBSD: files.virtio,v 1.2 2019/10/28 02:56:40 ozaki-r Exp $
>  
>  # VirtIO
>  device	virtio {}
> @@ -23,3 +23,6 @@ device	vioscsi: scsi
>  attach	vioscsi at virtio
>  file	dev/pci/vioscsi.c	vioscsi
>  
> +device	vio9p
> +attach	vio9p at virtio
> +file	dev/pci/vio9p.c		vio9p
>
> Index: src/sys/modules/Makefile
> diff -u src/sys/modules/Makefile:1.227 src/sys/modules/Makefile:1.228
> --- src/sys/modules/Makefile:1.227	Sun Sep 22 22:59:39 2019
> +++ src/sys/modules/Makefile	Mon Oct 28 02:56:40 2019
> @@ -1,4 +1,4 @@
> -#	$NetBSD: Makefile,v 1.227 2019/09/22 22:59:39 christos Exp $
> +#	$NetBSD: Makefile,v 1.228 2019/10/28 02:56:40 ozaki-r Exp $
>  
>  .include <bsd.own.mk>
>  
> @@ -458,6 +458,7 @@ SUBDIR+=	ld_nvme
>  SUBDIR+=	nvme
>  SUBDIR+=	twa
>  SUBDIR+=	twe
> +SUBDIR+=	vio9p
>  SUBDIR+=	viomb
>  SUBDIR+=	virtio
>  SUBDIR+=	virtio_pci
>
> Added files:
>
> Index: src/share/man/man4/vio9p.4
> diff -u /dev/null src/share/man/man4/vio9p.4:1.1
> --- /dev/null	Mon Oct 28 02:56:41 2019
> +++ src/share/man/man4/vio9p.4	Mon Oct 28 02:56:40 2019
> @@ -0,0 +1,70 @@
> +.\" $NetBSD: vio9p.4,v 1.1 2019/10/28 02:56:40 ozaki-r Exp $
> +.\"
> +.\" Copyright (c) 2019 Internet Initiative Japan, Inc.
> +.\" All rights reserved.
> +.\"
> +.\" Redistribution and use in source and binary forms, with or without
> +.\" modification, are permitted provided that the following conditions
> +.\" are met:
> +.\" 1. Redistributions of source code must retain the above copyright
> +.\"    notice, this list of conditions and the following disclaimer.
> +.\" 2. Redistributions in binary form must reproduce the above copyright
> +.\"    notice, this list of conditions and the following disclaimer in the
> +.\"    documentation and/or other materials provided with the distribution.
> +.\"
> +.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
> +.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
> +.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> +.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
> +.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
> +.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
> +.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
> +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
> +.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> +.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
> +.\" POSSIBILITY OF SUCH DAMAGE.
> +.\"
> +.Dd October 24, 2019
> +.Dt VIO9P 4
> +.Os
> +.Sh NAME
> +.Nm vio9p
> +.Nd VirtIO 9p front-end driver
> +.Sh SYNOPSIS
> +.Cd "vio9p* at virtio?"
> +.Sh DESCRIPTION
> +In conjunction with
> +.Xr mount_9p 8 , the
> +.Nm
> +driver enables a NetBSD system running as a VM guest to mount an exported filesystem
> +by the host via virtio-9p.
> +It exports a 9p end-point of virtio-9p via a character device file for
> +.Xr mount_9p 8 .
> +.Pp
> +Each exported filesystem is assigned a character device and accesible via
> +.Pa /dev/vio9p0 ,
> +.Pa /dev/vio9p1
> +and so on, respectively in exporting order by the host.
> +.Sh FILES
> +.Bl -tag -width XdevXvio9pX -compact
> +.It Pa /dev/vio9p?
> +.El
> +.Sh EXAMPLES
> +The following command mounts a first exported filesystem by the host at
> +.Pa /mnt/9p :
> +.Bd -literal -offset indent
> +# mount_9p -cu /dev/vio9p0 /mnt/9p
> +.Ed
> +.Sh SEE ALSO
> +.Xr mount_9p 8 ,
> +.Xr virtio 4
> +.Sh HISTORY
> +The
> +.Nm
> +driver first appeared in
> +.Nx 10.0 .
> +.Sh AUTHORS
> +The
> +.Nm
> +driver was written by
> +.An Ryota Ozaki Aq Mt ozaki-r%iij.ad.jp@localhost .
>
> Index: src/sys/dev/pci/vio9p.c
> diff -u /dev/null src/sys/dev/pci/vio9p.c:1.1
> --- /dev/null	Mon Oct 28 02:56:41 2019
> +++ src/sys/dev/pci/vio9p.c	Mon Oct 28 02:56:40 2019
> @@ -0,0 +1,643 @@
> +/*	$NetBSD: vio9p.c,v 1.1 2019/10/28 02:56:40 ozaki-r Exp $	*/
> +
> +/*
> + * Copyright (c) 2019 Internet Initiative Japan, Inc.
> + * All rights reserved.
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + * 1. Redistributions of source code must retain the above copyright
> + *    notice, this list of conditions and the following disclaimer.
> + * 2. Redistributions in binary form must reproduce the above copyright
> + *    notice, this list of conditions and the following disclaimer in the
> + *    documentation and/or other materials provided with the distribution.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
> + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
> + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
> + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
> + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
> + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
> + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
> + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
> + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#include <sys/cdefs.h>
> +__KERNEL_RCSID(0, "$NetBSD: vio9p.c,v 1.1 2019/10/28 02:56:40 ozaki-r Exp $");
> +
> +#include <sys/param.h>
> +#include <sys/systm.h>
> +#include <sys/kernel.h>
> +#include <sys/bus.h>
> +#include <sys/conf.h>
> +#include <sys/condvar.h>
> +#include <sys/device.h>
> +#include <sys/mutex.h>
> +#include <sys/sysctl.h>
> +#include <sys/module.h>
> +#include <sys/syslog.h>
> +#include <sys/select.h>
> +#include <sys/kmem.h>
> +
> +#include <sys/file.h>
> +#include <sys/filedesc.h>
> +#include <sys/uio.h>
> +
> +#include <dev/pci/virtioreg.h>
> +#include <dev/pci/virtiovar.h>
> +
> +#include "ioconf.h"
> +
> +//#define VIO9P_DEBUG	1
> +//#define VIO9P_DUMP	1
> +#ifdef VIO9P_DEBUG
> +#define DLOG(fmt, args...) \
> +	do { log(LOG_DEBUG, "%s: " fmt "\n", __func__, ##args); } while (0)
> +#else
> +#define DLOG(fmt, args...) __nothing
> +#endif
> +
> +/* Configuration registers */
> +#define VIO9P_CONFIG_TAG_LEN	0 /* 16bit */
> +#define VIO9P_CONFIG_TAG	2
> +
> +#define VIO9P_FLAG_BITS		VIRTIO_COMMON_FLAG_BITS
> +
> +// Must be the same as P9P_DEFREQLEN of usr.sbin/puffs/mount_9p/ninepuffs.h
> +#define VIO9P_MAX_REQLEN	(16 * 1024)
> +#define VIO9P_SEGSIZE		PAGE_SIZE
> +#define VIO9P_N_SEGMENTS	(VIO9P_MAX_REQLEN / VIO9P_SEGSIZE)
> +
> +#define P9_MAX_TAG_LEN		16
> +
> +CTASSERT((PAGE_SIZE) == (VIRTIO_PAGE_SIZE)); /* XXX */
> +
> +struct vio9p_softc {
> +	device_t		sc_dev;
> +
> +	struct virtio_softc	*sc_virtio;
> +	struct virtqueue	sc_vq[1];
> +
> +	uint16_t		sc_taglen;
> +	uint8_t			sc_tag[P9_MAX_TAG_LEN + 1];
> +
> +	int			sc_flags;
> +#define VIO9P_INUSE		__BIT(0)
> +
> +	int			sc_state;
> +#define VIO9P_S_INIT		0
> +#define VIO9P_S_REQUESTING	1
> +#define VIO9P_S_REPLIED		2
> +#define VIO9P_S_CONSUMING	3
> +	kcondvar_t		sc_wait;
> +	struct selinfo		sc_sel;
> +	kmutex_t		sc_lock;
> +
> +	bus_dmamap_t		sc_dmamap_tx;
> +	bus_dmamap_t		sc_dmamap_rx;
> +	char			*sc_buf_tx;
> +	char			*sc_buf_rx;
> +	size_t			sc_buf_rx_len;
> +	off_t			sc_buf_rx_offset;
> +};
> +
> +/*
> + * Locking notes:
> + * - sc_state, sc_wait and sc_sel are protected by sc_lock
> + *
> + * The state machine (sc_state):
> + * - INIT       =(write from client)=> REQUESTING
> + * - REQUESTING =(reply from host)=>   REPLIED
> + * - REPLIED    =(read from client)=>  CONSUMING
> + * - CONSUMING  =(read completed(*))=> INIT
> + *
> + * (*) read may not finish by one read(2) request, then
> + *     the state remains CONSUMING.
> + */
> +
> +static int	vio9p_match(device_t, cfdata_t, void *);
> +static void	vio9p_attach(device_t, device_t, void *);
> +static void	vio9p_read_config(struct vio9p_softc *);
> +static int	vio9p_request_done(struct virtqueue *);
> +
> +static int	vio9p_read(struct file *, off_t *, struct uio *, kauth_cred_t,
> +		    int);
> +static int	vio9p_write(struct file *, off_t *, struct uio *,
> +		    kauth_cred_t, int);
> +static int	vio9p_ioctl(struct file *, u_long, void *);
> +static int	vio9p_close(struct file *);
> +static int	vio9p_kqfilter(struct file *, struct knote *);
> +
> +static const struct fileops vio9p_fileops = {
> +	.fo_name = "vio9p",
> +	.fo_read = vio9p_read,
> +	.fo_write = vio9p_write,
> +	.fo_ioctl = vio9p_ioctl,
> +	.fo_fcntl = fnullop_fcntl,
> +	.fo_poll = fnullop_poll,
> +	.fo_stat = fbadop_stat,
> +	.fo_close = vio9p_close,
> +	.fo_kqfilter = vio9p_kqfilter,
> +	.fo_restart = fnullop_restart,
> +};
> +
> +static dev_type_open(vio9p_dev_open);
> +
> +const struct cdevsw vio9p_cdevsw = {
> +	.d_open = vio9p_dev_open,
> +	.d_read = noread,
> +	.d_write = nowrite,
> +	.d_ioctl = noioctl,
> +	.d_stop = nostop,
> +	.d_tty = notty,
> +	.d_poll = nopoll,
> +	.d_mmap = nommap,
> +	.d_kqfilter = nokqfilter,
> +	.d_discard = nodiscard,
> +	.d_flag = D_OTHER | D_MPSAFE,
> +};
> +
> +static int
> +vio9p_dev_open(dev_t dev, int flag, int mode, struct lwp *l)
> +{
> +	struct vio9p_softc *sc;
> +	struct file *fp;
> +	int error, fd;
> +
> +	sc = device_lookup_private(&vio9p_cd, minor(dev));
> +	if (sc == NULL)
> +		return ENXIO;
> +
> +	/* FIXME TOCTOU */
> +	if (ISSET(sc->sc_flags, VIO9P_INUSE))
> +		return EBUSY;
> +
> +	/* falloc() will fill in the descriptor for us. */
> +	error = fd_allocfile(&fp, &fd);
> +	if (error != 0)
> +		return error;
> +
> +	sc->sc_flags |= VIO9P_INUSE;
> +
> +	return fd_clone(fp, fd, flag, &vio9p_fileops, sc);
> +}
> +
> +static int
> +vio9p_ioctl(struct file *fp, u_long cmd, void *addr)
> +{
> +	int error = 0;
> +
> +	switch (cmd) {
> +	case FIONBIO:
> +		break;
> +	default:
> +		error = EINVAL;
> +		break;
> +	}
> +
> +	return error;
> +}
> +
> +static int
> +vio9p_read(struct file *fp, off_t *offp, struct uio *uio,
> +    kauth_cred_t cred, int flags)
> +{
> +	struct vio9p_softc *sc = fp->f_data;
> +	struct virtio_softc *vsc = sc->sc_virtio;
> +	struct virtqueue *vq = &sc->sc_vq[0];
> +	int error, slot, len;
> +
> +	DLOG("enter");
> +
> +	mutex_enter(&sc->sc_lock);
> +
> +	if (sc->sc_state == VIO9P_S_INIT) {
> +		DLOG("%s: not requested", device_xname(sc->sc_dev));
> +		error = EAGAIN;
> +		goto out;
> +	}
> +
> +	if (sc->sc_state == VIO9P_S_CONSUMING) {
> +		KASSERT(sc->sc_buf_rx_len > 0);
> +		/* We already have some remaining, consume it. */
> +		len = sc->sc_buf_rx_len - sc->sc_buf_rx_offset;
> +		goto consume;
> +	}
> +
> +#if 0
> +	if (uio->uio_resid != VIO9P_MAX_REQLEN)
> +		return EINVAL;
> +#else
> +	if (uio->uio_resid > VIO9P_MAX_REQLEN) {
> +		error = EINVAL;
> +		goto out;
> +	}
> +#endif
> +
> +	error = 0;
> +	while (sc->sc_state == VIO9P_S_REQUESTING) {
> +		error = cv_timedwait_sig(&sc->sc_wait, &sc->sc_lock, hz);
> +		if (error != 0)
> +			break;
> +	}
> +	if (sc->sc_state == VIO9P_S_REPLIED)
> +		sc->sc_state = VIO9P_S_CONSUMING;
> +
> +	if (error != 0)
> +		goto out;
> +
> +	error = virtio_dequeue(vsc, vq, &slot, &len);
> +	if (error != 0) {
> +		log(LOG_ERR, "%s: virtio_dequeue failed: %d\n",
> +		       device_xname(sc->sc_dev), error);
> +		goto out;
> +	}
> +	DLOG("len=%d", len);
> +	sc->sc_buf_rx_len = len;
> +	sc->sc_buf_rx_offset = 0;
> +	bus_dmamap_sync(virtio_dmat(vsc), sc->sc_dmamap_tx, 0, VIO9P_MAX_REQLEN,
> +	    BUS_DMASYNC_POSTWRITE);
> +	bus_dmamap_sync(virtio_dmat(vsc), sc->sc_dmamap_rx, 0, VIO9P_MAX_REQLEN,
> +	    BUS_DMASYNC_POSTREAD);
> +	virtio_dequeue_commit(vsc, vq, slot);
> +#ifdef VIO9P_DUMP
> +	int i;
> +	log(LOG_DEBUG, "%s: buf: ", __func__);
> +	for (i = 0; i < len; i++) {
> +		log(LOG_DEBUG, "%c", (char)sc->sc_buf_rx[i]);
> +	}
> +	log(LOG_DEBUG, "\n");
> +#endif
> +
> +consume:
> +	DLOG("uio_resid=%lu", uio->uio_resid);
> +	if (len < uio->uio_resid) {
> +		error = EINVAL;
> +		goto out;
> +	}
> +	len = uio->uio_resid;
> +	error = uiomove(sc->sc_buf_rx + sc->sc_buf_rx_offset, len, uio);
> +	if (error != 0)
> +		goto out;
> +
> +	sc->sc_buf_rx_offset += len;
> +	if (sc->sc_buf_rx_offset == sc->sc_buf_rx_len) {
> +		sc->sc_buf_rx_len = 0;
> +		sc->sc_buf_rx_offset = 0;
> +
> +		sc->sc_state = VIO9P_S_INIT;
> +		selnotify(&sc->sc_sel, 0, 1);
> +	}
> +
> +out:
> +	mutex_exit(&sc->sc_lock);
> +	return error;
> +}
> +
> +static int
> +vio9p_write(struct file *fp, off_t *offp, struct uio *uio,
> +    kauth_cred_t cred, int flags)
> +{
> +	struct vio9p_softc *sc = fp->f_data;
> +	struct virtio_softc *vsc = sc->sc_virtio;
> +	struct virtqueue *vq = &sc->sc_vq[0];
> +	int error, slot;
> +	size_t len;
> +
> +	DLOG("enter");
> +
> +	mutex_enter(&sc->sc_lock);
> +
> +	if (sc->sc_state != VIO9P_S_INIT) {
> +		DLOG("already requesting");
> +		error = EAGAIN;
> +		goto out;
> +	}
> +
> +	if (uio->uio_resid == 0) {
> +		error = 0;
> +		goto out;
> +	}
> +
> +	if (uio->uio_resid > VIO9P_MAX_REQLEN) {
> +		error = EINVAL;
> +		goto out;
> +	}
> +
> +	len = uio->uio_resid;
> +	error = uiomove(sc->sc_buf_tx, len, uio);
> +	if (error != 0)
> +		goto out;
> +
> +	DLOG("len=%lu", len);
> +#ifdef VIO9P_DUMP
> +	int i;
> +	log(LOG_DEBUG, "%s: buf: ", __func__);
> +	for (i = 0; i < len; i++) {
> +		log(LOG_DEBUG, "%c", (char)sc->sc_buf_tx[i]);
> +	}
> +	log(LOG_DEBUG, "\n");
> +#endif
> +
> +	error = virtio_enqueue_prep(vsc, vq, &slot);
> +	if (error != 0) {
> +		log(LOG_ERR, "%s: virtio_enqueue_prep failed\n",
> +		       device_xname(sc->sc_dev));
> +		goto out;
> +	}
> +	DLOG("slot=%d", slot);
> +	error = virtio_enqueue_reserve(vsc, vq, slot,
> +	    sc->sc_dmamap_tx->dm_nsegs + sc->sc_dmamap_rx->dm_nsegs);
> +	if (error != 0) {
> +		log(LOG_ERR, "%s: virtio_enqueue_reserve failed\n",
> +		       device_xname(sc->sc_dev));
> +		goto out;
> +	}
> +
> +	/* Tx */
> +	bus_dmamap_sync(virtio_dmat(vsc), sc->sc_dmamap_tx, 0,
> +	    len, BUS_DMASYNC_PREWRITE);
> +	virtio_enqueue(vsc, vq, slot, sc->sc_dmamap_tx, true);
> +	/* Rx */
> +	bus_dmamap_sync(virtio_dmat(vsc), sc->sc_dmamap_rx, 0,
> +	    VIO9P_MAX_REQLEN, BUS_DMASYNC_PREREAD);
> +	virtio_enqueue(vsc, vq, slot, sc->sc_dmamap_rx, false);
> +	virtio_enqueue_commit(vsc, vq, slot, true);
> +
> +	sc->sc_state = VIO9P_S_REQUESTING;
> +out:
> +	mutex_exit(&sc->sc_lock);
> +	return error;
> +}
> +
> +static int
> +vio9p_close(struct file *fp)
> +{
> +	struct vio9p_softc *sc = fp->f_data;
> +
> +	KASSERT(ISSET(sc->sc_flags, VIO9P_INUSE));
> +	sc->sc_flags &= ~VIO9P_INUSE;
> +
> +	return 0;
> +}
> +
> +static void
> +filt_vio9p_detach(struct knote *kn)
> +{
> +	struct vio9p_softc *sc = kn->kn_hook;
> +
> +	mutex_enter(&sc->sc_lock);
> +	SLIST_REMOVE(&sc->sc_sel.sel_klist, kn, knote, kn_selnext);
> +	mutex_exit(&sc->sc_lock);
> +}
> +
> +static int
> +filt_vio9p_read(struct knote *kn, long hint)
> +{
> +	struct vio9p_softc *sc = kn->kn_hook;
> +	int rv;
> +
> +	kn->kn_data = sc->sc_buf_rx_len;
> +	/* XXX need sc_lock? */
> +	rv = (kn->kn_data > 0) || sc->sc_state != VIO9P_S_INIT;
> +
> +	return rv;
> +}
> +
> +static const struct filterops vio9p_read_filtops = {
> +	.f_isfd = 1,
> +	.f_attach = NULL,
> +	.f_detach = filt_vio9p_detach,
> +	.f_event = filt_vio9p_read,
> +};
> +
> +static int
> +filt_vio9p_write(struct knote *kn, long hint)
> +{
> +	struct vio9p_softc *sc = kn->kn_hook;
> +
> +	/* XXX need sc_lock? */
> +	return sc->sc_state == VIO9P_S_INIT;
> +}
> +
> +static const struct filterops vio9p_write_filtops = {
> +	.f_isfd = 1,
> +	.f_attach = NULL,
> +	.f_detach = filt_vio9p_detach,
> +	.f_event = filt_vio9p_write,
> +};
> +
> +static int
> +vio9p_kqfilter(struct file *fp, struct knote *kn)
> +{
> +	struct vio9p_softc *sc = fp->f_data;
> +	struct klist *klist;
> +
> +	mutex_enter(&sc->sc_lock);
> +	switch (kn->kn_filter) {
> +	case EVFILT_READ:
> +		klist = &sc->sc_sel.sel_klist;
> +		kn->kn_fop = &vio9p_read_filtops;
> +		break;
> +
> +	case EVFILT_WRITE:
> +		klist = &sc->sc_sel.sel_klist;
> +		kn->kn_fop = &vio9p_write_filtops;
> +		break;
> +
> +	default:
> +		mutex_exit(&sc->sc_lock);
> +		log(LOG_ERR, "%s: kn_filter=%u\n", __func__, kn->kn_filter);
> +		return EINVAL;
> +	}
> +
> +	kn->kn_hook = sc;
> +
> +	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
> +	mutex_exit(&sc->sc_lock);
> +
> +	return 0;
> +}
> +
> +CFATTACH_DECL_NEW(vio9p, sizeof(struct vio9p_softc),
> +    vio9p_match, vio9p_attach, NULL, NULL);
> +
> +static int
> +vio9p_match(device_t parent, cfdata_t match, void *aux)
> +{
> +	struct virtio_attach_args *va = aux;
> +
> +	if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_9P)
> +		return 1;
> +
> +	return 0;
> +}
> +
> +static void
> +vio9p_attach(device_t parent, device_t self, void *aux)
> +{
> +	struct vio9p_softc *sc = device_private(self);
> +	struct virtio_softc *vsc = device_private(parent);
> +	int error;
> +
> +	if (virtio_child(vsc) != NULL) {
> +		aprint_normal(": child already attached for %s; "
> +			      "something wrong...\n", device_xname(parent));
> +		return;
> +	}
> +
> +	sc->sc_dev = self;
> +	sc->sc_virtio = vsc;
> +
> +	virtio_child_attach_start(vsc, self, IPL_VM, NULL,
> +	    NULL, virtio_vq_intr,
> +	    VIRTIO_F_PCI_INTR_MPSAFE | VIRTIO_F_PCI_INTR_SOFTINT, 0,
> +	    VIO9P_FLAG_BITS);
> +
> +	error = virtio_alloc_vq(vsc, &sc->sc_vq[0], 0, VIO9P_MAX_REQLEN,
> +	    VIO9P_N_SEGMENTS * 2, "vio9p");
> +	if (error != 0)
> +		goto err_none;
> +
> +	sc->sc_vq[0].vq_done = vio9p_request_done;
> +
> +	virtio_child_attach_set_vqs(vsc, sc->sc_vq, 1);
> +
> +	sc->sc_buf_tx = kmem_alloc(VIO9P_MAX_REQLEN, KM_SLEEP);
> +	sc->sc_buf_rx = kmem_alloc(VIO9P_MAX_REQLEN, KM_SLEEP);
> +
> +	error = bus_dmamap_create(virtio_dmat(vsc), VIO9P_MAX_REQLEN,
> +	    VIO9P_N_SEGMENTS, VIO9P_SEGSIZE, 0, BUS_DMA_WAITOK, &sc->sc_dmamap_tx);
> +	if (error != 0) {
> +		aprint_error_dev(sc->sc_dev, "bus_dmamap_create failed: %d\n",
> +		    error);
> +		goto err_vq;
> +	}
> +	error = bus_dmamap_create(virtio_dmat(vsc), VIO9P_MAX_REQLEN,
> +	    VIO9P_N_SEGMENTS, VIO9P_SEGSIZE, 0, BUS_DMA_WAITOK, &sc->sc_dmamap_rx);
> +	if (error != 0) {
> +		aprint_error_dev(sc->sc_dev, "bus_dmamap_create failed: %d\n",
> +		    error);
> +		goto err_vq;
> +	}
> +
> +	error = bus_dmamap_load(virtio_dmat(vsc), sc->sc_dmamap_tx,
> +	    sc->sc_buf_tx, VIO9P_MAX_REQLEN, NULL, BUS_DMA_WAITOK | BUS_DMA_WRITE);
> +	if (error != 0) {
> +		aprint_error_dev(sc->sc_dev, "bus_dmamap_load failed: %d\n",
> +		    error);
> +		goto err_dmamap;
> +	}
> +	error = bus_dmamap_load(virtio_dmat(vsc), sc->sc_dmamap_rx,
> +	    sc->sc_buf_rx, VIO9P_MAX_REQLEN, NULL, BUS_DMA_WAITOK | BUS_DMA_READ);
> +	if (error != 0) {
> +		aprint_error_dev(sc->sc_dev, "bus_dmamap_load failed: %d\n",
> +		    error);
> +		goto err_dmamap;
> +	}
> +
> +	sc->sc_state = VIO9P_S_INIT;
> +	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
> +	cv_init(&sc->sc_wait, "vio9p");
> +
> +	vio9p_read_config(sc);
> +	aprint_normal_dev(self, "tagged as %s\n", sc->sc_tag);
> +
> +	error = virtio_child_attach_finish(vsc);
> +	if (error != 0)
> +		goto err_mutex;
> +
> +	return;
> +
> +err_mutex:
> +	cv_destroy(&sc->sc_wait);
> +	mutex_destroy(&sc->sc_lock);
> +err_dmamap:
> +	bus_dmamap_destroy(virtio_dmat(vsc), sc->sc_dmamap_tx);
> +	bus_dmamap_destroy(virtio_dmat(vsc), sc->sc_dmamap_rx);
> +err_vq:
> +	virtio_free_vq(vsc, &sc->sc_vq[0]);
> +err_none:
> +	virtio_child_attach_failed(vsc);
> +	return;
> +}
> +
> +static void
> +vio9p_read_config(struct vio9p_softc *sc)
> +{
> +	device_t dev = sc->sc_dev;
> +	uint8_t reg;
> +	int i;
> +
> +	/* these values are explicitly specified as little-endian */
> +	reg = virtio_read_device_config_2(sc->sc_virtio, VIO9P_CONFIG_TAG_LEN);
> +	sc->sc_taglen = le16toh(reg);
> +
> +	if (sc->sc_taglen > P9_MAX_TAG_LEN) {
> +		aprint_error_dev(dev, "warning: tag is trimmed from %u to %u\n",
> +		    sc->sc_taglen, P9_MAX_TAG_LEN);
> +		sc->sc_taglen = P9_MAX_TAG_LEN;
> +	}
> +
> +	for (i = 0; i < sc->sc_taglen; i++) {
> +		reg = virtio_read_device_config_1(sc->sc_virtio,
> +		    VIO9P_CONFIG_TAG + i);
> +		sc->sc_tag[i] = reg;
> +	}
> +	sc->sc_tag[i] = '\0';
> +}
> +
> +static int
> +vio9p_request_done(struct virtqueue *vq)
> +{
> +	struct virtio_softc *vsc = vq->vq_owner;
> +	struct vio9p_softc *sc = device_private(virtio_child(vsc));
> +
> +	DLOG("enter");
> +
> +	mutex_enter(&sc->sc_lock);
> +	sc->sc_state = VIO9P_S_REPLIED;
> +	cv_broadcast(&sc->sc_wait);
> +	selnotify(&sc->sc_sel, 0, 1);
> +	mutex_exit(&sc->sc_lock);
> +
> +	return 1;
> +}
> +
> +MODULE(MODULE_CLASS_DRIVER, vio9p, "virtio");
> +
> +#ifdef _MODULE
> +#include "ioconf.c"
> +#endif
> +
> +static int
> +vio9p_modcmd(modcmd_t cmd, void *opaque)
> +{
> +#ifdef _MODULE
> +	devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
> +#endif
> +	int error = 0;
> +
> +#ifdef _MODULE
> +	switch (cmd) {
> +	case MODULE_CMD_INIT:
> +		error = config_init_component(cfdriver_ioconf_vio9p,
> +		    cfattach_ioconf_vio9p, cfdata_ioconf_vio9p);
> +		devsw_attach(vio9p_cd.cd_name, NULL, &bmajor,
> +		    &vio9p_cdevsw, &cmajor);
> +		break;
> +	case MODULE_CMD_FINI:
> +		error = config_fini_component(cfdriver_ioconf_vio9p,
> +		    cfattach_ioconf_vio9p, cfdata_ioconf_vio9p);
> +		break;
> +	default:
> +		error = ENOTTY;
> +		break;
> +	}
> +#endif
> +
> +	return error;
> +}
>
> Index: src/sys/modules/vio9p/Makefile
> diff -u /dev/null src/sys/modules/vio9p/Makefile:1.1
> --- /dev/null	Mon Oct 28 02:56:41 2019
> +++ src/sys/modules/vio9p/Makefile	Mon Oct 28 02:56:40 2019
> @@ -0,0 +1,12 @@
> +#	$NetBSD: Makefile,v 1.1 2019/10/28 02:56:40 ozaki-r Exp $
> +
> +.include "../Makefile.inc"
> +
> +.PATH:	${S}/dev/pci
> +
> +KMOD=	vio9p
> +IOCONF=	vio9p.ioconf
> +
> +SRCS+=	vio9p.c
> +
> +.include <bsd.kmodule.mk>
> Index: src/sys/modules/vio9p/vio9p.ioconf
> diff -u /dev/null src/sys/modules/vio9p/vio9p.ioconf:1.1
> --- /dev/null	Mon Oct 28 02:56:41 2019
> +++ src/sys/modules/vio9p/vio9p.ioconf	Mon Oct 28 02:56:40 2019
> @@ -0,0 +1,10 @@
> +#       $NetBSD: vio9p.ioconf,v 1.1 2019/10/28 02:56:40 ozaki-r Exp $
> +
> +ioconf vio9p
> +
> +include "conf/files"
> +include "dev/pci/files.pci"
> +
> +pseudo-root virtio*
> +
> +vio9p* at virtio?
>

-- 
Ryo ONODERA // ryo%tetera.org@localhost
PGP fingerprint = 82A2 DC91 76E0 A10A 8ABB  FD1B F404 27FA C7D1 15F3


Home | Main Index | Thread Index | Old Index