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