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