2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013, Bryan Venteicher <bryanv@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /* Driver for VirtIO entropy device. */
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/sglist.h>
38 #include <sys/callout.h>
39 #include <sys/random.h>
41 #include <machine/bus.h>
42 #include <machine/resource.h>
45 #include <dev/virtio/virtio.h>
46 #include <dev/virtio/virtqueue.h>
50 uint64_t vtrnd_features;
51 struct callout vtrnd_callout;
52 struct virtqueue *vtrnd_vq;
55 static int vtrnd_modevent(module_t, int, void *);
57 static int vtrnd_probe(device_t);
58 static int vtrnd_attach(device_t);
59 static int vtrnd_detach(device_t);
61 static void vtrnd_negotiate_features(struct vtrnd_softc *);
62 static int vtrnd_alloc_virtqueue(struct vtrnd_softc *);
63 static void vtrnd_harvest(struct vtrnd_softc *);
64 static void vtrnd_timer(void *);
66 #define VTRND_FEATURES 0
68 static struct virtio_feature_desc vtrnd_feature_desc[] = {
72 static device_method_t vtrnd_methods[] = {
74 DEVMETHOD(device_probe, vtrnd_probe),
75 DEVMETHOD(device_attach, vtrnd_attach),
76 DEVMETHOD(device_detach, vtrnd_detach),
81 static driver_t vtrnd_driver = {
84 sizeof(struct vtrnd_softc)
86 static devclass_t vtrnd_devclass;
88 DRIVER_MODULE(virtio_random, virtio_pci, vtrnd_driver, vtrnd_devclass,
90 MODULE_VERSION(virtio_random, 1);
91 MODULE_DEPEND(virtio_random, virtio, 1, 1, 1);
94 vtrnd_modevent(module_t mod, int type, void *unused)
114 vtrnd_probe(device_t dev)
117 if (virtio_get_device_type(dev) != VIRTIO_ID_ENTROPY)
120 device_set_desc(dev, "VirtIO Entropy Adapter");
122 return (BUS_PROBE_DEFAULT);
126 vtrnd_attach(device_t dev)
128 struct vtrnd_softc *sc;
131 sc = device_get_softc(dev);
134 callout_init(&sc->vtrnd_callout, 1);
136 virtio_set_feature_desc(dev, vtrnd_feature_desc);
137 vtrnd_negotiate_features(sc);
139 error = vtrnd_alloc_virtqueue(sc);
141 device_printf(dev, "cannot allocate virtqueue\n");
145 callout_reset(&sc->vtrnd_callout, 5 * hz, vtrnd_timer, sc);
155 vtrnd_detach(device_t dev)
157 struct vtrnd_softc *sc;
159 sc = device_get_softc(dev);
161 callout_drain(&sc->vtrnd_callout);
167 vtrnd_negotiate_features(struct vtrnd_softc *sc)
173 features = VTRND_FEATURES;
175 sc->vtrnd_features = virtio_negotiate_features(dev, features);
179 vtrnd_alloc_virtqueue(struct vtrnd_softc *sc)
182 struct vq_alloc_info vq_info;
186 VQ_ALLOC_INFO_INIT(&vq_info, 0, NULL, sc, &sc->vtrnd_vq,
187 "%s request", device_get_nameunit(dev));
189 return (virtio_alloc_virtqueues(dev, 0, 1, &vq_info));
193 vtrnd_harvest(struct vtrnd_softc *sc)
195 struct sglist_seg segs[1];
197 struct virtqueue *vq;
203 sglist_init(&sg, 1, segs);
204 error = sglist_append(&sg, &value, sizeof(value));
205 KASSERT(error == 0 && sg.sg_nseg == 1,
206 ("%s: error %d adding buffer to sglist", __func__, error));
208 if (!virtqueue_empty(vq))
210 if (virtqueue_enqueue(vq, &value, &sg, 0, 1) != 0)
214 * Poll for the response, but the command is likely already
215 * done when we return from the notify.
217 virtqueue_notify(vq);
218 virtqueue_poll(vq, NULL);
220 random_harvest_queue(&value, sizeof(value), RANDOM_PURE_VIRTIO);
224 vtrnd_timer(void *xsc)
226 struct vtrnd_softc *sc;
231 callout_schedule(&sc->vtrnd_callout, 5 * hz);