1 /* $OpenBSD: viornd.c,v 1.5 2021/11/05 11:38:29 mpi Exp $ */ 2 3 /* 4 * Copyright (c) 2014 Stefan Fritsch <sf@sfritsch.de> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/kernel.h> 22 #include <sys/timeout.h> 23 #include <machine/bus.h> 24 #include <sys/device.h> 25 #include <sys/pool.h> 26 #include <dev/pv/virtioreg.h> 27 #include <dev/pv/virtiovar.h> 28 29 /* 30 * The host may not have an unlimited supply of entropy. Therefore, we must 31 * not blindly get as much entropy as we can. Instead, we just request 32 * VIORND_BUFSIZE bytes at boot and every 15 * (1 << interval_shift) seconds. 33 * XXX There should be an API to check if we actually need more entropy. 34 * 35 * The lowest byte in the flags is used for transport specific settings. 36 * Therefore we use the second byte. 37 */ 38 #define VIORND_INTERVAL_SHIFT(f) ((f >> 8) & 0xf) 39 #define VIORND_INTERVAL_SHIFT_DEFAULT 5 40 #define VIORND_ONESHOT 0x1000 41 #define VIORND_BUFSIZE 16 42 43 #define VIORND_DEBUG 0 44 45 struct viornd_softc { 46 struct device sc_dev; 47 struct virtio_softc *sc_virtio; 48 49 struct virtqueue sc_vq; 50 int *sc_buf; 51 bus_dmamap_t sc_dmamap; 52 53 unsigned int sc_interval; 54 struct timeout sc_tick; 55 }; 56 57 int viornd_match(struct device *, void *, void *); 58 void viornd_attach(struct device *, struct device *, void *); 59 int viornd_vq_done(struct virtqueue *); 60 void viornd_tick(void *); 61 62 const struct cfattach viornd_ca = { 63 sizeof(struct viornd_softc), 64 viornd_match, 65 viornd_attach, 66 NULL 67 }; 68 69 struct cfdriver viornd_cd = { 70 NULL, "viornd", DV_DULL 71 }; 72 73 74 int viornd_match(struct device *parent, void *match, void *aux) 75 { 76 struct virtio_softc *va = aux; 77 if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_ENTROPY) 78 return 1; 79 return 0; 80 } 81 82 void 83 viornd_attach(struct device *parent, struct device *self, void *aux) 84 { 85 struct viornd_softc *sc = (struct viornd_softc *)self; 86 struct virtio_softc *vsc = (struct virtio_softc *)parent; 87 unsigned int shift; 88 89 vsc->sc_vqs = &sc->sc_vq; 90 vsc->sc_nvqs = 1; 91 vsc->sc_config_change = 0; 92 if (vsc->sc_child != NULL) 93 panic("already attached to something else"); 94 vsc->sc_child = self; 95 vsc->sc_ipl = IPL_NET; 96 sc->sc_virtio = vsc; 97 98 virtio_negotiate_features(vsc, NULL); 99 100 if (sc->sc_dev.dv_cfdata->cf_flags & VIORND_ONESHOT) { 101 sc->sc_interval = 0; 102 } else { 103 shift = VIORND_INTERVAL_SHIFT(sc->sc_dev.dv_cfdata->cf_flags); 104 if (shift == 0) 105 shift = VIORND_INTERVAL_SHIFT_DEFAULT; 106 sc->sc_interval = 15 * (1 << shift); 107 } 108 #if VIORND_DEBUG 109 printf(": request interval: %us\n", sc->sc_interval); 110 #endif 111 112 sc->sc_buf = dma_alloc(VIORND_BUFSIZE, PR_NOWAIT|PR_ZERO); 113 if (sc->sc_buf == NULL) { 114 printf(": Can't alloc dma buffer\n"); 115 goto err; 116 } 117 if (bus_dmamap_create(sc->sc_virtio->sc_dmat, VIORND_BUFSIZE, 1, 118 VIORND_BUFSIZE, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, 119 &sc->sc_dmamap)) { 120 printf(": Can't alloc dmamap\n"); 121 goto err; 122 } 123 if (bus_dmamap_load(sc->sc_virtio->sc_dmat, sc->sc_dmamap, 124 sc->sc_buf, VIORND_BUFSIZE, NULL, BUS_DMA_NOWAIT|BUS_DMA_READ)) { 125 printf(": Can't load dmamap\n"); 126 goto err2; 127 } 128 129 if (virtio_alloc_vq(vsc, &sc->sc_vq, 0, VIORND_BUFSIZE, 1, 130 "Entropy request") != 0) { 131 printf(": Can't alloc virtqueue\n"); 132 goto err2; 133 } 134 135 sc->sc_vq.vq_done = viornd_vq_done; 136 virtio_start_vq_intr(vsc, &sc->sc_vq); 137 timeout_set(&sc->sc_tick, viornd_tick, sc); 138 timeout_add(&sc->sc_tick, 1); 139 140 printf("\n"); 141 return; 142 err2: 143 bus_dmamap_destroy(vsc->sc_dmat, sc->sc_dmamap); 144 err: 145 vsc->sc_child = VIRTIO_CHILD_ERROR; 146 if (sc->sc_buf != NULL) { 147 dma_free(sc->sc_buf, VIORND_BUFSIZE); 148 sc->sc_buf = NULL; 149 } 150 return; 151 } 152 153 int 154 viornd_vq_done(struct virtqueue *vq) 155 { 156 struct virtio_softc *vsc = vq->vq_owner; 157 struct viornd_softc *sc = (struct viornd_softc *)vsc->sc_child; 158 int slot, len, i; 159 160 if (virtio_dequeue(vsc, vq, &slot, &len) != 0) 161 return 0; 162 bus_dmamap_sync(vsc->sc_dmat, sc->sc_dmamap, 0, VIORND_BUFSIZE, 163 BUS_DMASYNC_POSTREAD); 164 if (len > VIORND_BUFSIZE) { 165 printf("%s: inconsistent descriptor length %d > %d\n", 166 sc->sc_dev.dv_xname, len, VIORND_BUFSIZE); 167 goto out; 168 } 169 170 #if VIORND_DEBUG 171 printf("%s: got %d bytes of entropy\n", __func__, len); 172 #endif 173 for (i = 0; (i + 1) * sizeof(int) <= len; i++) 174 enqueue_randomness(sc->sc_buf[i]); 175 176 if (sc->sc_interval) 177 timeout_add_sec(&sc->sc_tick, sc->sc_interval); 178 179 out: 180 virtio_dequeue_commit(vq, slot); 181 return 1; 182 } 183 184 void 185 viornd_tick(void *arg) 186 { 187 struct viornd_softc *sc = arg; 188 struct virtio_softc *vsc = sc->sc_virtio; 189 struct virtqueue *vq = &sc->sc_vq; 190 int slot; 191 192 bus_dmamap_sync(vsc->sc_dmat, sc->sc_dmamap, 0, VIORND_BUFSIZE, 193 BUS_DMASYNC_PREREAD); 194 if (virtio_enqueue_prep(vq, &slot) != 0 || 195 virtio_enqueue_reserve(vq, slot, 1) != 0) { 196 panic("%s: virtqueue enqueue failed", sc->sc_dev.dv_xname); 197 } 198 virtio_enqueue(vq, slot, sc->sc_dmamap, 0); 199 virtio_enqueue_commit(vsc, vq, slot, 1); 200 } 201