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: stable/12/sys/dev/virtio/random/virtio_random.c 371675 2022-02-22 07:30:38Z obrien $");
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 MODULE_DEPEND(virtio_random, random_device, 1, 1, 1);
93 
94 static int
vtrnd_modevent(module_t mod,int type,void * unused)95 vtrnd_modevent(module_t mod, int type, void *unused)
96 {
97 	int error;
98 
99 	switch (type) {
100 	case MOD_LOAD:
101 	case MOD_QUIESCE:
102 	case MOD_UNLOAD:
103 	case MOD_SHUTDOWN:
104 		error = 0;
105 		break;
106 	default:
107 		error = EOPNOTSUPP;
108 		break;
109 	}
110 
111 	return (error);
112 }
113 
114 static int
vtrnd_probe(device_t dev)115 vtrnd_probe(device_t dev)
116 {
117 
118 	if (virtio_get_device_type(dev) != VIRTIO_ID_ENTROPY)
119 		return (ENXIO);
120 
121 	device_set_desc(dev, "VirtIO Entropy Adapter");
122 
123 	return (BUS_PROBE_DEFAULT);
124 }
125 
126 static int
vtrnd_attach(device_t dev)127 vtrnd_attach(device_t dev)
128 {
129 	struct vtrnd_softc *sc;
130 	int error;
131 
132 	sc = device_get_softc(dev);
133 	sc->vtrnd_dev = dev;
134 
135 	callout_init(&sc->vtrnd_callout, 1);
136 
137 	virtio_set_feature_desc(dev, vtrnd_feature_desc);
138 	vtrnd_negotiate_features(sc);
139 
140 	error = vtrnd_alloc_virtqueue(sc);
141 	if (error) {
142 		device_printf(dev, "cannot allocate virtqueue\n");
143 		goto fail;
144 	}
145 
146 	callout_reset(&sc->vtrnd_callout, 5 * hz, vtrnd_timer, sc);
147 
148 fail:
149 	if (error)
150 		vtrnd_detach(dev);
151 
152 	return (error);
153 }
154 
155 static int
vtrnd_detach(device_t dev)156 vtrnd_detach(device_t dev)
157 {
158 	struct vtrnd_softc *sc;
159 
160 	sc = device_get_softc(dev);
161 
162 	callout_drain(&sc->vtrnd_callout);
163 
164 	return (0);
165 }
166 
167 static void
vtrnd_negotiate_features(struct vtrnd_softc * sc)168 vtrnd_negotiate_features(struct vtrnd_softc *sc)
169 {
170 	device_t dev;
171 	uint64_t features;
172 
173 	dev = sc->vtrnd_dev;
174 	features = VTRND_FEATURES;
175 
176 	sc->vtrnd_features = virtio_negotiate_features(dev, features);
177 }
178 
179 static int
vtrnd_alloc_virtqueue(struct vtrnd_softc * sc)180 vtrnd_alloc_virtqueue(struct vtrnd_softc *sc)
181 {
182 	device_t dev;
183 	struct vq_alloc_info vq_info;
184 
185 	dev = sc->vtrnd_dev;
186 
187 	VQ_ALLOC_INFO_INIT(&vq_info, 0, NULL, sc, &sc->vtrnd_vq,
188 	    "%s request", device_get_nameunit(dev));
189 
190 	return (virtio_alloc_virtqueues(dev, 0, 1, &vq_info));
191 }
192 
193 static void
vtrnd_harvest(struct vtrnd_softc * sc)194 vtrnd_harvest(struct vtrnd_softc *sc)
195 {
196 	struct sglist_seg segs[1];
197 	struct sglist sg;
198 	struct virtqueue *vq;
199 	uint32_t value;
200 	int error;
201 
202 	vq = sc->vtrnd_vq;
203 
204 	sglist_init(&sg, 1, segs);
205 	error = sglist_append(&sg, &value, sizeof(value));
206 	KASSERT(error == 0 && sg.sg_nseg == 1,
207 	    ("%s: error %d adding buffer to sglist", __func__, error));
208 
209 	if (!virtqueue_empty(vq))
210 		return;
211 	if (virtqueue_enqueue(vq, &value, &sg, 0, 1) != 0)
212 		return;
213 
214 	/*
215 	 * Poll for the response, but the command is likely already
216 	 * done when we return from the notify.
217 	 */
218 	virtqueue_notify(vq);
219 	virtqueue_poll(vq, NULL);
220 
221 	random_harvest_queue(&value, sizeof(value), RANDOM_PURE_VIRTIO);
222 }
223 
224 static void
vtrnd_timer(void * xsc)225 vtrnd_timer(void *xsc)
226 {
227 	struct vtrnd_softc *sc;
228 
229 	sc = xsc;
230 
231 	vtrnd_harvest(sc);
232 	callout_schedule(&sc->vtrnd_callout, 5 * hz);
233 }
234