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