Home | History | Annotate | Line # | Download | only in pci
viornd.c revision 1.19
      1 /* 	$NetBSD: viornd.c,v 1.19 2023/03/23 03:27:48 yamaguchi 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 (at) 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 (at) 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/virtioreg.h>
     57 #include <dev/pci/virtiovar.h>
     58 
     59 #define	VIORND_BUFSIZE			32
     60 
     61 #define VIORND_DEBUG 0
     62 
     63 struct viornd_softc {
     64 	device_t		sc_dev;
     65 	struct virtio_softc	*sc_virtio;
     66 
     67 	kmutex_t		sc_mutex;
     68 	bool			sc_active;
     69 
     70 	void			*sc_buf;
     71 	struct virtqueue	sc_vq;
     72 	bus_dmamap_t		sc_dmamap;
     73 	krndsource_t		sc_rndsource;
     74 };
     75 
     76 int	viornd_match(device_t, cfdata_t, void *);
     77 void	viornd_attach(device_t, device_t, void *);
     78 int	viornd_vq_done(struct virtqueue *);
     79 
     80 CFATTACH_DECL_NEW(viornd, sizeof(struct viornd_softc),
     81 		  viornd_match, viornd_attach, NULL, NULL);
     82 
     83 static void
     84 viornd_get(size_t bytes, void *priv)
     85 {
     86         struct viornd_softc *sc = priv;
     87         struct virtio_softc *vsc = sc->sc_virtio;
     88         struct virtqueue *vq = &sc->sc_vq;
     89         int slot;
     90 
     91 #if VIORND_DEBUG
     92 	aprint_normal("%s: asked for %d bytes of entropy\n", __func__,
     93 		      VIORND_BUFSIZE);
     94 #endif
     95 	mutex_enter(&sc->sc_mutex);
     96 
     97 	if (sc->sc_active) {
     98 		goto out;
     99 	}
    100 
    101         bus_dmamap_sync(virtio_dmat(vsc), sc->sc_dmamap, 0, VIORND_BUFSIZE,
    102             BUS_DMASYNC_PREREAD);
    103 	if (virtio_enqueue_prep(vsc, vq, &slot)) {
    104 		goto out;
    105 	}
    106         if (virtio_enqueue_reserve(vsc, vq, slot, 1)) {
    107 		goto out;
    108 	}
    109         virtio_enqueue(vsc, vq, slot, sc->sc_dmamap, 0);
    110         virtio_enqueue_commit(vsc, vq, slot, 1);
    111 	sc->sc_active = true;
    112 out:
    113 	mutex_exit(&sc->sc_mutex);
    114 }
    115 
    116 int
    117 viornd_match(device_t parent, cfdata_t match, void *aux)
    118 {
    119 	struct virtio_attach_args *va = aux;
    120 
    121 	if (va->sc_childdevid == VIRTIO_DEVICE_ID_ENTROPY)
    122 		return 1;
    123 
    124 	return 0;
    125 }
    126 
    127 void
    128 viornd_attach(device_t parent, device_t self, void *aux)
    129 {
    130 	struct viornd_softc *sc = device_private(self);
    131 	struct virtio_softc *vsc = device_private(parent);
    132 	bus_dma_segment_t segs[1];
    133 	int nsegs;
    134 	int error;
    135 
    136 	if (virtio_child(vsc) != NULL)
    137 		panic("already attached to something else");
    138 
    139 	sc->sc_dev = self;
    140 	sc->sc_virtio = vsc;
    141 
    142 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
    143 
    144 	error = bus_dmamem_alloc(virtio_dmat(vsc),
    145 				 VIRTIO_PAGE_SIZE, 0, 0, segs, 1, &nsegs,
    146 				 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW);
    147 	if (error) {
    148 		aprint_error_dev(sc->sc_dev, "can't alloc dmamem: %d\n",
    149 				 error);
    150 		goto alloc_failed;
    151 	}
    152 
    153 	error = bus_dmamem_map(virtio_dmat(vsc), segs, nsegs, VIORND_BUFSIZE,
    154 			       &sc->sc_buf, BUS_DMA_NOWAIT);
    155 	if (error) {
    156 		aprint_error_dev(sc->sc_dev, "can't map dmamem: %d\n", error);
    157 		goto map_failed;
    158 	}
    159 
    160 	error = bus_dmamap_create(virtio_dmat(vsc), VIORND_BUFSIZE, 1,
    161 				  VIORND_BUFSIZE, 0,
    162 				  BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
    163 				  &sc->sc_dmamap);
    164 	if (error) {
    165 		aprint_error_dev(sc->sc_dev, "can't alloc dmamap: %d\n",
    166 				 error);
    167 		goto create_failed;
    168 	}
    169 
    170 	error = bus_dmamap_load(virtio_dmat(vsc), sc->sc_dmamap,
    171 	    			sc->sc_buf, VIORND_BUFSIZE, NULL,
    172 				BUS_DMA_NOWAIT|BUS_DMA_READ);
    173 	if (error) {
    174 		aprint_error_dev(sc->sc_dev, "can't load dmamap: %d\n",
    175 				 error);
    176 		goto load_failed;
    177 	}
    178 
    179 	virtio_child_attach_start(vsc, self, IPL_NET,
    180 	    0, VIRTIO_COMMON_FLAG_BITS);
    181 
    182 	error = virtio_alloc_vq(vsc, &sc->sc_vq, 0, VIORND_BUFSIZE, 1,
    183 	    "Entropy request");
    184 	if (error) {
    185 		aprint_error_dev(sc->sc_dev, "can't alloc virtqueue: %d\n",
    186 				 error);
    187 		goto vio_failed;
    188 	}
    189 	sc->sc_vq.vq_done = viornd_vq_done;
    190 
    191 	error = virtio_child_attach_finish(vsc, &sc->sc_vq, 1,
    192 	    NULL, virtio_vq_intr, 0);
    193 	if (error) {
    194 		virtio_free_vq(vsc, &sc->sc_vq);
    195 		goto vio_failed;
    196 	}
    197 
    198 	rndsource_setcb(&sc->sc_rndsource, viornd_get, sc);
    199 	rnd_attach_source(&sc->sc_rndsource, device_xname(sc->sc_dev),
    200 			  RND_TYPE_RNG,
    201 			  RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
    202 
    203 	return;
    204 
    205 vio_failed:
    206 	bus_dmamap_unload(virtio_dmat(vsc), sc->sc_dmamap);
    207 load_failed:
    208 	bus_dmamap_destroy(virtio_dmat(vsc), sc->sc_dmamap);
    209 create_failed:
    210 	bus_dmamem_unmap(virtio_dmat(vsc), sc->sc_buf, VIORND_BUFSIZE);
    211 map_failed:
    212 	bus_dmamem_free(virtio_dmat(vsc), segs, nsegs);
    213 alloc_failed:
    214 	virtio_child_attach_failed(vsc);
    215 	return;
    216 }
    217 
    218 int
    219 viornd_vq_done(struct virtqueue *vq)
    220 {
    221 	struct virtio_softc *vsc = vq->vq_owner;
    222 	struct viornd_softc *sc = device_private(virtio_child(vsc));
    223 	int slot, len;
    224 
    225 	mutex_enter(&sc->sc_mutex);
    226 
    227 	if (virtio_dequeue(vsc, vq, &slot, &len) != 0) {
    228 		mutex_exit(&sc->sc_mutex);
    229 		return 0;
    230 	}
    231 
    232 	sc->sc_active = false;
    233 
    234 	bus_dmamap_sync(virtio_dmat(vsc), sc->sc_dmamap, 0, VIORND_BUFSIZE,
    235 	    BUS_DMASYNC_POSTREAD);
    236 	if (len > VIORND_BUFSIZE) {
    237 		aprint_error_dev(sc->sc_dev,
    238 				 "inconsistent descriptor length %d > %d\n",
    239 				 len, VIORND_BUFSIZE);
    240 		goto out;
    241 	}
    242 
    243 #if VIORND_DEBUG
    244 	aprint_normal("%s: got %d bytes of entropy\n", __func__, len);
    245 #endif
    246 	rnd_add_data(&sc->sc_rndsource, sc->sc_buf, VIORND_BUFSIZE,
    247 		     VIORND_BUFSIZE * NBBY);
    248 out:
    249 	virtio_dequeue_commit(vsc, vq, slot);
    250 	mutex_exit(&sc->sc_mutex);
    251 
    252 	return 1;
    253 }
    254