Home | History | Annotate | Line # | Download | only in ic
tpm.c revision 1.28
      1  1.28  riastrad /*	$NetBSD: tpm.c,v 1.28 2023/07/04 01:02:26 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.28  riastrad __KERNEL_RCSID(0, "$NetBSD: tpm.c,v 1.28 2023/07/04 01:02:26 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.19  riastrad #include <sys/atomic.h>
     57  1.17  riastrad #include <sys/bus.h>
     58  1.17  riastrad #include <sys/conf.h>
     59  1.17  riastrad #include <sys/device.h>
     60   1.1  christos #include <sys/kernel.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.19  riastrad #include <sys/workqueue.h>
     65   1.1  christos 
     66   1.1  christos #include <dev/ic/tpmreg.h>
     67   1.1  christos #include <dev/ic/tpmvar.h>
     68   1.1  christos 
     69  1.12  riastrad #include "ioconf.h"
     70  1.12  riastrad 
     71  1.15      maxv CTASSERT(sizeof(struct tpm_header) == 10);
     72  1.15      maxv 
     73  1.13      maxv #define TPM_BUFSIZ	1024
     74  1.14      maxv 
     75  1.13      maxv #define TPM_PARAM_SIZE	0x0001	/* that's a flag */
     76  1.13      maxv 
     77  1.13      maxv /* Timeouts. */
     78  1.13      maxv #define TPM_ACCESS_TMO	2000	/* 2sec */
     79  1.13      maxv #define TPM_READY_TMO	2000	/* 2sec */
     80  1.13      maxv #define TPM_READ_TMO	2000	/* 2sec */
     81  1.13      maxv #define TPM_BURST_TMO	2000	/* 2sec */
     82  1.13      maxv 
     83  1.13      maxv #define TPM_CAPS_REQUIRED \
     84  1.13      maxv 	(TPM_INTF_DATA_AVAIL_INT|TPM_INTF_LOCALITY_CHANGE_INT| \
     85  1.13      maxv 	 TPM_INTF_INT_LEVEL_LOW)
     86   1.1  christos 
     87  1.13      maxv static inline int
     88  1.13      maxv tpm_tmotohz(int tmo)
     89   1.1  christos {
     90  1.13      maxv 	struct timeval tv;
     91   1.1  christos 
     92  1.13      maxv 	tv.tv_sec = tmo / 1000;
     93  1.13      maxv 	tv.tv_usec = 1000 * (tmo % 1000);
     94   1.1  christos 
     95  1.13      maxv 	return tvtohz(&tv);
     96   1.1  christos }
     97   1.1  christos 
     98  1.13      maxv static int
     99   1.1  christos tpm_getburst(struct tpm_softc *sc)
    100   1.1  christos {
    101   1.1  christos 	int burst, to, rv;
    102   1.1  christos 
    103   1.1  christos 	to = tpm_tmotohz(TPM_BURST_TMO);
    104   1.1  christos 
    105  1.13      maxv 	while (to--) {
    106   1.1  christos 		/*
    107  1.13      maxv 		 * Burst count is in bits 23:8, so read the two higher bytes.
    108   1.1  christos 		 */
    109   1.1  christos 		burst = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS + 1);
    110   1.1  christos 		burst |= bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS + 2)
    111   1.1  christos 		    << 8;
    112  1.13      maxv 
    113   1.1  christos 		if (burst)
    114   1.1  christos 			return burst;
    115   1.1  christos 
    116  1.28  riastrad 		rv = kpause("tpm_getburst", /*intr*/true, /*timo*/1,
    117  1.28  riastrad 		    /*lock*/NULL);
    118   1.1  christos 		if (rv && rv != EWOULDBLOCK) {
    119   1.1  christos 			return 0;
    120   1.1  christos 		}
    121   1.1  christos 	}
    122   1.1  christos 
    123   1.1  christos 	return 0;
    124   1.1  christos }
    125   1.1  christos 
    126  1.13      maxv static inline uint8_t
    127   1.1  christos tpm_status(struct tpm_softc *sc)
    128   1.1  christos {
    129  1.13      maxv 	return bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_STS) &
    130  1.13      maxv 	    TPM_STS_STATUS_BITS;
    131   1.1  christos }
    132   1.1  christos 
    133  1.13      maxv /* -------------------------------------------------------------------------- */
    134   1.1  christos 
    135  1.15      maxv static bool
    136  1.15      maxv tpm12_suspend(struct tpm_softc *sc)
    137  1.15      maxv {
    138  1.15      maxv 	static const uint8_t command[10] = {
    139  1.15      maxv 		0x00, 0xC1,		/* TPM_TAG_RQU_COMMAND */
    140  1.15      maxv 		0x00, 0x00, 0x00, 10,	/* Length in bytes */
    141  1.15      maxv 		0x00, 0x00, 0x00, 0x98	/* TPM_ORD_SaveState */
    142  1.15      maxv 	};
    143  1.15      maxv 	struct tpm_header response;
    144  1.24  riastrad 	size_t nread;
    145  1.24  riastrad 	bool endwrite = false, endread = false;
    146  1.24  riastrad 	int error;
    147  1.24  riastrad 
    148  1.24  riastrad 	/*
    149  1.24  riastrad 	 * Write the command.
    150  1.24  riastrad 	 */
    151  1.24  riastrad 	error = (*sc->sc_intf->start)(sc, UIO_WRITE);
    152  1.24  riastrad 	if (error) {
    153  1.24  riastrad 		device_printf(sc->sc_dev, "start write failed: %d", error);
    154  1.24  riastrad 		goto out;
    155  1.24  riastrad 	}
    156  1.24  riastrad 
    157  1.24  riastrad 	endwrite = true;
    158  1.24  riastrad 
    159  1.24  riastrad 	error = (*sc->sc_intf->write)(sc, &command, sizeof(command));
    160  1.24  riastrad 	if (error) {
    161  1.24  riastrad 		device_printf(sc->sc_dev, "write TPM_ORD_SaveState failed: %d",
    162  1.24  riastrad 		    error);
    163  1.24  riastrad 		goto out;
    164  1.24  riastrad 	}
    165  1.24  riastrad 
    166  1.24  riastrad 	endwrite = false;
    167  1.24  riastrad 
    168  1.24  riastrad 	error = (*sc->sc_intf->end)(sc, UIO_WRITE, 0);
    169  1.24  riastrad 	if (error) {
    170  1.24  riastrad 		device_printf(sc->sc_dev, "end write failed: %d", error);
    171  1.24  riastrad 		goto out;
    172  1.24  riastrad 	}
    173  1.24  riastrad 
    174  1.24  riastrad 	/*
    175  1.24  riastrad 	 * Read the response -- just the header; we don't expect a
    176  1.24  riastrad 	 * payload.
    177  1.24  riastrad 	 */
    178  1.24  riastrad 	error = (*sc->sc_intf->start)(sc, UIO_READ);
    179  1.24  riastrad 	if (error) {
    180  1.24  riastrad 		device_printf(sc->sc_dev, "start read failed: %d", error);
    181  1.24  riastrad 		goto out;
    182  1.24  riastrad 	}
    183  1.24  riastrad 
    184  1.24  riastrad 	endread = true;
    185  1.24  riastrad 
    186  1.24  riastrad 	error = (*sc->sc_intf->read)(sc, &response, sizeof(response), &nread,
    187  1.24  riastrad 	    0);
    188  1.24  riastrad 	if (error) {
    189  1.24  riastrad 		device_printf(sc->sc_dev, "read failed: %d", error);
    190  1.24  riastrad 		goto out;
    191  1.24  riastrad 	}
    192  1.24  riastrad 	if (nread != sizeof(response)) {
    193  1.24  riastrad 		device_printf(sc->sc_dev, "short header read: %zu", nread);
    194  1.24  riastrad 		goto out;
    195  1.24  riastrad 	}
    196  1.24  riastrad 
    197  1.24  riastrad 	endread = false;
    198  1.24  riastrad 
    199  1.24  riastrad 	error = (*sc->sc_intf->end)(sc, UIO_READ, 0);
    200  1.24  riastrad 	if (error) {
    201  1.24  riastrad 		device_printf(sc->sc_dev, "end read failed: %d", error);
    202  1.24  riastrad 		goto out;
    203  1.24  riastrad 	}
    204  1.24  riastrad 
    205  1.24  riastrad 	/*
    206  1.24  riastrad 	 * Verify the response looks reasonable.
    207  1.24  riastrad 	 */
    208  1.25  riastrad 	if (be16toh(response.tag) != TPM_TAG_RSP_COMMAND ||
    209  1.25  riastrad 	    be32toh(response.length) != sizeof(response) ||
    210  1.25  riastrad 	    be32toh(response.code) != 0) {
    211  1.24  riastrad 		device_printf(sc->sc_dev,
    212  1.24  riastrad 		    "TPM_ORD_SaveState failed: tag=0x%x length=0x%x code=0x%x",
    213  1.25  riastrad 		    be16toh(response.tag),
    214  1.25  riastrad 		    be32toh(response.length),
    215  1.25  riastrad 		    be32toh(response.code));
    216  1.24  riastrad 		error = EIO;
    217  1.24  riastrad 		goto out;
    218  1.24  riastrad 	}
    219  1.24  riastrad 
    220  1.24  riastrad 	/* Success!  */
    221  1.24  riastrad 	error = 0;
    222  1.15      maxv 
    223  1.24  riastrad out:	if (endwrite)
    224  1.24  riastrad 		error = (*sc->sc_intf->end)(sc, UIO_WRITE, error);
    225  1.24  riastrad 	if (endread)
    226  1.24  riastrad 		error = (*sc->sc_intf->end)(sc, UIO_READ, error);
    227  1.24  riastrad 	if (error)
    228  1.15      maxv 		return false;
    229  1.15      maxv 	return true;
    230  1.15      maxv }
    231   1.1  christos 
    232  1.15      maxv static bool
    233  1.15      maxv tpm20_suspend(struct tpm_softc *sc)
    234   1.1  christos {
    235  1.15      maxv 	static const uint8_t command[12] = {
    236  1.15      maxv 		0x80, 0x01,		/* TPM_ST_NO_SESSIONS */
    237  1.15      maxv 		0x00, 0x00, 0x00, 12,	/* Length in bytes */
    238  1.15      maxv 		0x00, 0x00, 0x01, 0x45,	/* TPM_CC_Shutdown */
    239  1.15      maxv 		0x00, 0x01		/* TPM_SU_STATE */
    240   1.1  christos 	};
    241  1.15      maxv 	struct tpm_header response;
    242  1.24  riastrad 	size_t nread;
    243  1.24  riastrad 	bool endwrite = false, endread = false;
    244  1.24  riastrad 	int error;
    245  1.24  riastrad 
    246  1.24  riastrad 	/*
    247  1.24  riastrad 	 * Write the command.
    248  1.24  riastrad 	 */
    249  1.24  riastrad 	error = (*sc->sc_intf->start)(sc, UIO_WRITE);
    250  1.24  riastrad 	if (error) {
    251  1.24  riastrad 		device_printf(sc->sc_dev, "start write failed: %d", error);
    252  1.24  riastrad 		goto out;
    253  1.24  riastrad 	}
    254  1.24  riastrad 
    255  1.24  riastrad 	endwrite = true;
    256  1.24  riastrad 
    257  1.24  riastrad 	error = (*sc->sc_intf->write)(sc, &command, sizeof(command));
    258  1.24  riastrad 	if (error) {
    259  1.24  riastrad 		device_printf(sc->sc_dev, "write TPM_ORD_SaveState failed: %d",
    260  1.24  riastrad 		    error);
    261  1.24  riastrad 		goto out;
    262  1.24  riastrad 	}
    263   1.1  christos 
    264  1.24  riastrad 	endwrite = false;
    265  1.24  riastrad 
    266  1.24  riastrad 	error = (*sc->sc_intf->end)(sc, UIO_WRITE, 0);
    267  1.24  riastrad 	if (error) {
    268  1.24  riastrad 		device_printf(sc->sc_dev, "end write failed: %d", error);
    269  1.24  riastrad 		goto out;
    270  1.24  riastrad 	}
    271  1.24  riastrad 
    272  1.24  riastrad 	/*
    273  1.24  riastrad 	 * Read the response -- just the header; we don't expect a
    274  1.24  riastrad 	 * payload.
    275  1.24  riastrad 	 */
    276  1.24  riastrad 	error = (*sc->sc_intf->start)(sc, UIO_READ);
    277  1.24  riastrad 	if (error) {
    278  1.24  riastrad 		device_printf(sc->sc_dev, "start read failed: %d", error);
    279  1.24  riastrad 		goto out;
    280  1.24  riastrad 	}
    281  1.24  riastrad 
    282  1.24  riastrad 	endread = true;
    283  1.24  riastrad 
    284  1.24  riastrad 	error = (*sc->sc_intf->read)(sc, &response, sizeof(response), &nread,
    285  1.24  riastrad 	    0);
    286  1.24  riastrad 	if (error) {
    287  1.24  riastrad 		device_printf(sc->sc_dev, "read failed: %d", error);
    288  1.24  riastrad 		goto out;
    289  1.24  riastrad 	}
    290  1.24  riastrad 	if (nread != sizeof(response)) {
    291  1.24  riastrad 		device_printf(sc->sc_dev, "short header read: %zu", nread);
    292  1.24  riastrad 		goto out;
    293  1.24  riastrad 	}
    294  1.24  riastrad 
    295  1.24  riastrad 	endread = false;
    296  1.24  riastrad 
    297  1.24  riastrad 	error = (*sc->sc_intf->end)(sc, UIO_READ, 0);
    298  1.24  riastrad 	if (error) {
    299  1.24  riastrad 		device_printf(sc->sc_dev, "end read failed: %d", error);
    300  1.24  riastrad 		goto out;
    301  1.24  riastrad 	}
    302  1.24  riastrad 
    303  1.24  riastrad 	/*
    304  1.24  riastrad 	 * Verify the response looks reasonable.
    305  1.24  riastrad 	 */
    306  1.25  riastrad 	if (be16toh(response.tag) != TPM2_ST_NO_SESSIONS ||
    307  1.25  riastrad 	    be32toh(response.length) != sizeof(response) ||
    308  1.25  riastrad 	    be32toh(response.code) != TPM2_RC_SUCCESS) {
    309  1.24  riastrad 		device_printf(sc->sc_dev,
    310  1.24  riastrad 		    "TPM_CC_Shutdown failed: tag=0x%x length=0x%x code=0x%x",
    311  1.25  riastrad 		    be16toh(response.tag),
    312  1.25  riastrad 		    be32toh(response.length),
    313  1.25  riastrad 		    be32toh(response.code));
    314  1.24  riastrad 		error = EIO;
    315  1.24  riastrad 		goto out;
    316  1.24  riastrad 	}
    317  1.24  riastrad 
    318  1.24  riastrad 	/* Success!  */
    319  1.24  riastrad 	error = 0;
    320  1.24  riastrad 
    321  1.24  riastrad out:	if (endwrite)
    322  1.24  riastrad 		error = (*sc->sc_intf->end)(sc, UIO_WRITE, error);
    323  1.24  riastrad 	if (endread)
    324  1.24  riastrad 		error = (*sc->sc_intf->end)(sc, UIO_READ, error);
    325  1.24  riastrad 	if (error)
    326  1.15      maxv 		return false;
    327   1.7  christos 	return true;
    328   1.1  christos }
    329   1.1  christos 
    330   1.1  christos bool
    331  1.15      maxv tpm_suspend(device_t dev, const pmf_qual_t *qual)
    332   1.1  christos {
    333  1.15      maxv 	struct tpm_softc *sc = device_private(dev);
    334  1.15      maxv 
    335  1.15      maxv 	switch (sc->sc_ver) {
    336  1.15      maxv 	case TPM_1_2:
    337  1.15      maxv 		return tpm12_suspend(sc);
    338  1.15      maxv 	case TPM_2_0:
    339  1.15      maxv 		return tpm20_suspend(sc);
    340  1.15      maxv 	default:
    341  1.15      maxv 		panic("%s: impossible", __func__);
    342  1.15      maxv 	}
    343  1.15      maxv }
    344  1.15      maxv 
    345  1.15      maxv bool
    346  1.15      maxv tpm_resume(device_t dev, const pmf_qual_t *qual)
    347  1.15      maxv {
    348  1.15      maxv 	/*
    349  1.15      maxv 	 * Don't do anything, the BIOS is supposed to restore the previously
    350  1.15      maxv 	 * saved state.
    351  1.15      maxv 	 */
    352   1.7  christos 	return true;
    353   1.1  christos }
    354   1.1  christos 
    355  1.13      maxv /* -------------------------------------------------------------------------- */
    356  1.13      maxv 
    357  1.13      maxv static int
    358  1.14      maxv tpm_poll(struct tpm_softc *sc, uint8_t mask, int to, wchan_t chan)
    359   1.1  christos {
    360   1.1  christos 	int rv;
    361   1.1  christos 
    362  1.13      maxv 	while (((sc->sc_status = tpm_status(sc)) & mask) != mask && to--) {
    363  1.28  riastrad 		rv = kpause("tpm_poll", /*intr*/true, /*timo*/1, /*lock*/NULL);
    364   1.1  christos 		if (rv && rv != EWOULDBLOCK) {
    365   1.1  christos 			return rv;
    366   1.1  christos 		}
    367   1.1  christos 	}
    368   1.1  christos 
    369   1.1  christos 	return 0;
    370   1.1  christos }
    371   1.1  christos 
    372  1.13      maxv static int
    373  1.13      maxv tpm_waitfor(struct tpm_softc *sc, uint8_t bits, int tmo, wchan_t chan)
    374   1.1  christos {
    375  1.13      maxv 	int retry, to, rv;
    376  1.13      maxv 	uint8_t todo;
    377   1.1  christos 
    378  1.14      maxv 	to = tpm_tmotohz(tmo);
    379  1.13      maxv 	retry = 3;
    380  1.13      maxv 
    381   1.1  christos restart:
    382  1.14      maxv 	todo = bits;
    383  1.14      maxv 
    384   1.1  christos 	/*
    385  1.14      maxv 	 * TPM_STS_VALID has priority over the others.
    386   1.1  christos 	 */
    387  1.14      maxv 	if (todo & TPM_STS_VALID) {
    388  1.14      maxv 		if ((rv = tpm_poll(sc, TPM_STS_VALID, to+1, chan)) != 0)
    389  1.14      maxv 			return rv;
    390  1.14      maxv 		todo &= ~TPM_STS_VALID;
    391  1.14      maxv 	}
    392  1.14      maxv 
    393  1.14      maxv 	if ((rv = tpm_poll(sc, todo, to, chan)) != 0)
    394   1.1  christos 		return rv;
    395   1.1  christos 
    396  1.13      maxv 	if ((todo & sc->sc_status) != todo) {
    397  1.14      maxv 		if ((retry-- > 0) && (bits & TPM_STS_VALID)) {
    398   1.1  christos 			bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
    399   1.1  christos 			    TPM_STS_RESP_RETRY);
    400   1.1  christos 			goto restart;
    401   1.1  christos 		}
    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.13      maxv /* -------------------------------------------------------------------------- */
    409  1.13      maxv 
    410  1.13      maxv /*
    411  1.16      maxv  * TPM using the TIS 1.2 interface.
    412  1.13      maxv  */
    413  1.13      maxv 
    414  1.16      maxv static int
    415  1.16      maxv tpm12_request_locality(struct tpm_softc *sc, int l)
    416  1.16      maxv {
    417  1.16      maxv 	uint32_t r;
    418  1.16      maxv 	int to, rv;
    419  1.16      maxv 
    420  1.16      maxv 	if (l != 0)
    421  1.16      maxv 		return EINVAL;
    422  1.16      maxv 
    423  1.16      maxv 	if ((bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
    424  1.16      maxv 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) ==
    425  1.16      maxv 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY))
    426  1.16      maxv 		return 0;
    427  1.16      maxv 
    428  1.16      maxv 	bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
    429  1.16      maxv 	    TPM_ACCESS_REQUEST_USE);
    430  1.16      maxv 
    431  1.16      maxv 	to = tpm_tmotohz(TPM_ACCESS_TMO);
    432  1.16      maxv 
    433  1.16      maxv 	while ((r = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS) &
    434  1.16      maxv 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
    435  1.16      maxv 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && to--) {
    436  1.28  riastrad 		rv = kpause("tpm_locality", /*intr*/true, /*timo*/1,
    437  1.28  riastrad 		    /*lock*/NULL);
    438  1.16      maxv 		if (rv && rv != EWOULDBLOCK) {
    439  1.16      maxv 			return rv;
    440  1.16      maxv 		}
    441  1.16      maxv 	}
    442  1.16      maxv 
    443  1.16      maxv 	if ((r & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
    444  1.16      maxv 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
    445  1.16      maxv 		return EBUSY;
    446  1.16      maxv 	}
    447  1.16      maxv 
    448  1.16      maxv 	return 0;
    449  1.16      maxv }
    450  1.16      maxv 
    451  1.16      maxv static int
    452  1.13      maxv tpm_tis12_probe(bus_space_tag_t bt, bus_space_handle_t bh)
    453  1.13      maxv {
    454  1.13      maxv 	uint32_t cap;
    455  1.13      maxv 	uint8_t reg;
    456  1.13      maxv 	int tmo;
    457  1.13      maxv 
    458  1.13      maxv 	cap = bus_space_read_4(bt, bh, TPM_INTF_CAPABILITY);
    459  1.13      maxv 	if (cap == 0xffffffff)
    460  1.16      maxv 		return EINVAL;
    461  1.13      maxv 	if ((cap & TPM_CAPS_REQUIRED) != TPM_CAPS_REQUIRED)
    462  1.16      maxv 		return ENOTSUP;
    463  1.13      maxv 
    464  1.13      maxv 	/* Request locality 0. */
    465  1.13      maxv 	bus_space_write_1(bt, bh, TPM_ACCESS, TPM_ACCESS_REQUEST_USE);
    466  1.13      maxv 
    467  1.13      maxv 	/* Wait for it to become active. */
    468  1.13      maxv 	tmo = TPM_ACCESS_TMO; /* Milliseconds. */
    469  1.13      maxv 	while ((reg = bus_space_read_1(bt, bh, TPM_ACCESS) &
    470  1.13      maxv 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
    471  1.13      maxv 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY) && tmo--) {
    472  1.13      maxv 		DELAY(1000); /* 1 millisecond. */
    473  1.13      maxv 	}
    474  1.13      maxv 	if ((reg & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) !=
    475  1.13      maxv 	    (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY)) {
    476  1.16      maxv 		return ETIMEDOUT;
    477  1.13      maxv 	}
    478  1.13      maxv 
    479  1.13      maxv 	if (bus_space_read_4(bt, bh, TPM_ID) == 0xffffffff)
    480  1.16      maxv 		return EINVAL;
    481  1.13      maxv 
    482  1.16      maxv 	return 0;
    483  1.13      maxv }
    484  1.13      maxv 
    485  1.21  riastrad static int
    486  1.21  riastrad tpm12_rng(struct tpm_softc *sc, unsigned *entropybitsp)
    487  1.19  riastrad {
    488  1.19  riastrad 	/*
    489  1.19  riastrad 	 * TPM Specification Version 1.2, Main Part 3: Commands,
    490  1.19  riastrad 	 * Sec. 13.6 TPM_GetRandom
    491  1.19  riastrad 	 */
    492  1.19  riastrad 	struct {
    493  1.19  riastrad 		struct tpm_header hdr;
    494  1.19  riastrad 		uint32_t bytesRequested;
    495  1.19  riastrad 	} __packed command;
    496  1.19  riastrad 	struct response {
    497  1.19  riastrad 		struct tpm_header hdr;
    498  1.19  riastrad 		uint32_t randomBytesSize;
    499  1.19  riastrad 		uint8_t	bytes[64];
    500  1.19  riastrad 	} __packed response;
    501  1.21  riastrad 	bool endwrite = false, endread = false;
    502  1.19  riastrad 	size_t nread;
    503  1.19  riastrad 	uint16_t tag;
    504  1.21  riastrad 	uint32_t pktlen, code, nbytes, entropybits = 0;
    505  1.19  riastrad 	int rv;
    506  1.19  riastrad 
    507  1.19  riastrad 	/* Encode the command.  */
    508  1.19  riastrad 	memset(&command, 0, sizeof(command));
    509  1.19  riastrad 	command.hdr.tag = htobe16(TPM_TAG_RQU_COMMAND);
    510  1.19  riastrad 	command.hdr.length = htobe32(sizeof(command));
    511  1.19  riastrad 	command.hdr.code = htobe32(TPM_ORD_GetRandom);
    512  1.19  riastrad 	command.bytesRequested = htobe32(sizeof(response.bytes));
    513  1.19  riastrad 
    514  1.19  riastrad 	/* Write the command.   */
    515  1.19  riastrad 	if ((rv = (*sc->sc_intf->start)(sc, UIO_WRITE)) != 0) {
    516  1.19  riastrad 		device_printf(sc->sc_dev, "start write failed, error=%d\n",
    517  1.19  riastrad 		    rv);
    518  1.19  riastrad 		goto out;
    519  1.19  riastrad 	}
    520  1.19  riastrad 	endwrite = true;
    521  1.19  riastrad 	if ((rv = (*sc->sc_intf->write)(sc, &command, sizeof(command))) != 0) {
    522  1.19  riastrad 		device_printf(sc->sc_dev, "write failed, error=%d\n", rv);
    523  1.19  riastrad 		goto out;
    524  1.19  riastrad 	}
    525  1.19  riastrad 	rv = (*sc->sc_intf->end)(sc, UIO_WRITE, 0);
    526  1.19  riastrad 	endwrite = false;
    527  1.19  riastrad 	if (rv) {
    528  1.19  riastrad 		device_printf(sc->sc_dev, "end write failed, error=%d\n", rv);
    529  1.19  riastrad 		goto out;
    530  1.19  riastrad 	}
    531  1.19  riastrad 
    532  1.19  riastrad 	/* Read the response header.  */
    533  1.19  riastrad 	if ((rv = (*sc->sc_intf->start)(sc, UIO_READ)) != 0) {
    534  1.19  riastrad 		device_printf(sc->sc_dev, "start write failed, error=%d\n",
    535  1.19  riastrad 		    rv);
    536  1.19  riastrad 		goto out;
    537  1.19  riastrad 	}
    538  1.19  riastrad 	endread = true;
    539  1.19  riastrad 	if ((rv = (*sc->sc_intf->read)(sc, &response.hdr, sizeof(response.hdr),
    540  1.19  riastrad 		    &nread, 0)) != 0) {
    541  1.19  riastrad 		device_printf(sc->sc_dev, "read failed, error=%d\n", rv);
    542  1.19  riastrad 		goto out;
    543  1.19  riastrad 	}
    544  1.19  riastrad 
    545  1.19  riastrad 	/* Verify the response header looks sensible.  */
    546  1.19  riastrad 	if (nread != sizeof(response.hdr)) {
    547  1.26   khorben 		device_printf(sc->sc_dev, "read %zu bytes, expected %zu\n",
    548  1.19  riastrad 		    nread, sizeof(response.hdr));
    549  1.19  riastrad 		goto out;
    550  1.19  riastrad 	}
    551  1.19  riastrad 	tag = be16toh(response.hdr.tag);
    552  1.19  riastrad 	pktlen = be32toh(response.hdr.length);
    553  1.19  riastrad 	code = be32toh(response.hdr.code);
    554  1.19  riastrad 	if (tag != TPM_TAG_RSP_COMMAND ||
    555  1.19  riastrad 	    pktlen < offsetof(struct response, bytes) ||
    556  1.19  riastrad 	    pktlen > sizeof(response) ||
    557  1.19  riastrad 	    code != 0) {
    558  1.19  riastrad 		/*
    559  1.19  riastrad 		 * If the tpm itself is busy (e.g., it has yet to run a
    560  1.19  riastrad 		 * self-test, or it's in a timeout period to defend
    561  1.19  riastrad 		 * against brute force attacks), then we can try again
    562  1.19  riastrad 		 * later.  Otherwise, give up.
    563  1.19  riastrad 		 */
    564  1.19  riastrad 		if (code & TPM_NON_FATAL) {
    565  1.19  riastrad 			aprint_debug_dev(sc->sc_dev, "%s: tpm busy, code=%u\n",
    566  1.19  riastrad 			    __func__, code & ~TPM_NON_FATAL);
    567  1.19  riastrad 			rv = 0;
    568  1.19  riastrad 		} else if (code == TPM_DEACTIVATED) {
    569  1.19  riastrad 			device_printf(sc->sc_dev, "tpm is deactivated\n");
    570  1.19  riastrad 			rv = ENXIO;
    571  1.19  riastrad 		} else {
    572  1.19  riastrad 			device_printf(sc->sc_dev, "bad tpm response:"
    573  1.19  riastrad 			    " tag=%u len=%u code=%u\n", tag, pktlen, code);
    574  1.19  riastrad 			hexdump(aprint_debug, "tpm response header",
    575  1.19  riastrad 			    (const void *)&response.hdr,
    576  1.19  riastrad 			    sizeof(response.hdr));
    577  1.19  riastrad 			rv = EIO;
    578  1.19  riastrad 		}
    579  1.19  riastrad 		goto out;
    580  1.19  riastrad 	}
    581  1.19  riastrad 
    582  1.19  riastrad 	/* Read the response payload.  */
    583  1.19  riastrad 	if ((rv = (*sc->sc_intf->read)(sc,
    584  1.19  riastrad 		    (char *)&response + nread, pktlen - nread,
    585  1.19  riastrad 		    NULL, TPM_PARAM_SIZE)) != 0) {
    586  1.19  riastrad 		device_printf(sc->sc_dev, "read failed, error=%d\n", rv);
    587  1.19  riastrad 		goto out;
    588  1.19  riastrad 	}
    589  1.19  riastrad 	endread = false;
    590  1.19  riastrad 	if ((rv = (*sc->sc_intf->end)(sc, UIO_READ, 0)) != 0) {
    591  1.19  riastrad 		device_printf(sc->sc_dev, "end read failed, error=%d\n", rv);
    592  1.19  riastrad 		goto out;
    593  1.19  riastrad 	}
    594  1.19  riastrad 
    595  1.19  riastrad 	/* Verify the number of bytes read looks sensible.  */
    596  1.19  riastrad 	nbytes = be32toh(response.randomBytesSize);
    597  1.19  riastrad 	if (nbytes > pktlen - offsetof(struct response, bytes)) {
    598  1.19  riastrad 		device_printf(sc->sc_dev, "overlong GetRandom length:"
    599  1.19  riastrad 		    " %u, max %zu\n",
    600  1.19  riastrad 		    nbytes, pktlen - offsetof(struct response, bytes));
    601  1.19  riastrad 		nbytes = pktlen - offsetof(struct response, bytes);
    602  1.19  riastrad 	}
    603  1.19  riastrad 
    604  1.19  riastrad 	/*
    605  1.19  riastrad 	 * Enter the data into the entropy pool.  Conservatively (or,
    606  1.19  riastrad 	 * perhaps, cargocultily) estimate half a bit of entropy per
    607  1.19  riastrad 	 * bit of data.
    608  1.19  riastrad 	 */
    609  1.21  riastrad 	CTASSERT(sizeof(response.bytes) <= UINT_MAX/(NBBY/2));
    610  1.21  riastrad 	entropybits = (NBBY/2)*nbytes;
    611  1.21  riastrad 	rnd_add_data(&sc->sc_rnd, response.bytes, nbytes, entropybits);
    612  1.21  riastrad 
    613  1.21  riastrad out:	/* End the read or write if still ongoing.  */
    614  1.21  riastrad 	if (endread)
    615  1.21  riastrad 		rv = (*sc->sc_intf->end)(sc, UIO_READ, rv);
    616  1.21  riastrad 	if (endwrite)
    617  1.21  riastrad 		rv = (*sc->sc_intf->end)(sc, UIO_WRITE, rv);
    618  1.21  riastrad 
    619  1.21  riastrad 	*entropybitsp = entropybits;
    620  1.21  riastrad 	return rv;
    621  1.21  riastrad }
    622  1.21  riastrad 
    623  1.21  riastrad static int
    624  1.21  riastrad tpm20_rng(struct tpm_softc *sc, unsigned *entropybitsp)
    625  1.21  riastrad {
    626  1.21  riastrad 	/*
    627  1.21  riastrad 	 * Trusted Platform Module Library, Family "2.0", Level 00
    628  1.21  riastrad 	 * Revision 01.38, Part 3: Commands, Sec. 16.1 `TPM2_GetRandom'
    629  1.21  riastrad 	 *
    630  1.21  riastrad 	 * https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-3-Commands-01.38.pdf#page=133
    631  1.21  riastrad 	 */
    632  1.21  riastrad 	struct {
    633  1.21  riastrad 		struct tpm_header hdr;
    634  1.21  riastrad 		uint16_t bytesRequested;
    635  1.21  riastrad 	} __packed command;
    636  1.21  riastrad 	struct response {
    637  1.21  riastrad 		struct tpm_header hdr;
    638  1.21  riastrad 		uint16_t randomBytesSize;
    639  1.21  riastrad 		uint8_t bytes[64];
    640  1.21  riastrad 	} __packed response;
    641  1.21  riastrad 	bool endwrite = false, endread = false;
    642  1.21  riastrad 	size_t nread;
    643  1.21  riastrad 	uint16_t tag;
    644  1.21  riastrad 	uint32_t pktlen, code, nbytes, entropybits = 0;
    645  1.21  riastrad 	int rv;
    646  1.21  riastrad 
    647  1.21  riastrad 	/* Encode the command.  */
    648  1.21  riastrad 	memset(&command, 0, sizeof(command));
    649  1.21  riastrad 	command.hdr.tag = htobe16(TPM2_ST_NO_SESSIONS);
    650  1.21  riastrad 	command.hdr.length = htobe32(sizeof(command));
    651  1.21  riastrad 	command.hdr.code = htobe32(TPM2_CC_GetRandom);
    652  1.21  riastrad 	command.bytesRequested = htobe16(sizeof(response.bytes));
    653  1.21  riastrad 
    654  1.21  riastrad 	/* Write the command.   */
    655  1.21  riastrad 	if ((rv = (*sc->sc_intf->start)(sc, UIO_WRITE)) != 0) {
    656  1.21  riastrad 		device_printf(sc->sc_dev, "start write failed, error=%d\n",
    657  1.21  riastrad 		    rv);
    658  1.21  riastrad 		goto out;
    659  1.21  riastrad 	}
    660  1.21  riastrad 	endwrite = true;
    661  1.21  riastrad 	if ((rv = (*sc->sc_intf->write)(sc, &command, sizeof(command))) != 0) {
    662  1.21  riastrad 		device_printf(sc->sc_dev, "write failed, error=%d\n", rv);
    663  1.21  riastrad 		goto out;
    664  1.21  riastrad 	}
    665  1.21  riastrad 	rv = (*sc->sc_intf->end)(sc, UIO_WRITE, 0);
    666  1.21  riastrad 	endwrite = false;
    667  1.21  riastrad 	if (rv) {
    668  1.21  riastrad 		device_printf(sc->sc_dev, "end write failed, error=%d\n", rv);
    669  1.21  riastrad 		goto out;
    670  1.21  riastrad 	}
    671  1.21  riastrad 
    672  1.21  riastrad 	/* Read the response header.  */
    673  1.21  riastrad 	if ((rv = (*sc->sc_intf->start)(sc, UIO_READ)) != 0) {
    674  1.21  riastrad 		device_printf(sc->sc_dev, "start write failed, error=%d\n",
    675  1.21  riastrad 		    rv);
    676  1.21  riastrad 		goto out;
    677  1.21  riastrad 	}
    678  1.21  riastrad 	endread = true;
    679  1.21  riastrad 	if ((rv = (*sc->sc_intf->read)(sc, &response.hdr, sizeof(response.hdr),
    680  1.21  riastrad 		    &nread, 0)) != 0) {
    681  1.21  riastrad 		device_printf(sc->sc_dev, "read failed, error=%d\n", rv);
    682  1.21  riastrad 		goto out;
    683  1.21  riastrad 	}
    684  1.21  riastrad 
    685  1.21  riastrad 	/* Verify the response header looks sensible.  */
    686  1.21  riastrad 	if (nread != sizeof(response.hdr)) {
    687  1.21  riastrad 		device_printf(sc->sc_dev, "read %zu bytes, expected %zu",
    688  1.21  riastrad 		    nread, sizeof(response.hdr));
    689  1.21  riastrad 		goto out;
    690  1.21  riastrad 	}
    691  1.21  riastrad 	tag = be16toh(response.hdr.tag);
    692  1.21  riastrad 	pktlen = be32toh(response.hdr.length);
    693  1.21  riastrad 	code = be32toh(response.hdr.code);
    694  1.21  riastrad 	if (tag != TPM2_ST_NO_SESSIONS ||
    695  1.21  riastrad 	    pktlen < offsetof(struct response, bytes) ||
    696  1.21  riastrad 	    pktlen > sizeof(response) ||
    697  1.21  riastrad 	    code != 0) {
    698  1.21  riastrad 		/*
    699  1.21  riastrad 		 * If the tpm itself is busy (e.g., it has yet to run a
    700  1.21  riastrad 		 * self-test, or it's in a timeout period to defend
    701  1.21  riastrad 		 * against brute force attacks), then we can try again
    702  1.21  riastrad 		 * later.  Otherwise, give up.
    703  1.21  riastrad 		 */
    704  1.21  riastrad 		if (code & TPM2_RC_WARN) {
    705  1.21  riastrad 			aprint_debug_dev(sc->sc_dev, "%s: tpm busy,"
    706  1.21  riastrad 			    " code=TPM_RC_WARN+0x%x\n",
    707  1.21  riastrad 			    __func__, code & ~TPM2_RC_WARN);
    708  1.21  riastrad 			rv = 0;
    709  1.21  riastrad 		} else {
    710  1.21  riastrad 			device_printf(sc->sc_dev, "bad tpm response:"
    711  1.21  riastrad 			    " tag=%u len=%u code=0x%x\n", tag, pktlen, code);
    712  1.21  riastrad 			hexdump(aprint_debug, "tpm response header",
    713  1.21  riastrad 			    (const void *)&response.hdr,
    714  1.21  riastrad 			    sizeof(response.hdr));
    715  1.21  riastrad 			rv = EIO;
    716  1.21  riastrad 		}
    717  1.21  riastrad 		goto out;
    718  1.21  riastrad 	}
    719  1.21  riastrad 
    720  1.21  riastrad 	/* Read the response payload.  */
    721  1.21  riastrad 	if ((rv = (*sc->sc_intf->read)(sc,
    722  1.21  riastrad 		    (char *)&response + nread, pktlen - nread,
    723  1.21  riastrad 		    NULL, TPM_PARAM_SIZE)) != 0) {
    724  1.21  riastrad 		device_printf(sc->sc_dev, "read failed, error=%d\n", rv);
    725  1.21  riastrad 		goto out;
    726  1.21  riastrad 	}
    727  1.21  riastrad 	endread = false;
    728  1.21  riastrad 	if ((rv = (*sc->sc_intf->end)(sc, UIO_READ, 0)) != 0) {
    729  1.21  riastrad 		device_printf(sc->sc_dev, "end read failed, error=%d\n", rv);
    730  1.21  riastrad 		goto out;
    731  1.21  riastrad 	}
    732  1.21  riastrad 
    733  1.21  riastrad 	/* Verify the number of bytes read looks sensible.  */
    734  1.21  riastrad 	nbytes = be16toh(response.randomBytesSize);
    735  1.21  riastrad 	if (nbytes > pktlen - offsetof(struct response, bytes)) {
    736  1.21  riastrad 		device_printf(sc->sc_dev, "overlong GetRandom length:"
    737  1.21  riastrad 		    " %u, max %zu\n",
    738  1.21  riastrad 		    nbytes, pktlen - offsetof(struct response, bytes));
    739  1.21  riastrad 		nbytes = pktlen - offsetof(struct response, bytes);
    740  1.21  riastrad 	}
    741  1.21  riastrad 
    742  1.21  riastrad 	/*
    743  1.21  riastrad 	 * Enter the data into the entropy pool.  Conservatively (or,
    744  1.21  riastrad 	 * perhaps, cargocultily) estimate half a bit of entropy per
    745  1.21  riastrad 	 * bit of data.
    746  1.21  riastrad 	 */
    747  1.21  riastrad 	CTASSERT(sizeof(response.bytes) <= UINT_MAX/(NBBY/2));
    748  1.21  riastrad 	entropybits = (NBBY/2)*nbytes;
    749  1.21  riastrad 	rnd_add_data(&sc->sc_rnd, response.bytes, nbytes, entropybits);
    750  1.21  riastrad 
    751  1.21  riastrad out:	/* End the read or write if still ongoing.  */
    752  1.21  riastrad 	if (endread)
    753  1.21  riastrad 		rv = (*sc->sc_intf->end)(sc, UIO_READ, rv);
    754  1.21  riastrad 	if (endwrite)
    755  1.21  riastrad 		rv = (*sc->sc_intf->end)(sc, UIO_WRITE, rv);
    756  1.21  riastrad 
    757  1.21  riastrad 	*entropybitsp = entropybits;
    758  1.21  riastrad 	return rv;
    759  1.21  riastrad }
    760  1.21  riastrad 
    761  1.21  riastrad static void
    762  1.21  riastrad tpm_rng_work(struct work *wk, void *cookie)
    763  1.21  riastrad {
    764  1.21  riastrad 	struct tpm_softc *sc = cookie;
    765  1.21  riastrad 	unsigned nbytes, entropybits;
    766  1.21  riastrad 	int rv;
    767  1.21  riastrad 
    768  1.21  riastrad 	/* Acknowledge the request.  */
    769  1.21  riastrad 	nbytes = atomic_swap_uint(&sc->sc_rndpending, 0);
    770  1.21  riastrad 
    771  1.24  riastrad 	/* Lock the tpm while we do I/O transactions with it.  */
    772  1.21  riastrad 	mutex_enter(&sc->sc_lock);
    773  1.21  riastrad 
    774  1.21  riastrad 	/*
    775  1.21  riastrad 	 * Issue as many commands as needed to fulfill the request, but
    776  1.21  riastrad 	 * stop if anything fails.
    777  1.21  riastrad 	 */
    778  1.21  riastrad 	for (; nbytes; nbytes -= MIN(nbytes, MAX(1, entropybits/NBBY))) {
    779  1.21  riastrad 		switch (sc->sc_ver) {
    780  1.21  riastrad 		case TPM_1_2:
    781  1.21  riastrad 			rv = tpm12_rng(sc, &entropybits);
    782  1.21  riastrad 			break;
    783  1.21  riastrad 		case TPM_2_0:
    784  1.21  riastrad 			rv = tpm20_rng(sc, &entropybits);
    785  1.21  riastrad 			break;
    786  1.21  riastrad 		default:
    787  1.21  riastrad 			panic("bad tpm version: %d", sc->sc_ver);
    788  1.21  riastrad 		}
    789  1.21  riastrad 		if (rv)
    790  1.21  riastrad 			break;
    791  1.21  riastrad 	}
    792  1.19  riastrad 
    793  1.21  riastrad 	/*
    794  1.19  riastrad 	 * If the tpm is busted, no sense in trying again -- most
    795  1.19  riastrad 	 * likely, it is deactivated, and by the spec it cannot be
    796  1.19  riastrad 	 * reactivated until after a reboot.
    797  1.19  riastrad 	 */
    798  1.19  riastrad 	if (rv) {
    799  1.19  riastrad 		device_printf(sc->sc_dev, "deactivating entropy source\n");
    800  1.23  riastrad 		atomic_store_relaxed(&sc->sc_rnddisabled, true);
    801  1.19  riastrad 		/* XXX worker thread can't workqueue_destroy its own queue */
    802  1.19  riastrad 	}
    803  1.19  riastrad 
    804  1.24  riastrad 	/* Relinquish the tpm.  */
    805  1.19  riastrad 	mutex_exit(&sc->sc_lock);
    806  1.19  riastrad }
    807  1.19  riastrad 
    808  1.19  riastrad static void
    809  1.21  riastrad tpm_rng_get(size_t nbytes, void *cookie)
    810  1.19  riastrad {
    811  1.19  riastrad 	struct tpm_softc *sc = cookie;
    812  1.19  riastrad 
    813  1.23  riastrad 	if (atomic_load_relaxed(&sc->sc_rnddisabled))
    814  1.23  riastrad 		return;		/* tough */
    815  1.21  riastrad 	if (atomic_swap_uint(&sc->sc_rndpending, MIN(nbytes, UINT_MAX/NBBY))
    816  1.21  riastrad 	    == 0)
    817  1.19  riastrad 		workqueue_enqueue(sc->sc_rndwq, &sc->sc_rndwk, NULL);
    818  1.19  riastrad }
    819  1.19  riastrad 
    820  1.16      maxv static int
    821  1.14      maxv tpm_tis12_init(struct tpm_softc *sc)
    822  1.13      maxv {
    823  1.16      maxv 	int rv;
    824  1.16      maxv 
    825  1.14      maxv 	sc->sc_caps = bus_space_read_4(sc->sc_bt, sc->sc_bh,
    826  1.13      maxv 	    TPM_INTF_CAPABILITY);
    827  1.13      maxv 	sc->sc_devid = bus_space_read_4(sc->sc_bt, sc->sc_bh, TPM_ID);
    828  1.13      maxv 	sc->sc_rev = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_REV);
    829  1.13      maxv 
    830  1.14      maxv 	aprint_normal_dev(sc->sc_dev, "device 0x%08x rev 0x%x\n",
    831  1.14      maxv 	    sc->sc_devid, sc->sc_rev);
    832  1.13      maxv 
    833  1.16      maxv 	if ((rv = tpm12_request_locality(sc, 0)) != 0)
    834  1.16      maxv 		return rv;
    835  1.13      maxv 
    836  1.13      maxv 	/* Abort whatever it thought it was doing. */
    837  1.13      maxv 	bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
    838  1.13      maxv 
    839  1.19  riastrad 	/* XXX Run this at higher priority?  */
    840  1.19  riastrad 	if ((rv = workqueue_create(&sc->sc_rndwq, device_xname(sc->sc_dev),
    841  1.21  riastrad 		    tpm_rng_work, sc, PRI_NONE, IPL_VM, WQ_MPSAFE)) != 0)
    842  1.19  riastrad 		return rv;
    843  1.21  riastrad 	rndsource_setcb(&sc->sc_rnd, tpm_rng_get, sc);
    844  1.19  riastrad 	rnd_attach_source(&sc->sc_rnd, device_xname(sc->sc_dev),
    845  1.19  riastrad 	    RND_TYPE_RNG,
    846  1.19  riastrad 	    RND_FLAG_COLLECT_VALUE|RND_FLAG_ESTIMATE_VALUE|RND_FLAG_HASCB);
    847  1.19  riastrad 
    848  1.13      maxv 	return 0;
    849  1.13      maxv }
    850  1.13      maxv 
    851  1.16      maxv static int
    852  1.14      maxv tpm_tis12_start(struct tpm_softc *sc, int rw)
    853   1.1  christos {
    854   1.1  christos 	int rv;
    855   1.1  christos 
    856  1.14      maxv 	if (rw == UIO_READ) {
    857   1.1  christos 		rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
    858  1.16      maxv 		    TPM_READ_TMO, sc->sc_intf->read);
    859   1.1  christos 		return rv;
    860   1.1  christos 	}
    861   1.1  christos 
    862  1.13      maxv 	/* Request the 0th locality. */
    863  1.16      maxv 	if ((rv = tpm12_request_locality(sc, 0)) != 0)
    864   1.1  christos 		return rv;
    865   1.1  christos 
    866  1.13      maxv 	sc->sc_status = tpm_status(sc);
    867  1.13      maxv 	if (sc->sc_status & TPM_STS_CMD_READY)
    868   1.1  christos 		return 0;
    869   1.1  christos 
    870   1.1  christos 	/* Abort previous and restart. */
    871   1.1  christos 	bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS, TPM_STS_CMD_READY);
    872  1.16      maxv 	rv = tpm_waitfor(sc, TPM_STS_CMD_READY, TPM_READY_TMO, sc->sc_intf->write);
    873  1.13      maxv 	if (rv)
    874   1.1  christos 		return rv;
    875   1.1  christos 
    876   1.1  christos 	return 0;
    877   1.1  christos }
    878   1.1  christos 
    879  1.16      maxv static int
    880   1.3  christos tpm_tis12_read(struct tpm_softc *sc, void *buf, size_t len, size_t *count,
    881   1.1  christos     int flags)
    882   1.1  christos {
    883   1.1  christos 	uint8_t *p = buf;
    884   1.1  christos 	size_t cnt;
    885  1.14      maxv 	int rv, n;
    886   1.1  christos 
    887   1.1  christos 	cnt = 0;
    888   1.1  christos 	while (len > 0) {
    889  1.13      maxv 		rv = tpm_waitfor(sc, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
    890  1.16      maxv 		    TPM_READ_TMO, sc->sc_intf->read);
    891  1.13      maxv 		if (rv)
    892   1.1  christos 			return rv;
    893   1.1  christos 
    894  1.14      maxv 		n = MIN(len, tpm_getburst(sc));
    895  1.14      maxv 		while (n > 0) {
    896   1.1  christos 			*p++ = bus_space_read_1(sc->sc_bt, sc->sc_bh, TPM_DATA);
    897   1.1  christos 			cnt++;
    898  1.14      maxv 			len--;
    899  1.14      maxv 			n--;
    900   1.1  christos 		}
    901   1.1  christos 
    902   1.1  christos 		if ((flags & TPM_PARAM_SIZE) == 0 && cnt >= 6)
    903   1.1  christos 			break;
    904   1.1  christos 	}
    905   1.1  christos 
    906   1.1  christos 	if (count)
    907   1.1  christos 		*count = cnt;
    908   1.1  christos 
    909   1.1  christos 	return 0;
    910   1.1  christos }
    911   1.1  christos 
    912  1.16      maxv static int
    913   1.3  christos tpm_tis12_write(struct tpm_softc *sc, const void *buf, size_t len)
    914   1.1  christos {
    915   1.3  christos 	const uint8_t *p = buf;
    916   1.1  christos 	size_t cnt;
    917   1.1  christos 	int rv, r;
    918   1.1  christos 
    919   1.3  christos 	if (len == 0)
    920   1.3  christos 		return 0;
    921  1.16      maxv 	if ((rv = tpm12_request_locality(sc, 0)) != 0)
    922   1.1  christos 		return rv;
    923   1.1  christos 
    924   1.1  christos 	cnt = 0;
    925   1.1  christos 	while (cnt < len - 1) {
    926   1.1  christos 		for (r = tpm_getburst(sc); r > 0 && cnt < len - 1; r--) {
    927   1.1  christos 			bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_DATA, *p++);
    928   1.1  christos 			cnt++;
    929   1.1  christos 		}
    930   1.1  christos 		if ((rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc))) {
    931   1.1  christos 			return rv;
    932   1.1  christos 		}
    933  1.13      maxv 		sc->sc_status = tpm_status(sc);
    934  1.13      maxv 		if (!(sc->sc_status & TPM_STS_DATA_EXPECT)) {
    935   1.1  christos 			return EIO;
    936   1.1  christos 		}
    937   1.1  christos 	}
    938   1.1  christos 
    939   1.1  christos 	bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_DATA, *p++);
    940   1.1  christos 	cnt++;
    941   1.1  christos 
    942   1.1  christos 	if ((rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc))) {
    943   1.1  christos 		return rv;
    944   1.1  christos 	}
    945  1.13      maxv 	if ((sc->sc_status & TPM_STS_DATA_EXPECT) != 0) {
    946   1.1  christos 		return EIO;
    947   1.1  christos 	}
    948   1.1  christos 
    949   1.1  christos 	return 0;
    950   1.1  christos }
    951   1.1  christos 
    952  1.16      maxv static int
    953  1.14      maxv tpm_tis12_end(struct tpm_softc *sc, int rw, int err)
    954   1.1  christos {
    955   1.1  christos 	int rv = 0;
    956   1.1  christos 
    957  1.14      maxv 	if (rw == UIO_READ) {
    958  1.16      maxv 		rv = tpm_waitfor(sc, TPM_STS_VALID, TPM_READ_TMO, sc->sc_intf->read);
    959  1.13      maxv 		if (rv)
    960  1.22  riastrad 			goto out;
    961   1.1  christos 
    962   1.1  christos 		/* Still more data? */
    963  1.13      maxv 		sc->sc_status = tpm_status(sc);
    964  1.14      maxv 		if (!err && (sc->sc_status & TPM_STS_DATA_AVAIL)) {
    965   1.1  christos 			rv = EIO;
    966   1.1  christos 		}
    967   1.1  christos 
    968   1.1  christos 		bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
    969   1.1  christos 		    TPM_STS_CMD_READY);
    970   1.1  christos 
    971  1.13      maxv 		/* Release the 0th locality. */
    972  1.13      maxv 		bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_ACCESS,
    973   1.1  christos 		    TPM_ACCESS_ACTIVE_LOCALITY);
    974   1.1  christos 	} else {
    975   1.1  christos 		/* Hungry for more? */
    976  1.13      maxv 		sc->sc_status = tpm_status(sc);
    977  1.13      maxv 		if (!err && (sc->sc_status & TPM_STS_DATA_EXPECT)) {
    978   1.1  christos 			rv = EIO;
    979   1.1  christos 		}
    980   1.1  christos 
    981   1.1  christos 		bus_space_write_1(sc->sc_bt, sc->sc_bh, TPM_STS,
    982   1.1  christos 		    err ? TPM_STS_CMD_READY : TPM_STS_GO);
    983   1.1  christos 	}
    984   1.1  christos 
    985  1.22  riastrad out:	return err ? err : rv;
    986   1.1  christos }
    987   1.1  christos 
    988  1.16      maxv const struct tpm_intf tpm_intf_tis12 = {
    989  1.16      maxv 	.version = TIS_1_2,
    990  1.16      maxv 	.probe = tpm_tis12_probe,
    991  1.16      maxv 	.init = tpm_tis12_init,
    992  1.16      maxv 	.start = tpm_tis12_start,
    993  1.16      maxv 	.read = tpm_tis12_read,
    994  1.16      maxv 	.write = tpm_tis12_write,
    995  1.16      maxv 	.end = tpm_tis12_end
    996  1.16      maxv };
    997  1.16      maxv 
    998  1.13      maxv /* -------------------------------------------------------------------------- */
    999   1.1  christos 
   1000  1.13      maxv static dev_type_open(tpmopen);
   1001  1.13      maxv static dev_type_close(tpmclose);
   1002  1.13      maxv static dev_type_read(tpmread);
   1003  1.13      maxv static dev_type_write(tpmwrite);
   1004  1.13      maxv static dev_type_ioctl(tpmioctl);
   1005   1.1  christos 
   1006  1.13      maxv const struct cdevsw tpm_cdevsw = {
   1007  1.13      maxv 	.d_open = tpmopen,
   1008  1.13      maxv 	.d_close = tpmclose,
   1009  1.13      maxv 	.d_read = tpmread,
   1010  1.13      maxv 	.d_write = tpmwrite,
   1011  1.13      maxv 	.d_ioctl = tpmioctl,
   1012  1.13      maxv 	.d_stop = nostop,
   1013  1.13      maxv 	.d_tty = notty,
   1014  1.13      maxv 	.d_poll = nopoll,
   1015  1.13      maxv 	.d_mmap = nommap,
   1016  1.13      maxv 	.d_kqfilter = nokqfilter,
   1017  1.13      maxv 	.d_discard = nodiscard,
   1018  1.14      maxv 	.d_flag = D_OTHER | D_MPSAFE,
   1019  1.13      maxv };
   1020   1.1  christos 
   1021  1.13      maxv static int
   1022   1.1  christos tpmopen(dev_t dev, int flag, int mode, struct lwp *l)
   1023   1.1  christos {
   1024  1.14      maxv 	struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
   1025  1.14      maxv 	int ret = 0;
   1026   1.1  christos 
   1027  1.13      maxv 	if (sc == NULL)
   1028   1.1  christos 		return ENXIO;
   1029   1.1  christos 
   1030  1.14      maxv 	mutex_enter(&sc->sc_lock);
   1031  1.14      maxv 	if (sc->sc_busy) {
   1032  1.14      maxv 		ret = EBUSY;
   1033  1.14      maxv 	} else {
   1034  1.14      maxv 		sc->sc_busy = true;
   1035  1.14      maxv 	}
   1036  1.14      maxv 	mutex_exit(&sc->sc_lock);
   1037   1.1  christos 
   1038  1.14      maxv 	return ret;
   1039   1.1  christos }
   1040   1.1  christos 
   1041  1.13      maxv static int
   1042   1.1  christos tpmclose(dev_t dev, int flag, int mode, struct lwp *l)
   1043   1.1  christos {
   1044  1.14      maxv 	struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
   1045  1.14      maxv 	int ret = 0;
   1046   1.1  christos 
   1047  1.13      maxv 	if (sc == NULL)
   1048   1.1  christos 		return ENXIO;
   1049   1.1  christos 
   1050  1.14      maxv 	mutex_enter(&sc->sc_lock);
   1051  1.14      maxv 	if (!sc->sc_busy) {
   1052  1.14      maxv 		ret = EINVAL;
   1053  1.14      maxv 	} else {
   1054  1.14      maxv 		sc->sc_busy = false;
   1055  1.14      maxv 	}
   1056  1.14      maxv 	mutex_exit(&sc->sc_lock);
   1057   1.1  christos 
   1058  1.14      maxv 	return ret;
   1059   1.1  christos }
   1060   1.1  christos 
   1061  1.13      maxv static int
   1062   1.1  christos tpmread(dev_t dev, struct uio *uio, int flags)
   1063   1.1  christos {
   1064  1.14      maxv 	struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
   1065  1.15      maxv 	struct tpm_header hdr;
   1066  1.14      maxv 	uint8_t buf[TPM_BUFSIZ];
   1067  1.24  riastrad 	size_t cnt, len = 0/*XXXGCC*/;
   1068  1.24  riastrad 	bool end = false;
   1069  1.14      maxv 	int rv;
   1070   1.1  christos 
   1071  1.13      maxv 	if (sc == NULL)
   1072   1.1  christos 		return ENXIO;
   1073   1.1  christos 
   1074  1.24  riastrad 	mutex_enter(&sc->sc_lock);
   1075  1.24  riastrad 
   1076  1.16      maxv 	if ((rv = (*sc->sc_intf->start)(sc, UIO_READ)))
   1077  1.24  riastrad 		goto out;
   1078  1.24  riastrad 	end = true;
   1079   1.1  christos 
   1080  1.14      maxv 	/* Get the header. */
   1081  1.16      maxv 	if ((rv = (*sc->sc_intf->read)(sc, &hdr, sizeof(hdr), &cnt, 0))) {
   1082   1.3  christos 		goto out;
   1083   1.1  christos 	}
   1084  1.24  riastrad 	if (cnt != sizeof(hdr)) {
   1085  1.24  riastrad 		rv = EIO;
   1086  1.24  riastrad 		goto out;
   1087  1.24  riastrad 	}
   1088  1.25  riastrad 	len = be32toh(hdr.length);
   1089  1.24  riastrad 	if (len > MIN(sizeof(buf), uio->uio_resid) || len < sizeof(hdr)) {
   1090   1.1  christos 		rv = EIO;
   1091   1.3  christos 		goto out;
   1092   1.1  christos 	}
   1093   1.1  christos 
   1094  1.24  riastrad 	/* Get the payload. */
   1095  1.24  riastrad 	len -= sizeof(hdr);
   1096  1.24  riastrad 	if ((rv = (*sc->sc_intf->read)(sc, buf, len, NULL, TPM_PARAM_SIZE))) {
   1097  1.24  riastrad 		goto out;
   1098  1.24  riastrad 	}
   1099  1.24  riastrad 
   1100  1.24  riastrad out:	if (end)
   1101  1.24  riastrad 		rv = (*sc->sc_intf->end)(sc, UIO_READ, rv);
   1102  1.24  riastrad 
   1103  1.24  riastrad 	mutex_exit(&sc->sc_lock);
   1104  1.24  riastrad 
   1105  1.24  riastrad 	/* If anything went wrong, stop here -- nothing to copy out. */
   1106  1.24  riastrad 	if (rv)
   1107  1.24  riastrad 		return rv;
   1108  1.24  riastrad 
   1109  1.14      maxv 	/* Copy out the header. */
   1110  1.24  riastrad 	if ((rv = uiomove(&hdr, sizeof(hdr), uio))) {
   1111  1.24  riastrad 		return rv;
   1112   1.1  christos 	}
   1113   1.1  christos 
   1114  1.24  riastrad 	/* Copy out the payload.  */
   1115  1.24  riastrad 	if ((rv = uiomove(buf, len, uio))) {
   1116  1.24  riastrad 		return rv;
   1117   1.1  christos 	}
   1118   1.1  christos 
   1119  1.24  riastrad 	/* Success! */
   1120  1.24  riastrad 	return 0;
   1121   1.1  christos }
   1122   1.1  christos 
   1123  1.13      maxv static int
   1124   1.1  christos tpmwrite(dev_t dev, struct uio *uio, int flags)
   1125   1.1  christos {
   1126  1.14      maxv 	struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
   1127   1.1  christos 	uint8_t buf[TPM_BUFSIZ];
   1128  1.24  riastrad 	bool end = false;
   1129  1.14      maxv 	int n, rv;
   1130   1.1  christos 
   1131  1.13      maxv 	if (sc == NULL)
   1132   1.1  christos 		return ENXIO;
   1133   1.1  christos 
   1134   1.1  christos 	n = MIN(sizeof(buf), uio->uio_resid);
   1135   1.1  christos 	if ((rv = uiomove(buf, n, uio))) {
   1136  1.24  riastrad 		return rv;
   1137   1.1  christos 	}
   1138  1.24  riastrad 
   1139  1.24  riastrad 	mutex_enter(&sc->sc_lock);
   1140  1.24  riastrad 
   1141  1.16      maxv 	if ((rv = (*sc->sc_intf->start)(sc, UIO_WRITE))) {
   1142  1.13      maxv 		goto out;
   1143   1.1  christos 	}
   1144  1.24  riastrad 	end = true;
   1145  1.24  riastrad 
   1146  1.16      maxv 	if ((rv = (*sc->sc_intf->write)(sc, buf, n))) {
   1147  1.13      maxv 		goto out;
   1148   1.1  christos 	}
   1149   1.1  christos 
   1150  1.24  riastrad out:	if (end)
   1151  1.24  riastrad 		rv = (*sc->sc_intf->end)(sc, UIO_WRITE, rv);
   1152  1.24  riastrad 
   1153  1.24  riastrad 	mutex_exit(&sc->sc_lock);
   1154   1.1  christos 	return rv;
   1155   1.1  christos }
   1156   1.1  christos 
   1157  1.13      maxv static int
   1158  1.13      maxv tpmioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1159   1.1  christos {
   1160  1.14      maxv 	struct tpm_softc *sc = device_lookup_private(&tpm_cd, minor(dev));
   1161  1.13      maxv 	struct tpm_ioc_getinfo *info;
   1162  1.13      maxv 
   1163  1.13      maxv 	if (sc == NULL)
   1164  1.13      maxv 		return ENXIO;
   1165  1.13      maxv 
   1166  1.13      maxv 	switch (cmd) {
   1167  1.13      maxv 	case TPM_IOC_GETINFO:
   1168  1.13      maxv 		info = addr;
   1169  1.13      maxv 		info->api_version = TPM_API_VERSION;
   1170  1.13      maxv 		info->tpm_version = sc->sc_ver;
   1171  1.16      maxv 		info->itf_version = sc->sc_intf->version;
   1172  1.13      maxv 		info->device_id = sc->sc_devid;
   1173  1.13      maxv 		info->device_rev = sc->sc_rev;
   1174  1.14      maxv 		info->device_caps = sc->sc_caps;
   1175  1.13      maxv 		return 0;
   1176  1.13      maxv 	default:
   1177  1.13      maxv 		break;
   1178  1.13      maxv 	}
   1179  1.13      maxv 
   1180   1.1  christos 	return ENOTTY;
   1181   1.1  christos }
   1182