Skip to content

Instantly share code, notes, and snippets.

@enukane
Created July 29, 2012 18:20
Show Gist options
  • Save enukane/3200815 to your computer and use it in GitHub Desktop.
Save enukane/3200815 to your computer and use it in GitHub Desktop.
WIP: guest-side virtio-9p device driver mock
Index: sys/dev/virtio/9p/virtio_9p.c
===================================================================
--- sys/dev/virtio/9p/virtio_9p.c (revision 0)
+++ sys/dev/virtio/9p/virtio_9p.c (working copy)
@@ -0,0 +1,181 @@
+/*-
+ * Copyright (c) 2011, Bryan Venteicher <[email protected]>
+ * 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 unmodified, 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.
+ */
+
+/* Driver for VirtFS devices. */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/endian.h>
+#include <sys/kthread.h>
+#include <sys/malloc.h>
+#include <sys/module.h>
+#include <sys/sglist.h>
+#include <sys/sysctl.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/queue.h>
+
+#include <machine/bus.h>
+#include <machine/resource.h>
+#include <sys/bus.h>
+#include <sys/rman.h>
+
+#include <dev/virtio/virtio.h>
+#include <dev/virtio/virtqueue.h>
+#include <dev/virtio/9p/virtio_9p.h>
+
+#include "virtio_if.h"
+
+struct vt9p_softc {
+ device_t vt9p_dev;
+ struct mtx vt9p_mtx;
+};
+
+static int vt9p_modevent(module_t, int, void *);
+
+static int vt9p_probe(device_t);
+static int vt9p_attach(device_t);
+static int vt9p_detach(device_t);
+static int vt9p_config_change(device_t);
+
+static device_method_t vt9p_methods[] = {
+ /* Device methods. */
+ DEVMETHOD(device_probe, vt9p_probe),
+ DEVMETHOD(device_attach, vt9p_attach),
+ DEVMETHOD(device_detach, vt9p_detach),
+
+ /* VirtIO methods. */
+ DEVMETHOD(virtio_config_change, vt9p_config_change),
+
+ { 0, 0 }
+};
+
+static driver_t vt9p_driver = {
+ "vt9p",
+ vt9p_methods,
+ sizeof(struct vt9p_softc)
+};
+static devclass_t vt9p_devclass;
+
+DRIVER_MODULE(virtio_9p, virtio_pci, vt9p_driver,
+ vt9p_devclass, vt9p_modevent, 0);
+MODULE_VERSION(virtio_9p, 1);
+MODULE_DEPEND(virito_9p, virtio, 1, 1, 1);
+
+static int
+vt9p_modevent(module_t mod, int type, void *unused)
+{
+ int error = 0;
+
+ printf("%u %s : type = %d\n", __LINE__, __func__, type);
+
+ switch (type) {
+ case MOD_LOAD:
+ break;
+ case MOD_QUIESCE:
+ case MOD_UNLOAD:
+ break;
+ case MOD_SHUTDOWN:
+ break;
+ default:
+ error = EOPNOTSUPP;
+ break;
+ }
+
+ return (error);
+
+}
+
+static int
+vt9p_probe(device_t dev)
+{
+ printf("%u %s : probing (%x <> VIRTIO_ID_9P %x)\n", __LINE__, __func__, virtio_get_device_type(dev), VIRTIO_ID_9P);
+ if (virtio_get_device_type(dev) != VIRTIO_ID_9P) {
+ return (ENXIO);
+ }
+
+ device_set_desc(dev, "VirtFS");
+
+ return (BUS_PROBE_DEFAULT);
+}
+
+static int
+vt9p_attach(device_t dev)
+{
+ struct vt9p_softc *sc;
+ int error = 0;
+
+ printf("%u %s attach\n", __LINE__, __func__);
+
+ sc = device_get_softc(dev);
+ sc->vt9p_dev = dev;
+
+ /* set feature desc */
+
+ /* negotiate feature */
+
+ /* get page */
+
+ /* alloc virtqueue */
+
+ /* setup intr */
+
+ /* enable queue intr */
+
+//fail:
+ if (error)
+ vt9p_detach(dev);
+
+ return error;
+}
+
+static int
+vt9p_detach(device_t dev)
+{
+ struct vt9p_softc *sc;
+
+ sc = device_get_softc(dev);
+
+ /* free got pages */
+
+ return 0;
+}
+
+static int
+vt9p_config_change(device_t dev)
+{
+ struct vt9p_softc *sc;
+
+ sc = device_get_softc(dev);
+
+ /* do something ? */
+
+ return 1;
+}
Index: sys/dev/virtio/9p/virtio_9p.h
===================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment