xref: /freebsd/sys/dev/virtio/random/virtio_random.c (revision 0957b409)
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 	device_t		 vtrnd_dev;
50 	uint64_t		 vtrnd_features;
51 	struct callout		 vtrnd_callout;
52 	struct virtqueue	*vtrnd_vq;
53 };
54 
55 static int	vtrnd_modevent(module_t, int, void *);
56 
57 static int	vtrnd_probe(device_t);
58 static int	vtrnd_attach(device_t);
59 static int	vtrnd_detach(device_t);
60 
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 *);
65 
66 #define VTRND_FEATURES	0
67 
68 static struct virtio_feature_desc vtrnd_feature_desc[] = {
69 	{ 0, NULL }
70 };
71 
72 static device_method_t vtrnd_methods[] = {
73 	/* Device methods. */
74 	DEVMETHOD(device_probe,		vtrnd_probe),
75 	DEVMETHOD(device_attach,	vtrnd_attach),
76 	DEVMETHOD(device_detach,	vtrnd_detach),
77 
78 	DEVMETHOD_END
79 };
80 
81 static driver_t vtrnd_driver = {
82 	"vtrnd",
83 	vtrnd_methods,
84 	sizeof(struct vtrnd_softc)
85 };
86 static devclass_t vtrnd_devclass;
87 
88 DRIVER_MODULE(virtio_random, virtio_pci, vtrnd_driver, vtrnd_devclass,
89     vtrnd_modevent, 0);
90 MODULE_VERSION(virtio_random, 1);
91 MODULE_DEPEND(virtio_random, virtio, 1, 1, 1);
92 
93 static int
94 vtrnd_modevent(module_t mod, int type, void *unused)
95 {
96 	int error;
97 
98 	switch (type) {
99 	case MOD_LOAD:
100 	case MOD_QUIESCE:
101 	case MOD_UNLOAD:
102 	case MOD_SHUTDOWN:
103 		error = 0;
104 		break;
105 	default:
106 		error = EOPNOTSUPP;
107 		break;
108 	}
109 
110 	return (error);
111 }
112 
113 static int
114 vtrnd_probe(device_t dev)
115 {
116 
117 	if (virtio_get_device_type(dev) != VIRTIO_ID_ENTROPY)
118 		return (ENXIO);
119 
120 	device_set_desc(dev, "VirtIO Entropy Adapter");
121 
122 	return (BUS_PROBE_DEFAULT);
123 }
124 
125 static int
126 vtrnd_attach(device_t dev)
127 {
128 	struct vtrnd_softc *sc;
129 	int error;
130 
131 	sc = device_get_softc(dev);
132 	sc->vtrnd_dev = dev;
133 
134 	callout_init(&sc->vtrnd_callout, 1);
135 
136 	virtio_set_feature_desc(dev, vtrnd_feature_desc);
137 	vtrnd_negotiate_features(sc);
138 
139 	error = vtrnd_alloc_virtqueue(sc);
140 	if (error) {
141 		device_printf(dev, "cannot allocate virtqueue\n");
142 		goto fail;
143 	}
144 
145 	callout_reset(&sc->vtrnd_callout, 5 * hz, vtrnd_timer, sc);
146 
147 fail:
148 	if (error)
149 		vtrnd_detach(dev);
150 
151 	return (error);
152 }
153 
154 static int
155 vtrnd_detach(device_t dev)
156 {
157 	struct vtrnd_softc *sc;
158 
159 	sc = device_get_softc(dev);
160 
161 	callout_drain(&sc->vtrnd_callout);
162 
163 	return (0);
164 }
165 
166 static void
167 vtrnd_negotiate_features(struct vtrnd_softc *sc)
168 {
169 	device_t dev;
170 	uint64_t features;
171 
172 	dev = sc->vtrnd_dev;
173 	features = VTRND_FEATURES;
174 
175 	sc->vtrnd_features = virtio_negotiate_features(dev, features);
176 }
177 
178 static int
179 vtrnd_alloc_virtqueue(struct vtrnd_softc *sc)
180 {
181 	device_t dev;
182 	struct vq_alloc_info vq_info;
183 
184 	dev = sc->vtrnd_dev;
185 
186 	VQ_ALLOC_INFO_INIT(&vq_info, 0, NULL, sc, &sc->vtrnd_vq,
187 	    "%s request", device_get_nameunit(dev));
188 
189 	return (virtio_alloc_virtqueues(dev, 0, 1, &vq_info));
190 }
191 
192 static void
193 vtrnd_harvest(struct vtrnd_softc *sc)
194 {
195 	struct sglist_seg segs[1];
196 	struct sglist sg;
197 	struct virtqueue *vq;
198 	uint32_t value;
199 	int error;
200 
201 	vq = sc->vtrnd_vq;
202 
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));
207 
208 	if (!virtqueue_empty(vq))
209 		return;
210 	if (virtqueue_enqueue(vq, &value, &sg, 0, 1) != 0)
211 		return;
212 
213 	/*
214 	 * Poll for the response, but the command is likely already
215 	 * done when we return from the notify.
216 	 */
217 	virtqueue_notify(vq);
218 	virtqueue_poll(vq, NULL);
219 
220 	random_harvest_queue(&value, sizeof(value), RANDOM_PURE_VIRTIO);
221 }
222 
223 static void
224 vtrnd_timer(void *xsc)
225 {
226 	struct vtrnd_softc *sc;
227 
228 	sc = xsc;
229 
230 	vtrnd_harvest(sc);
231 	callout_schedule(&sc->vtrnd_callout, 5 * hz);
232 }
233