tpm.c revision 1.13.2.2 1 1.13.2.2 martin /* $NetBSD: tpm.c,v 1.13.2.2 2022/08/03 16:00:47 martin Exp $ */
2 1.13 maxv
3 1.13 maxv /*
4 1.13 maxv * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 1.13 maxv * All rights reserved.
6 1.13 maxv *
7 1.13 maxv * This code is derived from software contributed to The NetBSD Foundation
8 1.13 maxv * by Maxime Villard.
9 1.13 maxv *
10 1.13 maxv * Redistribution and use in source and binary forms, with or without
11 1.13 maxv * modification, are permitted provided that the following conditions
12 1.13 maxv * are met:
13 1.13 maxv * 1. Redistributions of source code must retain the above copyright
14 1.13 maxv * notice, this list of conditions and the following disclaimer.
15 1.13 maxv * 2. Redistributions in binary form must reproduce the above copyright
16 1.13 maxv * notice, this list of conditions and the following disclaimer in the
17 1.13 maxv * documentation and/or other materials provided with the distribution.
18 1.13 maxv *
19 1.13 maxv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.13 maxv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.13 maxv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.13 maxv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.13 maxv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.13 maxv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.13 maxv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.13 maxv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.13 maxv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.13 maxv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.13 maxv * POSSIBILITY OF SUCH DAMAGE.
30 1.13 maxv */
31 1.13 maxv
32 1.1 christos /*
33 1.1 christos * Copyright (c) 2008, 2009 Michael Shalayeff
34 1.13 maxv * Copyright (c) 2009, 2010 Hans-Joerg Hoexer
35 1.1 christos * All rights reserved.
36 1.1 christos *
37 1.1 christos * Permission to use, copy, modify, and distribute this software for any
38 1.1 christos * purpose with or without fee is hereby granted, provided that the above
39 1.1 christos * copyright notice and this permission notice appear in all copies.
40 1.1 christos *
41 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
42 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
43 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
44 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
45 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
46 1.1 christos * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
47 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48 1.1 christos */
49 1.1 christos
50 1.1 christos #include <sys/cdefs.h>
51 1.13.2.2 martin __KERNEL_RCSID(0, "$NetBSD: tpm.c,v 1.13.2.2 2022/08/03 16:00:47 martin Exp $");
52 1.1 christos
53 1.1 christos #include <sys/param.h>
54 1.13.2.2 martin #include <sys/types.h>
55 1.13.2.2 martin
56 1.13.2.2 martin #include <sys/atomic.h>
57 1.13.2.2 martin #include <sys/bus.h>
58 1.13.2.2 martin #include <sys/conf.h>
59 1.13.2.2 martin #include <sys/device.h>
60 1.1 christos #include <sys/kernel.h>
61 1.1 christos #include <sys/malloc.h>
62 1.1 christos #include <sys/pmf.h>
63 1.13.2.2 martin #include <sys/proc.h>
64 1.13.2.2 martin #include <sys/systm.h>
65 1.13.2.2 martin #include <sys/workqueue.h>
66 1.1 christos
67 1.1 christos #include <dev/ic/tpmreg.h>
68 1.1 christos #include <dev/ic/tpmvar.h>
69 1.1 christos
70 1.12 riastrad #include "ioconf.h"
71 1.12 riastrad
72 1.13.2.1 martin CTASSERT(sizeof(struct tpm_header) == 10);
73 1.13.2.1 martin
74 1.13 maxv #define TPM_BUFSIZ 1024
75 1.13.2.1 martin
76 1.13 maxv #define TPM_PARAM_SIZE 0x0001 /* that's a flag */
77 1.13 maxv
78 1.13 maxv /* Timeouts. */
79 1.13 maxv #define TPM_ACCESS_TMO 2000 /* 2sec */
80 1.13 maxv #define TPM_READY_TMO 2000 /* 2sec */
81 1.13 maxv #define TPM_READ_TMO 2000 /* 2sec */
82 1.13 maxv #define TPM_BURST_TMO 2000 /* 2sec */
83 1.13 maxv
84 1.13 maxv #define TPM_CAPS_REQUIRED \
85 1.13 maxv (TPM_INTF_DATA_AVAIL_INT|TPM_INTF_LOCALITY_CHANGE_INT| \
86 1.13 maxv TPM_INTF_INT_LEVEL_LOW)
87 1.1 christos
88 1.13 maxv static inline int
89 1.13 maxv tpm_tmotohz(int tmo)
90 1.1 christos {
91 1.13 maxv struct timeval tv;
92 1.1 christos
93 1.13 maxv tv.tv_sec = tmo / 1000;
94 1.13 maxv tv.tv_usec = 1000 * (tmo % 1000);
95 1.1 christos
96 1.13 maxv return tvtohz(&tv);
97 1.1 christos }
98 1.1 christos
99 1.13 maxv static int
100 1.1 christos tpm_getburst(struct tpm_softc *sc)
101 1.1 christos {
102 1.1 christos int burst, to, rv;
103 1.1 christos
104 1.1 christos to = tpm_tmotohz(TPM_BURST_TMO);
105 1.1 christos
106 1.13 maxv while (to--) {
107 1.1 christos /*
108 1.13 maxv * Burst count is in bits 23:8, so read the two higher bytes.
109 1.1 christos */
110 1.1 christos burst = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS + 1);
111 1.1 christos burst |= bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS + 2)
112 1.1 christos << 8;
113 1.13 maxv
114 1.1 christos if (burst)
115 1.1 christos return burst;
116 1.1 christos
117 1.13.2.1 martin rv = tsleep(sc, PCATCH, "tpm_getburst", 1);
118 1.1 christos if (rv && rv != EWOULDBLOCK) {
119 1.1 christos return 0;
120 1.1 christos }
121 1.1 christos }
122 1.1 christos
123 1.1 christos return 0;
124 1.1 christos }
125 1.1 christos
126 1.13 maxv static inline uint8_t
127 1.1 christos tpm_status(struct tpm_softc *sc)
128 1.1 christos {
129 1.13 maxv return bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS) &
130 1.13 maxv TPM_STS_STATUS_BITS;
131 1.1 christos }
132 1.1 christos
133 1.13 maxv /* -------------------------------------------------------------------------- */
134 1.1 christos
135 1.13.2.1 martin static bool
136 1.13.2.1 martin tpm12_suspend(struct tpm_softc *sc)
137 1.13.2.1 martin {
138 1.13.2.1 martin static const uint8_t command[10] = {
139 1.13.2.1 martin 0x00, 0xC1, /* TPM_TAG_RQU_COMMAND */
140 1.13.2.1 martin 0x00, 0x00, 0x00, 10, /* Length in bytes */
141 1.13.2.1 martin 0x00, 0x00, 0x00, 0x98 /* TPM_ORD_SaveState */
142 1.13.2.1 martin };
143 1.13.2.1 martin struct tpm_header response;
144 1.13.2.2 martin size_t nread;
145 1.13.2.2 martin bool endwrite = false, endread = false;
146 1.13.2.2 martin int error;
147 1.1 christos
148 1.13.2.2 martin /*
149 1.13.2.2 martin * Write the command.
150 1.13.2.2 martin */
151 1.13.2.2 martin error = (*sc->sc_intf->start)(sc, UIO_WRITE);
152 1.13.2.2 martin if (error) {
153 1.13.2.2 martin device_printf(sc->sc_dev, "start write failed: %d", error);
154 1.13.2.2 martin goto out;
155 1.13.2.2 martin }
156 1.13.2.2 martin
157 1.13.2.2 martin endwrite = true;
158 1.13.2.2 martin
159 1.13.2.2 martin error = (*sc->sc_intf->write)(sc, &command, sizeof(command));
160 1.13.2.2 martin if (error) {
161 1.13.2.2 martin device_printf(sc->sc_dev, "write TPM_ORD_SaveState failed: %d",
162 1.13.2.2 martin error);
163 1.13.2.2 martin goto out;
164 1.13.2.2 martin }
165 1.13.2.2 martin
166 1.13.2.2 martin endwrite = false;
167 1.13.2.2 martin
168 1.13.2.2 martin error = (*sc->sc_intf->end)(sc, UIO_WRITE, 0);
169 1.13.2.2 martin if (error) {
170 1.13.2.2 martin device_printf(sc->sc_dev, "end write failed: %d", error);
171 1.13.2.2 martin goto out;
172 1.13.2.2 martin }
173 1.13.2.2 martin
174 1.13.2.2 martin /*
175 1.13.2.2 martin * Read the response -- just the header; we don't expect a
176 1.13.2.2 martin * payload.
177 1.13.2.2 martin */
178 1.13.2.2 martin error = (*sc->sc_intf->start)(sc, UIO_READ);
179 1.13.2.2 martin if (error) {
180 1.13.2.2 martin device_printf(sc->sc_dev, "start read failed: %d", error);
181 1.13.2.2 martin goto out;
182 1.13.2.2 martin }
183 1.13.2.2 martin
184 1.13.2.2 martin endread = true;
185 1.13.2.2 martin
186 1.13.2.2 martin error = (*sc->sc_intf->read)(sc, &response, sizeof(response), &nread,
187 1.13.2.2 martin 0);
188 1.13.2.2 martin if (error) {
189 1.13.2.2 martin device_printf(sc->sc_dev, "read failed: %d", error);
190 1.13.2.2 martin goto out;
191 1.13.2.2 martin }
192 1.13.2.2 martin if (nread != sizeof(response)) {
193 1.13.2.2 martin device_printf(sc->sc_dev, "short header read: %zu", nread);
194 1.13.2.2 martin goto out;
195 1.13.2.2 martin }
196 1.13.2.1 martin
197 1.13.2.2 martin endread = false;
198 1.13.2.2 martin
199 1.13.2.2 martin error = (*sc->sc_intf->end)(sc, UIO_READ, 0);
200 1.13.2.2 martin if (error) {
201 1.13.2.2 martin device_printf(sc->sc_dev, "end read failed: %d", error);
202 1.13.2.2 martin goto out;
203 1.13.2.2 martin }
204 1.13.2.2 martin
205 1.13.2.2 martin /*
206 1.13.2.2 martin * Verify the response looks reasonable.
207 1.13.2.2 martin */
208 1.13.2.2 martin if (be16toh(response.tag) != TPM_TAG_RSP_COMMAND ||
209 1.13.2.2 martin be32toh(response.length) != sizeof(response) ||
210 1.13.2.2 martin be32toh(response.code) != 0) {
211 1.13.2.2 martin device_printf(sc->sc_dev,
212 1.13.2.2 martin "TPM_ORD_SaveState failed: tag=0x%x length=0x%x code=0x%x",
213 1.13.2.2 martin be16toh(response.tag),
214 1.13.2.2 martin be32toh(response.length),
215 1.13.2.2 martin be32toh(response.code));
216 1.13.2.2 martin error = EIO;
217 1.13.2.2 martin goto out;
218 1.13.2.2 martin }
219 1.13.2.2 martin
220 1.13.2.2 martin /* Success! */
221 1.13.2.2 martin error = 0;
222 1.13.2.2 martin
223 1.13.2.2 martin out: if (endwrite)
224 1.13.2.2 martin error = (*sc->sc_intf->end)(sc, UIO_WRITE, error);
225 1.13.2.2 martin if (endread)
226 1.13.2.2 martin error = (*sc->sc_intf->end)(sc, UIO_READ, error);
227 1.13.2.2 martin if (error)
228 1.13.2.2 martin return false;
229 1.13.2.1 martin return true;
230 1.13.2.1 martin }
231 1.13.2.1 martin
232 1.13.2.1 martin static bool
233 1.13.2.1 martin tpm20_suspend(struct tpm_softc *sc)
234 1.1 christos {
235 1.13.2.1 martin static const uint8_t command[12] = {
236 1.13.2.1 martin 0x80, 0x01, /* TPM_ST_NO_SESSIONS */
237 1.13.2.1 martin 0x00, 0x00, 0x00, 12, /* Length in bytes */
238 1.13.2.1 martin 0x00, 0x00, 0x01, 0x45, /* TPM_CC_Shutdown */
239 1.13.2.1 martin 0x00, 0x01 /* TPM_SU_STATE */
240 1.1 christos };
241 1.13.2.1 martin struct tpm_header response;
242 1.13.2.2 martin size_t nread;
243 1.13.2.2 martin bool endwrite = false, endread = false;
244 1.13.2.2 martin int error;
245 1.1 christos
246 1.13.2.2 martin /*
247 1.13.2.2 martin * Write the command.
248 1.13.2.2 martin */
249 1.13.2.2 martin error = (*sc->sc_intf->start)(sc, UIO_WRITE);
250 1.13.2.2 martin if (error) {
251 1.13.2.2 martin device_printf(sc->sc_dev, "start write failed: %d", error);
252 1.13.2.2 martin goto out;
253 1.13.2.2 martin }
254 1.13.2.2 martin
255 1.13.2.2 martin endwrite = true;
256 1.13.2.2 martin
257 1.13.2.2 martin error = (*sc->sc_intf->write)(sc, &command, sizeof(command));
258 1.13.2.2 martin if (error) {
259 1.13.2.2 martin device_printf(sc->sc_dev, "write TPM_ORD_SaveState failed: %d",
260 1.13.2.2 martin error);
261 1.13.2.2 martin goto out;
262 1.13.2.2 martin }
263 1.13.2.2 martin
264 1.13.2.2 martin endwrite = false;
265 1.13.2.2 martin
266 1.13.2.2 martin error = (*sc->sc_intf->end)(sc, UIO_WRITE, 0);
267 1.13.2.2 martin if (error) {
268 1.13.2.2 martin device_printf(sc->sc_dev, "end write failed: %d", error);
269 1.13.2.2 martin goto out;
270 1.13.2.2 martin }
271 1.13 maxv
272 1.13.2.2 martin /*
273 1.13.2.2 martin * Read the response -- just the header; we don't expect a
274 1.13.2.2 martin * payload.
275 1.13.2.2 martin */
276 1.13.2.2 martin error = (*sc->sc_intf->start)(sc, UIO_READ);
277 1.13.2.2 martin if (error) {
278 1.13.2.2 martin device_printf(sc->sc_dev, "start read failed: %d", error);
279 1.13.2.2 martin goto out;
280 1.13.2.2 martin }
281 1.13.2.2 martin
282 1.13.2.2 martin endread = true;
283 1.13.2.2 martin
284 1.13.2.2 martin error = (*sc->sc_intf->read)(sc, &response, sizeof(response), &nread,
285 1.13.2.2 martin 0);
286 1.13.2.2 martin if (error) {
287 1.13.2.2 martin device_printf(sc->sc_dev, "read failed: %d", error);
288 1.13.2.2 martin goto out;
289 1.13.2.2 martin }
290 1.13.2.2 martin if (nread != sizeof(response)) {
291 1.13.2.2 martin device_printf(sc->sc_dev, "short header read: %zu", nread);
292 1.13.2.2 martin goto out;
293 1.13.2.2 martin }
294 1.13.2.2 martin
295 1.13.2.2 martin endread = false;
296 1.13.2.2 martin
297 1.13.2.2 martin error = (*sc->sc_intf->end)(sc, UIO_READ, 0);
298 1.13.2.2 martin if (error) {
299 1.13.2.2 martin device_printf(sc->sc_dev, "end read failed: %d", error);
300 1.13.2.2 martin goto out;
301 1.13.2.2 martin }
302 1.13.2.2 martin
303 1.13.2.2 martin /*
304 1.13.2.2 martin * Verify the response looks reasonable.
305 1.13.2.2 martin */
306 1.13.2.2 martin if (be16toh(response.tag) != TPM2_ST_NO_SESSIONS ||
307 1.13.2.2 martin be32toh(response.length) != sizeof(response) ||
308 1.13.2.2 martin be32toh(response.code) != TPM2_RC_SUCCESS) {
309 1.13.2.2 martin device_printf(sc->sc_dev,
310 1.13.2.2 martin "TPM_CC_Shutdown failed: tag=0x%x length=0x%x code=0x%x",
311 1.13.2.2 martin be16toh(response.tag),
312 1.13.2.2 martin be32toh(response.length),
313 1.13.2.2 martin be32toh(response.code));
314 1.13.2.2 martin error = EIO;
315 1.13.2.2 martin goto out;
316 1.13.2.2 martin }
317 1.13.2.2 martin
318 1.13.2.2 martin /* Success! */
319 1.13.2.2 martin error = 0;
320 1.13.2.2 martin
321 1.13.2.2 martin out: if (endwrite)
322 1.13.2.2 martin error = (*sc->sc_intf->end)(sc, UIO_WRITE, error);
323 1.13.2.2 martin if (endread)
324 1.13.2.2 martin error = (*sc->sc_intf->end)(sc, UIO_READ, error);
325 1.13.2.2 martin if (error)
326 1.13.2.2 martin return false;
327 1.7 christos return true;
328 1.1 christos }
329 1.1 christos
330 1.1 christos bool
331 1.13.2.1 martin tpm_suspend(device_t dev, const pmf_qual_t *qual)
332 1.1 christos {
333 1.13.2.1 martin struct tpm_softc *sc = device_private(dev);
334 1.13.2.1 martin
335 1.13.2.1 martin switch (sc->sc_ver) {
336 1.13.2.1 martin case TPM_1_2:
337 1.13.2.1 martin return tpm12_suspend(sc);
338 1.13.2.1 martin case TPM_2_0:
339 1.13.2.1 martin return tpm20_suspend(sc);
340 1.13.2.1 martin default:
341 1.13.2.1 martin panic("%s: impossible", __func__);
342 1.13.2.1 martin }
343 1.13.2.1 martin }
344 1.13.2.1 martin
345 1.13.2.1 martin bool
346 1.13.2.1 martin tpm_resume(device_t dev, const pmf_qual_t *qual)
347 1.13.2.1 martin {
348 1.13.2.1 martin /*
349 1.13.2.1 martin * Don't do anything, the BIOS is supposed to restore the previously
350 1.13.2.1 martin * saved state.
351 1.13.2.1 martin */
352 1.7 christos return true;
353 1.1 christos }
354 1.1 christos
355 1.13 maxv /* -------------------------------------------------------------------------- */
356 1.13 maxv
357 1.13 maxv static int
358 1.13.2.1 martin tpm_poll(struct tpm_softc *sc, uint8_t mask, int to, wchan_t chan)
359 1.1 christos {
360 1.1 christos int rv;
361 1.1 christos
362 1.13 maxv while (((sc->sc_status = tpm_status(sc)) & mask) != mask && to--) {
363 1.13.2.1 martin rv = tsleep(chan, PCATCH, "tpm_poll", 1);
364 1.1 christos if (rv && rv != EWOULDBLOCK) {
365 1.1 christos return rv;
366 1.1 christos }
367 1.1 christos }
368 1.1 christos
369 1.1 christos return 0;
370 1.1 christos }
371 1.1 christos
372 1.13 maxv static int
373 1.13 maxv tpm_waitfor(struct tpm_softc *sc, uint8_t bits, int tmo, wchan_t chan)
374 1.1 christos {
375 1.13 maxv int retry, to, rv;
376 1.13 maxv uint8_t todo;
377 1.1 christos
378 1.13.2.1 martin to = tpm_tmotohz(tmo);
379 1.13 maxv retry = 3;
380 1.13 maxv
381 1.1 christos restart:
382 1.13 maxv todo = bits;
383 1.1 christos
384 1.13.2.1 martin /*
385 1.13.2.1 martin * TPM_STS_VALID has priority over the others.
386 1.13.2.1 martin */
387 1.13.2.1 martin if (todo & TPM_STS_VALID) {
388 1.13.2.1 martin if ((rv = tpm_poll(sc, TPM_STS_VALID, to+1, chan)) != 0)
389 1.13.2.1 martin return rv;
390 1.13.2.1 martin todo &= ~TPM_STS_VALID;
391 1.1 christos }
392 1.1 christos
393 1.13.2.1 martin if ((rv = tpm_poll(sc, todo, to, chan)) != 0)
394 1.13.2.1 martin return rv;
395 1.13.2.1 martin
396 1.13 maxv if ((todo & sc->sc_status) != todo) {
397 1.13.2.1 martin if ((retry-- > 0) && (bits & TPM_STS_VALID)) {
398 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
399 1.1 christos TPM_STS_RESP_RETRY);
400 1.1 christos goto restart;
401 1.1 christos }
402 1.1 christos return EIO;
403 1.1 christos }
404 1.1 christos
405 1.1 christos return 0;
406 1.1 christos }
407 1.1 christos
408 1.13.2.1 martin /* -------------------------------------------------------------------------- */
409 1.13.2.1 martin
410 1.13.2.1 martin /*
411 1.13.2.1 martin * TPM using the TIS 1.2 interface.
412 1.13.2.1 martin */
413 1.13.2.1 martin
414 1.13.2.1 martin static int
415 1.13.2.1 martin tpm12_request_locality(struct tpm_softc *sc, int l)
416 1.13 maxv {
417 1.13.2.1 martin uint32_t r;
418 1.13.2.1 martin int to, rv;
419 1.13.2.1 martin
420 1.13.2.1 martin if (l != 0)
421 1.13.2.1 martin return EINVAL;
422 1.13 maxv
423 1.13.2.1 martin if ((bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
424 1.13.2.1 martin (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) ==
425 1.13.2.1 martin (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY))
426 1.13 maxv return 0;
427 1.13 maxv
428 1.13.2.1 martin bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
429 1.13.2.1 martin TPM_ACCESS_REQUEST_USE);
430 1.13 maxv
431 1.13.2.1 martin to = tpm_tmotohz(TPM_ACCESS_TMO);
432 1.13 maxv
433 1.13.2.1 martin while ((r = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
434 1.13.2.1 martin (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
435 1.13.2.1 martin (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && to--) {
436 1.13.2.1 martin rv = tsleep(sc->sc_intf->init, PCATCH, "tpm_locality", 1);
437 1.13.2.1 martin if (rv && rv != EWOULDBLOCK) {
438 1.13.2.1 martin return rv;
439 1.13.2.1 martin }
440 1.13.2.1 martin }
441 1.13 maxv
442 1.13.2.1 martin if ((r & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
443 1.13.2.1 martin (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
444 1.13.2.1 martin return EBUSY;
445 1.13.2.1 martin }
446 1.13 maxv
447 1.13.2.1 martin return 0;
448 1.13.2.1 martin }
449 1.13 maxv
450 1.13.2.1 martin static int
451 1.13 maxv tpm_tis12_probe(bus_space_tag_t bt, bus_space_handle_t bh)
452 1.13 maxv {
453 1.13 maxv uint32_t cap;
454 1.13 maxv uint8_t reg;
455 1.13 maxv int tmo;
456 1.13 maxv
457 1.13 maxv cap = bus_space_read_4(bt, bh, TPM_INTF_CAPABILITY);
458 1.13 maxv if (cap == 0xffffffff)
459 1.13.2.1 martin return EINVAL;
460 1.13 maxv if ((cap & TPM_CAPS_REQUIRED) != TPM_CAPS_REQUIRED)
461 1.13.2.1 martin return ENOTSUP;
462 1.13 maxv
463 1.13 maxv /* Request locality 0. */
464 1.13 maxv bus_space_write_1(bt, bh, TPM_ACCESS, TPM_ACCESS_REQUEST_USE);
465 1.13 maxv
466 1.13 maxv /* Wait for it to become active. */
467 1.13 maxv tmo = TPM_ACCESS_TMO; /* Milliseconds. */
468 1.13 maxv while ((reg = bus_space_read_1(bt, bh, TPM_ACCESS) &
469 1.13 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
470 1.13 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && tmo--) {
471 1.13 maxv DELAY(1000); /* 1 millisecond. */
472 1.13 maxv }
473 1.13 maxv if ((reg & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
474 1.13 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
475 1.13.2.1 martin return ETIMEDOUT;
476 1.13 maxv }
477 1.13 maxv
478 1.13 maxv if (bus_space_read_4(bt, bh, TPM_ID) == 0xffffffff)
479 1.13.2.1 martin return EINVAL;
480 1.13 maxv
481 1.13 maxv return 0;
482 1.13 maxv }
483 1.13 maxv
484 1.13.2.1 martin static int
485 1.13.2.2 martin tpm12_rng(struct tpm_softc *sc, unsigned *entropybitsp)
486 1.13.2.2 martin {
487 1.13.2.2 martin /*
488 1.13.2.2 martin * TPM Specification Version 1.2, Main Part 3: Commands,
489 1.13.2.2 martin * Sec. 13.6 TPM_GetRandom
490 1.13.2.2 martin */
491 1.13.2.2 martin struct {
492 1.13.2.2 martin struct tpm_header hdr;
493 1.13.2.2 martin uint32_t bytesRequested;
494 1.13.2.2 martin } __packed command;
495 1.13.2.2 martin struct response {
496 1.13.2.2 martin struct tpm_header hdr;
497 1.13.2.2 martin uint32_t randomBytesSize;
498 1.13.2.2 martin uint8_t bytes[64];
499 1.13.2.2 martin } __packed response;
500 1.13.2.2 martin bool endwrite = false, endread = false;
501 1.13.2.2 martin size_t nread;
502 1.13.2.2 martin uint16_t tag;
503 1.13.2.2 martin uint32_t pktlen, code, nbytes, entropybits = 0;
504 1.13.2.2 martin int rv;
505 1.13.2.2 martin
506 1.13.2.2 martin /* Encode the command. */
507 1.13.2.2 martin memset(&command, 0, sizeof(command));
508 1.13.2.2 martin command.hdr.tag = htobe16(TPM_TAG_RQU_COMMAND);
509 1.13.2.2 martin command.hdr.length = htobe32(sizeof(command));
510 1.13.2.2 martin command.hdr.code = htobe32(TPM_ORD_GetRandom);
511 1.13.2.2 martin command.bytesRequested = htobe32(sizeof(response.bytes));
512 1.13.2.2 martin
513 1.13.2.2 martin /* Write the command. */
514 1.13.2.2 martin if ((rv = (*sc->sc_intf->start)(sc, UIO_WRITE)) != 0) {
515 1.13.2.2 martin device_printf(sc->sc_dev, "start write failed, error=%d\n",
516 1.13.2.2 martin rv);
517 1.13.2.2 martin goto out;
518 1.13.2.2 martin }
519 1.13.2.2 martin endwrite = true;
520 1.13.2.2 martin if ((rv = (*sc->sc_intf->write)(sc, &command, sizeof(command))) != 0) {
521 1.13.2.2 martin device_printf(sc->sc_dev, "write failed, error=%d\n", rv);
522 1.13.2.2 martin goto out;
523 1.13.2.2 martin }
524 1.13.2.2 martin rv = (*sc->sc_intf->end)(sc, UIO_WRITE, 0);
525 1.13.2.2 martin endwrite = false;
526 1.13.2.2 martin if (rv) {
527 1.13.2.2 martin device_printf(sc->sc_dev, "end write failed, error=%d\n", rv);
528 1.13.2.2 martin goto out;
529 1.13.2.2 martin }
530 1.13.2.2 martin
531 1.13.2.2 martin /* Read the response header. */
532 1.13.2.2 martin if ((rv = (*sc->sc_intf->start)(sc, UIO_READ)) != 0) {
533 1.13.2.2 martin device_printf(sc->sc_dev, "start write failed, error=%d\n",
534 1.13.2.2 martin rv);
535 1.13.2.2 martin goto out;
536 1.13.2.2 martin }
537 1.13.2.2 martin endread = true;
538 1.13.2.2 martin if ((rv = (*sc->sc_intf->read)(sc, &response.hdr, sizeof(response.hdr),
539 1.13.2.2 martin &nread, 0)) != 0) {
540 1.13.2.2 martin device_printf(sc->sc_dev, "read failed, error=%d\n", rv);
541 1.13.2.2 martin goto out;
542 1.13.2.2 martin }
543 1.13.2.2 martin
544 1.13.2.2 martin /* Verify the response header looks sensible. */
545 1.13.2.2 martin if (nread != sizeof(response.hdr)) {
546 1.13.2.2 martin device_printf(sc->sc_dev, "read %zu bytes, expected %zu",
547 1.13.2.2 martin nread, sizeof(response.hdr));
548 1.13.2.2 martin goto out;
549 1.13.2.2 martin }
550 1.13.2.2 martin tag = be16toh(response.hdr.tag);
551 1.13.2.2 martin pktlen = be32toh(response.hdr.length);
552 1.13.2.2 martin code = be32toh(response.hdr.code);
553 1.13.2.2 martin if (tag != TPM_TAG_RSP_COMMAND ||
554 1.13.2.2 martin pktlen < offsetof(struct response, bytes) ||
555 1.13.2.2 martin pktlen > sizeof(response) ||
556 1.13.2.2 martin code != 0) {
557 1.13.2.2 martin /*
558 1.13.2.2 martin * If the tpm itself is busy (e.g., it has yet to run a
559 1.13.2.2 martin * self-test, or it's in a timeout period to defend
560 1.13.2.2 martin * against brute force attacks), then we can try again
561 1.13.2.2 martin * later. Otherwise, give up.
562 1.13.2.2 martin */
563 1.13.2.2 martin if (code & TPM_NON_FATAL) {
564 1.13.2.2 martin aprint_debug_dev(sc->sc_dev, "%s: tpm busy, code=%u\n",
565 1.13.2.2 martin __func__, code & ~TPM_NON_FATAL);
566 1.13.2.2 martin rv = 0;
567 1.13.2.2 martin } else if (code == TPM_DEACTIVATED) {
568 1.13.2.2 martin device_printf(sc->sc_dev, "tpm is deactivated\n");
569 1.13.2.2 martin rv = ENXIO;
570 1.13.2.2 martin } else {
571 1.13.2.2 martin device_printf(sc->sc_dev, "bad tpm response:"
572 1.13.2.2 martin " tag=%u len=%u code=%u\n", tag, pktlen, code);
573 1.13.2.2 martin hexdump(aprint_debug, "tpm response header",
574 1.13.2.2 martin (const void *)&response.hdr,
575 1.13.2.2 martin sizeof(response.hdr));
576 1.13.2.2 martin rv = EIO;
577 1.13.2.2 martin }
578 1.13.2.2 martin goto out;
579 1.13.2.2 martin }
580 1.13.2.2 martin
581 1.13.2.2 martin /* Read the response payload. */
582 1.13.2.2 martin if ((rv = (*sc->sc_intf->read)(sc,
583 1.13.2.2 martin (char *)&response + nread, pktlen - nread,
584 1.13.2.2 martin NULL, TPM_PARAM_SIZE)) != 0) {
585 1.13.2.2 martin device_printf(sc->sc_dev, "read failed, error=%d\n", rv);
586 1.13.2.2 martin goto out;
587 1.13.2.2 martin }
588 1.13.2.2 martin endread = false;
589 1.13.2.2 martin if ((rv = (*sc->sc_intf->end)(sc, UIO_READ, 0)) != 0) {
590 1.13.2.2 martin device_printf(sc->sc_dev, "end read failed, error=%d\n", rv);
591 1.13.2.2 martin goto out;
592 1.13.2.2 martin }
593 1.13.2.2 martin
594 1.13.2.2 martin /* Verify the number of bytes read looks sensible. */
595 1.13.2.2 martin nbytes = be32toh(response.randomBytesSize);
596 1.13.2.2 martin if (nbytes > pktlen - offsetof(struct response, bytes)) {
597 1.13.2.2 martin device_printf(sc->sc_dev, "overlong GetRandom length:"
598 1.13.2.2 martin " %u, max %zu\n",
599 1.13.2.2 martin nbytes, pktlen - offsetof(struct response, bytes));
600 1.13.2.2 martin nbytes = pktlen - offsetof(struct response, bytes);
601 1.13.2.2 martin }
602 1.13.2.2 martin
603 1.13.2.2 martin /*
604 1.13.2.2 martin * Enter the data into the entropy pool. Conservatively (or,
605 1.13.2.2 martin * perhaps, cargocultily) estimate half a bit of entropy per
606 1.13.2.2 martin * bit of data.
607 1.13.2.2 martin */
608 1.13.2.2 martin CTASSERT(sizeof(response.bytes) <= UINT_MAX/(NBBY/2));
609 1.13.2.2 martin entropybits = (NBBY/2)*nbytes;
610 1.13.2.2 martin rnd_add_data(&sc->sc_rnd, response.bytes, nbytes, entropybits);
611 1.13.2.2 martin
612 1.13.2.2 martin out: /* End the read or write if still ongoing. */
613 1.13.2.2 martin if (endread)
614 1.13.2.2 martin rv = (*sc->sc_intf->end)(sc, UIO_READ, rv);
615 1.13.2.2 martin if (endwrite)
616 1.13.2.2 martin rv = (*sc->sc_intf->end)(sc, UIO_WRITE, rv);
617 1.13.2.2 martin
618 1.13.2.2 martin *entropybitsp = entropybits;
619 1.13.2.2 martin return rv;
620 1.13.2.2 martin }
621 1.13.2.2 martin
622 1.13.2.2 martin static int
623 1.13.2.2 martin tpm20_rng(struct tpm_softc *sc, unsigned *entropybitsp)
624 1.13.2.2 martin {
625 1.13.2.2 martin /*
626 1.13.2.2 martin * Trusted Platform Module Library, Family "2.0", Level 00
627 1.13.2.2 martin * Revision 01.38, Part 3: Commands, Sec. 16.1 `TPM2_GetRandom'
628 1.13.2.2 martin *
629 1.13.2.2 martin * https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-3-Commands-01.38.pdf#page=133
630 1.13.2.2 martin */
631 1.13.2.2 martin struct {
632 1.13.2.2 martin struct tpm_header hdr;
633 1.13.2.2 martin uint16_t bytesRequested;
634 1.13.2.2 martin } __packed command;
635 1.13.2.2 martin struct response {
636 1.13.2.2 martin struct tpm_header hdr;
637 1.13.2.2 martin uint16_t randomBytesSize;
638 1.13.2.2 martin uint8_t bytes[64];
639 1.13.2.2 martin } __packed response;
640 1.13.2.2 martin bool endwrite = false, endread = false;
641 1.13.2.2 martin size_t nread;
642 1.13.2.2 martin uint16_t tag;
643 1.13.2.2 martin uint32_t pktlen, code, nbytes, entropybits = 0;
644 1.13.2.2 martin int rv;
645 1.13.2.2 martin
646 1.13.2.2 martin /* Encode the command. */
647 1.13.2.2 martin memset(&command, 0, sizeof(command));
648 1.13.2.2 martin command.hdr.tag = htobe16(TPM2_ST_NO_SESSIONS);
649 1.13.2.2 martin command.hdr.length = htobe32(sizeof(command));
650 1.13.2.2 martin command.hdr.code = htobe32(TPM2_CC_GetRandom);
651 1.13.2.2 martin command.bytesRequested = htobe16(sizeof(response.bytes));
652 1.13.2.2 martin
653 1.13.2.2 martin /* Write the command. */
654 1.13.2.2 martin if ((rv = (*sc->sc_intf->start)(sc, UIO_WRITE)) != 0) {
655 1.13.2.2 martin device_printf(sc->sc_dev, "start write failed, error=%d\n",
656 1.13.2.2 martin rv);
657 1.13.2.2 martin goto out;
658 1.13.2.2 martin }
659 1.13.2.2 martin endwrite = true;
660 1.13.2.2 martin if ((rv = (*sc->sc_intf->write)(sc, &command, sizeof(command))) != 0) {
661 1.13.2.2 martin device_printf(sc->sc_dev, "write failed, error=%d\n", rv);
662 1.13.2.2 martin goto out;
663 1.13.2.2 martin }
664 1.13.2.2 martin rv = (*sc->sc_intf->end)(sc, UIO_WRITE, 0);
665 1.13.2.2 martin endwrite = false;
666 1.13.2.2 martin if (rv) {
667 1.13.2.2 martin device_printf(sc->sc_dev, "end write failed, error=%d\n", rv);
668 1.13.2.2 martin goto out;
669 1.13.2.2 martin }
670 1.13.2.2 martin
671 1.13.2.2 martin /* Read the response header. */
672 1.13.2.2 martin if ((rv = (*sc->sc_intf->start)(sc, UIO_READ)) != 0) {
673 1.13.2.2 martin device_printf(sc->sc_dev, "start write failed, error=%d\n",
674 1.13.2.2 martin rv);
675 1.13.2.2 martin goto out;
676 1.13.2.2 martin }
677 1.13.2.2 martin endread = true;
678 1.13.2.2 martin if ((rv = (*sc->sc_intf->read)(sc, &response.hdr, sizeof(response.hdr),
679 1.13.2.2 martin &nread, 0)) != 0) {
680 1.13.2.2 martin device_printf(sc->sc_dev, "read failed, error=%d\n", rv);
681 1.13.2.2 martin goto out;
682 1.13.2.2 martin }
683 1.13.2.2 martin
684 1.13.2.2 martin /* Verify the response header looks sensible. */
685 1.13.2.2 martin if (nread != sizeof(response.hdr)) {
686 1.13.2.2 martin device_printf(sc->sc_dev, "read %zu bytes, expected %zu",
687 1.13.2.2 martin nread, sizeof(response.hdr));
688 1.13.2.2 martin goto out;
689 1.13.2.2 martin }
690 1.13.2.2 martin tag = be16toh(response.hdr.tag);
691 1.13.2.2 martin pktlen = be32toh(response.hdr.length);
692 1.13.2.2 martin code = be32toh(response.hdr.code);
693 1.13.2.2 martin if (tag != TPM2_ST_NO_SESSIONS ||
694 1.13.2.2 martin pktlen < offsetof(struct response, bytes) ||
695 1.13.2.2 martin pktlen > sizeof(response) ||
696 1.13.2.2 martin code != 0) {
697 1.13.2.2 martin /*
698 1.13.2.2 martin * If the tpm itself is busy (e.g., it has yet to run a
699 1.13.2.2 martin * self-test, or it's in a timeout period to defend
700 1.13.2.2 martin * against brute force attacks), then we can try again
701 1.13.2.2 martin * later. Otherwise, give up.
702 1.13.2.2 martin */
703 1.13.2.2 martin if (code & TPM2_RC_WARN) {
704 1.13.2.2 martin aprint_debug_dev(sc->sc_dev, "%s: tpm busy,"
705 1.13.2.2 martin " code=TPM_RC_WARN+0x%x\n",
706 1.13.2.2 martin __func__, code & ~TPM2_RC_WARN);
707 1.13.2.2 martin rv = 0;
708 1.13.2.2 martin } else {
709 1.13.2.2 martin device_printf(sc->sc_dev, "bad tpm response:"
710 1.13.2.2 martin " tag=%u len=%u code=0x%x\n", tag, pktlen, code);
711 1.13.2.2 martin hexdump(aprint_debug, "tpm response header",
712 1.13.2.2 martin (const void *)&response.hdr,
713 1.13.2.2 martin sizeof(response.hdr));
714 1.13.2.2 martin rv = EIO;
715 1.13.2.2 martin }
716 1.13.2.2 martin goto out;
717 1.13.2.2 martin }
718 1.13.2.2 martin
719 1.13.2.2 martin /* Read the response payload. */
720 1.13.2.2 martin if ((rv = (*sc->sc_intf->read)(sc,
721 1.13.2.2 martin (char *)&response + nread, pktlen - nread,
722 1.13.2.2 martin NULL, TPM_PARAM_SIZE)) != 0) {
723 1.13.2.2 martin device_printf(sc->sc_dev, "read failed, error=%d\n", rv);
724 1.13.2.2 martin goto out;
725 1.13.2.2 martin }
726 1.13.2.2 martin endread = false;
727 1.13.2.2 martin if ((rv = (*sc->sc_intf->end)(sc, UIO_READ, 0)) != 0) {
728 1.13.2.2 martin device_printf(sc->sc_dev, "end read failed, error=%d\n", rv);
729 1.13.2.2 martin goto out;
730 1.13.2.2 martin }
731 1.13.2.2 martin
732 1.13.2.2 martin /* Verify the number of bytes read looks sensible. */
733 1.13.2.2 martin nbytes = be16toh(response.randomBytesSize);
734 1.13.2.2 martin if (nbytes > pktlen - offsetof(struct response, bytes)) {
735 1.13.2.2 martin device_printf(sc->sc_dev, "overlong GetRandom length:"
736 1.13.2.2 martin " %u, max %zu\n",
737 1.13.2.2 martin nbytes, pktlen - offsetof(struct response, bytes));
738 1.13.2.2 martin nbytes = pktlen - offsetof(struct response, bytes);
739 1.13.2.2 martin }
740 1.13.2.2 martin
741 1.13.2.2 martin /*
742 1.13.2.2 martin * Enter the data into the entropy pool. Conservatively (or,
743 1.13.2.2 martin * perhaps, cargocultily) estimate half a bit of entropy per
744 1.13.2.2 martin * bit of data.
745 1.13.2.2 martin */
746 1.13.2.2 martin CTASSERT(sizeof(response.bytes) <= UINT_MAX/(NBBY/2));
747 1.13.2.2 martin entropybits = (NBBY/2)*nbytes;
748 1.13.2.2 martin rnd_add_data(&sc->sc_rnd, response.bytes, nbytes, entropybits);
749 1.13.2.2 martin
750 1.13.2.2 martin out: /* End the read or write if still ongoing. */
751 1.13.2.2 martin if (endread)
752 1.13.2.2 martin rv = (*sc->sc_intf->end)(sc, UIO_READ, rv);
753 1.13.2.2 martin if (endwrite)
754 1.13.2.2 martin rv = (*sc->sc_intf->end)(sc, UIO_WRITE, rv);
755 1.13.2.2 martin
756 1.13.2.2 martin *entropybitsp = entropybits;
757 1.13.2.2 martin return rv;
758 1.13.2.2 martin }
759 1.13.2.2 martin
760 1.13.2.2 martin static void
761 1.13.2.2 martin tpm_rng_work(struct work *wk, void *cookie)
762 1.13.2.2 martin {
763 1.13.2.2 martin struct tpm_softc *sc = cookie;
764 1.13.2.2 martin unsigned nbytes, entropybits;
765 1.13.2.2 martin int rv;
766 1.13.2.2 martin
767 1.13.2.2 martin /* Acknowledge the request. */
768 1.13.2.2 martin nbytes = atomic_swap_uint(&sc->sc_rndpending, 0);
769 1.13.2.2 martin
770 1.13.2.2 martin /* Lock the tpm while we do I/O transactions with it. */
771 1.13.2.2 martin mutex_enter(&sc->sc_lock);
772 1.13.2.2 martin
773 1.13.2.2 martin /*
774 1.13.2.2 martin * Issue as many commands as needed to fulfill the request, but
775 1.13.2.2 martin * stop if anything fails.
776 1.13.2.2 martin */
777 1.13.2.2 martin for (; nbytes; nbytes -= MIN(nbytes, MAX(1, entropybits/NBBY))) {
778 1.13.2.2 martin switch (sc->sc_ver) {
779 1.13.2.2 martin case TPM_1_2:
780 1.13.2.2 martin rv = tpm12_rng(sc, &entropybits);
781 1.13.2.2 martin break;
782 1.13.2.2 martin case TPM_2_0:
783 1.13.2.2 martin rv = tpm20_rng(sc, &entropybits);
784 1.13.2.2 martin break;
785 1.13.2.2 martin default:
786 1.13.2.2 martin panic("bad tpm version: %d", sc->sc_ver);
787 1.13.2.2 martin }
788 1.13.2.2 martin if (rv)
789 1.13.2.2 martin break;
790 1.13.2.2 martin }
791 1.13.2.2 martin
792 1.13.2.2 martin /*
793 1.13.2.2 martin * If the tpm is busted, no sense in trying again -- most
794 1.13.2.2 martin * likely, it is deactivated, and by the spec it cannot be
795 1.13.2.2 martin * reactivated until after a reboot.
796 1.13.2.2 martin */
797 1.13.2.2 martin if (rv) {
798 1.13.2.2 martin device_printf(sc->sc_dev, "deactivating entropy source\n");
799 1.13.2.2 martin atomic_store_relaxed(&sc->sc_rnddisabled, true);
800 1.13.2.2 martin /* XXX worker thread can't workqueue_destroy its own queue */
801 1.13.2.2 martin }
802 1.13.2.2 martin
803 1.13.2.2 martin /* Relinquish the tpm. */
804 1.13.2.2 martin mutex_exit(&sc->sc_lock);
805 1.13.2.2 martin }
806 1.13.2.2 martin
807 1.13.2.2 martin static void
808 1.13.2.2 martin tpm_rng_get(size_t nbytes, void *cookie)
809 1.13.2.2 martin {
810 1.13.2.2 martin struct tpm_softc *sc = cookie;
811 1.13.2.2 martin
812 1.13.2.2 martin if (atomic_load_relaxed(&sc->sc_rnddisabled))
813 1.13.2.2 martin return; /* tough */
814 1.13.2.2 martin if (atomic_swap_uint(&sc->sc_rndpending, MIN(nbytes, UINT_MAX/NBBY))
815 1.13.2.2 martin == 0)
816 1.13.2.2 martin workqueue_enqueue(sc->sc_rndwq, &sc->sc_rndwk, NULL);
817 1.13.2.2 martin }
818 1.13.2.2 martin
819 1.13.2.2 martin static int
820 1.13.2.1 martin tpm_tis12_init(struct tpm_softc *sc)
821 1.13 maxv {
822 1.13.2.1 martin int rv;
823 1.13 maxv
824 1.13.2.2 martin aprint_naive("\n");
825 1.13.2.2 martin aprint_normal("\n");
826 1.13.2.2 martin
827 1.13.2.1 martin sc->sc_caps = bus_space_read_4(sc->sc_bt, sc->sc_bh,
828 1.13 maxv TPM_INTF_CAPABILITY);
829 1.13 maxv sc->sc_devid = bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_ID);
830 1.13 maxv sc->sc_rev = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_REV);
831 1.13 maxv
832 1.13.2.1 martin aprint_normal_dev(sc->sc_dev, "device 0x%08x rev 0x%x\n",
833 1.13.2.1 martin sc->sc_devid, sc->sc_rev);
834 1.13 maxv
835 1.13.2.1 martin if ((rv = tpm12_request_locality(sc, 0)) != 0)
836 1.13.2.1 martin return rv;
837 1.13 maxv
838 1.13 maxv /* Abort whatever it thought it was doing. */
839 1.13 maxv bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
840 1.13 maxv
841 1.13.2.2 martin /* XXX Run this at higher priority? */
842 1.13.2.2 martin if ((rv = workqueue_create(&sc->sc_rndwq, device_xname(sc->sc_dev),
843 1.13.2.2 martin tpm_rng_work, sc, PRI_NONE, IPL_VM, WQ_MPSAFE)) != 0)
844 1.13.2.2 martin return rv;
845 1.13.2.2 martin rndsource_setcb(&sc->sc_rnd, tpm_rng_get, sc);
846 1.13.2.2 martin rnd_attach_source(&sc->sc_rnd, device_xname(sc->sc_dev),
847 1.13.2.2 martin RND_TYPE_RNG,
848 1.13.2.2 martin RND_FLAG_COLLECT_VALUE|RND_FLAG_ESTIMATE_VALUE|RND_FLAG_HASCB);
849 1.13.2.2 martin
850 1.13 maxv return 0;
851 1.13 maxv }
852 1.13 maxv
853 1.13.2.1 martin static int
854 1.13.2.1 martin tpm_tis12_start(struct tpm_softc *sc, int rw)
855 1.1 christos {
856 1.1 christos int rv;
857 1.1 christos
858 1.13.2.1 martin if (rw == UIO_READ) {
859 1.1 christos rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
860 1.13.2.1 martin TPM_READ_TMO, sc->sc_intf->read);
861 1.1 christos return rv;
862 1.1 christos }
863 1.1 christos
864 1.13 maxv /* Request the 0th locality. */
865 1.13.2.1 martin if ((rv = tpm12_request_locality(sc, 0)) != 0)
866 1.1 christos return rv;
867 1.1 christos
868 1.13 maxv sc->sc_status = tpm_status(sc);
869 1.13 maxv if (sc->sc_status & TPM_STS_CMD_READY)
870 1.1 christos return 0;
871 1.1 christos
872 1.1 christos /* Abort previous and restart. */
873 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
874 1.13.2.1 martin rv = tpm_waitfor(sc, TPM_STS_CMD_READY, TPM_READY_TMO, sc->sc_intf->write);
875 1.13 maxv if (rv)
876 1.1 christos return rv;
877 1.1 christos
878 1.1 christos return 0;
879 1.1 christos }
880 1.1 christos
881 1.13.2.1 martin static int
882 1.3 christos tpm_tis12_read(struct tpm_softc *sc, void *buf, size_t len, size_t *count,
883 1.1 christos int flags)
884 1.1 christos {
885 1.1 christos uint8_t *p = buf;
886 1.1 christos size_t cnt;
887 1.13.2.1 martin int rv, n;
888 1.1 christos
889 1.1 christos cnt = 0;
890 1.1 christos while (len > 0) {
891 1.13 maxv rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
892 1.13.2.1 martin TPM_READ_TMO, sc->sc_intf->read);
893 1.13 maxv if (rv)
894 1.1 christos return rv;
895 1.1 christos
896 1.13.2.1 martin n = MIN(len, tpm_getburst(sc));
897 1.13.2.1 martin while (n > 0) {
898 1.1 christos *p++ = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_DATA);
899 1.1 christos cnt++;
900 1.13.2.1 martin len--;
901 1.13.2.1 martin n--;
902 1.1 christos }
903 1.1 christos
904 1.1 christos if ((flags & TPM_PARAM_SIZE) == 0 && cnt >= 6)
905 1.1 christos break;
906 1.1 christos }
907 1.1 christos
908 1.1 christos if (count)
909 1.1 christos *count = cnt;
910 1.1 christos
911 1.1 christos return 0;
912 1.1 christos }
913 1.1 christos
914 1.13.2.1 martin static int
915 1.3 christos tpm_tis12_write(struct tpm_softc *sc, const void *buf, size_t len)
916 1.1 christos {
917 1.3 christos const uint8_t *p = buf;
918 1.1 christos size_t cnt;
919 1.1 christos int rv, r;
920 1.1 christos
921 1.3 christos if (len == 0)
922 1.3 christos return 0;
923 1.13.2.1 martin if ((rv = tpm12_request_locality(sc, 0)) != 0)
924 1.1 christos return rv;
925 1.1 christos
926 1.1 christos cnt = 0;
927 1.1 christos while (cnt < len - 1) {
928 1.1 christos for (r = tpm_getburst(sc); r > 0 && cnt < len - 1; r--) {
929 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_DATA, *p++);
930 1.1 christos cnt++;
931 1.1 christos }
932 1.1 christos if ((rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc))) {
933 1.1 christos return rv;
934 1.1 christos }
935 1.13 maxv sc->sc_status = tpm_status(sc);
936 1.13 maxv if (!(sc->sc_status & TPM_STS_DATA_EXPECT)) {
937 1.1 christos return EIO;
938 1.1 christos }
939 1.1 christos }
940 1.1 christos
941 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_DATA, *p++);
942 1.1 christos cnt++;
943 1.1 christos
944 1.1 christos if ((rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc))) {
945 1.1 christos return rv;
946 1.1 christos }
947 1.13 maxv if ((sc->sc_status & TPM_STS_DATA_EXPECT) != 0) {
948 1.1 christos return EIO;
949 1.1 christos }
950 1.1 christos
951 1.1 christos return 0;
952 1.1 christos }
953 1.1 christos
954 1.13.2.1 martin static int
955 1.13.2.1 martin tpm_tis12_end(struct tpm_softc *sc, int rw, int err)
956 1.1 christos {
957 1.1 christos int rv = 0;
958 1.1 christos
959 1.13.2.1 martin if (rw == UIO_READ) {
960 1.13.2.1 martin rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc->sc_intf->read);
961 1.13 maxv if (rv)
962 1.13.2.2 martin goto out;
963 1.1 christos
964 1.1 christos /* Still more data? */
965 1.13 maxv sc->sc_status = tpm_status(sc);
966 1.13.2.1 martin if (!err && (sc->sc_status & TPM_STS_DATA_AVAIL)) {
967 1.1 christos rv = EIO;
968 1.1 christos }
969 1.1 christos
970 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
971 1.1 christos TPM_STS_CMD_READY);
972 1.1 christos
973 1.13 maxv /* Release the 0th locality. */
974 1.13 maxv bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
975 1.1 christos TPM_ACCESS_ACTIVE_LOCALITY);
976 1.1 christos } else {
977 1.1 christos /* Hungry for more? */
978 1.13 maxv sc->sc_status = tpm_status(sc);
979 1.13 maxv if (!err && (sc->sc_status & TPM_STS_DATA_EXPECT)) {
980 1.1 christos rv = EIO;
981 1.1 christos }
982 1.1 christos
983 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
984 1.1 christos err ? TPM_STS_CMD_READY : TPM_STS_GO);
985 1.1 christos }
986 1.1 christos
987 1.13.2.2 martin out: return err ? err : rv;
988 1.1 christos }
989 1.1 christos
990 1.13.2.1 martin const struct tpm_intf tpm_intf_tis12 = {
991 1.13.2.1 martin .version = TIS_1_2,
992 1.13.2.1 martin .probe = tpm_tis12_probe,
993 1.13.2.1 martin .init = tpm_tis12_init,
994 1.13.2.1 martin .start = tpm_tis12_start,
995 1.13.2.1 martin .read = tpm_tis12_read,
996 1.13.2.1 martin .write = tpm_tis12_write,
997 1.13.2.1 martin .end = tpm_tis12_end
998 1.13.2.1 martin };
999 1.13.2.1 martin
1000 1.13 maxv /* -------------------------------------------------------------------------- */
1001 1.1 christos
1002 1.13 maxv static dev_type_open(tpmopen);
1003 1.13 maxv static dev_type_close(tpmclose);
1004 1.13 maxv static dev_type_read(tpmread);
1005 1.13 maxv static dev_type_write(tpmwrite);
1006 1.13 maxv static dev_type_ioctl(tpmioctl);
1007 1.1 christos
1008 1.13 maxv const struct cdevsw tpm_cdevsw = {
1009 1.13 maxv .d_open = tpmopen,
1010 1.13 maxv .d_close = tpmclose,
1011 1.13 maxv .d_read = tpmread,
1012 1.13 maxv .d_write = tpmwrite,
1013 1.13 maxv .d_ioctl = tpmioctl,
1014 1.13 maxv .d_stop = nostop,
1015 1.13 maxv .d_tty = notty,
1016 1.13 maxv .d_poll = nopoll,
1017 1.13 maxv .d_mmap = nommap,
1018 1.13 maxv .d_kqfilter = nokqfilter,
1019 1.13 maxv .d_discard = nodiscard,
1020 1.13.2.1 martin .d_flag = D_OTHER | D_MPSAFE,
1021 1.13 maxv };
1022 1.1 christos
1023 1.13 maxv static int
1024 1.1 christos tpmopen(dev_t dev, int flag, int mode, struct lwp *l)
1025 1.1 christos {
1026 1.13.2.1 martin struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
1027 1.13.2.1 martin int ret = 0;
1028 1.1 christos
1029 1.13 maxv if (sc == NULL)
1030 1.1 christos return ENXIO;
1031 1.1 christos
1032 1.13.2.1 martin mutex_enter(&sc->sc_lock);
1033 1.13.2.1 martin if (sc->sc_busy) {
1034 1.13.2.1 martin ret = EBUSY;
1035 1.13.2.1 martin } else {
1036 1.13.2.1 martin sc->sc_busy = true;
1037 1.13.2.1 martin }
1038 1.13.2.1 martin mutex_exit(&sc->sc_lock);
1039 1.1 christos
1040 1.13.2.1 martin return ret;
1041 1.1 christos }
1042 1.1 christos
1043 1.13 maxv static int
1044 1.1 christos tpmclose(dev_t dev, int flag, int mode, struct lwp *l)
1045 1.1 christos {
1046 1.13.2.1 martin struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
1047 1.13.2.1 martin int ret = 0;
1048 1.1 christos
1049 1.13 maxv if (sc == NULL)
1050 1.1 christos return ENXIO;
1051 1.1 christos
1052 1.13.2.1 martin mutex_enter(&sc->sc_lock);
1053 1.13.2.1 martin if (!sc->sc_busy) {
1054 1.13.2.1 martin ret = EINVAL;
1055 1.13.2.1 martin } else {
1056 1.13.2.1 martin sc->sc_busy = false;
1057 1.13.2.1 martin }
1058 1.13.2.1 martin mutex_exit(&sc->sc_lock);
1059 1.1 christos
1060 1.13.2.1 martin return ret;
1061 1.1 christos }
1062 1.1 christos
1063 1.13 maxv static int
1064 1.1 christos tpmread(dev_t dev, struct uio *uio, int flags)
1065 1.1 christos {
1066 1.13.2.1 martin struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
1067 1.13.2.1 martin struct tpm_header hdr;
1068 1.13.2.1 martin uint8_t buf[TPM_BUFSIZ];
1069 1.13.2.2 martin size_t cnt, len = 0/*XXXGCC*/;
1070 1.13.2.2 martin bool end = false;
1071 1.13.2.1 martin int rv;
1072 1.1 christos
1073 1.13 maxv if (sc == NULL)
1074 1.1 christos return ENXIO;
1075 1.1 christos
1076 1.13.2.2 martin mutex_enter(&sc->sc_lock);
1077 1.13.2.2 martin
1078 1.13.2.1 martin if ((rv = (*sc->sc_intf->start)(sc, UIO_READ)))
1079 1.13.2.2 martin goto out;
1080 1.13.2.2 martin end = true;
1081 1.1 christos
1082 1.13.2.1 martin /* Get the header. */
1083 1.13.2.1 martin if ((rv = (*sc->sc_intf->read)(sc, &hdr, sizeof(hdr), &cnt, 0))) {
1084 1.3 christos goto out;
1085 1.1 christos }
1086 1.13.2.2 martin if (cnt != sizeof(hdr)) {
1087 1.13.2.2 martin rv = EIO;
1088 1.13.2.2 martin goto out;
1089 1.13.2.2 martin }
1090 1.13.2.2 martin len = be32toh(hdr.length);
1091 1.13.2.2 martin if (len > MIN(sizeof(buf), uio->uio_resid) || len < sizeof(hdr)) {
1092 1.1 christos rv = EIO;
1093 1.3 christos goto out;
1094 1.1 christos }
1095 1.1 christos
1096 1.13.2.2 martin /* Get the payload. */
1097 1.13.2.2 martin len -= sizeof(hdr);
1098 1.13.2.2 martin if ((rv = (*sc->sc_intf->read)(sc, buf, len, NULL, TPM_PARAM_SIZE))) {
1099 1.3 christos goto out;
1100 1.1 christos }
1101 1.1 christos
1102 1.13.2.2 martin out: if (end)
1103 1.13.2.2 martin rv = (*sc->sc_intf->end)(sc, UIO_READ, rv);
1104 1.13.2.2 martin
1105 1.13.2.2 martin mutex_exit(&sc->sc_lock);
1106 1.13.2.2 martin
1107 1.13.2.2 martin /* If anything went wrong, stop here -- nothing to copy out. */
1108 1.13.2.2 martin if (rv)
1109 1.13.2.2 martin return rv;
1110 1.13.2.2 martin
1111 1.13.2.2 martin /* Copy out the header. */
1112 1.13.2.2 martin if ((rv = uiomove(&hdr, sizeof(hdr), uio))) {
1113 1.13.2.2 martin return rv;
1114 1.1 christos }
1115 1.1 christos
1116 1.13.2.2 martin /* Copy out the payload. */
1117 1.13.2.2 martin if ((rv = uiomove(buf, len, uio))) {
1118 1.13.2.2 martin return rv;
1119 1.13.2.2 martin }
1120 1.13.2.2 martin
1121 1.13.2.2 martin /* Success! */
1122 1.13.2.2 martin return 0;
1123 1.1 christos }
1124 1.1 christos
1125 1.13 maxv static int
1126 1.1 christos tpmwrite(dev_t dev, struct uio *uio, int flags)
1127 1.1 christos {
1128 1.13.2.1 martin struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
1129 1.1 christos uint8_t buf[TPM_BUFSIZ];
1130 1.13.2.2 martin bool end = false;
1131 1.13.2.1 martin int n, rv;
1132 1.1 christos
1133 1.13 maxv if (sc == NULL)
1134 1.1 christos return ENXIO;
1135 1.1 christos
1136 1.1 christos n = MIN(sizeof(buf), uio->uio_resid);
1137 1.1 christos if ((rv = uiomove(buf, n, uio))) {
1138 1.13.2.2 martin return rv;
1139 1.1 christos }
1140 1.13.2.2 martin
1141 1.13.2.2 martin mutex_enter(&sc->sc_lock);
1142 1.13.2.2 martin
1143 1.13.2.1 martin if ((rv = (*sc->sc_intf->start)(sc, UIO_WRITE))) {
1144 1.13 maxv goto out;
1145 1.1 christos }
1146 1.13.2.2 martin end = true;
1147 1.13.2.2 martin
1148 1.13.2.1 martin if ((rv = (*sc->sc_intf->write)(sc, buf, n))) {
1149 1.13 maxv goto out;
1150 1.1 christos }
1151 1.1 christos
1152 1.13.2.2 martin out: if (end)
1153 1.13.2.2 martin rv = (*sc->sc_intf->end)(sc, UIO_WRITE, rv);
1154 1.13.2.2 martin
1155 1.13.2.2 martin mutex_exit(&sc->sc_lock);
1156 1.1 christos return rv;
1157 1.1 christos }
1158 1.1 christos
1159 1.13 maxv static int
1160 1.13 maxv tpmioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1161 1.1 christos {
1162 1.13.2.1 martin struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
1163 1.13 maxv struct tpm_ioc_getinfo *info;
1164 1.13 maxv
1165 1.13 maxv if (sc == NULL)
1166 1.13 maxv return ENXIO;
1167 1.13 maxv
1168 1.13 maxv switch (cmd) {
1169 1.13 maxv case TPM_IOC_GETINFO:
1170 1.13 maxv info = addr;
1171 1.13 maxv info->api_version = TPM_API_VERSION;
1172 1.13 maxv info->tpm_version = sc->sc_ver;
1173 1.13.2.1 martin info->itf_version = sc->sc_intf->version;
1174 1.13 maxv info->device_id = sc->sc_devid;
1175 1.13 maxv info->device_rev = sc->sc_rev;
1176 1.13.2.1 martin info->device_caps = sc->sc_caps;
1177 1.13 maxv return 0;
1178 1.13 maxv default:
1179 1.13 maxv break;
1180 1.13 maxv }
1181 1.13 maxv
1182 1.1 christos return ENOTTY;
1183 1.1 christos }
1184