Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2004 IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2014 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Leendert van Doorn <leendert@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Dave Safford <safford@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Reiner Sailer <sailer@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Kylene Hall <kjhall@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Device driver for TCG/TCPA TPM (trusted platform module).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * Specifications at www.trustedcomputinggroup.org
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * Note, the TPM chip is not interrupt driven (only polling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * and can have very long timeouts (minutes!). Hence the unusual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * calls to msleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/freezer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/tpm_eventlog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include "tpm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * Bug workaround - some TPM's don't flush the most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * recently changed pcr on suspend, so force the flush
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * with an extend to the selected _unused_ non-volatile pcr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static u32 tpm_suspend_pcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) MODULE_PARM_DESC(suspend_pcr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		 "PCR to use for dummy writes to facilitate flush on suspend.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * tpm_calc_ordinal_duration() - calculate the maximum command duration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * @chip:    TPM chip to use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * @ordinal: TPM command ordinal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * The function returns the maximum amount of time the chip could take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * to return the result for a particular ordinal in jiffies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * Return: A maximal duration time for an ordinal in jiffies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		return tpm2_calc_ordinal_duration(chip, ordinal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		return tpm1_calc_ordinal_duration(chip, ordinal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct tpm_header *header = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	ssize_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	u32 count, ordinal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	unsigned long stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (bufsiz < TPM_HEADER_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (bufsiz > TPM_BUFSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		bufsiz = TPM_BUFSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	count = be32_to_cpu(header->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	ordinal = be32_to_cpu(header->ordinal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (count > bufsiz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		dev_err(&chip->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			"invalid count value %x %zx\n", count, bufsiz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	rc = chip->ops->send(chip, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		if (rc != -EPIPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			dev_err(&chip->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 				"%s: send(): error %d\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	/* A sanity check. send() should just return zero on success e.g.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 * not the command length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (rc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		dev_warn(&chip->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			 "%s: send(): invalid value %d\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (chip->flags & TPM_CHIP_FLAG_IRQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		goto out_recv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		u8 status = chip->ops->status(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		if ((status & chip->ops->req_complete_mask) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		    chip->ops->req_complete_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			goto out_recv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		if (chip->ops->req_canceled(chip, status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			dev_err(&chip->dev, "Operation Canceled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			return -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		tpm_msleep(TPM_TIMEOUT_POLL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	} while (time_before(jiffies, stop));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	chip->ops->cancel(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	dev_err(&chip->dev, "Operation Timed out\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	return -ETIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) out_recv:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	len = chip->ops->recv(chip, buf, bufsiz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (len < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		rc = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		dev_err(&chip->dev, "tpm_transmit: tpm_recv: error %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	} else if (len < TPM_HEADER_SIZE || len != be32_to_cpu(header->length))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return rc ? rc : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  * tpm_transmit - Internal kernel interface to transmit TPM commands.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  * @chip:	a TPM chip to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  * @buf:	a TPM command buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  * @bufsiz:	length of the TPM command buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  * the TPM and retransmits the command after a delay up to a maximum wait of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  * TPM2_DURATION_LONG.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  * only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  * * The response length	- OK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  * * -errno			- A system error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct tpm_header *header = (struct tpm_header *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	/* space for header and handles */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	unsigned int delay_msec = TPM2_DURATION_SHORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	u32 rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	const size_t save_size = min(sizeof(save), bufsiz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	/* the command code is where the return code will be */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	u32 cc = be32_to_cpu(header->return_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	 * Subtlety here: if we have a space, the handles will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	 * transformed, so when we restore the header we also have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	 * restore the handles.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	memcpy(save, buf, save_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		ret = tpm_try_transmit(chip, buf, bufsiz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		rc = be32_to_cpu(header->return_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		 * return immediately if self test returns test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		 * still running to shorten boot time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		if (delay_msec > TPM2_DURATION_LONG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			if (rc == TPM2_RC_RETRY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				dev_err(&chip->dev, "in retry loop\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 				dev_err(&chip->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 					"self test is still running\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		tpm_msleep(delay_msec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		delay_msec *= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		memcpy(buf, save, save_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)  * tpm_transmit_cmd - send a tpm command to the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)  * @chip:			a TPM chip to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)  * @buf:			a TPM command buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  * @min_rsp_body_length:	minimum expected length of response body
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  * @desc:			command description used in the error message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)  * * 0		- OK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)  * * -errno	- A system error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  * * TPM_RC	- A TPM error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			 size_t min_rsp_body_length, const char *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	const struct tpm_header *header = (struct tpm_header *)buf->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	ssize_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	len = tpm_transmit(chip, buf->data, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (len <  0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	err = be32_to_cpu(header->return_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	    && err != TPM2_RC_TESTING && desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (len < min_rsp_body_length + TPM_HEADER_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int tpm_get_timeouts(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		return tpm2_get_timeouts(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		return tpm1_get_timeouts(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) EXPORT_SYMBOL_GPL(tpm_get_timeouts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  * tpm_is_tpm2 - do we a have a TPM2 chip?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  * 1 if we have a TPM2 chip.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  * 0 if we don't have a TPM2 chip.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  * A negative number for system errors (errno).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) int tpm_is_tpm2(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	chip = tpm_find_get_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) EXPORT_SYMBOL_GPL(tpm_is_tpm2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  * tpm_pcr_read - read a PCR value from SHA1 bank
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)  * @pcr_idx:	the PCR to be retrieved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)  * @digest:	the PCR bank and buffer current PCR value is written to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  * Return: same as with tpm_transmit_cmd()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		 struct tpm_digest *digest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	chip = tpm_find_get_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		rc = tpm2_pcr_read(chip, pcr_idx, digest, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		rc = tpm1_pcr_read(chip, pcr_idx, digest->digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) EXPORT_SYMBOL_GPL(tpm_pcr_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)  * tpm_pcr_extend - extend a PCR value in SHA1 bank.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  * @pcr_idx:	the PCR to be retrieved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  * @digests:	array of tpm_digest structures used to extend PCRs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  * Note: callers must pass a digest for every allocated PCR bank, in the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  * order of the banks in chip->allocated_banks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)  * Return: same as with tpm_transmit_cmd()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		   struct tpm_digest *digests)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	chip = tpm_find_get_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	for (i = 0; i < chip->nr_allocated_banks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		if (digests[i].alg_id != chip->allocated_banks[i].alg_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		rc = tpm2_pcr_extend(chip, pcr_idx, digests);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	rc = tpm1_pcr_extend(chip, pcr_idx, digests[0].digest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			     "attempting extend a PCR value");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) EXPORT_SYMBOL_GPL(tpm_pcr_extend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  * tpm_send - send a TPM command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)  * @cmd:	a TPM command buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)  * @buflen:	the length of the TPM command buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)  * Return: same as with tpm_transmit_cmd()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	struct tpm_buf buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	chip = tpm_find_get_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	buf.data = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to a send a command");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) EXPORT_SYMBOL_GPL(tpm_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int tpm_auto_startup(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		rc = tpm2_auto_startup(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		rc = tpm1_auto_startup(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)  * We are about to suspend. Save the TPM state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)  * so that it can be restored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) int tpm_pm_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	struct tpm_chip *chip = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		goto suspended;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	if ((chip->flags & TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	    !pm_suspend_via_firmware())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		goto suspended;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (!tpm_chip_start(chip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		if (chip->flags & TPM_CHIP_FLAG_TPM2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			tpm2_shutdown(chip, TPM2_SU_STATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		tpm_chip_stop(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) suspended:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) EXPORT_SYMBOL_GPL(tpm_pm_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)  * Resume from a power safe. The BIOS already restored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)  * the TPM state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) int tpm_pm_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	struct tpm_chip *chip = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	if (chip == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) EXPORT_SYMBOL_GPL(tpm_pm_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)  * tpm_get_random() - get random bytes from the TPM's RNG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)  * @out:	destination buffer for the random bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)  * @max:	the max number of bytes to write to @out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)  * Return: number of random bytes read or a negative error value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (!out || max > TPM_MAX_RNG_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	chip = tpm_find_get_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		rc = tpm2_get_random(chip, out, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		rc = tpm1_get_random(chip, out, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) EXPORT_SYMBOL_GPL(tpm_get_random);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) static int __init tpm_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	tpm_class = class_create(THIS_MODULE, "tpm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	if (IS_ERR(tpm_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		pr_err("couldn't create tpm class\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		return PTR_ERR(tpm_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	tpmrm_class = class_create(THIS_MODULE, "tpmrm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	if (IS_ERR(tpmrm_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		pr_err("couldn't create tpmrm class\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		rc = PTR_ERR(tpmrm_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		goto out_destroy_tpm_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		pr_err("tpm: failed to allocate char dev region\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		goto out_destroy_tpmrm_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	rc = tpm_dev_common_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		pr_err("tpm: failed to allocate char dev region\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		goto out_unreg_chrdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) out_unreg_chrdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) out_destroy_tpmrm_class:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	class_destroy(tpmrm_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) out_destroy_tpm_class:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	class_destroy(tpm_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) static void __exit tpm_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	idr_destroy(&dev_nums_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	class_destroy(tpm_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	class_destroy(tpmrm_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	tpm_dev_common_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) subsys_initcall(tpm_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) module_exit(tpm_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) MODULE_DESCRIPTION("TPM Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) MODULE_VERSION("2.0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) MODULE_LICENSE("GPL");