fwhrng.c revision 1.4 1 1.4 tls /* $NetBSD: fwhrng.c,v 1.4 2011/11/19 22:51:21 tls 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.4 tls __KERNEL_RCSID(0, "$NetBSD: fwhrng.c,v 1.4 2011/11/19 22:51:21 tls Exp $");
33 1.1 jakllsch
34 1.1 jakllsch #include "rnd.h"
35 1.1 jakllsch
36 1.1 jakllsch #if NRND == 0
37 1.1 jakllsch #error fwhrng requires rnd pseudo-device
38 1.1 jakllsch #endif
39 1.1 jakllsch
40 1.1 jakllsch #include <sys/param.h>
41 1.1 jakllsch #include <sys/systm.h>
42 1.1 jakllsch #include <sys/device.h>
43 1.1 jakllsch #include <sys/time.h>
44 1.1 jakllsch #include <sys/rnd.h>
45 1.1 jakllsch
46 1.3 dyoung #include <sys/bus.h>
47 1.1 jakllsch
48 1.1 jakllsch #include <arch/x86/pci/i82802reg.h>
49 1.1 jakllsch
50 1.1 jakllsch struct fwhrng_softc {
51 1.1 jakllsch device_t sc_dev;
52 1.1 jakllsch
53 1.1 jakllsch bus_space_tag_t sc_st;
54 1.1 jakllsch bus_space_handle_t sc_sh;
55 1.1 jakllsch
56 1.1 jakllsch struct callout sc_rnd_ch;
57 1.4 tls krndsource_t sc_rnd_source;
58 1.1 jakllsch
59 1.1 jakllsch int sc_rnd_i;
60 1.1 jakllsch uint32_t sc_rnd_ax;
61 1.1 jakllsch };
62 1.1 jakllsch
63 1.1 jakllsch static int fwhrng_match(device_t, cfdata_t, void *);
64 1.1 jakllsch static void fwhrng_attach(device_t, device_t, void *);
65 1.1 jakllsch static int fwhrng_detach(device_t, int);
66 1.1 jakllsch
67 1.1 jakllsch static void fwhrng_callout(void *v);
68 1.1 jakllsch
69 1.1 jakllsch #define FWHRNG_RETRIES 1000
70 1.1 jakllsch #define FWHRNG_MIN_SAMPLES 10
71 1.1 jakllsch
72 1.1 jakllsch CFATTACH_DECL_NEW(fwhrng, sizeof(struct fwhrng_softc),
73 1.1 jakllsch fwhrng_match, fwhrng_attach, fwhrng_detach, NULL);
74 1.1 jakllsch
75 1.1 jakllsch static int
76 1.1 jakllsch fwhrng_match(device_t parent, cfdata_t match, void *aux)
77 1.1 jakllsch {
78 1.1 jakllsch bus_space_tag_t bst;
79 1.1 jakllsch bus_space_handle_t bsh;
80 1.1 jakllsch int ret;
81 1.1 jakllsch uint8_t id0, id1, data0, data1;
82 1.1 jakllsch
83 1.1 jakllsch ret = 0;
84 1.1 jakllsch
85 1.1 jakllsch bst = x86_bus_space_mem;
86 1.1 jakllsch
87 1.1 jakllsch /* read chip ID */
88 1.2 jakllsch if (bus_space_map(bst, I82802AB_MEMBASE, I82802AB_WINSIZE, 0, &bsh))
89 1.1 jakllsch return 0;
90 1.1 jakllsch
91 1.1 jakllsch bus_space_write_1(bst, bsh, 0, 0xff); /* reset */
92 1.1 jakllsch data0 = bus_space_read_1(bst, bsh, 0);
93 1.1 jakllsch data1 = bus_space_read_1(bst, bsh, 1);
94 1.1 jakllsch bus_space_write_1(bst, bsh, 0, 0x90); /* enter read id */
95 1.1 jakllsch id0 = bus_space_read_1(bst, bsh, 0);
96 1.1 jakllsch id1 = bus_space_read_1(bst, bsh, 1);
97 1.1 jakllsch bus_space_write_1(bst, bsh, 0, 0xff); /* reset */
98 1.1 jakllsch
99 1.2 jakllsch bus_space_unmap(bst, bsh, I82802AB_WINSIZE);
100 1.1 jakllsch
101 1.1 jakllsch aprint_debug_dev(parent, "fwh: data %02x,%02x, id %02x,%02x\n",
102 1.1 jakllsch data0, data1, id0, id1);
103 1.1 jakllsch
104 1.1 jakllsch /* unlikely to have these match if we actually read the ID */
105 1.1 jakllsch if ((id0 == data0) && (id1 == data1))
106 1.1 jakllsch return 0;
107 1.1 jakllsch
108 1.1 jakllsch /* check for chips with RNG */
109 1.1 jakllsch if (!(id0 == I82802_MFG))
110 1.1 jakllsch return 0;
111 1.1 jakllsch if (!((id1 == I82802AB_ID) || (id1 == I82802AC_ID)))
112 1.1 jakllsch return 0;
113 1.1 jakllsch
114 1.1 jakllsch /* check for RNG presence */
115 1.1 jakllsch if (bus_space_map(bst, I82802AC_REGBASE, I82802AC_WINSIZE, 0, &bsh))
116 1.1 jakllsch return 0;
117 1.1 jakllsch data0 = bus_space_read_1(bst, bsh, I82802_RNG_HSR);
118 1.1 jakllsch bus_space_unmap(bst, bsh, I82802AC_WINSIZE);
119 1.1 jakllsch if ((data0 & I82802_RNG_HSR_PRESENT) == I82802_RNG_HSR_PRESENT)
120 1.1 jakllsch return 1;
121 1.1 jakllsch
122 1.1 jakllsch return 0;
123 1.1 jakllsch }
124 1.1 jakllsch
125 1.1 jakllsch static void
126 1.1 jakllsch fwhrng_attach(device_t parent, device_t self, void *aux)
127 1.1 jakllsch {
128 1.1 jakllsch struct fwhrng_softc *sc;
129 1.1 jakllsch int i, j, count_ff;
130 1.1 jakllsch uint8_t reg8;
131 1.1 jakllsch
132 1.1 jakllsch sc = device_private(self);
133 1.1 jakllsch sc->sc_dev = self;
134 1.1 jakllsch
135 1.1 jakllsch aprint_naive("\n");
136 1.1 jakllsch aprint_normal(": Intel Firmware Hub Random Number Generator\n");
137 1.1 jakllsch
138 1.1 jakllsch sc->sc_st = x86_bus_space_mem;
139 1.1 jakllsch
140 1.1 jakllsch if (bus_space_map(sc->sc_st, I82802AC_REGBASE, I82802AC_WINSIZE, 0,
141 1.1 jakllsch &sc->sc_sh) != 0) {
142 1.1 jakllsch aprint_error_dev(self, "unable to map registers\n");
143 1.1 jakllsch return;
144 1.1 jakllsch }
145 1.1 jakllsch
146 1.1 jakllsch /* Enable the RNG. */
147 1.1 jakllsch reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR);
148 1.1 jakllsch bus_space_write_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR,
149 1.1 jakllsch reg8 | I82802_RNG_HSR_ENABLE);
150 1.1 jakllsch reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR);
151 1.1 jakllsch if ((reg8 & I82802_RNG_HSR_ENABLE) == 0) {
152 1.1 jakllsch aprint_error_dev(self, "unable to enable\n");
153 1.1 jakllsch bus_space_unmap(sc->sc_st, sc->sc_sh, I82802AC_WINSIZE);
154 1.1 jakllsch return;
155 1.1 jakllsch }
156 1.1 jakllsch
157 1.1 jakllsch /* Check to see if we can read data from the RNG. */
158 1.1 jakllsch count_ff = 0;
159 1.1 jakllsch for (j = 0; j < FWHRNG_MIN_SAMPLES; ++j) {
160 1.1 jakllsch for (i = 0; i < FWHRNG_RETRIES; i++) {
161 1.1 jakllsch reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh,
162 1.1 jakllsch I82802_RNG_DSR);
163 1.1 jakllsch if (!(reg8 & I82802_RNG_DSR_VALID)) {
164 1.1 jakllsch delay(10);
165 1.1 jakllsch continue;
166 1.1 jakllsch }
167 1.1 jakllsch reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh,
168 1.1 jakllsch I82802_RNG_DR);
169 1.1 jakllsch break;
170 1.1 jakllsch }
171 1.1 jakllsch if (i == FWHRNG_RETRIES) {
172 1.1 jakllsch bus_space_unmap(sc->sc_st, sc->sc_sh, I82802AC_WINSIZE);
173 1.1 jakllsch aprint_verbose_dev(sc->sc_dev,
174 1.1 jakllsch "timeout reading test samples, RNG disabled.\n");
175 1.1 jakllsch return;
176 1.1 jakllsch }
177 1.1 jakllsch if (reg8 == 0xff)
178 1.1 jakllsch ++count_ff;
179 1.1 jakllsch }
180 1.1 jakllsch
181 1.1 jakllsch if (count_ff == FWHRNG_MIN_SAMPLES) {
182 1.1 jakllsch /* Disable the RNG. */
183 1.1 jakllsch reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR);
184 1.1 jakllsch bus_space_write_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR,
185 1.1 jakllsch reg8 & ~I82802_RNG_HSR_ENABLE);
186 1.1 jakllsch bus_space_unmap(sc->sc_st, sc->sc_sh, I82802AC_WINSIZE);
187 1.1 jakllsch aprint_error_dev(sc->sc_dev,
188 1.1 jakllsch "returns constant 0xff stream, RNG disabled.\n");
189 1.1 jakllsch return;
190 1.1 jakllsch }
191 1.1 jakllsch
192 1.1 jakllsch /*
193 1.1 jakllsch * Should test entropy source to ensure
194 1.1 jakllsch * that it passes the Statistical Random
195 1.1 jakllsch * Number Generator Tests in section 4.11.1,
196 1.1 jakllsch * FIPS PUB 140-1.
197 1.1 jakllsch *
198 1.1 jakllsch * http://csrc.nist.gov/fips/fips1401.htm
199 1.1 jakllsch */
200 1.1 jakllsch
201 1.1 jakllsch aprint_debug_dev(sc->sc_dev, "random number generator enabled\n");
202 1.1 jakllsch
203 1.1 jakllsch callout_init(&sc->sc_rnd_ch, 0);
204 1.1 jakllsch /* FWH is polled for entropy, so no estimate is available. */
205 1.1 jakllsch rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
206 1.1 jakllsch RND_TYPE_RNG, RND_FLAG_NO_ESTIMATE);
207 1.1 jakllsch sc->sc_rnd_i = sizeof(sc->sc_rnd_ax);
208 1.1 jakllsch fwhrng_callout(sc);
209 1.1 jakllsch
210 1.1 jakllsch return;
211 1.1 jakllsch }
212 1.1 jakllsch
213 1.1 jakllsch static int
214 1.1 jakllsch fwhrng_detach(device_t self, int flags)
215 1.1 jakllsch {
216 1.1 jakllsch struct fwhrng_softc *sc;
217 1.1 jakllsch uint8_t reg8;
218 1.1 jakllsch
219 1.1 jakllsch sc = device_private(self);
220 1.1 jakllsch
221 1.1 jakllsch rnd_detach_source(&sc->sc_rnd_source);
222 1.1 jakllsch
223 1.1 jakllsch callout_stop(&sc->sc_rnd_ch);
224 1.1 jakllsch callout_destroy(&sc->sc_rnd_ch);
225 1.1 jakllsch
226 1.1 jakllsch /* Disable the RNG. */
227 1.1 jakllsch reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR);
228 1.1 jakllsch bus_space_write_1(sc->sc_st, sc->sc_sh, I82802_RNG_HSR,
229 1.1 jakllsch reg8 & ~I82802_RNG_HSR_ENABLE);
230 1.1 jakllsch
231 1.1 jakllsch bus_space_unmap(sc->sc_st, sc->sc_sh, I82802AC_WINSIZE);
232 1.1 jakllsch
233 1.1 jakllsch return 0;
234 1.1 jakllsch }
235 1.1 jakllsch
236 1.1 jakllsch static void
237 1.1 jakllsch fwhrng_callout(void *v)
238 1.1 jakllsch {
239 1.1 jakllsch struct fwhrng_softc *sc = v;
240 1.1 jakllsch
241 1.1 jakllsch if ((bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_DSR) &
242 1.1 jakllsch I82802_RNG_DSR_VALID) != 0) {
243 1.1 jakllsch sc->sc_rnd_ax = (sc->sc_rnd_ax << NBBY) |
244 1.1 jakllsch bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_DR);
245 1.1 jakllsch if (--sc->sc_rnd_i == 0) {
246 1.1 jakllsch sc->sc_rnd_i = sizeof(sc->sc_rnd_ax);
247 1.1 jakllsch rnd_add_data(&sc->sc_rnd_source, &sc->sc_rnd_ax,
248 1.1 jakllsch sizeof(sc->sc_rnd_ax),
249 1.1 jakllsch sizeof(sc->sc_rnd_ax) * NBBY);
250 1.1 jakllsch }
251 1.1 jakllsch }
252 1.1 jakllsch callout_reset(&sc->sc_rnd_ch, 1, fwhrng_callout, sc);
253 1.1 jakllsch }
254