Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch Almost entirely rework Intel Firmware Hub random nu...



details:   https://anonhg.NetBSD.org/src/rev/ad9a9516375a
branches:  trunk
changeset: 756552:ad9a9516375a
user:      jakllsch <jakllsch%NetBSD.org@localhost>
date:      Fri Jul 23 00:43:20 2010 +0000

description:
Almost entirely rework Intel Firmware Hub random number generator support.

This introduces fwhrng(4) which attaches via ichlpcib(4), replacing
the rnd(4) support in pchb(4).

diffstat:

 sys/arch/amd64/conf/GENERIC  |    9 +-
 sys/arch/i386/conf/GENERIC   |    7 +-
 sys/arch/x86/pci/files.pci   |   10 +-
 sys/arch/x86/pci/fwhrng.c    |  253 +++++++++++++++++++++++++++++++++++++++++++
 sys/arch/x86/pci/i82802reg.h |   87 +++++++-------
 sys/arch/x86/pci/ichlpcib.c  |   76 ++++++++++++-
 sys/arch/x86/pci/pchb.c      |   20 +---
 sys/arch/x86/pci/pchb_rnd.c  |  215 ------------------------------------
 sys/arch/x86/pci/pchbvar.h   |   12 +-
 9 files changed, 388 insertions(+), 301 deletions(-)

diffs (truncated from 906 to 300 lines):

diff -r 6363c562a33f -r ad9a9516375a sys/arch/amd64/conf/GENERIC
--- a/sys/arch/amd64/conf/GENERIC       Thu Jul 22 21:00:07 2010 +0000
+++ b/sys/arch/amd64/conf/GENERIC       Fri Jul 23 00:43:20 2010 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.282 2010/07/18 09:29:11 jruoho Exp $
+# $NetBSD: GENERIC,v 1.283 2010/07/23 00:43:20 jakllsch Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@
 
 options        INCLUDE_CONFIG_FILE     # embed config file in kernel binary
 
-#ident                 "GENERIC-$Revision: 1.282 $"
+#ident                 "GENERIC-$Revision: 1.283 $"
 
 maxusers       64              # estimated number of users
 
@@ -336,8 +336,9 @@
 #amdpcib* at pci? dev ? function ?     # AMD 8111 PCI-ISA w/ HPET
 #hpet*         at amdpcib?
 
-ichlpcib* at pci? dev ? function ?     # Intel ICH PCI-ISA w/ timecounter,
-                                       # watchdog and Speedstep support.
+ichlpcib* at pci? dev ? function ?     # Intel ICH PCI-LPC w/ timecounter,
+                                       # watchdog and Speedstep and HPET
+fwhrng* at ichlpcib?           # Intel 82802 FWH Random Number Generator
 #hpet*         at ichlpcib?
 
 aapic*         at pci? dev ? function ?        # AMD 8131 IO apic
diff -r 6363c562a33f -r ad9a9516375a sys/arch/i386/conf/GENERIC
--- a/sys/arch/i386/conf/GENERIC        Thu Jul 22 21:00:07 2010 +0000
+++ b/sys/arch/i386/conf/GENERIC        Fri Jul 23 00:43:20 2010 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.987 2010/07/18 09:29:12 jruoho Exp $
+# $NetBSD: GENERIC,v 1.988 2010/07/23 00:43:20 jakllsch Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@
 
 options        INCLUDE_CONFIG_FILE     # embed config file in kernel binary
 
-#ident                 "GENERIC-$Revision: 1.987 $"
+#ident                 "GENERIC-$Revision: 1.988 $"
 
 maxusers       64              # estimated number of users
 
@@ -457,8 +457,9 @@
 # PCI bridges
 #amdpcib* at pci? dev ? function ?     # AMD 8111 PCI-ISA w/ HPET
 #hpet*         at amdpcib?
-ichlpcib* at pci? dev ? function ?     # Intel ICH PCI-ISA w/ timecounter,
+ichlpcib* at pci? dev ? function ?     # Intel ICH PCI-LPC w/ timecounter,
                                        # watchdog, SpeedStep and HPET
+fwhrng* at ichlpcib?           # Intel 82802 FWH Random Number Generator
 #hpet*         at ichlpcib?
 gcscpcib* at pci? dev ? function ?     # AMD CS5535/CS5536 PCI-ISA w/
 gpio*  at gcscpcib?                    # timecounter, watchdog and GPIO
