tpm.c revision 1.12.4.1 1 1.12.4.1 martin /* $NetBSD: tpm.c,v 1.12.4.1 2020/04/13 08:04:22 martin Exp $ */
2 1.12.4.1 martin
3 1.12.4.1 martin /*
4 1.12.4.1 martin * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 1.12.4.1 martin * All rights reserved.
6 1.12.4.1 martin *
7 1.12.4.1 martin * This code is derived from software contributed to The NetBSD Foundation
8 1.12.4.1 martin * by Maxime Villard.
9 1.12.4.1 martin *
10 1.12.4.1 martin * Redistribution and use in source and binary forms, with or without
11 1.12.4.1 martin * modification, are permitted provided that the following conditions
12 1.12.4.1 martin * are met:
13 1.12.4.1 martin * 1. Redistributions of source code must retain the above copyright
14 1.12.4.1 martin * notice, this list of conditions and the following disclaimer.
15 1.12.4.1 martin * 2. Redistributions in binary form must reproduce the above copyright
16 1.12.4.1 martin * notice, this list of conditions and the following disclaimer in the
17 1.12.4.1 martin * documentation and/or other materials provided with the distribution.
18 1.12.4.1 martin *
19 1.12.4.1 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.12.4.1 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.12.4.1 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.12.4.1 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.12.4.1 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.12.4.1 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.12.4.1 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.12.4.1 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.12.4.1 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.12.4.1 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.12.4.1 martin * POSSIBILITY OF SUCH DAMAGE.
30 1.12.4.1 martin */
31 1.12.4.1 martin
32 1.1 christos /*
33 1.1 christos * Copyright (c) 2008, 2009 Michael Shalayeff
34 1.12.4.1 martin * 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.12.4.1 martin __KERNEL_RCSID(0, "$NetBSD: tpm.c,v 1.12.4.1 2020/04/13 08:04:22 martin 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.12.4.1 martin CTASSERT(sizeof(struct tpm_header) == 10);
69 1.1 christos
70 1.12.4.1 martin #define TPM_BUFSIZ 1024
71 1.1 christos
72 1.12.4.1 martin #define TPM_PARAM_SIZE 0x0001 /* that's a flag */
73 1.1 christos
74 1.12.4.1 martin /* Timeouts. */
75 1.12.4.1 martin #define TPM_ACCESS_TMO 2000 /* 2sec */
76 1.12.4.1 martin #define TPM_READY_TMO 2000 /* 2sec */
77 1.12.4.1 martin #define TPM_READ_TMO 2000 /* 2sec */
78 1.12.4.1 martin #define TPM_BURST_TMO 2000 /* 2sec */
79 1.1 christos
80 1.12.4.1 martin #define TPM_CAPS_REQUIRED \
81 1.12.4.1 martin (TPM_INTF_DATA_AVAIL_INT|TPM_INTF_LOCALITY_CHANGE_INT| \
82 1.12.4.1 martin TPM_INTF_INT_LEVEL_LOW)
83 1.1 christos
84 1.12.4.1 martin static inline int
85 1.12.4.1 martin tpm_tmotohz(int tmo)
86 1.1 christos {
87 1.12.4.1 martin struct timeval tv;
88 1.1 christos
89 1.12.4.1 martin tv.tv_sec = tmo / 1000;
90 1.12.4.1 martin tv.tv_usec = 1000 * (tmo % 1000);
91 1.1 christos
92 1.12.4.1 martin return tvtohz(&tv);
93 1.1 christos }
94 1.1 christos
95 1.12.4.1 martin 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.12.4.1 martin while (to--) {
103 1.1 christos /*
104 1.12.4.1 martin * 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.12.4.1 martin
110 1.1 christos if (burst)
111 1.1 christos return burst;
112 1.1 christos
113 1.12.4.1 martin 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.12.4.1 martin static inline uint8_t
123 1.1 christos tpm_status(struct tpm_softc *sc)
124 1.1 christos {
125 1.12.4.1 martin return bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS) &
126 1.12.4.1 martin TPM_STS_STATUS_BITS;
127 1.1 christos }
128 1.1 christos
129 1.12.4.1 martin /* -------------------------------------------------------------------------- */
130 1.12.4.1 martin
131 1.12.4.1 martin static bool
132 1.12.4.1 martin tpm12_suspend(struct tpm_softc *sc)
133 1.1 christos {
134 1.12.4.1 martin static const uint8_t command[10] = {
135 1.12.4.1 martin 0x00, 0xC1, /* TPM_TAG_RQU_COMMAND */
136 1.12.4.1 martin 0x00, 0x00, 0x00, 10, /* Length in bytes */
137 1.12.4.1 martin 0x00, 0x00, 0x00, 0x98 /* TPM_ORD_SaveState */
138 1.12.4.1 martin };
139 1.12.4.1 martin struct tpm_header response;
140 1.1 christos
141 1.12.4.1 martin if ((*sc->sc_intf->write)(sc, &command, sizeof(command)) != 0)
142 1.12.4.1 martin return false;
143 1.12.4.1 martin if ((*sc->sc_intf->read)(sc, &response, sizeof(response), NULL, 0) != 0)
144 1.12.4.1 martin return false;
145 1.12.4.1 martin if (TPM_BE32(response.code) != 0)
146 1.12.4.1 martin return false;
147 1.1 christos
148 1.12.4.1 martin return true;
149 1.12.4.1 martin }
150 1.12.4.1 martin
151 1.12.4.1 martin static bool
152 1.12.4.1 martin tpm20_suspend(struct tpm_softc *sc)
153 1.12.4.1 martin {
154 1.12.4.1 martin static const uint8_t command[12] = {
155 1.12.4.1 martin 0x80, 0x01, /* TPM_ST_NO_SESSIONS */
156 1.12.4.1 martin 0x00, 0x00, 0x00, 12, /* Length in bytes */
157 1.12.4.1 martin 0x00, 0x00, 0x01, 0x45, /* TPM_CC_Shutdown */
158 1.12.4.1 martin 0x00, 0x01 /* TPM_SU_STATE */
159 1.12.4.1 martin };
160 1.12.4.1 martin struct tpm_header response;
161 1.12.4.1 martin
162 1.12.4.1 martin if ((*sc->sc_intf->write)(sc, &command, sizeof(command)) != 0)
163 1.12.4.1 martin return false;
164 1.12.4.1 martin if ((*sc->sc_intf->read)(sc, &response, sizeof(response), NULL, 0) != 0)
165 1.12.4.1 martin return false;
166 1.12.4.1 martin if (TPM_BE32(response.code) != 0)
167 1.12.4.1 martin return false;
168 1.12.4.1 martin
169 1.12.4.1 martin return true;
170 1.1 christos }
171 1.1 christos
172 1.1 christos bool
173 1.1 christos tpm_suspend(device_t dev, const pmf_qual_t *qual)
174 1.1 christos {
175 1.1 christos struct tpm_softc *sc = device_private(dev);
176 1.1 christos
177 1.12.4.1 martin switch (sc->sc_ver) {
178 1.12.4.1 martin case TPM_1_2:
179 1.12.4.1 martin return tpm12_suspend(sc);
180 1.12.4.1 martin case TPM_2_0:
181 1.12.4.1 martin return tpm20_suspend(sc);
182 1.12.4.1 martin default:
183 1.12.4.1 martin panic("%s: impossible", __func__);
184 1.12.4.1 martin }
185 1.1 christos }
186 1.1 christos
187 1.1 christos bool
188 1.1 christos tpm_resume(device_t dev, const pmf_qual_t *qual)
189 1.1 christos {
190 1.12.4.1 martin /*
191 1.12.4.1 martin * Don't do anything, the BIOS is supposed to restore the previously
192 1.12.4.1 martin * saved state.
193 1.12.4.1 martin */
194 1.7 christos return true;
195 1.1 christos }
196 1.1 christos
197 1.12.4.1 martin /* -------------------------------------------------------------------------- */
198 1.12.4.1 martin
199 1.12.4.1 martin static int
200 1.12.4.1 martin 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.12.4.1 martin while (((sc->sc_status = tpm_status(sc)) & mask) != mask && to--) {
205 1.12.4.1 martin 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.12.4.1 martin static int
215 1.12.4.1 martin tpm_waitfor(struct tpm_softc *sc, uint8_t bits, int tmo, wchan_t chan)
216 1.12.4.1 martin {
217 1.12.4.1 martin int retry, to, rv;
218 1.12.4.1 martin uint8_t todo;
219 1.1 christos
220 1.12.4.1 martin to = tpm_tmotohz(tmo);
221 1.12.4.1 martin retry = 3;
222 1.12.4.1 martin
223 1.12.4.1 martin restart:
224 1.12.4.1 martin todo = bits;
225 1.1 christos
226 1.1 christos /*
227 1.12.4.1 martin * TPM_STS_VALID has priority over the others.
228 1.1 christos */
229 1.12.4.1 martin if (todo & TPM_STS_VALID) {
230 1.12.4.1 martin if ((rv = tpm_poll(sc, TPM_STS_VALID, to+1, chan)) != 0)
231 1.12.4.1 martin return rv;
232 1.12.4.1 martin todo &= ~TPM_STS_VALID;
233 1.1 christos }
234 1.1 christos
235 1.12.4.1 martin if ((rv = tpm_poll(sc, todo, to, chan)) != 0)
236 1.12.4.1 martin return rv;
237 1.1 christos
238 1.12.4.1 martin if ((todo & sc->sc_status) != todo) {
239 1.12.4.1 martin if ((retry-- > 0) && (bits & TPM_STS_VALID)) {
240 1.12.4.1 martin bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
241 1.12.4.1 martin TPM_STS_RESP_RETRY);
242 1.12.4.1 martin goto restart;
243 1.12.4.1 martin }
244 1.12.4.1 martin return EIO;
245 1.12.4.1 martin }
246 1.1 christos
247 1.12.4.1 martin return 0;
248 1.1 christos }
249 1.1 christos
250 1.12.4.1 martin /* -------------------------------------------------------------------------- */
251 1.12.4.1 martin
252 1.1 christos /*
253 1.12.4.1 martin * TPM using the TIS 1.2 interface.
254 1.1 christos */
255 1.12.4.1 martin
256 1.12.4.1 martin static int
257 1.12.4.1 martin tpm12_request_locality(struct tpm_softc *sc, int l)
258 1.1 christos {
259 1.12.4.1 martin uint32_t r;
260 1.12.4.1 martin int to, rv;
261 1.1 christos
262 1.12.4.1 martin if (l != 0)
263 1.12.4.1 martin return EINVAL;
264 1.1 christos
265 1.12.4.1 martin if ((bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
266 1.12.4.1 martin (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) ==
267 1.12.4.1 martin (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY))
268 1.12.4.1 martin return 0;
269 1.1 christos
270 1.12.4.1 martin bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
271 1.12.4.1 martin TPM_ACCESS_REQUEST_USE);
272 1.12.4.1 martin
273 1.12.4.1 martin to = tpm_tmotohz(TPM_ACCESS_TMO);
274 1.12.4.1 martin
275 1.12.4.1 martin while ((r = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
276 1.12.4.1 martin (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
277 1.12.4.1 martin (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && to--) {
278 1.12.4.1 martin rv = tsleep(sc->sc_intf->init, PCATCH, "tpm_locality", 1);
279 1.12.4.1 martin if (rv && rv != EWOULDBLOCK) {
280 1.12.4.1 martin return rv;
281 1.1 christos }
282 1.12.4.1 martin }
283 1.1 christos
284 1.12.4.1 martin if ((r & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
285 1.12.4.1 martin (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
286 1.12.4.1 martin return EBUSY;
287 1.1 christos }
288 1.1 christos
289 1.12.4.1 martin return 0;
290 1.12.4.1 martin }
291 1.1 christos
292 1.12.4.1 martin static int
293 1.12.4.1 martin tpm_tis12_probe(bus_space_tag_t bt, bus_space_handle_t bh)
294 1.12.4.1 martin {
295 1.12.4.1 martin uint32_t cap;
296 1.12.4.1 martin uint8_t reg;
297 1.12.4.1 martin int tmo;
298 1.1 christos
299 1.12.4.1 martin cap = bus_space_read_4(bt, bh, TPM_INTF_CAPABILITY);
300 1.12.4.1 martin if (cap == 0xffffffff)
301 1.12.4.1 martin return EINVAL;
302 1.12.4.1 martin if ((cap & TPM_CAPS_REQUIRED) != TPM_CAPS_REQUIRED)
303 1.12.4.1 martin return ENOTSUP;
304 1.12.4.1 martin
305 1.12.4.1 martin /* Request locality 0. */
306 1.12.4.1 martin bus_space_write_1(bt, bh, TPM_ACCESS, TPM_ACCESS_REQUEST_USE);
307 1.12.4.1 martin
308 1.12.4.1 martin /* Wait for it to become active. */
309 1.12.4.1 martin tmo = TPM_ACCESS_TMO; /* Milliseconds. */
310 1.12.4.1 martin while ((reg = bus_space_read_1(bt, bh, TPM_ACCESS) &
311 1.12.4.1 martin (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
312 1.12.4.1 martin (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && tmo--) {
313 1.12.4.1 martin DELAY(1000); /* 1 millisecond. */
314 1.1 christos }
315 1.12.4.1 martin if ((reg & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
316 1.12.4.1 martin (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
317 1.12.4.1 martin return ETIMEDOUT;
318 1.12.4.1 martin }
319 1.12.4.1 martin
320 1.12.4.1 martin if (bus_space_read_4(bt, bh, TPM_ID) == 0xffffffff)
321 1.12.4.1 martin return EINVAL;
322 1.12.4.1 martin
323 1.12.4.1 martin return 0;
324 1.12.4.1 martin }
325 1.12.4.1 martin
326 1.12.4.1 martin static int
327 1.12.4.1 martin tpm_tis12_init(struct tpm_softc *sc)
328 1.12.4.1 martin {
329 1.12.4.1 martin int rv;
330 1.12.4.1 martin
331 1.12.4.1 martin sc->sc_caps = bus_space_read_4(sc->sc_bt, sc->sc_bh,
332 1.12.4.1 martin TPM_INTF_CAPABILITY);
333 1.12.4.1 martin sc->sc_devid = bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_ID);
334 1.12.4.1 martin sc->sc_rev = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_REV);
335 1.12.4.1 martin
336 1.12.4.1 martin aprint_normal_dev(sc->sc_dev, "device 0x%08x rev 0x%x\n",
337 1.12.4.1 martin sc->sc_devid, sc->sc_rev);
338 1.12.4.1 martin
339 1.12.4.1 martin if ((rv = tpm12_request_locality(sc, 0)) != 0)
340 1.12.4.1 martin return rv;
341 1.12.4.1 martin
342 1.12.4.1 martin /* Abort whatever it thought it was doing. */
343 1.12.4.1 martin bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
344 1.1 christos
345 1.1 christos return 0;
346 1.1 christos }
347 1.1 christos
348 1.12.4.1 martin static int
349 1.12.4.1 martin 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.12.4.1 martin if (rw == UIO_READ) {
354 1.1 christos rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
355 1.12.4.1 martin TPM_READ_TMO, sc->sc_intf->read);
356 1.1 christos return rv;
357 1.1 christos }
358 1.1 christos
359 1.12.4.1 martin /* Request the 0th locality. */
360 1.12.4.1 martin if ((rv = tpm12_request_locality(sc, 0)) != 0)
361 1.1 christos return rv;
362 1.1 christos
363 1.12.4.1 martin sc->sc_status = tpm_status(sc);
364 1.12.4.1 martin 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.12.4.1 martin rv = tpm_waitfor(sc, TPM_STS_CMD_READY, TPM_READY_TMO, sc->sc_intf->write);
370 1.12.4.1 martin 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.12.4.1 martin 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.12.4.1 martin int rv, n;
383 1.1 christos
384 1.1 christos cnt = 0;
385 1.1 christos while (len > 0) {
386 1.12.4.1 martin rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
387 1.12.4.1 martin TPM_READ_TMO, sc->sc_intf->read);
388 1.12.4.1 martin if (rv)
389 1.1 christos return rv;
390 1.1 christos
391 1.12.4.1 martin n = MIN(len, tpm_getburst(sc));
392 1.12.4.1 martin 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.12.4.1 martin len--;
396 1.12.4.1 martin 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.12.4.1 martin 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.12.4.1 martin 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.12.4.1 martin sc->sc_status = tpm_status(sc);
431 1.12.4.1 martin 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.12.4.1 martin 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.12.4.1 martin static int
450 1.12.4.1 martin 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.12.4.1 martin if (rw == UIO_READ) {
455 1.12.4.1 martin rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc->sc_intf->read);
456 1.12.4.1 martin if (rv)
457 1.1 christos return rv;
458 1.1 christos
459 1.1 christos /* Still more data? */
460 1.12.4.1 martin sc->sc_status = tpm_status(sc);
461 1.12.4.1 martin 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.12.4.1 martin /* Release the 0th locality. */
469 1.12.4.1 martin 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.12.4.1 martin sc->sc_status = tpm_status(sc);
474 1.12.4.1 martin 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.12.4.1 martin const struct tpm_intf tpm_intf_tis12 = {
486 1.12.4.1 martin .version = TIS_1_2,
487 1.12.4.1 martin .probe = tpm_tis12_probe,
488 1.12.4.1 martin .init = tpm_tis12_init,
489 1.12.4.1 martin .start = tpm_tis12_start,
490 1.12.4.1 martin .read = tpm_tis12_read,
491 1.12.4.1 martin .write = tpm_tis12_write,
492 1.12.4.1 martin .end = tpm_tis12_end
493 1.12.4.1 martin };
494 1.1 christos
495 1.12.4.1 martin /* -------------------------------------------------------------------------- */
496 1.1 christos
497 1.12.4.1 martin static dev_type_open(tpmopen);
498 1.12.4.1 martin static dev_type_close(tpmclose);
499 1.12.4.1 martin static dev_type_read(tpmread);
500 1.12.4.1 martin static dev_type_write(tpmwrite);
501 1.12.4.1 martin static dev_type_ioctl(tpmioctl);
502 1.1 christos
503 1.12.4.1 martin const struct cdevsw tpm_cdevsw = {
504 1.12.4.1 martin .d_open = tpmopen,
505 1.12.4.1 martin .d_close = tpmclose,
506 1.12.4.1 martin .d_read = tpmread,
507 1.12.4.1 martin .d_write = tpmwrite,
508 1.12.4.1 martin .d_ioctl = tpmioctl,
509 1.12.4.1 martin .d_stop = nostop,
510 1.12.4.1 martin .d_tty = notty,
511 1.12.4.1 martin .d_poll = nopoll,
512 1.12.4.1 martin .d_mmap = nommap,
513 1.12.4.1 martin .d_kqfilter = nokqfilter,
514 1.12.4.1 martin .d_discard = nodiscard,
515 1.12.4.1 martin .d_flag = D_OTHER | D_MPSAFE,
516 1.12.4.1 martin };
517 1.1 christos
518 1.12.4.1 martin static int
519 1.1 christos tpmopen(dev_t dev, int flag, int mode, struct lwp *l)
520 1.1 christos {
521 1.12.4.1 martin struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
522 1.12.4.1 martin int ret = 0;
523 1.1 christos
524 1.12.4.1 martin if (sc == NULL)
525 1.1 christos return ENXIO;
526 1.1 christos
527 1.12.4.1 martin mutex_enter(&sc->sc_lock);
528 1.12.4.1 martin if (sc->sc_busy) {
529 1.12.4.1 martin ret = EBUSY;
530 1.12.4.1 martin } else {
531 1.12.4.1 martin sc->sc_busy = true;
532 1.12.4.1 martin }
533 1.12.4.1 martin mutex_exit(&sc->sc_lock);
534 1.1 christos
535 1.12.4.1 martin return ret;
536 1.1 christos }
537 1.1 christos
538 1.12.4.1 martin static int
539 1.1 christos tpmclose(dev_t dev, int flag, int mode, struct lwp *l)
540 1.1 christos {
541 1.12.4.1 martin struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
542 1.12.4.1 martin int ret = 0;
543 1.1 christos
544 1.12.4.1 martin if (sc == NULL)
545 1.1 christos return ENXIO;
546 1.1 christos
547 1.12.4.1 martin mutex_enter(&sc->sc_lock);
548 1.12.4.1 martin if (!sc->sc_busy) {
549 1.12.4.1 martin ret = EINVAL;
550 1.12.4.1 martin } else {
551 1.12.4.1 martin sc->sc_busy = false;
552 1.12.4.1 martin }
553 1.12.4.1 martin mutex_exit(&sc->sc_lock);
554 1.1 christos
555 1.12.4.1 martin return ret;
556 1.1 christos }
557 1.1 christos
558 1.12.4.1 martin static int
559 1.1 christos tpmread(dev_t dev, struct uio *uio, int flags)
560 1.1 christos {
561 1.12.4.1 martin struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
562 1.12.4.1 martin struct tpm_header hdr;
563 1.12.4.1 martin uint8_t buf[TPM_BUFSIZ];
564 1.1 christos size_t cnt, len, n;
565 1.12.4.1 martin int rv;
566 1.1 christos
567 1.12.4.1 martin if (sc == NULL)
568 1.1 christos return ENXIO;
569 1.1 christos
570 1.12.4.1 martin if ((rv = (*sc->sc_intf->start)(sc, UIO_READ)))
571 1.12.4.1 martin return rv;
572 1.1 christos
573 1.12.4.1 martin /* Get the header. */
574 1.12.4.1 martin if ((rv = (*sc->sc_intf->read)(sc, &hdr, sizeof(hdr), &cnt, 0))) {
575 1.3 christos goto out;
576 1.1 christos }
577 1.12.4.1 martin len = TPM_BE32(hdr.length);
578 1.12.4.1 martin 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.12.4.1 martin /* Copy out the header. */
584 1.12.4.1 martin if ((rv = uiomove(&hdr, cnt, uio))) {
585 1.3 christos goto out;
586 1.1 christos }
587 1.1 christos
588 1.12.4.1 martin /* Process the rest. */
589 1.12.4.1 martin len -= cnt;
590 1.12.4.1 martin while (len > 0) {
591 1.12.4.1 martin n = MIN(sizeof(buf), len);
592 1.12.4.1 martin 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.12.4.1 martin if ((rv = uiomove(buf, n, uio))) {
596 1.3 christos goto out;
597 1.1 christos }
598 1.12.4.1 martin len -= n;
599 1.1 christos }
600 1.1 christos
601 1.3 christos out:
602 1.12.4.1 martin 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.12.4.1 martin static int
607 1.1 christos tpmwrite(dev_t dev, struct uio *uio, int flags)
608 1.1 christos {
609 1.12.4.1 martin struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
610 1.1 christos uint8_t buf[TPM_BUFSIZ];
611 1.12.4.1 martin int n, rv;
612 1.1 christos
613 1.12.4.1 martin 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.12.4.1 martin goto out;
619 1.1 christos }
620 1.12.4.1 martin if ((rv = (*sc->sc_intf->start)(sc, UIO_WRITE))) {
621 1.12.4.1 martin goto out;
622 1.1 christos }
623 1.12.4.1 martin if ((rv = (*sc->sc_intf->write)(sc, buf, n))) {
624 1.12.4.1 martin goto out;
625 1.1 christos }
626 1.1 christos
627 1.12.4.1 martin rv = (*sc->sc_intf->end)(sc, UIO_WRITE, rv);
628 1.12.4.1 martin out:
629 1.1 christos return rv;
630 1.1 christos }
631 1.1 christos
632 1.12.4.1 martin static int
633 1.12.4.1 martin tpmioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
634 1.1 christos {
635 1.12.4.1 martin struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
636 1.12.4.1 martin struct tpm_ioc_getinfo *info;
637 1.12.4.1 martin
638 1.12.4.1 martin if (sc == NULL)
639 1.12.4.1 martin return ENXIO;
640 1.12.4.1 martin
641 1.12.4.1 martin switch (cmd) {
642 1.12.4.1 martin case TPM_IOC_GETINFO:
643 1.12.4.1 martin info = addr;
644 1.12.4.1 martin info->api_version = TPM_API_VERSION;
645 1.12.4.1 martin info->tpm_version = sc->sc_ver;
646 1.12.4.1 martin info->itf_version = sc->sc_intf->version;
647 1.12.4.1 martin info->device_id = sc->sc_devid;
648 1.12.4.1 martin info->device_rev = sc->sc_rev;
649 1.12.4.1 martin info->device_caps = sc->sc_caps;
650 1.12.4.1 martin return 0;
651 1.12.4.1 martin default:
652 1.12.4.1 martin break;
653 1.12.4.1 martin }
654 1.12.4.1 martin
655 1.1 christos return ENOTTY;
656 1.1 christos }
657