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