diff -r 6363c562a33f -r ad9a9516375a sys/arch/x86/pci/files.pci
--- a/sys/arch/x86/pci/files.pci        Thu Jul 22 21:00:07 2010 +0000
+++ b/sys/arch/x86/pci/files.pci        Fri Jul 23 00:43:20 2010 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.pci,v 1.9 2010/05/14 18:11:25 phx Exp $
+#      $NetBSD: files.pci,v 1.10 2010/07/23 00:43:21 jakllsch Exp $
 
 device         aapic
 attach         aapic at pci
@@ -11,7 +11,6 @@
                agp_intel, agp_sis, agp_via
 attach pchb at pci
 file   arch/x86/pci/pchb.c             pchb            needs-flag
-file   arch/x86/pci/pchb_rnd.c         pchb & rnd
 
 # PCI-ISA bridges
 device pcib: isabus
@@ -32,9 +31,14 @@
 file   arch/x86/pci/amdtemp.c          amdtemp
 
 # PCI-LPC bridges
+define fwhichbus {}
 define hpetichbus {}
-device ichlpcib: acpipmtimer, isabus, sysmon_wdog, hpetichbus, gpiobus
+device ichlpcib: acpipmtimer, isabus, sysmon_wdog, fwhichbus, hpetichbus, gpiobus
 attach ichlpcib at pci
 attach hpet at hpetichbus with ichlpcib_hpet
 
 file   arch/x86/pci/ichlpcib.c         ichlpcib
