Home | History | Annotate | Line # | Download | only in pci
fwhrng.c revision 1.2.8.2
      1  1.2.8.2  rmind /*	$NetBSD: fwhrng.c,v 1.2.8.2 2011/03/05 20:52:28 rmind Exp $	*/
      2  1.2.8.2  rmind 
      3  1.2.8.2  rmind /*
      4  1.2.8.2  rmind  * Copyright (c) 2000 Michael Shalayeff
      5  1.2.8.2  rmind  * All rights reserved.
      6  1.2.8.2  rmind  *
      7  1.2.8.2  rmind  * Redistribution and use in source and binary forms, with or without
      8  1.2.8.2  rmind  * modification, are permitted provided that the following conditions
      9  1.2.8.2  rmind  * are met:
     10  1.2.8.2  rmind  * 1. Redistributions of source code must retain the above copyright
     11  1.2.8.2  rmind  *    notice, this list of conditions and the following disclaimer.
     12  1.2.8.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.8.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.8.2  rmind  *    documentation and/or other materials provided with the distribution.
     15  1.2.8.2  rmind  *
     16  1.2.8.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.2.8.2  rmind  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.2.8.2  rmind  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.2.8.2  rmind  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
     20  1.2.8.2  rmind  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  1.2.8.2  rmind  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  1.2.8.2  rmind  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.2.8.2  rmind  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  1.2.8.2  rmind  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     25  1.2.8.2  rmind  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     26  1.2.8.2  rmind  * THE POSSIBILITY OF SUCH DAMAGE.
     27  1.2.8.2  rmind  *
     28  1.2.8.2  rmind  *	from OpenBSD: pchb.c,v 1.23 2000/10/23 20:07:30 deraadt Exp
     29  1.2.8.2  rmind  */
     30  1.2.8.2  rmind 
     31  1.2.8.2  rmind #include <sys/cdefs.h>
     32  1.2.8.2  rmind __KERNEL_RCSID(0, "$NetBSD: fwhrng.c,v 1.2.8.2 2011/03/05 20:52:28 rmind Exp $");
     33  1.2.8.2  rmind 
     34  1.2.8.2  rmind #include "rnd.h"
     35  1.2.8.2  rmind 
     36  1.2.8.2  rmind #if NRND == 0
     37  1.2.8.2  rmind #error fwhrng requires rnd pseudo-device
     38  1.2.8.2  rmind #endif
     39  1.2.8.2  rmind 
     40  1.2.8.2  rmind #include <sys/param.h>
     41  1.2.8.2  rmind #include <sys/systm.h>
     42  1.2.8.2  rmind #include <sys/device.h>
     43  1.2.8.2  rmind #include <sys/time.h>
     44  1.2.8.2  rmind #include <sys/rnd.h>
     45  1.2.8.2  rmind 
     46  1.2.8.2  rmind #include <machine/bus.h>
     47  1.2.8.2  rmind 
     48  1.2.8.2  rmind #include <arch/x86/pci/i82802reg.h>
     49  1.2.8.2  rmind 
     50  1.2.8.2  rmind struct fwhrng_softc {
     51  1.2.8.2  rmind 	device_t sc_dev;
     52  1.2.8.2  rmind 
     53  1.2.8.2  rmind 	bus_space_tag_t sc_st;
     54  1.2.8.2  rmind 	bus_space_handle_t sc_sh;
     55  1.2.8.2  rmind 
     56  1.2.8.2  rmind 	struct callout sc_rnd_ch;
     57  1.2.8.2  rmind 	rndsource_element_t sc_rnd_source;
     58  1.2.8.2  rmind 
     59  1.2.8.2  rmind 	int sc_rnd_i;
     60  1.2.8.2  rmind 	uint32_t sc_rnd_ax;
     61  1.2.8.2  rmind };
     62  1.2.8.2  rmind 
     63  1.2.8.2  rmind static int fwhrng_match(device_t, cfdata_t, void *);
     64  1.2.8.2  rmind static void fwhrng_attach(device_t, device_t, void *);
     65  1.2.8.2  rmind static int fwhrng_detach(device_t, int);
     66  1.2.8.2  rmind 
     67  1.2.8.2  rmind static void fwhrng_callout(void *v);
     68  1.2.8.2  rmind 
     69  1.2.8.2  rmind #define	FWHRNG_RETRIES		1000
     70  1.2.8.2  rmind #define	FWHRNG_MIN_SAMPLES	10
     71  1.2.8.2  rmind 
     72  1.2.8.2  rmind CFATTACH_DECL_NEW(fwhrng, sizeof(struct fwhrng_softc),
     73  1.2.8.2  rmind     fwhrng_match, fwhrng_attach, fwhrng_detach, NULL);
     74  1.2.8.2  rmind 
     75  1.2.8.2  rmind static int
     76  1.2.8.2  rmind fwhrng_match(device_t parent, cfdata_t match, void *aux)
     77  1.2.8.2  rmind {
     78  1.2.8.2  rmind 	bus_space_tag_t bst;
     79  1.2.8.2  rmind 	bus_space_handle_t bsh;
     80  1.2.8.2  rmind 	int ret;
     81  1.2.8.2  rmind 	uint8_t id0, id1, data0, data1;
     82  1.2.8.2  rmind 
     83  1.2.8.2  rmind 	ret = 0;
     84  1.2.8.2  rmind 
     85  1.2.8.2  rmind 	bst = x86_bus_space_mem;
     86  1.2.8.2  rmind 
     87  1.2.8.2  rmind 	/* read chip ID */
     88  1.2.8.2  rmind 	if (bus_space_map(bst, I82802AB_MEMBASE, I82802AB_WINSIZE, 0, &bsh))
     89  1.2.8.2  rmind 		return 0;
     90  1.2.8.2  rmind 
     91  1.2.8.2  rmind 	bus_space_write_1(bst, bsh, 0, 0xff); /* reset */
     92  1.2.8.2  rmind 	data0 = bus_space_read_1(bst, bsh, 0);
     93  1.2.8.2  rmind 	data1 = bus_space_read_1(bst, bsh, 1);
     94  1.2.8.2  rmind 	bus_space_write_1(bst, bsh, 0, 0x90); /* enter read id */
     95  1.2.8.2  rmind 	id0 = bus_space_read_1(bst, bsh, 0);
     96  1.2.8.2  rmind 	id1 = bus_space_read_1(bst, bsh, 1);
     97  1.2.8.2  rmind 	bus_space_write_1(bst, bsh, 0, 0xff); /* reset */
     98  1.2.8.2  rmind 
     99  1.2.8.2  rmind 	bus_space_unmap(bst, bsh, I82802AB_WINSIZE);
    100  1.2.8.2  rmind 
    101  1.2.8.2  rmind 	aprint_debug_dev(parent, "fwh: data %02x,%02x, id %02x,%02x\n",
    102  1.2.8.2  rmind 	    data0, data1, id0, id1);
    103  1.2.8.2  rmind 
    104  1.2.8.2  rmind 	/* unlikely to have these match if we actually read the ID */
    105  1.2.8.2  rmind 	if ((id0 == data0) && (id1 == data1))
    106  1.2.8.2  rmind 		return 0;
    107  1.2.8.2  rmind 
    108  1.2.8.2  rmind 	/* check for chips with RNG */
    109  1.2.8.2  rmind 	if (!(id0 == I82802_MFG))
    110  1.2.8.2  rmind 		return 0;
    111  1.2.8.2  rmind 	if (!((id1 == I82802AB_ID) || (id1 == I82802AC_ID)))
    112  1.2.8.2  rmind 		return 0;
    113  1.2.8.2  rmind 
    114  1.2.8.2  rmind 	/* check for RNG presence */
    115  1.2.8.2  rmind 	if (bus_space_map(bst, I82802AC_REGBASE, I82802AC_WINSIZE, 0, &bsh))
    116  1.2.8.2  rmind 		return 0;
    117  1.2.8.2  rmind 	data0 = bus_space_read_1(bst, bsh, I82802_RNG_HSR);
    118  1.2.8.2  rmind 	bus_space_unmap(bst, bsh, I82802AC_WINSIZE);
    119  1.2.8.2  rmind 	if ((data0 & I82802_RNG_HSR_PRESENT) == I82802_RNG_HSR_PRESENT)
    120  1.2.8.2  rmind 		return 1;
    121  1.2.8.2  rmind 
    122  1.2.8.2  rmind 	return 0;
    123  1.2.8.2  rmind }
    124  1.2.8.2  rmind 
    125  1.2.8.2  rmind static void
    126  1.2.8.2  rmind fwhrng_attach(device_t parent, device_t self, void *aux)
    127  1.2.8.2  rmind {
    128  1.2.8.2  rmind 	struct fwhrng_softc *sc;
    129  1.2.8.2  rmind 	int i, j, count_ff;
    130  1.2.8.2  rmind 	uint8_t reg8;
    131  1.2.8.2  rmind 
    132  1.2.8.2  rmind 	sc = device_private(self);
    133  1.2.8.2  rmind 	sc->sc_dev = self;
    134  1.2.8.2  rmind 
    135  1.2.8.2  rmind 	aprint_naive("\n");
    136  1.2.8.2  rmind 	aprint_normal(": Intel Firmware Hub Random Number Generator\n");
    137  1.2.8.2  rmind 
    138  1.2.8.2  rmind 	sc->sc_st = x86_bus_space_mem;
    139  1.2.8.2  rmind 
    140  1.2.8.2  rmind 	if (bus_space_map(sc->sc_st, I82802AC_REGBASE, I82802AC_WINSIZE, 0,
    141  1.2.8.2  rmind 	    &sc->sc_sh) != 0) {
    142  1.2.8.2  rmind 		aprint_error_dev(self, "unable to map registers\n");
    143  1.2.8.2  rmind 		return;
    144  1.2.8.2  rmind 	}
    145  1.2.8.2  rmind 
    146  1.2.8.2  rmind 	/* Enable the RNG. */
    147  1.2.8.2  rmind 	reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR);
    148  1.2.8.2  rmind 	bus_space_write_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR,
    149  1.2.8.2  rmind 	    reg8 | I82802_RNG_HSR_ENABLE);
    150  1.2.8.2  rmind 	reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR);
    151  1.2.8.2  rmind 	if ((reg8 & I82802_RNG_HSR_ENABLE) == 0) {
    152  1.2.8.2  rmind 		aprint_error_dev(self, "unable to enable\n");
    153  1.2.8.2  rmind 		bus_space_unmap(sc->sc_st, sc->sc_sh, I82802AC_WINSIZE);
    154  1.2.8.2  rmind 		return;
    155  1.2.8.2  rmind 	}
    156  1.2.8.2  rmind 
    157  1.2.8.2  rmind 	/* Check to see if we can read data from the RNG. */
    158  1.2.8.2  rmind 	count_ff = 0;
    159  1.2.8.2  rmind 	for (j = 0; j < FWHRNG_MIN_SAMPLES; ++j) {
    160  1.2.8.2  rmind 		for (i = 0; i < FWHRNG_RETRIES; i++) {
    161  1.2.8.2  rmind 			reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh,
    162  1.2.8.2  rmind 			    I82802_RNG_DSR);
    163  1.2.8.2  rmind 			if (!(reg8 & I82802_RNG_DSR_VALID)) {
    164  1.2.8.2  rmind 				delay(10);
    165  1.2.8.2  rmind 				continue;
    166  1.2.8.2  rmind 			}
    167  1.2.8.2  rmind 			reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh,
    168  1.2.8.2  rmind 			    I82802_RNG_DR);
    169  1.2.8.2  rmind 			break;
    170  1.2.8.2  rmind 		}
    171  1.2.8.2  rmind 		if (i == FWHRNG_RETRIES) {
    172  1.2.8.2  rmind 			bus_space_unmap(sc->sc_st, sc->sc_sh, I82802AC_WINSIZE);
    173  1.2.8.2  rmind 			aprint_verbose_dev(sc->sc_dev,
    174  1.2.8.2  rmind 			    "timeout reading test samples, RNG disabled.\n");
    175  1.2.8.2  rmind 			return;
    176  1.2.8.2  rmind 		}
    177  1.2.8.2  rmind 		if (reg8 == 0xff)
    178  1.2.8.2  rmind 			++count_ff;
    179  1.2.8.2  rmind 	}
    180  1.2.8.2  rmind 
    181  1.2.8.2  rmind 	if (count_ff == FWHRNG_MIN_SAMPLES) {
    182  1.2.8.2  rmind 		/* Disable the RNG. */
    183  1.2.8.2  rmind 		reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR);
    184  1.2.8.2  rmind 		bus_space_write_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR,
    185  1.2.8.2  rmind 		    reg8 & ~I82802_RNG_HSR_ENABLE);
    186  1.2.8.2  rmind 		bus_space_unmap(sc->sc_st, sc->sc_sh, I82802AC_WINSIZE);
    187  1.2.8.2  rmind 		aprint_error_dev(sc->sc_dev,
    188  1.2.8.2  rmind 		    "returns constant 0xff stream, RNG disabled.\n");
    189  1.2.8.2  rmind 		return;
    190  1.2.8.2  rmind 	}
    191  1.2.8.2  rmind 
    192  1.2.8.2  rmind 	/*
    193  1.2.8.2  rmind 	 * Should test entropy source to ensure
    194  1.2.8.2  rmind 	 * that it passes the Statistical Random
    195  1.2.8.2  rmind 	 * Number Generator Tests in section 4.11.1,
    196  1.2.8.2  rmind 	 * FIPS PUB 140-1.
    197  1.2.8.2  rmind 	 *
    198  1.2.8.2  rmind 	 *	http://csrc.nist.gov/fips/fips1401.htm
    199  1.2.8.2  rmind 	 */
    200  1.2.8.2  rmind 
    201  1.2.8.2  rmind 	aprint_debug_dev(sc->sc_dev, "random number generator enabled\n");
    202  1.2.8.2  rmind 
    203  1.2.8.2  rmind 	callout_init(&sc->sc_rnd_ch, 0);
    204  1.2.8.2  rmind 	/* FWH is polled for entropy, so no estimate is available. */
    205  1.2.8.2  rmind 	rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
    206  1.2.8.2  rmind 	    RND_TYPE_RNG, RND_FLAG_NO_ESTIMATE);
    207  1.2.8.2  rmind 	sc->sc_rnd_i = sizeof(sc->sc_rnd_ax);
    208  1.2.8.2  rmind 	fwhrng_callout(sc);
    209  1.2.8.2  rmind 
    210  1.2.8.2  rmind 	return;
    211  1.2.8.2  rmind }
    212  1.2.8.2  rmind 
    213  1.2.8.2  rmind static int
    214  1.2.8.2  rmind fwhrng_detach(device_t self, int flags)
    215  1.2.8.2  rmind {
    216  1.2.8.2  rmind 	struct fwhrng_softc *sc;
    217  1.2.8.2  rmind 	uint8_t reg8;
    218  1.2.8.2  rmind 
    219  1.2.8.2  rmind 	sc = device_private(self);
    220  1.2.8.2  rmind 
    221  1.2.8.2  rmind 	rnd_detach_source(&sc->sc_rnd_source);
    222  1.2.8.2  rmind 
    223  1.2.8.2  rmind 	callout_stop(&sc->sc_rnd_ch);
    224  1.2.8.2  rmind 	callout_destroy(&sc->sc_rnd_ch);
    225  1.2.8.2  rmind 
    226  1.2.8.2  rmind 	/* Disable the RNG. */
    227  1.2.8.2  rmind 	reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR);
    228  1.2.8.2  rmind 	bus_space_write_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR,
    229  1.2.8.2  rmind 	    reg8 & ~I82802_RNG_HSR_ENABLE);
    230  1.2.8.2  rmind 
    231  1.2.8.2  rmind 	bus_space_unmap(sc->sc_st, sc->sc_sh, I82802AC_WINSIZE);
    232  1.2.8.2  rmind 
    233  1.2.8.2  rmind 	return 0;
    234  1.2.8.2  rmind }
    235  1.2.8.2  rmind 
    236  1.2.8.2  rmind static void
    237  1.2.8.2  rmind fwhrng_callout(void *v)
    238  1.2.8.2  rmind {
    239  1.2.8.2  rmind 	struct fwhrng_softc *sc = v;
    240  1.2.8.2  rmind 
    241  1.2.8.2  rmind 	if ((bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_DSR) &
    242  1.2.8.2  rmind 	     I82802_RNG_DSR_VALID) != 0) {
    243  1.2.8.2  rmind 		sc->sc_rnd_ax = (sc->sc_rnd_ax << NBBY) |
    244  1.2.8.2  rmind 		    bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_DR);
    245  1.2.8.2  rmind 		if (--sc->sc_rnd_i == 0) {
    246  1.2.8.2  rmind 			sc->sc_rnd_i = sizeof(sc->sc_rnd_ax);
    247  1.2.8.2  rmind 			rnd_add_data(&sc->sc_rnd_source, &sc->sc_rnd_ax,
    248  1.2.8.2  rmind 			    sizeof(sc->sc_rnd_ax),
    249  1.2.8.2  rmind 			    sizeof(sc->sc_rnd_ax) * NBBY);
    250  1.2.8.2  rmind 		}
    251  1.2.8.2  rmind 	}
    252  1.2.8.2  rmind 	callout_reset(&sc->sc_rnd_ch, 1, fwhrng_callout, sc);
    253  1.2.8.2  rmind }
    254