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