2 * Copyright (c) 2013, 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 entropy device. */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/sglist.h>
36 #include <sys/callout.h>
37 #include <sys/random.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
43 #include <dev/virtio/virtio.h>
44 #include <dev/virtio/virtqueue.h>
48 uint64_t vtrnd_features;
49 struct callout vtrnd_callout;
50 struct virtqueue *vtrnd_vq;
53 static int vtrnd_modevent(module_t, int, void *);
55 static int vtrnd_probe(device_t);
56 static int vtrnd_attach(device_t);
57 static int vtrnd_detach(device_t);
59 static void vtrnd_negotiate_features(struct vtrnd_softc *);
60 static int vtrnd_alloc_virtqueue(struct vtrnd_softc *);
61 static void vtrnd_harvest(struct vtrnd_softc *);
62 static void vtrnd_timer(void *);
64 #define VTRND_FEATURES 0
66 static struct virtio_feature_desc vtrnd_feature_desc[] = {
70 static device_method_t vtrnd_methods[] = {
72 DEVMETHOD(device_probe, vtrnd_probe),
73 DEVMETHOD(device_attach, vtrnd_attach),
74 DEVMETHOD(device_detach, vtrnd_detach),
79 static driver_t vtrnd_driver = {
82 sizeof(struct vtrnd_softc)
84 static devclass_t vtrnd_devclass;
86 DRIVER_MODULE(virtio_random, virtio_pci, vtrnd_driver, vtrnd_devclass,
88 MODULE_VERSION(virtio_random, 1);
89 MODULE_DEPEND(virtio_random, virtio, 1, 1, 1);
92 vtrnd_modevent(module_t mod, int type, void *unused)
112 vtrnd_probe(device_t dev)
115 if (virtio_get_device_type(dev) != VIRTIO_ID_ENTROPY)
118 device_set_desc(dev, "VirtIO Entropy Adapter");
120 return (BUS_PROBE_DEFAULT);
124 vtrnd_attach(device_t dev)
126 struct vtrnd_softc *sc;
129 sc = device_get_softc(dev);
132 callout_init(&sc->vtrnd_callout, CALLOUT_MPSAFE);
134 virtio_set_feature_desc(dev, vtrnd_feature_desc);
135 vtrnd_negotiate_features(sc);
137 error = vtrnd_alloc_virtqueue(sc);
139 device_printf(dev, "cannot allocate virtqueue\n");
143 callout_reset(&sc->vtrnd_callout, 5 * hz, vtrnd_timer, sc);
153 vtrnd_detach(device_t dev)
155 struct vtrnd_softc *sc;
157 sc = device_get_softc(dev);
159 callout_drain(&sc->vtrnd_callout);
165 vtrnd_negotiate_features(struct vtrnd_softc *sc)
171 features = VTRND_FEATURES;
173 sc->vtrnd_features = virtio_negotiate_features(dev, features);
177 vtrnd_alloc_virtqueue(struct vtrnd_softc *sc)
180 struct vq_alloc_info vq_info;
184 VQ_ALLOC_INFO_INIT(&vq_info, 0, NULL, sc, &sc->vtrnd_vq,
185 "%s request", device_get_nameunit(dev));
187 return (virtio_alloc_virtqueues(dev, 0, 1, &vq_info));
191 vtrnd_harvest(struct vtrnd_softc *sc)
193 struct sglist_seg segs[1];
195 struct virtqueue *vq;
201 sglist_init(&sg, 1, segs);
202 error = sglist_append(&sg, &value, sizeof(value));
203 KASSERT(error == 0 && sg.sg_nseg == 1,
204 ("%s: error %d adding buffer to sglist", __func__, error));
206 if (!virtqueue_empty(vq))
208 if (virtqueue_enqueue(vq, &value, &sg, 0, 1) != 0)
212 * Poll for the response, but the command is likely already
213 * done when we return from the notify.
215 virtqueue_notify(vq);
216 virtqueue_poll(vq, NULL);
218 random_harvest(&value, sizeof(value), sizeof(value) * NBBY / 2,
223 vtrnd_timer(void *xsc)
225 struct vtrnd_softc *sc;
230 callout_schedule(&sc->vtrnd_callout, 5 * hz);