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