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.
28 * $FreeBSD: head/sys/dev/virtio/random/virtio_random.c 338324 2018-08-26 12:51:46Z markm $
31 /* Driver for VirtIO entropy device. */
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/thread.h>
37 #include <sys/sglist.h>
38 #include <sys/callout.h>
39 #include <sys/random.h>
40 #include <sys/systm.h>
43 #include <dev/virtual/virtio/virtio/virtio.h>
44 #include <dev/virtual/virtio/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,
87 vtrnd_modevent, NULL);
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)
111 vtrnd_probe(device_t dev)
114 if (virtio_get_device_type(dev) != VIRTIO_ID_ENTROPY)
117 device_set_desc(dev, "VirtIO Entropy Adapter");
119 return (BUS_PROBE_DEFAULT);
123 vtrnd_attach(device_t dev)
125 struct vtrnd_softc *sc;
128 sc = device_get_softc(dev);
131 callout_init_mp(&sc->vtrnd_callout);
133 virtio_set_feature_desc(dev, vtrnd_feature_desc);
134 vtrnd_negotiate_features(sc);
136 error = vtrnd_alloc_virtqueue(sc);
138 device_printf(dev, "cannot allocate virtqueue\n");
142 callout_reset(&sc->vtrnd_callout, 5 * hz, vtrnd_timer, sc);
152 vtrnd_detach(device_t dev)
154 struct vtrnd_softc *sc;
156 sc = device_get_softc(dev);
158 callout_terminate(&sc->vtrnd_callout);
164 vtrnd_negotiate_features(struct vtrnd_softc *sc)
170 features = VTRND_FEATURES;
172 sc->vtrnd_features = virtio_negotiate_features(dev, features);
176 vtrnd_alloc_virtqueue(struct vtrnd_softc *sc)
179 struct vq_alloc_info vq_info;
183 VQ_ALLOC_INFO_INIT(&vq_info, 0, &sc->vtrnd_vq,
184 "%s request", device_get_nameunit(dev));
186 return (virtio_alloc_virtqueues(dev, 1, &vq_info));
190 vtrnd_harvest(struct vtrnd_softc *sc)
192 struct sglist_seg segs[1];
194 struct virtqueue *vq;
200 sglist_init(&sg, 1, segs);
201 error = sglist_append(&sg, &value, sizeof(value));
202 KASSERT(error == 0 && sg.sg_nseg == 1,
203 ("%s: error %d adding buffer to sglist", __func__, error));
205 if (!virtqueue_empty(vq))
207 if (virtqueue_enqueue(vq, &value, &sg, 0, 1) != 0)
211 * Poll for the response, but the command is likely already
212 * done when we return from the notify.
214 virtqueue_notify(vq, NULL);
215 virtqueue_poll(vq, NULL);
217 add_buffer_randomness_src((const char *)&value, sizeof(value),
222 vtrnd_timer(void *xsc)
224 struct vtrnd_softc *sc;
229 callout_reset(&sc->vtrnd_callout, 5 * hz, vtrnd_timer, sc);