1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 #include <sys/param.h> 33 #include <sys/types.h> 34 #include <sys/eventhandler.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/sglist.h> 39 #include <sys/random.h> 40 #include <sys/stdatomic.h> 41 42 #include <machine/bus.h> 43 #include <machine/resource.h> 44 #include <sys/bus.h> 45 46 #include <dev/random/randomdev.h> 47 #include <dev/random/random_harvestq.h> 48 #include <dev/virtio/virtio.h> 49 #include <dev/virtio/virtqueue.h> 50 51 struct vtrnd_softc { 52 device_t vtrnd_dev; 53 uint64_t vtrnd_features; 54 struct virtqueue *vtrnd_vq; 55 eventhandler_tag eh; 56 bool inactive; 57 }; 58 59 static int vtrnd_modevent(module_t, int, void *); 60 61 static int vtrnd_probe(device_t); 62 static int vtrnd_attach(device_t); 63 static int vtrnd_detach(device_t); 64 static int vtrnd_shutdown(device_t); 65 66 static int vtrnd_negotiate_features(struct vtrnd_softc *); 67 static int vtrnd_setup_features(struct vtrnd_softc *); 68 static int vtrnd_alloc_virtqueue(struct vtrnd_softc *); 69 static int vtrnd_harvest(struct vtrnd_softc *, void *, size_t *); 70 static unsigned vtrnd_read(void *, unsigned); 71 72 #define VTRND_FEATURES 0 73 74 static struct virtio_feature_desc vtrnd_feature_desc[] = { 75 { 0, NULL } 76 }; 77 78 static struct random_source random_vtrnd = { 79 .rs_ident = "VirtIO Entropy Adapter", 80 .rs_source = RANDOM_PURE_VIRTIO, 81 .rs_read = vtrnd_read, 82 }; 83 84 /* Kludge for API limitations of random(4). */ 85 static _Atomic(struct vtrnd_softc *) g_vtrnd_softc; 86 87 static device_method_t vtrnd_methods[] = { 88 /* Device methods. */ 89 DEVMETHOD(device_probe, vtrnd_probe), 90 DEVMETHOD(device_attach, vtrnd_attach), 91 DEVMETHOD(device_detach, vtrnd_detach), 92 DEVMETHOD(device_shutdown, vtrnd_shutdown), 93 94 DEVMETHOD_END 95 }; 96 97 static driver_t vtrnd_driver = { 98 "vtrnd", 99 vtrnd_methods, 100 sizeof(struct vtrnd_softc) 101 }; 102 103 VIRTIO_DRIVER_MODULE(virtio_random, vtrnd_driver, vtrnd_modevent, NULL); 104 MODULE_VERSION(virtio_random, 1); 105 MODULE_DEPEND(virtio_random, virtio, 1, 1, 1); 106 MODULE_DEPEND(virtio_random, random_device, 1, 1, 1); 107 108 VIRTIO_SIMPLE_PNPINFO(virtio_random, VIRTIO_ID_ENTROPY, 109 "VirtIO Entropy Adapter"); 110 111 static int 112 vtrnd_modevent(module_t mod, int type, void *unused) 113 { 114 int error; 115 116 switch (type) { 117 case MOD_LOAD: 118 case MOD_QUIESCE: 119 case MOD_UNLOAD: 120 case MOD_SHUTDOWN: 121 error = 0; 122 break; 123 default: 124 error = EOPNOTSUPP; 125 break; 126 } 127 128 return (error); 129 } 130 131 static int 132 vtrnd_probe(device_t dev) 133 { 134 return (VIRTIO_SIMPLE_PROBE(dev, virtio_random)); 135 } 136 137 static int 138 vtrnd_attach(device_t dev) 139 { 140 struct vtrnd_softc *sc, *exp; 141 int error; 142 143 sc = device_get_softc(dev); 144 sc->vtrnd_dev = dev; 145 virtio_set_feature_desc(dev, vtrnd_feature_desc); 146 147 error = vtrnd_setup_features(sc); 148 if (error) { 149 device_printf(dev, "cannot setup features\n"); 150 goto fail; 151 } 152 153 error = vtrnd_alloc_virtqueue(sc); 154 if (error) { 155 device_printf(dev, "cannot allocate virtqueue\n"); 156 goto fail; 157 } 158 159 exp = NULL; 160 if (!atomic_compare_exchange_strong_explicit(&g_vtrnd_softc, &exp, sc, 161 memory_order_release, memory_order_acquire)) { 162 error = EEXIST; 163 goto fail; 164 } 165 166 sc->eh = EVENTHANDLER_REGISTER(shutdown_post_sync, 167 vtrnd_shutdown, dev, SHUTDOWN_PRI_LAST + 1); /* ??? */ 168 if (sc->eh == NULL) { 169 device_printf(dev, "Shutdown event registration failed\n"); 170 error = ENXIO; 171 goto fail; 172 } 173 174 sc->inactive = false; 175 random_source_register(&random_vtrnd); 176 177 fail: 178 if (error) 179 vtrnd_detach(dev); 180 181 return (error); 182 } 183 184 static int 185 vtrnd_detach(device_t dev) 186 { 187 struct vtrnd_softc *sc; 188 189 sc = device_get_softc(dev); 190 KASSERT( 191 atomic_load_explicit(&g_vtrnd_softc, memory_order_acquire) == sc, 192 ("only one global instance at a time")); 193 194 sc->inactive = true; 195 if (sc->eh != NULL) { 196 EVENTHANDLER_DEREGISTER(shutdown_post_sync, sc->eh); 197 sc->eh = NULL; 198 } 199 random_source_deregister(&random_vtrnd); 200 atomic_store_explicit(&g_vtrnd_softc, NULL, memory_order_release); 201 return (0); 202 } 203 204 static int 205 vtrnd_shutdown(device_t dev) 206 { 207 struct vtrnd_softc *sc; 208 209 sc = device_get_softc(dev); 210 sc->inactive = true; 211 212 return(0); 213 } 214 215 static int 216 vtrnd_negotiate_features(struct vtrnd_softc *sc) 217 { 218 device_t dev; 219 uint64_t features; 220 221 dev = sc->vtrnd_dev; 222 features = VTRND_FEATURES; 223 224 sc->vtrnd_features = virtio_negotiate_features(dev, features); 225 return (virtio_finalize_features(dev)); 226 } 227 228 static int 229 vtrnd_setup_features(struct vtrnd_softc *sc) 230 { 231 int error; 232 233 error = vtrnd_negotiate_features(sc); 234 if (error) 235 return (error); 236 237 return (0); 238 } 239 240 static int 241 vtrnd_alloc_virtqueue(struct vtrnd_softc *sc) 242 { 243 device_t dev; 244 struct vq_alloc_info vq_info; 245 246 dev = sc->vtrnd_dev; 247 248 VQ_ALLOC_INFO_INIT(&vq_info, 0, NULL, sc, &sc->vtrnd_vq, 249 "%s request", device_get_nameunit(dev)); 250 251 return (virtio_alloc_virtqueues(dev, 0, 1, &vq_info)); 252 } 253 254 static int 255 vtrnd_harvest(struct vtrnd_softc *sc, void *buf, size_t *sz) 256 { 257 struct sglist_seg segs[1]; 258 struct sglist sg; 259 struct virtqueue *vq; 260 uint32_t value[HARVESTSIZE] __aligned(sizeof(uint32_t) * HARVESTSIZE); 261 uint32_t rdlen; 262 int error; 263 264 _Static_assert(sizeof(value) < PAGE_SIZE, "sglist assumption"); 265 266 if (sc->inactive) 267 return (EDEADLK); 268 269 sglist_init(&sg, 1, segs); 270 error = sglist_append(&sg, value, *sz); 271 if (error != 0) 272 panic("%s: sglist_append error=%d", __func__, error); 273 274 vq = sc->vtrnd_vq; 275 KASSERT(virtqueue_empty(vq), ("%s: non-empty queue", __func__)); 276 277 error = virtqueue_enqueue(vq, buf, &sg, 0, 1); 278 if (error != 0) 279 return (error); 280 281 /* 282 * Poll for the response, but the command is likely already 283 * done when we return from the notify. 284 */ 285 virtqueue_notify(vq); 286 virtqueue_poll(vq, &rdlen); 287 288 if (rdlen > *sz) 289 panic("%s: random device wrote %zu bytes beyond end of provided" 290 " buffer %p:%zu", __func__, (size_t)rdlen - *sz, 291 (void *)value, *sz); 292 else if (rdlen == 0) 293 return (EAGAIN); 294 *sz = MIN(rdlen, *sz); 295 memcpy(buf, value, *sz); 296 explicit_bzero(value, *sz); 297 return (0); 298 } 299 300 static unsigned 301 vtrnd_read(void *buf, unsigned usz) 302 { 303 struct vtrnd_softc *sc; 304 size_t sz; 305 int error; 306 307 sc = g_vtrnd_softc; 308 if (sc == NULL) 309 return (0); 310 311 sz = usz; 312 error = vtrnd_harvest(sc, buf, &sz); 313 if (error != 0) 314 return (0); 315 316 return (sz); 317 } 318