+
+device fwhrng
+attach fwhrng at fwhichbus
+file   arch/x86/pci/fwhrng.c           fwhrng needs-flag
diff -r 6363c562a33f -r ad9a9516375a sys/arch/x86/pci/fwhrng.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/x86/pci/fwhrng.c Fri Jul 23 00:43:20 2010 +0000
@@ -0,0 +1,253 @@
+/*     $NetBSD: fwhrng.c,v 1.1 2010/07/23 00:43:21 jakllsch Exp $      */
+
+/*
+ * Copyright (c) 2000 Michael Shalayeff
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ *     from OpenBSD: pchb.c,v 1.23 2000/10/23 20:07:30 deraadt Exp
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: fwhrng.c,v 1.1 2010/07/23 00:43:21 jakllsch Exp $");
+
+#include "rnd.h"        
+                            
+#if NRND == 0
+#error fwhrng requires rnd pseudo-device
+#endif  
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/time.h>
+#include <sys/rnd.h>
+
+#include <machine/bus.h>
+
+#include <arch/x86/pci/i82802reg.h>
+
+struct fwhrng_softc {
+       device_t sc_dev;
+
+       bus_space_tag_t sc_st;
+       bus_space_handle_t sc_sh;
+
+       struct callout sc_rnd_ch;
+       rndsource_element_t sc_rnd_source;
+
+       int sc_rnd_i;
+       uint32_t sc_rnd_ax;
+};
+
+static int fwhrng_match(device_t, cfdata_t, void *);
+static void fwhrng_attach(device_t, device_t, void *);
+static int fwhrng_detach(device_t, int);
+
+static void fwhrng_callout(void *v);
+
+#define        FWHRNG_RETRIES          1000
+#define        FWHRNG_MIN_SAMPLES      10
+
+CFATTACH_DECL_NEW(fwhrng, sizeof(struct fwhrng_softc),
+    fwhrng_match, fwhrng_attach, fwhrng_detach, NULL);
+
+static int
+fwhrng_match(device_t parent, cfdata_t match, void *aux)
+{
+       bus_space_tag_t bst;
+       bus_space_handle_t bsh;
+       int ret;
+       uint8_t id0, id1, data0, data1;
+
+       ret = 0;
+
+       bst = x86_bus_space_mem;
+
+       /* read chip ID */
+       if (bus_space_map(bst, I82802AC_MEMBASE, I82802AC_WINSIZE, 0, &bsh))
+               return 0;
+
+       bus_space_write_1(bst, bsh, 0, 0xff); /* reset */
+       data0 = bus_space_read_1(bst, bsh, 0);
+       data1 = bus_space_read_1(bst, bsh, 1);
+       bus_space_write_1(bst, bsh, 0, 0x90); /* enter read id */
+       id0 = bus_space_read_1(bst, bsh, 0);
+       id1 = bus_space_read_1(bst, bsh, 1);
+       bus_space_write_1(bst, bsh, 0, 0xff); /* reset */
+
+       bus_space_unmap(bst, bsh, I82802AC_WINSIZE);
+
+       aprint_debug_dev(parent, "fwh: data %02x,%02x, id %02x,%02x\n",
+           data0, data1, id0, id1);
+
+       /* unlikely to have these match if we actually read the ID */
+       if ((id0 == data0) && (id1 == data1))
+               return 0;
+
+       /* check for chips with RNG */
+       if (!(id0 == I82802_MFG))
+               return 0;
+       if (!((id1 == I82802AB_ID) || (id1 == I82802AC_ID)))
+               return 0;
+
+       /* check for RNG presence */
+       if (bus_space_map(bst, I82802AC_REGBASE, I82802AC_WINSIZE, 0, &bsh))
+               return 0;
+       data0 = bus_space_read_1(bst, bsh, I82802_RNG_HSR);
+       bus_space_unmap(bst, bsh, I82802AC_WINSIZE);
+       if ((data0 & I82802_RNG_HSR_PRESENT) == I82802_RNG_HSR_PRESENT)
+               return 1;
+
+       return 0;
+}
+
+static void
+fwhrng_attach(device_t parent, device_t self, void *aux)
+{
+       struct fwhrng_softc *sc;
+       int i, j, count_ff;
+       uint8_t reg8;
+
+       sc = device_private(self);
+       sc->sc_dev = self;
+
+       aprint_naive("\n");
+       aprint_normal(": Intel Firmware Hub Random Number Generator\n");
+
+       sc->sc_st = x86_bus_space_mem;
+
+       if (bus_space_map(sc->sc_st, I82802AC_REGBASE, I82802AC_WINSIZE, 0,
+           &sc->sc_sh) != 0) {
+               aprint_error_dev(self, "unable to map registers\n");
+               return;
+       }
+
+       /* Enable the RNG. */
+       reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR);
+       bus_space_write_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR,
+           reg8 | I82802_RNG_HSR_ENABLE);
+       reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR);
+       if ((reg8 & I82802_RNG_HSR_ENABLE) == 0) {
+               aprint_error_dev(self, "unable to enable\n");
+               bus_space_unmap(sc->sc_st, sc->sc_sh, I82802AC_WINSIZE);
+               return;
+       }
+
+       /* Check to see if we can read data from the RNG. */
+       count_ff = 0;
+       for (j = 0; j < FWHRNG_MIN_SAMPLES; ++j) {
+               for (i = 0; i < FWHRNG_RETRIES; i++) {
+                       reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh,
+                           I82802_RNG_DSR);
+                       if (!(reg8 & I82802_RNG_DSR_VALID)) {
+                               delay(10);
+                               continue;
+                       }
+                       reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh,
+                           I82802_RNG_DR);
+                       break;
+               }
+               if (i == FWHRNG_RETRIES) {
+                       bus_space_unmap(sc->sc_st, sc->sc_sh, I82802AC_WINSIZE);
+                       aprint_verbose_dev(sc->sc_dev,
+                           "timeout reading test samples, RNG disabled.\n");
+                       return;
+               }
+               if (reg8 == 0xff)
+                       ++count_ff;
+       }
+
+       if (count_ff == FWHRNG_MIN_SAMPLES) {
+               /* Disable the RNG. */
+               reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR);
+               bus_space_write_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR,
+                   reg8 & ~I82802_RNG_HSR_ENABLE);
+               bus_space_unmap(sc->sc_st, sc->sc_sh, I82802AC_WINSIZE);
+               aprint_error_dev(sc->sc_dev,
+                   "returns constant 0xff stream, RNG disabled.\n");
+               return;
+       }
+
+       /*
+        * Should test entropy source to ensure
+        * that it passes the Statistical Random
+        * Number Generator Tests in section 4.11.1,
+        * FIPS PUB 140-1.
+        *
+        *      http://csrc.nist.gov/fips/fips1401.htm
+        */
+
+       aprint_debug_dev(sc->sc_dev, "random number generator enabled\n");
+
+       callout_init(&sc->sc_rnd_ch, 0);
+       /* FWH is polled for entropy, so no estimate is available. */



Home | Main Index | Thread Index | Old Index