| 1 | /* $NetBSD: viornd.c,v 1.9 2015/10/27 16:04:19 christos Exp $ */ |
| 2 | /* $OpenBSD: viornd.c,v 1.1 2014/01/21 21:14:58 sf Exp $ */ |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 2014 The NetBSD Foundation, Inc. |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * This code is derived from software contributed to The NetBSD Foundation |
| 9 | * by Thor Lancelot Simon (tls@NetBSD.org). |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * 1. Redistributions of source code must retain the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer. |
| 16 | * 2. Redistributions in binary form must reproduce the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer in the |
| 18 | * documentation and/or other materials provided with the distribution. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | * POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * Copyright (c) 2014 Stefan Fritsch <sf@sfritsch.de> |
| 35 | * |
| 36 | * Permission to use, copy, modify, and distribute this software for any |
| 37 | * purpose with or without fee is hereby granted, provided that the above |
| 38 | * copyright notice and this permission notice appear in all copies. |
| 39 | * |
| 40 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 41 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 42 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 43 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 44 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 45 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 46 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 47 | */ |
| 48 | |
| 49 | #include <sys/param.h> |
| 50 | #include <sys/systm.h> |
| 51 | #include <sys/kernel.h> |
| 52 | #include <sys/device.h> |
| 53 | #include <sys/callout.h> |
| 54 | #include <sys/rndsource.h> |
| 55 | #include <sys/mutex.h> |
| 56 | #include <dev/pci/pcidevs.h> |
| 57 | #include <dev/pci/pcivar.h> |
| 58 | #include <dev/pci/pcivar.h> |
| 59 | #include <dev/pci/virtioreg.h> |
| 60 | #include <dev/pci/virtiovar.h> |
| 61 | |
| 62 | #define VIORND_BUFSIZE 32 |
| 63 | |
| 64 | #define VIORND_DEBUG 0 |
| 65 | |
| 66 | struct viornd_softc { |
| 67 | device_t sc_dev; |
| 68 | struct virtio_softc *sc_virtio; |
| 69 | |
| 70 | kmutex_t sc_mutex; |
| 71 | bool sc_active; |
| 72 | |
| 73 | void *sc_buf; |
| 74 | struct virtqueue sc_vq; |
| 75 | bus_dmamap_t sc_dmamap; |
| 76 | krndsource_t sc_rndsource; |
| 77 | }; |
| 78 | |
| 79 | int viornd_match(device_t, cfdata_t, void *); |
| 80 | void viornd_attach(device_t, device_t, void *); |
| 81 | int viornd_vq_done(struct virtqueue *); |
| 82 | |
| 83 | CFATTACH_DECL_NEW(viornd, sizeof(struct viornd_softc), |
| 84 | viornd_match, viornd_attach, NULL, NULL); |
| 85 | |
| 86 | static void |
| 87 | viornd_get(size_t bytes, void *priv) |
| 88 | { |
| 89 | struct viornd_softc *sc = priv; |
| 90 | struct virtio_softc *vsc = sc->sc_virtio; |
| 91 | struct virtqueue *vq = &sc->sc_vq; |
| 92 | int slot; |
| 93 | |
| 94 | #if VIORND_DEBUG |
| 95 | aprint_normal("%s: asked for %d bytes of entropy\n" , __func__, |
| 96 | VIORND_BUFSIZE); |
| 97 | #endif |
| 98 | mutex_enter(&sc->sc_mutex); |
| 99 | |
| 100 | if (sc->sc_active) { |
| 101 | goto out; |
| 102 | } |
| 103 | |
| 104 | bus_dmamap_sync(vsc->sc_dmat, sc->sc_dmamap, 0, VIORND_BUFSIZE, |
| 105 | BUS_DMASYNC_PREREAD); |
| 106 | if (virtio_enqueue_prep(vsc, vq, &slot)) { |
| 107 | goto out; |
| 108 | } |
| 109 | if (virtio_enqueue_reserve(vsc, vq, slot, 1)) { |
| 110 | virtio_enqueue_abort(vsc, vq, slot); |
| 111 | goto out; |
| 112 | } |
| 113 | virtio_enqueue(vsc, vq, slot, sc->sc_dmamap, 0); |
| 114 | virtio_enqueue_commit(vsc, vq, slot, 1); |
| 115 | sc->sc_active = true; |
| 116 | out: |
| 117 | mutex_exit(&sc->sc_mutex); |
| 118 | } |
| 119 | |
| 120 | int |
| 121 | viornd_match(device_t parent, cfdata_t match, void *aux) |
| 122 | { |
| 123 | struct virtio_softc *va = aux; |
| 124 | if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_ENTROPY) |
| 125 | return 1; |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | viornd_attach( device_t parent, device_t self, void *aux) |
| 131 | { |
| 132 | struct viornd_softc *sc = device_private(self); |
| 133 | struct virtio_softc *vsc = device_private(parent); |
| 134 | bus_dma_segment_t segs[1]; |
| 135 | int nsegs; |
| 136 | int error; |
| 137 | uint32_t features; |
| 138 | char buf[256]; |
| 139 | |
| 140 | vsc->sc_vqs = &sc->sc_vq; |
| 141 | vsc->sc_nvqs = 1; |
| 142 | vsc->sc_config_change = NULL; |
| 143 | if (vsc->sc_child != NULL) |
| 144 | panic("already attached to something else" ); |
| 145 | vsc->sc_child = self; |
| 146 | vsc->sc_ipl = IPL_NET; |
| 147 | vsc->sc_intrhand = virtio_vq_intr; |
| 148 | sc->sc_virtio = vsc; |
| 149 | sc->sc_dev = self; |
| 150 | |
| 151 | features = virtio_negotiate_features(vsc, 0); |
| 152 | snprintb(buf, sizeof(buf), VIRTIO_COMMON_FLAG_BITS, features); |
| 153 | aprint_normal(": Features: %s\n" , buf); |
| 154 | aprint_naive("\n" ); |
| 155 | |
| 156 | |
| 157 | mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM); |
| 158 | |
| 159 | error = bus_dmamem_alloc(vsc->sc_dmat, |
| 160 | VIRTIO_PAGE_SIZE, 0, 0, segs, 1, &nsegs, |
| 161 | BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW); |
| 162 | if (error) { |
| 163 | aprint_error_dev(sc->sc_dev, "can't alloc dmamem: %d\n" , |
| 164 | error); |
| 165 | goto alloc_failed; |
| 166 | } |
| 167 | |
| 168 | error = bus_dmamem_map(vsc->sc_dmat, segs, nsegs, VIORND_BUFSIZE, |
| 169 | &sc->sc_buf, BUS_DMA_NOWAIT); |
| 170 | if (error) { |
| 171 | aprint_error_dev(sc->sc_dev, "can't map dmamem: %d\n" , error); |
| 172 | goto map_failed; |
| 173 | } |
| 174 | |
| 175 | error = bus_dmamap_create(vsc->sc_dmat, VIORND_BUFSIZE, 1, |
| 176 | VIORND_BUFSIZE, 0, |
| 177 | BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, |
| 178 | &sc->sc_dmamap); |
| 179 | if (error) { |
| 180 | aprint_error_dev(sc->sc_dev, "can't alloc dmamap: %d\n" , |
| 181 | error); |
| 182 | goto create_failed; |
| 183 | } |
| 184 | |
| 185 | error = bus_dmamap_load(vsc->sc_dmat, sc->sc_dmamap, |
| 186 | sc->sc_buf, VIORND_BUFSIZE, NULL, |
| 187 | BUS_DMA_NOWAIT|BUS_DMA_READ); |
| 188 | if (error) { |
| 189 | aprint_error_dev(sc->sc_dev, "can't load dmamap: %d\n" , |
| 190 | error); |
| 191 | goto load_failed; |
| 192 | } |
| 193 | |
| 194 | error = virtio_alloc_vq(vsc, &sc->sc_vq, 0, VIORND_BUFSIZE, 1, |
| 195 | "Entropy request" ); |
| 196 | if (error) { |
| 197 | aprint_error_dev(sc->sc_dev, "can't alloc virtqueue: %d\n" , |
| 198 | error); |
| 199 | goto vio_failed; |
| 200 | } |
| 201 | |
| 202 | sc->sc_vq.vq_done = viornd_vq_done; |
| 203 | virtio_start_vq_intr(vsc, &sc->sc_vq); |
| 204 | rndsource_setcb(&sc->sc_rndsource, viornd_get, sc); |
| 205 | rnd_attach_source(&sc->sc_rndsource, device_xname(sc->sc_dev), |
| 206 | RND_TYPE_RNG, |
| 207 | RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB); |
| 208 | viornd_get(VIORND_BUFSIZE, sc); |
| 209 | return; |
| 210 | vio_failed: |
| 211 | bus_dmamap_unload(vsc->sc_dmat, sc->sc_dmamap); |
| 212 | load_failed: |
| 213 | bus_dmamap_destroy(vsc->sc_dmat, sc->sc_dmamap); |
| 214 | create_failed: |
| 215 | bus_dmamem_unmap(vsc->sc_dmat, sc->sc_buf, VIORND_BUFSIZE); |
| 216 | map_failed: |
| 217 | bus_dmamem_free(vsc->sc_dmat, segs, nsegs); |
| 218 | alloc_failed: |
| 219 | vsc->sc_child = (void *)1; /* XXX bare constant 1 */ |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | int |
| 224 | viornd_vq_done(struct virtqueue *vq) |
| 225 | { |
| 226 | struct virtio_softc *vsc = vq->vq_owner; |
| 227 | struct viornd_softc *sc = device_private(vsc->sc_child); |
| 228 | int slot, len; |
| 229 | |
| 230 | mutex_enter(&sc->sc_mutex); |
| 231 | |
| 232 | if (virtio_dequeue(vsc, vq, &slot, &len) != 0) { |
| 233 | mutex_exit(&sc->sc_mutex); |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | sc->sc_active = false; |
| 238 | |
| 239 | bus_dmamap_sync(vsc->sc_dmat, sc->sc_dmamap, 0, VIORND_BUFSIZE, |
| 240 | BUS_DMASYNC_POSTREAD); |
| 241 | if (len > VIORND_BUFSIZE) { |
| 242 | aprint_error_dev(sc->sc_dev, |
| 243 | "inconsistent descriptor length %d > %d\n" , |
| 244 | len, VIORND_BUFSIZE); |
| 245 | goto out; |
| 246 | } |
| 247 | |
| 248 | #if VIORND_DEBUG |
| 249 | aprint_normal("%s: got %d bytes of entropy\n" , __func__, len); |
| 250 | #endif |
| 251 | rnd_add_data(&sc->sc_rndsource, sc->sc_buf, VIORND_BUFSIZE, |
| 252 | VIORND_BUFSIZE * NBBY); |
| 253 | out: |
| 254 | virtio_dequeue_commit(vsc, vq, slot); |
| 255 | mutex_exit(&sc->sc_mutex); |
| 256 | |
| 257 | return 1; |
| 258 | } |
| 259 | |