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