xref: /freebsd/sys/dev/virtio/random/virtio_random.c (revision bec2d7e9a286525cd2349cb11e35010021681045)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013, Bryan Venteicher <bryanv@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
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.
16  *
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.
27  */
28 
29 /* Driver for VirtIO entropy device. */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
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>
40 
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <sys/bus.h>
44 
45 #include <dev/virtio/virtio.h>
46 #include <dev/virtio/virtqueue.h>
47 
48 struct vtrnd_softc {
49 	uint64_t		 vtrnd_features;
50 	struct callout		 vtrnd_callout;
51 	struct virtqueue	*vtrnd_vq;
52 };
53 
54 static int	vtrnd_modevent(module_t, int, void *);
55 
56 static int	vtrnd_probe(device_t);
57 static int	vtrnd_attach(device_t);
58 static int	vtrnd_detach(device_t);
59 
60 static void	vtrnd_negotiate_features(device_t);
61 static int	vtrnd_alloc_virtqueue(device_t);
62 static void	vtrnd_harvest(struct vtrnd_softc *);
63 static void	vtrnd_timer(void *);
64 
65 #define VTRND_FEATURES	0
66 
67 static struct virtio_feature_desc vtrnd_feature_desc[] = {
68 	{ 0, NULL }
69 };
70 
71 static device_method_t vtrnd_methods[] = {
72 	/* Device methods. */
73 	DEVMETHOD(device_probe,		vtrnd_probe),
74 	DEVMETHOD(device_attach,	vtrnd_attach),
75 	DEVMETHOD(device_detach,	vtrnd_detach),
76 
77 	DEVMETHOD_END
78 };
79 
80 static driver_t vtrnd_driver = {
81 	"vtrnd",
82 	vtrnd_methods,
83 	sizeof(struct vtrnd_softc)
84 };
85 static devclass_t vtrnd_devclass;
86 
87 DRIVER_MODULE(virtio_random, virtio_pci, vtrnd_driver, vtrnd_devclass,
88     vtrnd_modevent, 0);
89 MODULE_VERSION(virtio_random, 1);
90 MODULE_DEPEND(virtio_random, virtio, 1, 1, 1);
91 
92 static int
93 vtrnd_modevent(module_t mod, int type, void *unused)
94 {
95 	int error;
96 
97 	switch (type) {
98 	case MOD_LOAD:
99 	case MOD_QUIESCE:
100 	case MOD_UNLOAD:
101 	case MOD_SHUTDOWN:
102 		error = 0;
103 		break;
104 	default:
105 		error = EOPNOTSUPP;
106 		break;
107 	}
108 
109 	return (error);
110 }
111 
112 static int
113 vtrnd_probe(device_t dev)
114 {
115 
116 	if (virtio_get_device_type(dev) != VIRTIO_ID_ENTROPY)
117 		return (ENXIO);
118 
119 	device_set_desc(dev, "VirtIO Entropy Adapter");
120 
121 	return (BUS_PROBE_DEFAULT);
122 }
123 
124 static int
125 vtrnd_attach(device_t dev)
126 {
127 	struct vtrnd_softc *sc;
128 	int error;
129 
130 	sc = device_get_softc(dev);
131 
132 	callout_init(&sc->vtrnd_callout, 1);
133 
134 	virtio_set_feature_desc(dev, vtrnd_feature_desc);
135 	vtrnd_negotiate_features(dev);
136 
137 	error = vtrnd_alloc_virtqueue(dev);
138 	if (error) {
139 		device_printf(dev, "cannot allocate virtqueue\n");
140 		goto fail;
141 	}
142 
143 	callout_reset(&sc->vtrnd_callout, 5 * hz, vtrnd_timer, sc);
144 
145 fail:
146 	if (error)
147 		vtrnd_detach(dev);
148 
149 	return (error);
150 }
151 
152 static int
153 vtrnd_detach(device_t dev)
154 {
155 	struct vtrnd_softc *sc;
156 
157 	sc = device_get_softc(dev);
158 
159 	callout_drain(&sc->vtrnd_callout);
160 
161 	return (0);
162 }
163 
164 static void
165 vtrnd_negotiate_features(device_t dev)
166 {
167 	struct vtrnd_softc *sc;
168 
169 	sc = device_get_softc(dev);
170 	sc->vtrnd_features = virtio_negotiate_features(dev, VTRND_FEATURES);
171 }
172 
173 static int
174 vtrnd_alloc_virtqueue(device_t dev)
175 {
176 	struct vtrnd_softc *sc;
177 	struct vq_alloc_info vq_info;
178 
179 	sc = device_get_softc(dev);
180 
181 	VQ_ALLOC_INFO_INIT(&vq_info, 0, NULL, sc, &sc->vtrnd_vq,
182 	    "%s request", device_get_nameunit(dev));
183 
184 	return (virtio_alloc_virtqueues(dev, 0, 1, &vq_info));
185 }
186 
187 static void
188 vtrnd_harvest(struct vtrnd_softc *sc)
189 {
190 	struct sglist_seg segs[1];
191 	struct sglist sg;
192 	struct virtqueue *vq;
193 	uint32_t value;
194 	int error;
195 
196 	vq = sc->vtrnd_vq;
197 
198 	sglist_init(&sg, 1, segs);
199 	error = sglist_append(&sg, &value, sizeof(value));
200 	KASSERT(error == 0 && sg.sg_nseg == 1,
201 	    ("%s: error %d adding buffer to sglist", __func__, error));
202 
203 	if (!virtqueue_empty(vq))
204 		return;
205 	if (virtqueue_enqueue(vq, &value, &sg, 0, 1) != 0)
206 		return;
207 
208 	/*
209 	 * Poll for the response, but the command is likely already
210 	 * done when we return from the notify.
211 	 */
212 	virtqueue_notify(vq);
213 	virtqueue_poll(vq, NULL);
214 
215 	random_harvest_queue(&value, sizeof(value), RANDOM_PURE_VIRTIO);
216 }
217 
218 static void
219 vtrnd_timer(void *xsc)
220 {
221 	struct vtrnd_softc *sc;
222 
223 	sc = xsc;
224 
225 	vtrnd_harvest(sc);
226 	callout_schedule(&sc->vtrnd_callout, 5 * hz);
227 }
228