xref: /openbmc/linux/drivers/char/tpm/tpm-interface.c (revision b886d83c5b621abc84ff9616f14c529be3f6b147)
1*b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29deb0eb7SJason Gunthorpe /*
39deb0eb7SJason Gunthorpe  * Copyright (C) 2004 IBM Corporation
4afb5abc2SJarkko Sakkinen  * Copyright (C) 2014 Intel Corporation
59deb0eb7SJason Gunthorpe  *
69deb0eb7SJason Gunthorpe  * Authors:
79deb0eb7SJason Gunthorpe  * Leendert van Doorn <leendert@watson.ibm.com>
89deb0eb7SJason Gunthorpe  * Dave Safford <safford@watson.ibm.com>
99deb0eb7SJason Gunthorpe  * Reiner Sailer <sailer@watson.ibm.com>
109deb0eb7SJason Gunthorpe  * Kylene Hall <kjhall@us.ibm.com>
119deb0eb7SJason Gunthorpe  *
129deb0eb7SJason Gunthorpe  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
139deb0eb7SJason Gunthorpe  *
149deb0eb7SJason Gunthorpe  * Device driver for TCG/TCPA TPM (trusted platform module).
159deb0eb7SJason Gunthorpe  * Specifications at www.trustedcomputinggroup.org
169deb0eb7SJason Gunthorpe  *
179deb0eb7SJason Gunthorpe  * Note, the TPM chip is not interrupt driven (only polling)
189deb0eb7SJason Gunthorpe  * and can have very long timeouts (minutes!). Hence the unusual
199deb0eb7SJason Gunthorpe  * calls to msleep.
209deb0eb7SJason Gunthorpe  */
219deb0eb7SJason Gunthorpe 
229deb0eb7SJason Gunthorpe #include <linux/poll.h>
239deb0eb7SJason Gunthorpe #include <linux/slab.h>
249deb0eb7SJason Gunthorpe #include <linux/mutex.h>
259deb0eb7SJason Gunthorpe #include <linux/spinlock.h>
269deb0eb7SJason Gunthorpe #include <linux/freezer.h>
27fd3ec366SThiebaud Weksteen #include <linux/tpm_eventlog.h>
289deb0eb7SJason Gunthorpe 
299deb0eb7SJason Gunthorpe #include "tpm.h"
309deb0eb7SJason Gunthorpe 
319deb0eb7SJason Gunthorpe /*
329deb0eb7SJason Gunthorpe  * Bug workaround - some TPM's don't flush the most
339deb0eb7SJason Gunthorpe  * recently changed pcr on suspend, so force the flush
349deb0eb7SJason Gunthorpe  * with an extend to the selected _unused_ non-volatile pcr.
359deb0eb7SJason Gunthorpe  */
3695adc6b4STomas Winkler static u32 tpm_suspend_pcr;
379deb0eb7SJason Gunthorpe module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
389deb0eb7SJason Gunthorpe MODULE_PARM_DESC(suspend_pcr,
3939f5712bSDmitry Torokhov 		 "PCR to use for dummy writes to facilitate flush on suspend.");
409deb0eb7SJason Gunthorpe 
41d856c00fSTomas Winkler /**
42d856c00fSTomas Winkler  * tpm_calc_ordinal_duration() - calculate the maximum command duration
43d856c00fSTomas Winkler  * @chip:    TPM chip to use.
44d856c00fSTomas Winkler  * @ordinal: TPM command ordinal.
45d856c00fSTomas Winkler  *
46d856c00fSTomas Winkler  * The function returns the maximum amount of time the chip could take
47d856c00fSTomas Winkler  * to return the result for a particular ordinal in jiffies.
48d856c00fSTomas Winkler  *
49d856c00fSTomas Winkler  * Return: A maximal duration time for an ordinal in jiffies.
50d856c00fSTomas Winkler  */
51d856c00fSTomas Winkler unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
52d856c00fSTomas Winkler {
53d856c00fSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
54d856c00fSTomas Winkler 		return tpm2_calc_ordinal_duration(chip, ordinal);
55d856c00fSTomas Winkler 	else
56d856c00fSTomas Winkler 		return tpm1_calc_ordinal_duration(chip, ordinal);
57d856c00fSTomas Winkler }
58d856c00fSTomas Winkler EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
59d856c00fSTomas Winkler 
6047a6c28bSJarkko Sakkinen static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
619deb0eb7SJason Gunthorpe {
62b34b77a9SJarkko Sakkinen 	struct tpm_header *header = buf;
63745b361eSJarkko Sakkinen 	int rc;
64745b361eSJarkko Sakkinen 	ssize_t len = 0;
659deb0eb7SJason Gunthorpe 	u32 count, ordinal;
669deb0eb7SJason Gunthorpe 	unsigned long stop;
679deb0eb7SJason Gunthorpe 
68c3465a37SJarkko Sakkinen 	if (bufsiz < TPM_HEADER_SIZE)
69c3465a37SJarkko Sakkinen 		return -EINVAL;
70ebfd7532SJarkko Sakkinen 
719deb0eb7SJason Gunthorpe 	if (bufsiz > TPM_BUFSIZE)
729deb0eb7SJason Gunthorpe 		bufsiz = TPM_BUFSIZE;
739deb0eb7SJason Gunthorpe 
74720b0711SJarkko Sakkinen 	count = be32_to_cpu(header->length);
75720b0711SJarkko Sakkinen 	ordinal = be32_to_cpu(header->ordinal);
769deb0eb7SJason Gunthorpe 	if (count == 0)
779deb0eb7SJason Gunthorpe 		return -ENODATA;
789deb0eb7SJason Gunthorpe 	if (count > bufsiz) {
798cfffc9dSJason Gunthorpe 		dev_err(&chip->dev,
809deb0eb7SJason Gunthorpe 			"invalid count value %x %zx\n", count, bufsiz);
819deb0eb7SJason Gunthorpe 		return -E2BIG;
829deb0eb7SJason Gunthorpe 	}
839deb0eb7SJason Gunthorpe 
8462c09e12SWinkler, Tomas 	rc = chip->ops->send(chip, buf, count);
859deb0eb7SJason Gunthorpe 	if (rc < 0) {
86402149c6SStefan Berger 		if (rc != -EPIPE)
878cfffc9dSJason Gunthorpe 			dev_err(&chip->dev,
88f5595f5bSJarkko Sakkinen 				"%s: send(): error %d\n", __func__, rc);
8929b47ce9SJarkko Sakkinen 		return rc;
909deb0eb7SJason Gunthorpe 	}
919deb0eb7SJason Gunthorpe 
92f5595f5bSJarkko Sakkinen 	/* A sanity check. send() should just return zero on success e.g.
93f5595f5bSJarkko Sakkinen 	 * not the command length.
94f5595f5bSJarkko Sakkinen 	 */
95f5595f5bSJarkko Sakkinen 	if (rc > 0) {
96f5595f5bSJarkko Sakkinen 		dev_warn(&chip->dev,
97f5595f5bSJarkko Sakkinen 			 "%s: send(): invalid value %d\n", __func__, rc);
98f5595f5bSJarkko Sakkinen 		rc = 0;
99f5595f5bSJarkko Sakkinen 	}
100f5595f5bSJarkko Sakkinen 
101570a3609SChristophe Ricard 	if (chip->flags & TPM_CHIP_FLAG_IRQ)
1029deb0eb7SJason Gunthorpe 		goto out_recv;
1039deb0eb7SJason Gunthorpe 
104d856c00fSTomas Winkler 	stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
1059deb0eb7SJason Gunthorpe 	do {
1065f82e9f0SJason Gunthorpe 		u8 status = chip->ops->status(chip);
1075f82e9f0SJason Gunthorpe 		if ((status & chip->ops->req_complete_mask) ==
1085f82e9f0SJason Gunthorpe 		    chip->ops->req_complete_val)
1099deb0eb7SJason Gunthorpe 			goto out_recv;
1109deb0eb7SJason Gunthorpe 
1115f82e9f0SJason Gunthorpe 		if (chip->ops->req_canceled(chip, status)) {
1128cfffc9dSJason Gunthorpe 			dev_err(&chip->dev, "Operation Canceled\n");
11329b47ce9SJarkko Sakkinen 			return -ECANCELED;
1149deb0eb7SJason Gunthorpe 		}
1159deb0eb7SJason Gunthorpe 
11659f5a6b0SNayna Jain 		tpm_msleep(TPM_TIMEOUT_POLL);
1179deb0eb7SJason Gunthorpe 		rmb();
1189deb0eb7SJason Gunthorpe 	} while (time_before(jiffies, stop));
1199deb0eb7SJason Gunthorpe 
1205f82e9f0SJason Gunthorpe 	chip->ops->cancel(chip);
1218cfffc9dSJason Gunthorpe 	dev_err(&chip->dev, "Operation Timed out\n");
12229b47ce9SJarkko Sakkinen 	return -ETIME;
1239deb0eb7SJason Gunthorpe 
1249deb0eb7SJason Gunthorpe out_recv:
12562c09e12SWinkler, Tomas 	len = chip->ops->recv(chip, buf, bufsiz);
126745b361eSJarkko Sakkinen 	if (len < 0) {
127745b361eSJarkko Sakkinen 		rc = len;
128304ff672SJarkko Sakkinen 		dev_err(&chip->dev, "tpm_transmit: tpm_recv: error %d\n", rc);
129304ff672SJarkko Sakkinen 	} else if (len < TPM_HEADER_SIZE || len != be32_to_cpu(header->length))
130a147918eSJarkko Sakkinen 		rc = -EFAULT;
131a147918eSJarkko Sakkinen 
132745b361eSJarkko Sakkinen 	return rc ? rc : len;
1339deb0eb7SJason Gunthorpe }
1349deb0eb7SJason Gunthorpe 
135f865c196SWinkler, Tomas /**
136e2fb992dSJames Bottomley  * tpm_transmit - Internal kernel interface to transmit TPM commands.
137412eb585SJarkko Sakkinen  * @chip:	a TPM chip to use
138412eb585SJarkko Sakkinen  * @buf:	a TPM command buffer
139e2fb992dSJames Bottomley  * @bufsiz:	length of the TPM command buffer
140e2fb992dSJames Bottomley  *
141412eb585SJarkko Sakkinen  * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from
142412eb585SJarkko Sakkinen  * the TPM and retransmits the command after a delay up to a maximum wait of
143412eb585SJarkko Sakkinen  * TPM2_DURATION_LONG.
144e2fb992dSJames Bottomley  *
145412eb585SJarkko Sakkinen  * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0
146412eb585SJarkko Sakkinen  * only.
147e2fb992dSJames Bottomley  *
148e2fb992dSJames Bottomley  * Return:
149412eb585SJarkko Sakkinen  * * The response length	- OK
150412eb585SJarkko Sakkinen  * * -errno			- A system error
151e2fb992dSJames Bottomley  */
15247a6c28bSJarkko Sakkinen ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
153e2fb992dSJames Bottomley {
154b34b77a9SJarkko Sakkinen 	struct tpm_header *header = (struct tpm_header *)buf;
155e2fb992dSJames Bottomley 	/* space for header and handles */
156e2fb992dSJames Bottomley 	u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
157e2fb992dSJames Bottomley 	unsigned int delay_msec = TPM2_DURATION_SHORT;
158e2fb992dSJames Bottomley 	u32 rc = 0;
159e2fb992dSJames Bottomley 	ssize_t ret;
1605faafbabSJarkko Sakkinen 	const size_t save_size = min(sizeof(save), bufsiz);
1612be8ffedSJames Bottomley 	/* the command code is where the return code will be */
1622be8ffedSJames Bottomley 	u32 cc = be32_to_cpu(header->return_code);
163e2fb992dSJames Bottomley 
164e2fb992dSJames Bottomley 	/*
165e2fb992dSJames Bottomley 	 * Subtlety here: if we have a space, the handles will be
166e2fb992dSJames Bottomley 	 * transformed, so when we restore the header we also have to
167e2fb992dSJames Bottomley 	 * restore the handles.
168e2fb992dSJames Bottomley 	 */
169e2fb992dSJames Bottomley 	memcpy(save, buf, save_size);
170e2fb992dSJames Bottomley 
171e2fb992dSJames Bottomley 	for (;;) {
17247a6c28bSJarkko Sakkinen 		ret = tpm_try_transmit(chip, buf, bufsiz);
173e2fb992dSJames Bottomley 		if (ret < 0)
174e2fb992dSJames Bottomley 			break;
175e2fb992dSJames Bottomley 		rc = be32_to_cpu(header->return_code);
1762be8ffedSJames Bottomley 		if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
1772be8ffedSJames Bottomley 			break;
1782be8ffedSJames Bottomley 		/*
1792be8ffedSJames Bottomley 		 * return immediately if self test returns test
1802be8ffedSJames Bottomley 		 * still running to shorten boot time.
1812be8ffedSJames Bottomley 		 */
1822be8ffedSJames Bottomley 		if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
183e2fb992dSJames Bottomley 			break;
18492980756SNayna Jain 
185e2fb992dSJames Bottomley 		if (delay_msec > TPM2_DURATION_LONG) {
1862be8ffedSJames Bottomley 			if (rc == TPM2_RC_RETRY)
1872be8ffedSJames Bottomley 				dev_err(&chip->dev, "in retry loop\n");
1882be8ffedSJames Bottomley 			else
1892be8ffedSJames Bottomley 				dev_err(&chip->dev,
1902be8ffedSJames Bottomley 					"self test is still running\n");
191e2fb992dSJames Bottomley 			break;
192e2fb992dSJames Bottomley 		}
193e2fb992dSJames Bottomley 		tpm_msleep(delay_msec);
19492980756SNayna Jain 		delay_msec *= 2;
195e2fb992dSJames Bottomley 		memcpy(buf, save, save_size);
196e2fb992dSJames Bottomley 	}
197e2fb992dSJames Bottomley 	return ret;
198e2fb992dSJames Bottomley }
199412eb585SJarkko Sakkinen 
200e2fb992dSJames Bottomley /**
20165520d46SWinkler, Tomas  * tpm_transmit_cmd - send a tpm command to the device
202412eb585SJarkko Sakkinen  * @chip:			a TPM chip to use
203412eb585SJarkko Sakkinen  * @buf:			a TPM command buffer
204c659af78SStefan Berger  * @min_rsp_body_length:	minimum expected length of response body
205f865c196SWinkler, Tomas  * @desc:			command description used in the error message
206f865c196SWinkler, Tomas  *
207f865c196SWinkler, Tomas  * Return:
208412eb585SJarkko Sakkinen  * * 0		- OK
209412eb585SJarkko Sakkinen  * * -errno	- A system error
210412eb585SJarkko Sakkinen  * * TPM_RC	- A TPM error
211f865c196SWinkler, Tomas  */
2125faafbabSJarkko Sakkinen ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
21347a6c28bSJarkko Sakkinen 			 size_t min_rsp_body_length, const char *desc)
2149deb0eb7SJason Gunthorpe {
215b34b77a9SJarkko Sakkinen 	const struct tpm_header *header = (struct tpm_header *)buf->data;
2169deb0eb7SJason Gunthorpe 	int err;
217c659af78SStefan Berger 	ssize_t len;
2189deb0eb7SJason Gunthorpe 
21947a6c28bSJarkko Sakkinen 	len = tpm_transmit(chip, buf->data, PAGE_SIZE);
2209deb0eb7SJason Gunthorpe 	if (len <  0)
2219deb0eb7SJason Gunthorpe 		return len;
22287155b73SJarkko Sakkinen 
22387155b73SJarkko Sakkinen 	err = be32_to_cpu(header->return_code);
2240d6d0d62SJavier Martinez Canillas 	if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED
22508a8112aSJerry Snitselaar 	    && err != TPM2_RC_TESTING && desc)
2268cfffc9dSJason Gunthorpe 		dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
22771ed848fSJarkko Sakkinen 			desc);
228c659af78SStefan Berger 	if (err)
2299deb0eb7SJason Gunthorpe 		return err;
230c659af78SStefan Berger 
231c659af78SStefan Berger 	if (len < min_rsp_body_length + TPM_HEADER_SIZE)
232c659af78SStefan Berger 		return -EFAULT;
233c659af78SStefan Berger 
234c659af78SStefan Berger 	return 0;
2359deb0eb7SJason Gunthorpe }
236be4c9acfSStefan Berger EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
2379deb0eb7SJason Gunthorpe 
2389deb0eb7SJason Gunthorpe int tpm_get_timeouts(struct tpm_chip *chip)
2399deb0eb7SJason Gunthorpe {
240d1d253cfSJason Gunthorpe 	if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
241d1d253cfSJason Gunthorpe 		return 0;
242d1d253cfSJason Gunthorpe 
24370a3199aSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
24470a3199aSTomas Winkler 		return tpm2_get_timeouts(chip);
24570a3199aSTomas Winkler 	else
24670a3199aSTomas Winkler 		return tpm1_get_timeouts(chip);
2479deb0eb7SJason Gunthorpe }
2489deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_timeouts);
2499deb0eb7SJason Gunthorpe 
2509deb0eb7SJason Gunthorpe /**
251aad887f6SJarkko Sakkinen  * tpm_is_tpm2 - do we a have a TPM2 chip?
252aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
253954650efSJarkko Sakkinen  *
254aad887f6SJarkko Sakkinen  * Return:
255aad887f6SJarkko Sakkinen  * 1 if we have a TPM2 chip.
256aad887f6SJarkko Sakkinen  * 0 if we don't have a TPM2 chip.
257aad887f6SJarkko Sakkinen  * A negative number for system errors (errno).
258954650efSJarkko Sakkinen  */
259aad887f6SJarkko Sakkinen int tpm_is_tpm2(struct tpm_chip *chip)
260954650efSJarkko Sakkinen {
261954650efSJarkko Sakkinen 	int rc;
262954650efSJarkko Sakkinen 
263fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
264aad887f6SJarkko Sakkinen 	if (!chip)
265954650efSJarkko Sakkinen 		return -ENODEV;
266954650efSJarkko Sakkinen 
267954650efSJarkko Sakkinen 	rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
268954650efSJarkko Sakkinen 
2694e26195fSJason Gunthorpe 	tpm_put_ops(chip);
270954650efSJarkko Sakkinen 
271954650efSJarkko Sakkinen 	return rc;
272954650efSJarkko Sakkinen }
273954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_is_tpm2);
274954650efSJarkko Sakkinen 
275954650efSJarkko Sakkinen /**
276aad887f6SJarkko Sakkinen  * tpm_pcr_read - read a PCR value from SHA1 bank
277aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
278aad887f6SJarkko Sakkinen  * @pcr_idx:	the PCR to be retrieved
279879b5892SRoberto Sassu  * @digest:	the PCR bank and buffer current PCR value is written to
2809deb0eb7SJason Gunthorpe  *
281aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
2829deb0eb7SJason Gunthorpe  */
283879b5892SRoberto Sassu int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
284879b5892SRoberto Sassu 		 struct tpm_digest *digest)
2859deb0eb7SJason Gunthorpe {
2869deb0eb7SJason Gunthorpe 	int rc;
2879deb0eb7SJason Gunthorpe 
288fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
289aad887f6SJarkko Sakkinen 	if (!chip)
2909deb0eb7SJason Gunthorpe 		return -ENODEV;
291d4a31756STomas Winkler 
2927a1d7e6dSJarkko Sakkinen 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
293879b5892SRoberto Sassu 		rc = tpm2_pcr_read(chip, pcr_idx, digest, NULL);
2947a1d7e6dSJarkko Sakkinen 	else
295879b5892SRoberto Sassu 		rc = tpm1_pcr_read(chip, pcr_idx, digest->digest);
296d4a31756STomas Winkler 
2974e26195fSJason Gunthorpe 	tpm_put_ops(chip);
2989deb0eb7SJason Gunthorpe 	return rc;
2999deb0eb7SJason Gunthorpe }
3009deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_read);
3019deb0eb7SJason Gunthorpe 
3029deb0eb7SJason Gunthorpe /**
303aad887f6SJarkko Sakkinen  * tpm_pcr_extend - extend a PCR value in SHA1 bank.
304aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
305aad887f6SJarkko Sakkinen  * @pcr_idx:	the PCR to be retrieved
3060b6cf6b9SRoberto Sassu  * @digests:	array of tpm_digest structures used to extend PCRs
3079deb0eb7SJason Gunthorpe  *
3080b6cf6b9SRoberto Sassu  * Note: callers must pass a digest for every allocated PCR bank, in the same
3090b6cf6b9SRoberto Sassu  * order of the banks in chip->allocated_banks.
310aad887f6SJarkko Sakkinen  *
311aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
3129deb0eb7SJason Gunthorpe  */
3130b6cf6b9SRoberto Sassu int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
3140b6cf6b9SRoberto Sassu 		   struct tpm_digest *digests)
3159deb0eb7SJason Gunthorpe {
3169deb0eb7SJason Gunthorpe 	int rc;
317c1f92b4bSNayna Jain 	int i;
3189deb0eb7SJason Gunthorpe 
319fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
320aad887f6SJarkko Sakkinen 	if (!chip)
3219deb0eb7SJason Gunthorpe 		return -ENODEV;
3229deb0eb7SJason Gunthorpe 
3230b6cf6b9SRoberto Sassu 	for (i = 0; i < chip->nr_allocated_banks; i++)
3240b6cf6b9SRoberto Sassu 		if (digests[i].alg_id != chip->allocated_banks[i].alg_id)
3250b6cf6b9SRoberto Sassu 			return -EINVAL;
3260b6cf6b9SRoberto Sassu 
3277a1d7e6dSJarkko Sakkinen 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
3280b6cf6b9SRoberto Sassu 		rc = tpm2_pcr_extend(chip, pcr_idx, digests);
3294e26195fSJason Gunthorpe 		tpm_put_ops(chip);
3307a1d7e6dSJarkko Sakkinen 		return rc;
3317a1d7e6dSJarkko Sakkinen 	}
3327a1d7e6dSJarkko Sakkinen 
3330b6cf6b9SRoberto Sassu 	rc = tpm1_pcr_extend(chip, pcr_idx, digests[0].digest,
3349deb0eb7SJason Gunthorpe 			     "attempting extend a PCR value");
3354e26195fSJason Gunthorpe 	tpm_put_ops(chip);
3369deb0eb7SJason Gunthorpe 	return rc;
3379deb0eb7SJason Gunthorpe }
3389deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_extend);
3399deb0eb7SJason Gunthorpe 
3409deb0eb7SJason Gunthorpe /**
341aad887f6SJarkko Sakkinen  * tpm_send - send a TPM command
342aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
343aad887f6SJarkko Sakkinen  * @cmd:	a TPM command buffer
344aad887f6SJarkko Sakkinen  * @buflen:	the length of the TPM command buffer
345aad887f6SJarkko Sakkinen  *
346aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
347aad887f6SJarkko Sakkinen  */
348aad887f6SJarkko Sakkinen int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
3499deb0eb7SJason Gunthorpe {
350412eb585SJarkko Sakkinen 	struct tpm_buf buf;
3519deb0eb7SJason Gunthorpe 	int rc;
3529deb0eb7SJason Gunthorpe 
353fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
354aad887f6SJarkko Sakkinen 	if (!chip)
3559deb0eb7SJason Gunthorpe 		return -ENODEV;
3569deb0eb7SJason Gunthorpe 
357412eb585SJarkko Sakkinen 	rc = tpm_buf_init(&buf, 0, 0);
358412eb585SJarkko Sakkinen 	if (rc)
359412eb585SJarkko Sakkinen 		goto out;
360412eb585SJarkko Sakkinen 
361412eb585SJarkko Sakkinen 	memcpy(buf.data, cmd, buflen);
36247a6c28bSJarkko Sakkinen 	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to a send a command");
363412eb585SJarkko Sakkinen 	tpm_buf_destroy(&buf);
364412eb585SJarkko Sakkinen out:
3654e26195fSJason Gunthorpe 	tpm_put_ops(chip);
3669deb0eb7SJason Gunthorpe 	return rc;
3679deb0eb7SJason Gunthorpe }
3689deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_send);
3699deb0eb7SJason Gunthorpe 
370b03c4370STomas Winkler int tpm_auto_startup(struct tpm_chip *chip)
371b03c4370STomas Winkler {
372b03c4370STomas Winkler 	int rc;
373b03c4370STomas Winkler 
374b03c4370STomas Winkler 	if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
375b03c4370STomas Winkler 		return 0;
376b03c4370STomas Winkler 
377b03c4370STomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
378b03c4370STomas Winkler 		rc = tpm2_auto_startup(chip);
379b03c4370STomas Winkler 	else
380b03c4370STomas Winkler 		rc = tpm1_auto_startup(chip);
381b03c4370STomas Winkler 
382b03c4370STomas Winkler 	return rc;
383b03c4370STomas Winkler }
384b03c4370STomas Winkler 
3859deb0eb7SJason Gunthorpe /*
3869deb0eb7SJason Gunthorpe  * We are about to suspend. Save the TPM state
3879deb0eb7SJason Gunthorpe  * so that it can be restored.
3889deb0eb7SJason Gunthorpe  */
3899deb0eb7SJason Gunthorpe int tpm_pm_suspend(struct device *dev)
3909deb0eb7SJason Gunthorpe {
391ec03c50bSStefan Berger 	struct tpm_chip *chip = dev_get_drvdata(dev);
392c82a330cSTomas Winkler 	int rc = 0;
3939deb0eb7SJason Gunthorpe 
394c82a330cSTomas Winkler 	if (!chip)
3959deb0eb7SJason Gunthorpe 		return -ENODEV;
3969deb0eb7SJason Gunthorpe 
397b5d0ebc9SEnric Balletbo i Serra 	if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
398b5d0ebc9SEnric Balletbo i Serra 		return 0;
399b5d0ebc9SEnric Balletbo i Serra 
40047a6c28bSJarkko Sakkinen 	if (!tpm_chip_start(chip)) {
401e891db1aSJarkko Sakkinen 		if (chip->flags & TPM_CHIP_FLAG_TPM2)
40274d6b3ceSJarkko Sakkinen 			tpm2_shutdown(chip, TPM2_SU_STATE);
403e891db1aSJarkko Sakkinen 		else
404c82a330cSTomas Winkler 			rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
405e891db1aSJarkko Sakkinen 
406e891db1aSJarkko Sakkinen 		tpm_chip_stop(chip);
407a3fbfae8SJarkko Sakkinen 	}
4089deb0eb7SJason Gunthorpe 
4099deb0eb7SJason Gunthorpe 	return rc;
4109deb0eb7SJason Gunthorpe }
4119deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_suspend);
4129deb0eb7SJason Gunthorpe 
4139deb0eb7SJason Gunthorpe /*
4149deb0eb7SJason Gunthorpe  * Resume from a power safe. The BIOS already restored
4159deb0eb7SJason Gunthorpe  * the TPM state.
4169deb0eb7SJason Gunthorpe  */
4179deb0eb7SJason Gunthorpe int tpm_pm_resume(struct device *dev)
4189deb0eb7SJason Gunthorpe {
419ec03c50bSStefan Berger 	struct tpm_chip *chip = dev_get_drvdata(dev);
4209deb0eb7SJason Gunthorpe 
4219deb0eb7SJason Gunthorpe 	if (chip == NULL)
4229deb0eb7SJason Gunthorpe 		return -ENODEV;
4239deb0eb7SJason Gunthorpe 
4249deb0eb7SJason Gunthorpe 	return 0;
4259deb0eb7SJason Gunthorpe }
4269deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_resume);
4279deb0eb7SJason Gunthorpe 
4289deb0eb7SJason Gunthorpe /**
429aad887f6SJarkko Sakkinen  * tpm_get_random() - get random bytes from the TPM's RNG
430aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
4319deb0eb7SJason Gunthorpe  * @out:	destination buffer for the random bytes
4329deb0eb7SJason Gunthorpe  * @max:	the max number of bytes to write to @out
4339deb0eb7SJason Gunthorpe  *
4347aee9c52STomas Winkler  * Return: number of random bytes read or a negative error value.
4359deb0eb7SJason Gunthorpe  */
436aad887f6SJarkko Sakkinen int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
4379deb0eb7SJason Gunthorpe {
438433d390fSTomas Winkler 	int rc;
4399deb0eb7SJason Gunthorpe 
440433d390fSTomas Winkler 	if (!out || max > TPM_MAX_RNG_DATA)
4413e14d83eSJarkko Sakkinen 		return -EINVAL;
4423e14d83eSJarkko Sakkinen 
443fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
444aad887f6SJarkko Sakkinen 	if (!chip)
4459deb0eb7SJason Gunthorpe 		return -ENODEV;
4469deb0eb7SJason Gunthorpe 
447433d390fSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
448433d390fSTomas Winkler 		rc = tpm2_get_random(chip, out, max);
449433d390fSTomas Winkler 	else
450433d390fSTomas Winkler 		rc = tpm1_get_random(chip, out, max);
4519deb0eb7SJason Gunthorpe 
4524e26195fSJason Gunthorpe 	tpm_put_ops(chip);
453433d390fSTomas Winkler 	return rc;
4549deb0eb7SJason Gunthorpe }
4559deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_random);
4569deb0eb7SJason Gunthorpe 
457954650efSJarkko Sakkinen /**
458aad887f6SJarkko Sakkinen  * tpm_seal_trusted() - seal a trusted key payload
459aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
460954650efSJarkko Sakkinen  * @options:	authentication values and other options
461954650efSJarkko Sakkinen  * @payload:	the key data in clear and encrypted form
462954650efSJarkko Sakkinen  *
463aad887f6SJarkko Sakkinen  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
464aad887f6SJarkko Sakkinen  * the keyring subsystem.
465aad887f6SJarkko Sakkinen  *
466aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
467954650efSJarkko Sakkinen  */
468aad887f6SJarkko Sakkinen int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
469954650efSJarkko Sakkinen 		     struct trusted_key_options *options)
470954650efSJarkko Sakkinen {
471954650efSJarkko Sakkinen 	int rc;
472954650efSJarkko Sakkinen 
473fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
474aad887f6SJarkko Sakkinen 	if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
475954650efSJarkko Sakkinen 		return -ENODEV;
476954650efSJarkko Sakkinen 
477954650efSJarkko Sakkinen 	rc = tpm2_seal_trusted(chip, payload, options);
478954650efSJarkko Sakkinen 
4794e26195fSJason Gunthorpe 	tpm_put_ops(chip);
480954650efSJarkko Sakkinen 	return rc;
481954650efSJarkko Sakkinen }
482954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_seal_trusted);
483954650efSJarkko Sakkinen 
484954650efSJarkko Sakkinen /**
485954650efSJarkko Sakkinen  * tpm_unseal_trusted() - unseal a trusted key
486aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
487954650efSJarkko Sakkinen  * @options:	authentication values and other options
488954650efSJarkko Sakkinen  * @payload:	the key data in clear and encrypted form
489954650efSJarkko Sakkinen  *
490aad887f6SJarkko Sakkinen  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
491aad887f6SJarkko Sakkinen  * the keyring subsystem.
492aad887f6SJarkko Sakkinen  *
493aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
494954650efSJarkko Sakkinen  */
495aad887f6SJarkko Sakkinen int tpm_unseal_trusted(struct tpm_chip *chip,
496aad887f6SJarkko Sakkinen 		       struct trusted_key_payload *payload,
497954650efSJarkko Sakkinen 		       struct trusted_key_options *options)
498954650efSJarkko Sakkinen {
499954650efSJarkko Sakkinen 	int rc;
500954650efSJarkko Sakkinen 
501fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
502aad887f6SJarkko Sakkinen 	if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
503954650efSJarkko Sakkinen 		return -ENODEV;
504954650efSJarkko Sakkinen 
505954650efSJarkko Sakkinen 	rc = tpm2_unseal_trusted(chip, payload, options);
506954650efSJarkko Sakkinen 
5074e26195fSJason Gunthorpe 	tpm_put_ops(chip);
5084e26195fSJason Gunthorpe 
509954650efSJarkko Sakkinen 	return rc;
510954650efSJarkko Sakkinen }
511954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
512954650efSJarkko Sakkinen 
513313d21eeSJarkko Sakkinen static int __init tpm_init(void)
514313d21eeSJarkko Sakkinen {
515313d21eeSJarkko Sakkinen 	int rc;
516313d21eeSJarkko Sakkinen 
517313d21eeSJarkko Sakkinen 	tpm_class = class_create(THIS_MODULE, "tpm");
518313d21eeSJarkko Sakkinen 	if (IS_ERR(tpm_class)) {
519313d21eeSJarkko Sakkinen 		pr_err("couldn't create tpm class\n");
520313d21eeSJarkko Sakkinen 		return PTR_ERR(tpm_class);
521313d21eeSJarkko Sakkinen 	}
522313d21eeSJarkko Sakkinen 
523fdc915f7SJames Bottomley 	tpmrm_class = class_create(THIS_MODULE, "tpmrm");
524fdc915f7SJames Bottomley 	if (IS_ERR(tpmrm_class)) {
525fdc915f7SJames Bottomley 		pr_err("couldn't create tpmrm class\n");
5269e1b74a6STadeusz Struk 		rc = PTR_ERR(tpmrm_class);
5279e1b74a6STadeusz Struk 		goto out_destroy_tpm_class;
528fdc915f7SJames Bottomley 	}
529fdc915f7SJames Bottomley 
530fdc915f7SJames Bottomley 	rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
531313d21eeSJarkko Sakkinen 	if (rc < 0) {
532313d21eeSJarkko Sakkinen 		pr_err("tpm: failed to allocate char dev region\n");
5339e1b74a6STadeusz Struk 		goto out_destroy_tpmrm_class;
5349e1b74a6STadeusz Struk 	}
5359e1b74a6STadeusz Struk 
5369e1b74a6STadeusz Struk 	rc = tpm_dev_common_init();
5379e1b74a6STadeusz Struk 	if (rc) {
5389e1b74a6STadeusz Struk 		pr_err("tpm: failed to allocate char dev region\n");
5399e1b74a6STadeusz Struk 		goto out_unreg_chrdev;
540313d21eeSJarkko Sakkinen 	}
541313d21eeSJarkko Sakkinen 
542313d21eeSJarkko Sakkinen 	return 0;
5439e1b74a6STadeusz Struk 
5449e1b74a6STadeusz Struk out_unreg_chrdev:
5459e1b74a6STadeusz Struk 	unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
5469e1b74a6STadeusz Struk out_destroy_tpmrm_class:
5479e1b74a6STadeusz Struk 	class_destroy(tpmrm_class);
5489e1b74a6STadeusz Struk out_destroy_tpm_class:
5499e1b74a6STadeusz Struk 	class_destroy(tpm_class);
5509e1b74a6STadeusz Struk 
5519e1b74a6STadeusz Struk 	return rc;
552313d21eeSJarkko Sakkinen }
553313d21eeSJarkko Sakkinen 
554313d21eeSJarkko Sakkinen static void __exit tpm_exit(void)
555313d21eeSJarkko Sakkinen {
55615516788SStefan Berger 	idr_destroy(&dev_nums_idr);
557313d21eeSJarkko Sakkinen 	class_destroy(tpm_class);
558fdc915f7SJames Bottomley 	class_destroy(tpmrm_class);
559fdc915f7SJames Bottomley 	unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
5609e1b74a6STadeusz Struk 	tpm_dev_common_exit();
561313d21eeSJarkko Sakkinen }
562313d21eeSJarkko Sakkinen 
563313d21eeSJarkko Sakkinen subsys_initcall(tpm_init);
564313d21eeSJarkko Sakkinen module_exit(tpm_exit);
565313d21eeSJarkko Sakkinen 
5669deb0eb7SJason Gunthorpe MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
5679deb0eb7SJason Gunthorpe MODULE_DESCRIPTION("TPM Driver");
5689deb0eb7SJason Gunthorpe MODULE_VERSION("2.0");
5699deb0eb7SJason Gunthorpe MODULE_LICENSE("GPL");
570