tpm.c revision 1.15 1 1.15 maxv /* $NetBSD: tpm.c,v 1.15 2019/10/09 07:30:58 maxv 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.15 maxv __KERNEL_RCSID(0, "$NetBSD: tpm.c,v 1.15 2019/10/09 07:30:58 maxv Exp $");
52 1.1 christos
53 1.1 christos #include <sys/param.h>
54 1.1 christos #include <sys/systm.h>
55 1.1 christos #include <sys/kernel.h>
56 1.1 christos #include <sys/malloc.h>
57 1.1 christos #include <sys/proc.h>
58 1.1 christos #include <sys/device.h>
59 1.1 christos #include <sys/conf.h>
60 1.1 christos #include <sys/bus.h>
61 1.1 christos #include <sys/pmf.h>
62 1.1 christos
63 1.1 christos #include <dev/ic/tpmreg.h>
64 1.1 christos #include <dev/ic/tpmvar.h>
65 1.1 christos
66 1.12 riastrad #include "ioconf.h"
67 1.12 riastrad
68 1.15 maxv CTASSERT(sizeof(struct tpm_header) == 10);
69 1.15 maxv
70 1.13 maxv #define TPM_BUFSIZ 1024
71 1.14 maxv
72 1.13 maxv #define TPM_PARAM_SIZE 0x0001 /* that's a flag */
73 1.13 maxv
74 1.13 maxv /* Timeouts. */
75 1.13 maxv #define TPM_ACCESS_TMO 2000 /* 2sec */
76 1.13 maxv #define TPM_READY_TMO 2000 /* 2sec */
77 1.13 maxv #define TPM_READ_TMO 2000 /* 2sec */
78 1.13 maxv #define TPM_BURST_TMO 2000 /* 2sec */
79 1.13 maxv
80 1.13 maxv #define TPM_CAPS_REQUIRED \
81 1.13 maxv (TPM_INTF_DATA_AVAIL_INT|TPM_INTF_LOCALITY_CHANGE_INT| \
82 1.13 maxv TPM_INTF_INT_LEVEL_LOW)
83 1.1 christos
84 1.13 maxv static inline int
85 1.13 maxv tpm_tmotohz(int tmo)
86 1.1 christos {
87 1.13 maxv struct timeval tv;
88 1.1 christos
89 1.13 maxv tv.tv_sec = tmo / 1000;
90 1.13 maxv tv.tv_usec = 1000 * (tmo % 1000);
91 1.1 christos
92 1.13 maxv return tvtohz(&tv);
93 1.1 christos }
94 1.1 christos
95 1.13 maxv static int
96 1.1 christos tpm_request_locality(struct tpm_softc *sc, int l)
97 1.1 christos {
98 1.1 christos uint32_t r;
99 1.1 christos int to, rv;
100 1.1 christos
101 1.1 christos if (l != 0)
102 1.1 christos return EINVAL;
103 1.1 christos
104 1.1 christos if ((bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
105 1.1 christos (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) ==
106 1.1 christos (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY))
107 1.1 christos return 0;
108 1.1 christos
109 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
110 1.1 christos TPM_ACCESS_REQUEST_USE);
111 1.1 christos
112 1.1 christos to = tpm_tmotohz(TPM_ACCESS_TMO);
113 1.1 christos
114 1.1 christos while ((r = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
115 1.1 christos (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
116 1.1 christos (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && to--) {
117 1.14 maxv rv = tsleep(sc->sc_init, PCATCH, "tpm_locality", 1);
118 1.13 maxv if (rv && rv != EWOULDBLOCK) {
119 1.1 christos return rv;
120 1.1 christos }
121 1.1 christos }
122 1.1 christos
123 1.1 christos if ((r & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
124 1.1 christos (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
125 1.1 christos return EBUSY;
126 1.1 christos }
127 1.1 christos
128 1.1 christos return 0;
129 1.1 christos }
130 1.1 christos
131 1.13 maxv static int
132 1.1 christos tpm_getburst(struct tpm_softc *sc)
133 1.1 christos {
134 1.1 christos int burst, to, rv;
135 1.1 christos
136 1.1 christos to = tpm_tmotohz(TPM_BURST_TMO);
137 1.1 christos
138 1.13 maxv while (to--) {
139 1.1 christos /*
140 1.13 maxv * Burst count is in bits 23:8, so read the two higher bytes.
141 1.1 christos */
142 1.1 christos burst = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS + 1);
143 1.1 christos burst |= bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS + 2)
144 1.1 christos << 8;
145 1.13 maxv
146 1.1 christos if (burst)
147 1.1 christos return burst;
148 1.1 christos
149 1.14 maxv rv = tsleep(sc, PCATCH, "tpm_getburst", 1);
150 1.1 christos if (rv && rv != EWOULDBLOCK) {
151 1.1 christos return 0;
152 1.1 christos }
153 1.1 christos }
154 1.1 christos
155 1.1 christos return 0;
156 1.1 christos }
157 1.1 christos
158 1.13 maxv static inline uint8_t
159 1.1 christos tpm_status(struct tpm_softc *sc)
160 1.1 christos {
161 1.13 maxv return bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS) &
162 1.13 maxv TPM_STS_STATUS_BITS;
163 1.1 christos }
164 1.1 christos
165 1.13 maxv /* -------------------------------------------------------------------------- */
166 1.1 christos
167 1.15 maxv static bool
168 1.15 maxv tpm12_suspend(struct tpm_softc *sc)
169 1.15 maxv {
170 1.15 maxv static const uint8_t command[10] = {
171 1.15 maxv 0x00, 0xC1, /* TPM_TAG_RQU_COMMAND */
172 1.15 maxv 0x00, 0x00, 0x00, 10, /* Length in bytes */
173 1.15 maxv 0x00, 0x00, 0x00, 0x98 /* TPM_ORD_SaveState */
174 1.15 maxv };
175 1.15 maxv struct tpm_header response;
176 1.15 maxv
177 1.15 maxv if ((*sc->sc_write)(sc, &command, sizeof(command)) != 0)
178 1.15 maxv return false;
179 1.15 maxv if ((*sc->sc_read)(sc, &response, sizeof(response), NULL, 0) != 0)
180 1.15 maxv return false;
181 1.15 maxv if (TPM_BE32(response.code) != 0)
182 1.15 maxv return false;
183 1.15 maxv
184 1.15 maxv return true;
185 1.15 maxv }
186 1.1 christos
187 1.15 maxv static bool
188 1.15 maxv tpm20_suspend(struct tpm_softc *sc)
189 1.1 christos {
190 1.15 maxv static const uint8_t command[12] = {
191 1.15 maxv 0x80, 0x01, /* TPM_ST_NO_SESSIONS */
192 1.15 maxv 0x00, 0x00, 0x00, 12, /* Length in bytes */
193 1.15 maxv 0x00, 0x00, 0x01, 0x45, /* TPM_CC_Shutdown */
194 1.15 maxv 0x00, 0x01 /* TPM_SU_STATE */
195 1.1 christos };
196 1.15 maxv struct tpm_header response;
197 1.1 christos
198 1.15 maxv if ((*sc->sc_write)(sc, &command, sizeof(command)) != 0)
199 1.15 maxv return false;
200 1.15 maxv if ((*sc->sc_read)(sc, &response, sizeof(response), NULL, 0) != 0)
201 1.15 maxv return false;
202 1.15 maxv if (TPM_BE32(response.code) != 0)
203 1.15 maxv return false;
204 1.13 maxv
205 1.7 christos return true;
206 1.1 christos }
207 1.1 christos
208 1.1 christos bool
209 1.15 maxv tpm_suspend(device_t dev, const pmf_qual_t *qual)
210 1.1 christos {
211 1.15 maxv struct tpm_softc *sc = device_private(dev);
212 1.15 maxv
213 1.15 maxv switch (sc->sc_ver) {
214 1.15 maxv case TPM_1_2:
215 1.15 maxv return tpm12_suspend(sc);
216 1.15 maxv case TPM_2_0:
217 1.15 maxv return tpm20_suspend(sc);
218 1.15 maxv default:
219 1.15 maxv panic("%s: impossible", __func__);
220 1.15 maxv }
221 1.15 maxv }
222 1.15 maxv
223 1.15 maxv bool
224 1.15 maxv tpm_resume(device_t dev, const pmf_qual_t *qual)
225 1.15 maxv {
226 1.15 maxv /*
227 1.15 maxv * Don't do anything, the BIOS is supposed to restore the previously
228 1.15 maxv * saved state.
229 1.15 maxv */
230 1.7 christos return true;
231 1.1 christos }
232 1.1 christos
233 1.13 maxv /* -------------------------------------------------------------------------- */
234 1.13 maxv
235 1.13 maxv static int
236 1.14 maxv tpm_poll(struct tpm_softc *sc, uint8_t mask, int to, wchan_t chan)
237 1.1 christos {
238 1.1 christos int rv;
239 1.1 christos
240 1.13 maxv while (((sc->sc_status = tpm_status(sc)) & mask) != mask && to--) {
241 1.14 maxv rv = tsleep(chan, PCATCH, "tpm_poll", 1);
242 1.1 christos if (rv && rv != EWOULDBLOCK) {
243 1.1 christos return rv;
244 1.1 christos }
245 1.1 christos }
246 1.1 christos
247 1.1 christos return 0;
248 1.1 christos }
249 1.1 christos
250 1.13 maxv static int
251 1.13 maxv tpm_waitfor(struct tpm_softc *sc, uint8_t bits, int tmo, wchan_t chan)
252 1.1 christos {
253 1.13 maxv int retry, to, rv;
254 1.13 maxv uint8_t todo;
255 1.1 christos
256 1.14 maxv to = tpm_tmotohz(tmo);
257 1.13 maxv retry = 3;
258 1.13 maxv
259 1.1 christos restart:
260 1.14 maxv todo = bits;
261 1.14 maxv
262 1.1 christos /*
263 1.14 maxv * TPM_STS_VALID has priority over the others.
264 1.1 christos */
265 1.14 maxv if (todo & TPM_STS_VALID) {
266 1.14 maxv if ((rv = tpm_poll(sc, TPM_STS_VALID, to+1, chan)) != 0)
267 1.14 maxv return rv;
268 1.14 maxv todo &= ~TPM_STS_VALID;
269 1.14 maxv }
270 1.14 maxv
271 1.14 maxv if ((rv = tpm_poll(sc, todo, to, chan)) != 0)
272 1.1 christos return rv;
273 1.1 christos
274 1.13 maxv if ((todo & sc->sc_status) != todo) {
275 1.14 maxv if ((retry-- > 0) && (bits & TPM_STS_VALID)) {
276 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
277 1.1 christos TPM_STS_RESP_RETRY);
278 1.1 christos goto restart;
279 1.1 christos }
280 1.1 christos return EIO;
281 1.1 christos }
282 1.1 christos
283 1.1 christos return 0;
284 1.1 christos }
285 1.1 christos
286 1.13 maxv /* -------------------------------------------------------------------------- */
287 1.13 maxv
288 1.13 maxv /*
289 1.13 maxv * TPM using TIS 1.2 interface.
290 1.13 maxv */
291 1.13 maxv
292 1.13 maxv int
293 1.13 maxv tpm_tis12_probe(bus_space_tag_t bt, bus_space_handle_t bh)
294 1.13 maxv {
295 1.13 maxv uint32_t cap;
296 1.13 maxv uint8_t reg;
297 1.13 maxv int tmo;
298 1.13 maxv
299 1.13 maxv cap = bus_space_read_4(bt, bh, TPM_INTF_CAPABILITY);
300 1.13 maxv if (cap == 0xffffffff)
301 1.13 maxv return 0;
302 1.13 maxv if ((cap & TPM_CAPS_REQUIRED) != TPM_CAPS_REQUIRED)
303 1.13 maxv return 0;
304 1.13 maxv
305 1.13 maxv /* Request locality 0. */
306 1.13 maxv bus_space_write_1(bt, bh, TPM_ACCESS, TPM_ACCESS_REQUEST_USE);
307 1.13 maxv
308 1.13 maxv /* Wait for it to become active. */
309 1.13 maxv tmo = TPM_ACCESS_TMO; /* Milliseconds. */
310 1.13 maxv while ((reg = bus_space_read_1(bt, bh, TPM_ACCESS) &
311 1.13 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
312 1.13 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && tmo--) {
313 1.13 maxv DELAY(1000); /* 1 millisecond. */
314 1.13 maxv }
315 1.13 maxv if ((reg & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
316 1.13 maxv (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
317 1.13 maxv return 0;
318 1.13 maxv }
319 1.13 maxv
320 1.13 maxv if (bus_space_read_4(bt, bh, TPM_ID) == 0xffffffff)
321 1.13 maxv return 0;
322 1.13 maxv
323 1.13 maxv return 1;
324 1.13 maxv }
325 1.13 maxv
326 1.13 maxv int
327 1.14 maxv tpm_tis12_init(struct tpm_softc *sc)
328 1.13 maxv {
329 1.14 maxv sc->sc_caps = bus_space_read_4(sc->sc_bt, sc->sc_bh,
330 1.13 maxv TPM_INTF_CAPABILITY);
331 1.13 maxv sc->sc_devid = bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_ID);
332 1.13 maxv sc->sc_rev = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_REV);
333 1.13 maxv
334 1.14 maxv aprint_normal_dev(sc->sc_dev, "device 0x%08x rev 0x%x\n",
335 1.14 maxv sc->sc_devid, sc->sc_rev);
336 1.13 maxv
337 1.13 maxv if (tpm_request_locality(sc, 0))
338 1.13 maxv return 1;
339 1.13 maxv
340 1.13 maxv /* Abort whatever it thought it was doing. */
341 1.13 maxv bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
342 1.13 maxv
343 1.13 maxv return 0;
344 1.13 maxv }
345 1.13 maxv
346 1.1 christos int
347 1.14 maxv tpm_tis12_start(struct tpm_softc *sc, int rw)
348 1.1 christos {
349 1.1 christos int rv;
350 1.1 christos
351 1.14 maxv if (rw == UIO_READ) {
352 1.1 christos rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
353 1.1 christos TPM_READ_TMO, sc->sc_read);
354 1.1 christos return rv;
355 1.1 christos }
356 1.1 christos
357 1.13 maxv /* Request the 0th locality. */
358 1.1 christos if ((rv = tpm_request_locality(sc, 0)) != 0)
359 1.1 christos return rv;
360 1.1 christos
361 1.13 maxv sc->sc_status = tpm_status(sc);
362 1.13 maxv if (sc->sc_status & TPM_STS_CMD_READY)
363 1.1 christos return 0;
364 1.1 christos
365 1.1 christos /* Abort previous and restart. */
366 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
367 1.13 maxv rv = tpm_waitfor(sc, TPM_STS_CMD_READY, TPM_READY_TMO, sc->sc_write);
368 1.13 maxv if (rv)
369 1.1 christos return rv;
370 1.1 christos
371 1.1 christos return 0;
372 1.1 christos }
373 1.1 christos
374 1.1 christos int
375 1.3 christos tpm_tis12_read(struct tpm_softc *sc, void *buf, size_t len, size_t *count,
376 1.1 christos int flags)
377 1.1 christos {
378 1.1 christos uint8_t *p = buf;
379 1.1 christos size_t cnt;
380 1.14 maxv int rv, n;
381 1.1 christos
382 1.1 christos cnt = 0;
383 1.1 christos while (len > 0) {
384 1.13 maxv rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
385 1.13 maxv TPM_READ_TMO, sc->sc_read);
386 1.13 maxv if (rv)
387 1.1 christos return rv;
388 1.1 christos
389 1.14 maxv n = MIN(len, tpm_getburst(sc));
390 1.14 maxv while (n > 0) {
391 1.1 christos *p++ = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_DATA);
392 1.1 christos cnt++;
393 1.14 maxv len--;
394 1.14 maxv n--;
395 1.1 christos }
396 1.1 christos
397 1.1 christos if ((flags & TPM_PARAM_SIZE) == 0 && cnt >= 6)
398 1.1 christos break;
399 1.1 christos }
400 1.1 christos
401 1.1 christos if (count)
402 1.1 christos *count = cnt;
403 1.1 christos
404 1.1 christos return 0;
405 1.1 christos }
406 1.1 christos
407 1.1 christos int
408 1.3 christos tpm_tis12_write(struct tpm_softc *sc, const void *buf, size_t len)
409 1.1 christos {
410 1.3 christos const uint8_t *p = buf;
411 1.1 christos size_t cnt;
412 1.1 christos int rv, r;
413 1.1 christos
414 1.3 christos if (len == 0)
415 1.3 christos return 0;
416 1.1 christos if ((rv = tpm_request_locality(sc, 0)) != 0)
417 1.1 christos return rv;
418 1.1 christos
419 1.1 christos cnt = 0;
420 1.1 christos while (cnt < len - 1) {
421 1.1 christos for (r = tpm_getburst(sc); r > 0 && cnt < len - 1; r--) {
422 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_DATA, *p++);
423 1.1 christos cnt++;
424 1.1 christos }
425 1.1 christos if ((rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc))) {
426 1.1 christos return rv;
427 1.1 christos }
428 1.13 maxv sc->sc_status = tpm_status(sc);
429 1.13 maxv if (!(sc->sc_status & TPM_STS_DATA_EXPECT)) {
430 1.1 christos return EIO;
431 1.1 christos }
432 1.1 christos }
433 1.1 christos
434 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_DATA, *p++);
435 1.1 christos cnt++;
436 1.1 christos
437 1.1 christos if ((rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc))) {
438 1.1 christos return rv;
439 1.1 christos }
440 1.13 maxv if ((sc->sc_status & TPM_STS_DATA_EXPECT) != 0) {
441 1.1 christos return EIO;
442 1.1 christos }
443 1.1 christos
444 1.1 christos return 0;
445 1.1 christos }
446 1.1 christos
447 1.1 christos int
448 1.14 maxv tpm_tis12_end(struct tpm_softc *sc, int rw, int err)
449 1.1 christos {
450 1.1 christos int rv = 0;
451 1.1 christos
452 1.14 maxv if (rw == UIO_READ) {
453 1.13 maxv rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc->sc_read);
454 1.13 maxv if (rv)
455 1.1 christos return rv;
456 1.1 christos
457 1.1 christos /* Still more data? */
458 1.13 maxv sc->sc_status = tpm_status(sc);
459 1.14 maxv if (!err && (sc->sc_status & TPM_STS_DATA_AVAIL)) {
460 1.1 christos rv = EIO;
461 1.1 christos }
462 1.1 christos
463 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
464 1.1 christos TPM_STS_CMD_READY);
465 1.1 christos
466 1.13 maxv /* Release the 0th locality. */
467 1.13 maxv bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
468 1.1 christos TPM_ACCESS_ACTIVE_LOCALITY);
469 1.1 christos } else {
470 1.1 christos /* Hungry for more? */
471 1.13 maxv sc->sc_status = tpm_status(sc);
472 1.13 maxv if (!err && (sc->sc_status & TPM_STS_DATA_EXPECT)) {
473 1.1 christos rv = EIO;
474 1.1 christos }
475 1.1 christos
476 1.1 christos bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
477 1.1 christos err ? TPM_STS_CMD_READY : TPM_STS_GO);
478 1.1 christos }
479 1.1 christos
480 1.1 christos return rv;
481 1.1 christos }
482 1.1 christos
483 1.13 maxv /* -------------------------------------------------------------------------- */
484 1.1 christos
485 1.13 maxv static dev_type_open(tpmopen);
486 1.13 maxv static dev_type_close(tpmclose);
487 1.13 maxv static dev_type_read(tpmread);
488 1.13 maxv static dev_type_write(tpmwrite);
489 1.13 maxv static dev_type_ioctl(tpmioctl);
490 1.1 christos
491 1.13 maxv const struct cdevsw tpm_cdevsw = {
492 1.13 maxv .d_open = tpmopen,
493 1.13 maxv .d_close = tpmclose,
494 1.13 maxv .d_read = tpmread,
495 1.13 maxv .d_write = tpmwrite,
496 1.13 maxv .d_ioctl = tpmioctl,
497 1.13 maxv .d_stop = nostop,
498 1.13 maxv .d_tty = notty,
499 1.13 maxv .d_poll = nopoll,
500 1.13 maxv .d_mmap = nommap,
501 1.13 maxv .d_kqfilter = nokqfilter,
502 1.13 maxv .d_discard = nodiscard,
503 1.14 maxv .d_flag = D_OTHER | D_MPSAFE,
504 1.13 maxv };
505 1.1 christos
506 1.13 maxv static int
507 1.1 christos tpmopen(dev_t dev, int flag, int mode, struct lwp *l)
508 1.1 christos {
509 1.14 maxv struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
510 1.14 maxv int ret = 0;
511 1.1 christos
512 1.13 maxv if (sc == NULL)
513 1.1 christos return ENXIO;
514 1.1 christos
515 1.14 maxv mutex_enter(&sc->sc_lock);
516 1.14 maxv if (sc->sc_busy) {
517 1.14 maxv ret = EBUSY;
518 1.14 maxv } else {
519 1.14 maxv sc->sc_busy = true;
520 1.14 maxv }
521 1.14 maxv mutex_exit(&sc->sc_lock);
522 1.1 christos
523 1.14 maxv return ret;
524 1.1 christos }
525 1.1 christos
526 1.13 maxv static int
527 1.1 christos tpmclose(dev_t dev, int flag, int mode, struct lwp *l)
528 1.1 christos {
529 1.14 maxv struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
530 1.14 maxv int ret = 0;
531 1.1 christos
532 1.13 maxv if (sc == NULL)
533 1.1 christos return ENXIO;
534 1.1 christos
535 1.14 maxv mutex_enter(&sc->sc_lock);
536 1.14 maxv if (!sc->sc_busy) {
537 1.14 maxv ret = EINVAL;
538 1.14 maxv } else {
539 1.14 maxv sc->sc_busy = false;
540 1.14 maxv }
541 1.14 maxv mutex_exit(&sc->sc_lock);
542 1.1 christos
543 1.14 maxv return ret;
544 1.1 christos }
545 1.1 christos
546 1.13 maxv static int
547 1.1 christos tpmread(dev_t dev, struct uio *uio, int flags)
548 1.1 christos {
549 1.14 maxv struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
550 1.15 maxv struct tpm_header hdr;
551 1.14 maxv uint8_t buf[TPM_BUFSIZ];
552 1.1 christos size_t cnt, len, n;
553 1.14 maxv int rv;
554 1.1 christos
555 1.13 maxv if (sc == NULL)
556 1.1 christos return ENXIO;
557 1.1 christos
558 1.3 christos if ((rv = (*sc->sc_start)(sc, UIO_READ)))
559 1.3 christos goto out;
560 1.1 christos
561 1.14 maxv /* Get the header. */
562 1.15 maxv if ((rv = (*sc->sc_read)(sc, &hdr, sizeof(hdr), &cnt, 0))) {
563 1.3 christos (*sc->sc_end)(sc, UIO_READ, rv);
564 1.3 christos goto out;
565 1.1 christos }
566 1.15 maxv len = TPM_BE32(hdr.length);
567 1.14 maxv if (len > uio->uio_resid || len < cnt) {
568 1.1 christos rv = EIO;
569 1.3 christos (*sc->sc_end)(sc, UIO_READ, rv);
570 1.3 christos goto out;
571 1.1 christos }
572 1.1 christos
573 1.14 maxv /* Copy out the header. */
574 1.15 maxv if ((rv = uiomove(&hdr, cnt, uio))) {
575 1.3 christos (*sc->sc_end)(sc, UIO_READ, rv);
576 1.3 christos goto out;
577 1.1 christos }
578 1.1 christos
579 1.14 maxv /* Process the rest. */
580 1.14 maxv len -= cnt;
581 1.14 maxv while (len > 0) {
582 1.14 maxv n = MIN(sizeof(buf), len);
583 1.14 maxv if ((rv = (*sc->sc_read)(sc, buf, n, NULL, TPM_PARAM_SIZE))) {
584 1.3 christos (*sc->sc_end)(sc, UIO_READ, rv);
585 1.3 christos goto out;
586 1.1 christos }
587 1.14 maxv if ((rv = uiomove(buf, n, uio))) {
588 1.3 christos (*sc->sc_end)(sc, UIO_READ, rv);
589 1.3 christos goto out;
590 1.1 christos }
591 1.14 maxv len -= n;
592 1.1 christos }
593 1.1 christos
594 1.3 christos rv = (*sc->sc_end)(sc, UIO_READ, rv);
595 1.3 christos out:
596 1.1 christos return rv;
597 1.1 christos }
598 1.1 christos
599 1.13 maxv static int
600 1.1 christos tpmwrite(dev_t dev, struct uio *uio, int flags)
601 1.1 christos {
602 1.14 maxv struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
603 1.1 christos uint8_t buf[TPM_BUFSIZ];
604 1.14 maxv int n, rv;
605 1.1 christos
606 1.13 maxv if (sc == NULL)
607 1.1 christos return ENXIO;
608 1.1 christos
609 1.1 christos n = MIN(sizeof(buf), uio->uio_resid);
610 1.1 christos if ((rv = uiomove(buf, n, uio))) {
611 1.13 maxv goto out;
612 1.1 christos }
613 1.3 christos if ((rv = (*sc->sc_start)(sc, UIO_WRITE))) {
614 1.13 maxv goto out;
615 1.1 christos }
616 1.3 christos if ((rv = (*sc->sc_write)(sc, buf, n))) {
617 1.13 maxv goto out;
618 1.1 christos }
619 1.1 christos
620 1.3 christos rv = (*sc->sc_end)(sc, UIO_WRITE, rv);
621 1.13 maxv out:
622 1.1 christos return rv;
623 1.1 christos }
624 1.1 christos
625 1.13 maxv static int
626 1.13 maxv tpmioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
627 1.1 christos {
628 1.14 maxv struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
629 1.13 maxv struct tpm_ioc_getinfo *info;
630 1.13 maxv
631 1.13 maxv if (sc == NULL)
632 1.13 maxv return ENXIO;
633 1.13 maxv
634 1.13 maxv switch (cmd) {
635 1.13 maxv case TPM_IOC_GETINFO:
636 1.13 maxv info = addr;
637 1.13 maxv info->api_version = TPM_API_VERSION;
638 1.13 maxv info->tpm_version = sc->sc_ver;
639 1.13 maxv info->device_id = sc->sc_devid;
640 1.13 maxv info->device_rev = sc->sc_rev;
641 1.14 maxv info->device_caps = sc->sc_caps;
642 1.13 maxv return 0;
643 1.13 maxv default:
644 1.13 maxv break;
645 1.13 maxv }
646 1.13 maxv
647 1.1 christos return ENOTTY;
648 1.1 christos }
649