xref: /openbmc/linux/drivers/char/tpm/tpm-interface.c (revision 879b589210a9a0c9f77d301aaf0ddee20f2c5052)
19deb0eb7SJason Gunthorpe /*
29deb0eb7SJason Gunthorpe  * Copyright (C) 2004 IBM Corporation
3afb5abc2SJarkko Sakkinen  * Copyright (C) 2014 Intel Corporation
49deb0eb7SJason Gunthorpe  *
59deb0eb7SJason Gunthorpe  * Authors:
69deb0eb7SJason Gunthorpe  * Leendert van Doorn <leendert@watson.ibm.com>
79deb0eb7SJason Gunthorpe  * Dave Safford <safford@watson.ibm.com>
89deb0eb7SJason Gunthorpe  * Reiner Sailer <sailer@watson.ibm.com>
99deb0eb7SJason Gunthorpe  * Kylene Hall <kjhall@us.ibm.com>
109deb0eb7SJason Gunthorpe  *
119deb0eb7SJason Gunthorpe  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
129deb0eb7SJason Gunthorpe  *
139deb0eb7SJason Gunthorpe  * Device driver for TCG/TCPA TPM (trusted platform module).
149deb0eb7SJason Gunthorpe  * Specifications at www.trustedcomputinggroup.org
159deb0eb7SJason Gunthorpe  *
169deb0eb7SJason Gunthorpe  * This program is free software; you can redistribute it and/or
179deb0eb7SJason Gunthorpe  * modify it under the terms of the GNU General Public License as
189deb0eb7SJason Gunthorpe  * published by the Free Software Foundation, version 2 of the
199deb0eb7SJason Gunthorpe  * License.
209deb0eb7SJason Gunthorpe  *
219deb0eb7SJason Gunthorpe  * Note, the TPM chip is not interrupt driven (only polling)
229deb0eb7SJason Gunthorpe  * and can have very long timeouts (minutes!). Hence the unusual
239deb0eb7SJason Gunthorpe  * calls to msleep.
249deb0eb7SJason Gunthorpe  *
259deb0eb7SJason Gunthorpe  */
269deb0eb7SJason Gunthorpe 
279deb0eb7SJason Gunthorpe #include <linux/poll.h>
289deb0eb7SJason Gunthorpe #include <linux/slab.h>
299deb0eb7SJason Gunthorpe #include <linux/mutex.h>
309deb0eb7SJason Gunthorpe #include <linux/spinlock.h>
319deb0eb7SJason Gunthorpe #include <linux/freezer.h>
32fd3ec366SThiebaud Weksteen #include <linux/tpm_eventlog.h>
339deb0eb7SJason Gunthorpe 
349deb0eb7SJason Gunthorpe #include "tpm.h"
359deb0eb7SJason Gunthorpe 
369deb0eb7SJason Gunthorpe /*
379deb0eb7SJason Gunthorpe  * Bug workaround - some TPM's don't flush the most
389deb0eb7SJason Gunthorpe  * recently changed pcr on suspend, so force the flush
399deb0eb7SJason Gunthorpe  * with an extend to the selected _unused_ non-volatile pcr.
409deb0eb7SJason Gunthorpe  */
4195adc6b4STomas Winkler static u32 tpm_suspend_pcr;
429deb0eb7SJason Gunthorpe module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
439deb0eb7SJason Gunthorpe MODULE_PARM_DESC(suspend_pcr,
4439f5712bSDmitry Torokhov 		 "PCR to use for dummy writes to facilitate flush on suspend.");
459deb0eb7SJason Gunthorpe 
46d856c00fSTomas Winkler /**
47d856c00fSTomas Winkler  * tpm_calc_ordinal_duration() - calculate the maximum command duration
48d856c00fSTomas Winkler  * @chip:    TPM chip to use.
49d856c00fSTomas Winkler  * @ordinal: TPM command ordinal.
50d856c00fSTomas Winkler  *
51d856c00fSTomas Winkler  * The function returns the maximum amount of time the chip could take
52d856c00fSTomas Winkler  * to return the result for a particular ordinal in jiffies.
53d856c00fSTomas Winkler  *
54d856c00fSTomas Winkler  * Return: A maximal duration time for an ordinal in jiffies.
55d856c00fSTomas Winkler  */
56d856c00fSTomas Winkler unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
57d856c00fSTomas Winkler {
58d856c00fSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
59d856c00fSTomas Winkler 		return tpm2_calc_ordinal_duration(chip, ordinal);
60d856c00fSTomas Winkler 	else
61d856c00fSTomas Winkler 		return tpm1_calc_ordinal_duration(chip, ordinal);
62d856c00fSTomas Winkler }
63d856c00fSTomas Winkler EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
64d856c00fSTomas Winkler 
6547a6c28bSJarkko Sakkinen static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
669deb0eb7SJason Gunthorpe {
67b34b77a9SJarkko Sakkinen 	struct tpm_header *header = buf;
68745b361eSJarkko Sakkinen 	int rc;
69745b361eSJarkko Sakkinen 	ssize_t len = 0;
709deb0eb7SJason Gunthorpe 	u32 count, ordinal;
719deb0eb7SJason Gunthorpe 	unsigned long stop;
729deb0eb7SJason Gunthorpe 
73c3465a37SJarkko Sakkinen 	if (bufsiz < TPM_HEADER_SIZE)
74c3465a37SJarkko Sakkinen 		return -EINVAL;
75ebfd7532SJarkko Sakkinen 
769deb0eb7SJason Gunthorpe 	if (bufsiz > TPM_BUFSIZE)
779deb0eb7SJason Gunthorpe 		bufsiz = TPM_BUFSIZE;
789deb0eb7SJason Gunthorpe 
79720b0711SJarkko Sakkinen 	count = be32_to_cpu(header->length);
80720b0711SJarkko Sakkinen 	ordinal = be32_to_cpu(header->ordinal);
819deb0eb7SJason Gunthorpe 	if (count == 0)
829deb0eb7SJason Gunthorpe 		return -ENODATA;
839deb0eb7SJason Gunthorpe 	if (count > bufsiz) {
848cfffc9dSJason Gunthorpe 		dev_err(&chip->dev,
859deb0eb7SJason Gunthorpe 			"invalid count value %x %zx\n", count, bufsiz);
869deb0eb7SJason Gunthorpe 		return -E2BIG;
879deb0eb7SJason Gunthorpe 	}
889deb0eb7SJason Gunthorpe 
8962c09e12SWinkler, Tomas 	rc = chip->ops->send(chip, buf, count);
909deb0eb7SJason Gunthorpe 	if (rc < 0) {
91402149c6SStefan Berger 		if (rc != -EPIPE)
928cfffc9dSJason Gunthorpe 			dev_err(&chip->dev,
93f5595f5bSJarkko Sakkinen 				"%s: send(): error %d\n", __func__, rc);
9429b47ce9SJarkko Sakkinen 		return rc;
959deb0eb7SJason Gunthorpe 	}
969deb0eb7SJason Gunthorpe 
97f5595f5bSJarkko Sakkinen 	/* A sanity check. send() should just return zero on success e.g.
98f5595f5bSJarkko Sakkinen 	 * not the command length.
99f5595f5bSJarkko Sakkinen 	 */
100f5595f5bSJarkko Sakkinen 	if (rc > 0) {
101f5595f5bSJarkko Sakkinen 		dev_warn(&chip->dev,
102f5595f5bSJarkko Sakkinen 			 "%s: send(): invalid value %d\n", __func__, rc);
103f5595f5bSJarkko Sakkinen 		rc = 0;
104f5595f5bSJarkko Sakkinen 	}
105f5595f5bSJarkko Sakkinen 
106570a3609SChristophe Ricard 	if (chip->flags & TPM_CHIP_FLAG_IRQ)
1079deb0eb7SJason Gunthorpe 		goto out_recv;
1089deb0eb7SJason Gunthorpe 
109d856c00fSTomas Winkler 	stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
1109deb0eb7SJason Gunthorpe 	do {
1115f82e9f0SJason Gunthorpe 		u8 status = chip->ops->status(chip);
1125f82e9f0SJason Gunthorpe 		if ((status & chip->ops->req_complete_mask) ==
1135f82e9f0SJason Gunthorpe 		    chip->ops->req_complete_val)
1149deb0eb7SJason Gunthorpe 			goto out_recv;
1159deb0eb7SJason Gunthorpe 
1165f82e9f0SJason Gunthorpe 		if (chip->ops->req_canceled(chip, status)) {
1178cfffc9dSJason Gunthorpe 			dev_err(&chip->dev, "Operation Canceled\n");
11829b47ce9SJarkko Sakkinen 			return -ECANCELED;
1199deb0eb7SJason Gunthorpe 		}
1209deb0eb7SJason Gunthorpe 
12159f5a6b0SNayna Jain 		tpm_msleep(TPM_TIMEOUT_POLL);
1229deb0eb7SJason Gunthorpe 		rmb();
1239deb0eb7SJason Gunthorpe 	} while (time_before(jiffies, stop));
1249deb0eb7SJason Gunthorpe 
1255f82e9f0SJason Gunthorpe 	chip->ops->cancel(chip);
1268cfffc9dSJason Gunthorpe 	dev_err(&chip->dev, "Operation Timed out\n");
12729b47ce9SJarkko Sakkinen 	return -ETIME;
1289deb0eb7SJason Gunthorpe 
1299deb0eb7SJason Gunthorpe out_recv:
13062c09e12SWinkler, Tomas 	len = chip->ops->recv(chip, buf, bufsiz);
131745b361eSJarkko Sakkinen 	if (len < 0) {
132745b361eSJarkko Sakkinen 		rc = len;
133304ff672SJarkko Sakkinen 		dev_err(&chip->dev, "tpm_transmit: tpm_recv: error %d\n", rc);
134304ff672SJarkko Sakkinen 	} else if (len < TPM_HEADER_SIZE || len != be32_to_cpu(header->length))
135a147918eSJarkko Sakkinen 		rc = -EFAULT;
136a147918eSJarkko Sakkinen 
137745b361eSJarkko Sakkinen 	return rc ? rc : len;
1389deb0eb7SJason Gunthorpe }
1399deb0eb7SJason Gunthorpe 
140f865c196SWinkler, Tomas /**
141e2fb992dSJames Bottomley  * tpm_transmit - Internal kernel interface to transmit TPM commands.
142412eb585SJarkko Sakkinen  * @chip:	a TPM chip to use
143412eb585SJarkko Sakkinen  * @buf:	a TPM command buffer
144e2fb992dSJames Bottomley  * @bufsiz:	length of the TPM command buffer
145e2fb992dSJames Bottomley  *
146412eb585SJarkko Sakkinen  * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from
147412eb585SJarkko Sakkinen  * the TPM and retransmits the command after a delay up to a maximum wait of
148412eb585SJarkko Sakkinen  * TPM2_DURATION_LONG.
149e2fb992dSJames Bottomley  *
150412eb585SJarkko Sakkinen  * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0
151412eb585SJarkko Sakkinen  * only.
152e2fb992dSJames Bottomley  *
153e2fb992dSJames Bottomley  * Return:
154412eb585SJarkko Sakkinen  * * The response length	- OK
155412eb585SJarkko Sakkinen  * * -errno			- A system error
156e2fb992dSJames Bottomley  */
15747a6c28bSJarkko Sakkinen ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
158e2fb992dSJames Bottomley {
159b34b77a9SJarkko Sakkinen 	struct tpm_header *header = (struct tpm_header *)buf;
160e2fb992dSJames Bottomley 	/* space for header and handles */
161e2fb992dSJames Bottomley 	u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
162e2fb992dSJames Bottomley 	unsigned int delay_msec = TPM2_DURATION_SHORT;
163e2fb992dSJames Bottomley 	u32 rc = 0;
164e2fb992dSJames Bottomley 	ssize_t ret;
1655faafbabSJarkko Sakkinen 	const size_t save_size = min(sizeof(save), bufsiz);
1662be8ffedSJames Bottomley 	/* the command code is where the return code will be */
1672be8ffedSJames Bottomley 	u32 cc = be32_to_cpu(header->return_code);
168e2fb992dSJames Bottomley 
169e2fb992dSJames Bottomley 	/*
170e2fb992dSJames Bottomley 	 * Subtlety here: if we have a space, the handles will be
171e2fb992dSJames Bottomley 	 * transformed, so when we restore the header we also have to
172e2fb992dSJames Bottomley 	 * restore the handles.
173e2fb992dSJames Bottomley 	 */
174e2fb992dSJames Bottomley 	memcpy(save, buf, save_size);
175e2fb992dSJames Bottomley 
176e2fb992dSJames Bottomley 	for (;;) {
17747a6c28bSJarkko Sakkinen 		ret = tpm_try_transmit(chip, buf, bufsiz);
178e2fb992dSJames Bottomley 		if (ret < 0)
179e2fb992dSJames Bottomley 			break;
180e2fb992dSJames Bottomley 		rc = be32_to_cpu(header->return_code);
1812be8ffedSJames Bottomley 		if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
1822be8ffedSJames Bottomley 			break;
1832be8ffedSJames Bottomley 		/*
1842be8ffedSJames Bottomley 		 * return immediately if self test returns test
1852be8ffedSJames Bottomley 		 * still running to shorten boot time.
1862be8ffedSJames Bottomley 		 */
1872be8ffedSJames Bottomley 		if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
188e2fb992dSJames Bottomley 			break;
18992980756SNayna Jain 
190e2fb992dSJames Bottomley 		if (delay_msec > TPM2_DURATION_LONG) {
1912be8ffedSJames Bottomley 			if (rc == TPM2_RC_RETRY)
1922be8ffedSJames Bottomley 				dev_err(&chip->dev, "in retry loop\n");
1932be8ffedSJames Bottomley 			else
1942be8ffedSJames Bottomley 				dev_err(&chip->dev,
1952be8ffedSJames Bottomley 					"self test is still running\n");
196e2fb992dSJames Bottomley 			break;
197e2fb992dSJames Bottomley 		}
198e2fb992dSJames Bottomley 		tpm_msleep(delay_msec);
19992980756SNayna Jain 		delay_msec *= 2;
200e2fb992dSJames Bottomley 		memcpy(buf, save, save_size);
201e2fb992dSJames Bottomley 	}
202e2fb992dSJames Bottomley 	return ret;
203e2fb992dSJames Bottomley }
204412eb585SJarkko Sakkinen 
205e2fb992dSJames Bottomley /**
20665520d46SWinkler, Tomas  * tpm_transmit_cmd - send a tpm command to the device
207412eb585SJarkko Sakkinen  * @chip:			a TPM chip to use
208412eb585SJarkko Sakkinen  * @buf:			a TPM command buffer
209c659af78SStefan Berger  * @min_rsp_body_length:	minimum expected length of response body
210f865c196SWinkler, Tomas  * @desc:			command description used in the error message
211f865c196SWinkler, Tomas  *
212f865c196SWinkler, Tomas  * Return:
213412eb585SJarkko Sakkinen  * * 0		- OK
214412eb585SJarkko Sakkinen  * * -errno	- A system error
215412eb585SJarkko Sakkinen  * * TPM_RC	- A TPM error
216f865c196SWinkler, Tomas  */
2175faafbabSJarkko Sakkinen ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
21847a6c28bSJarkko Sakkinen 			 size_t min_rsp_body_length, const char *desc)
2199deb0eb7SJason Gunthorpe {
220b34b77a9SJarkko Sakkinen 	const struct tpm_header *header = (struct tpm_header *)buf->data;
2219deb0eb7SJason Gunthorpe 	int err;
222c659af78SStefan Berger 	ssize_t len;
2239deb0eb7SJason Gunthorpe 
22447a6c28bSJarkko Sakkinen 	len = tpm_transmit(chip, buf->data, PAGE_SIZE);
2259deb0eb7SJason Gunthorpe 	if (len <  0)
2269deb0eb7SJason Gunthorpe 		return len;
22787155b73SJarkko Sakkinen 
22887155b73SJarkko Sakkinen 	err = be32_to_cpu(header->return_code);
2290d6d0d62SJavier Martinez Canillas 	if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED
23008a8112aSJerry Snitselaar 	    && err != TPM2_RC_TESTING && desc)
2318cfffc9dSJason Gunthorpe 		dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
23271ed848fSJarkko Sakkinen 			desc);
233c659af78SStefan Berger 	if (err)
2349deb0eb7SJason Gunthorpe 		return err;
235c659af78SStefan Berger 
236c659af78SStefan Berger 	if (len < min_rsp_body_length + TPM_HEADER_SIZE)
237c659af78SStefan Berger 		return -EFAULT;
238c659af78SStefan Berger 
239c659af78SStefan Berger 	return 0;
2409deb0eb7SJason Gunthorpe }
241be4c9acfSStefan Berger EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
2429deb0eb7SJason Gunthorpe 
2439deb0eb7SJason Gunthorpe int tpm_get_timeouts(struct tpm_chip *chip)
2449deb0eb7SJason Gunthorpe {
245d1d253cfSJason Gunthorpe 	if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
246d1d253cfSJason Gunthorpe 		return 0;
247d1d253cfSJason Gunthorpe 
24870a3199aSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
24970a3199aSTomas Winkler 		return tpm2_get_timeouts(chip);
25070a3199aSTomas Winkler 	else
25170a3199aSTomas Winkler 		return tpm1_get_timeouts(chip);
2529deb0eb7SJason Gunthorpe }
2539deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_timeouts);
2549deb0eb7SJason Gunthorpe 
2559deb0eb7SJason Gunthorpe /**
256aad887f6SJarkko Sakkinen  * tpm_is_tpm2 - do we a have a TPM2 chip?
257aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
258954650efSJarkko Sakkinen  *
259aad887f6SJarkko Sakkinen  * Return:
260aad887f6SJarkko Sakkinen  * 1 if we have a TPM2 chip.
261aad887f6SJarkko Sakkinen  * 0 if we don't have a TPM2 chip.
262aad887f6SJarkko Sakkinen  * A negative number for system errors (errno).
263954650efSJarkko Sakkinen  */
264aad887f6SJarkko Sakkinen int tpm_is_tpm2(struct tpm_chip *chip)
265954650efSJarkko Sakkinen {
266954650efSJarkko Sakkinen 	int rc;
267954650efSJarkko Sakkinen 
268fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
269aad887f6SJarkko Sakkinen 	if (!chip)
270954650efSJarkko Sakkinen 		return -ENODEV;
271954650efSJarkko Sakkinen 
272954650efSJarkko Sakkinen 	rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
273954650efSJarkko Sakkinen 
2744e26195fSJason Gunthorpe 	tpm_put_ops(chip);
275954650efSJarkko Sakkinen 
276954650efSJarkko Sakkinen 	return rc;
277954650efSJarkko Sakkinen }
278954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_is_tpm2);
279954650efSJarkko Sakkinen 
280954650efSJarkko Sakkinen /**
281aad887f6SJarkko Sakkinen  * tpm_pcr_read - read a PCR value from SHA1 bank
282aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
283aad887f6SJarkko Sakkinen  * @pcr_idx:	the PCR to be retrieved
284*879b5892SRoberto Sassu  * @digest:	the PCR bank and buffer current PCR value is written to
2859deb0eb7SJason Gunthorpe  *
286aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
2879deb0eb7SJason Gunthorpe  */
288*879b5892SRoberto Sassu int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
289*879b5892SRoberto Sassu 		 struct tpm_digest *digest)
2909deb0eb7SJason Gunthorpe {
2919deb0eb7SJason Gunthorpe 	int rc;
2929deb0eb7SJason Gunthorpe 
293fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
294aad887f6SJarkko Sakkinen 	if (!chip)
2959deb0eb7SJason Gunthorpe 		return -ENODEV;
296d4a31756STomas Winkler 
2977a1d7e6dSJarkko Sakkinen 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
298*879b5892SRoberto Sassu 		rc = tpm2_pcr_read(chip, pcr_idx, digest, NULL);
2997a1d7e6dSJarkko Sakkinen 	else
300*879b5892SRoberto Sassu 		rc = tpm1_pcr_read(chip, pcr_idx, digest->digest);
301d4a31756STomas Winkler 
3024e26195fSJason Gunthorpe 	tpm_put_ops(chip);
3039deb0eb7SJason Gunthorpe 	return rc;
3049deb0eb7SJason Gunthorpe }
3059deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_read);
3069deb0eb7SJason Gunthorpe 
3079deb0eb7SJason Gunthorpe /**
308aad887f6SJarkko Sakkinen  * tpm_pcr_extend - extend a PCR value in SHA1 bank.
309aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
310aad887f6SJarkko Sakkinen  * @pcr_idx:	the PCR to be retrieved
311aad887f6SJarkko Sakkinen  * @hash:	the hash value used to extend the PCR value
3129deb0eb7SJason Gunthorpe  *
313*879b5892SRoberto Sassu  * Note: with TPM 2.0 extends also those banks for which no digest was
314*879b5892SRoberto Sassu  * specified in order to prevent malicious use of those PCR banks.
315aad887f6SJarkko Sakkinen  *
316aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
3179deb0eb7SJason Gunthorpe  */
31895adc6b4STomas Winkler int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash)
3199deb0eb7SJason Gunthorpe {
3209deb0eb7SJason Gunthorpe 	int rc;
321aa042475SRoberto Sassu 	struct tpm_digest *digest_list;
322c1f92b4bSNayna Jain 	int i;
3239deb0eb7SJason Gunthorpe 
324fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
325aad887f6SJarkko Sakkinen 	if (!chip)
3269deb0eb7SJason Gunthorpe 		return -ENODEV;
3279deb0eb7SJason Gunthorpe 
3287a1d7e6dSJarkko Sakkinen 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
329bcfff838SRoberto Sassu 		digest_list = kcalloc(chip->nr_allocated_banks,
330bcfff838SRoberto Sassu 				      sizeof(*digest_list), GFP_KERNEL);
331bcfff838SRoberto Sassu 		if (!digest_list)
332bcfff838SRoberto Sassu 			return -ENOMEM;
333c1f92b4bSNayna Jain 
334bcfff838SRoberto Sassu 		for (i = 0; i < chip->nr_allocated_banks; i++) {
335*879b5892SRoberto Sassu 			digest_list[i].alg_id = chip->allocated_banks[i].alg_id;
336c1f92b4bSNayna Jain 			memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
337c1f92b4bSNayna Jain 		}
338c1f92b4bSNayna Jain 
339bcfff838SRoberto Sassu 		rc = tpm2_pcr_extend(chip, pcr_idx, chip->nr_allocated_banks,
340bcfff838SRoberto Sassu 				     digest_list);
341bcfff838SRoberto Sassu 		kfree(digest_list);
3424e26195fSJason Gunthorpe 		tpm_put_ops(chip);
3437a1d7e6dSJarkko Sakkinen 		return rc;
3447a1d7e6dSJarkko Sakkinen 	}
3457a1d7e6dSJarkko Sakkinen 
346175d5b2aSRoberto Sassu 	rc = tpm1_pcr_extend(chip, pcr_idx, hash,
3479deb0eb7SJason Gunthorpe 			     "attempting extend a PCR value");
3484e26195fSJason Gunthorpe 	tpm_put_ops(chip);
3499deb0eb7SJason Gunthorpe 	return rc;
3509deb0eb7SJason Gunthorpe }
3519deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_extend);
3529deb0eb7SJason Gunthorpe 
3539deb0eb7SJason Gunthorpe /**
354aad887f6SJarkko Sakkinen  * tpm_send - send a TPM command
355aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
356aad887f6SJarkko Sakkinen  * @cmd:	a TPM command buffer
357aad887f6SJarkko Sakkinen  * @buflen:	the length of the TPM command buffer
358aad887f6SJarkko Sakkinen  *
359aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
360aad887f6SJarkko Sakkinen  */
361aad887f6SJarkko Sakkinen int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
3629deb0eb7SJason Gunthorpe {
363412eb585SJarkko Sakkinen 	struct tpm_buf buf;
3649deb0eb7SJason Gunthorpe 	int rc;
3659deb0eb7SJason Gunthorpe 
366fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
367aad887f6SJarkko Sakkinen 	if (!chip)
3689deb0eb7SJason Gunthorpe 		return -ENODEV;
3699deb0eb7SJason Gunthorpe 
370412eb585SJarkko Sakkinen 	rc = tpm_buf_init(&buf, 0, 0);
371412eb585SJarkko Sakkinen 	if (rc)
372412eb585SJarkko Sakkinen 		goto out;
373412eb585SJarkko Sakkinen 
374412eb585SJarkko Sakkinen 	memcpy(buf.data, cmd, buflen);
37547a6c28bSJarkko Sakkinen 	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to a send a command");
376412eb585SJarkko Sakkinen 	tpm_buf_destroy(&buf);
377412eb585SJarkko Sakkinen out:
3784e26195fSJason Gunthorpe 	tpm_put_ops(chip);
3799deb0eb7SJason Gunthorpe 	return rc;
3809deb0eb7SJason Gunthorpe }
3819deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_send);
3829deb0eb7SJason Gunthorpe 
383b03c4370STomas Winkler int tpm_auto_startup(struct tpm_chip *chip)
384b03c4370STomas Winkler {
385b03c4370STomas Winkler 	int rc;
386b03c4370STomas Winkler 
387b03c4370STomas Winkler 	if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
388b03c4370STomas Winkler 		return 0;
389b03c4370STomas Winkler 
390b03c4370STomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
391b03c4370STomas Winkler 		rc = tpm2_auto_startup(chip);
392b03c4370STomas Winkler 	else
393b03c4370STomas Winkler 		rc = tpm1_auto_startup(chip);
394b03c4370STomas Winkler 
395b03c4370STomas Winkler 	return rc;
396b03c4370STomas Winkler }
397b03c4370STomas Winkler 
3989deb0eb7SJason Gunthorpe /*
3999deb0eb7SJason Gunthorpe  * We are about to suspend. Save the TPM state
4009deb0eb7SJason Gunthorpe  * so that it can be restored.
4019deb0eb7SJason Gunthorpe  */
4029deb0eb7SJason Gunthorpe int tpm_pm_suspend(struct device *dev)
4039deb0eb7SJason Gunthorpe {
404ec03c50bSStefan Berger 	struct tpm_chip *chip = dev_get_drvdata(dev);
405c82a330cSTomas Winkler 	int rc = 0;
4069deb0eb7SJason Gunthorpe 
407c82a330cSTomas Winkler 	if (!chip)
4089deb0eb7SJason Gunthorpe 		return -ENODEV;
4099deb0eb7SJason Gunthorpe 
410b5d0ebc9SEnric Balletbo i Serra 	if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
411b5d0ebc9SEnric Balletbo i Serra 		return 0;
412b5d0ebc9SEnric Balletbo i Serra 
413a3fbfae8SJarkko Sakkinen 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
414a3fbfae8SJarkko Sakkinen 		mutex_lock(&chip->tpm_mutex);
41547a6c28bSJarkko Sakkinen 		if (!tpm_chip_start(chip)) {
41674d6b3ceSJarkko Sakkinen 			tpm2_shutdown(chip, TPM2_SU_STATE);
41747a6c28bSJarkko Sakkinen 			tpm_chip_stop(chip);
418a3fbfae8SJarkko Sakkinen 		}
419a3fbfae8SJarkko Sakkinen 		mutex_unlock(&chip->tpm_mutex);
420a3fbfae8SJarkko Sakkinen 	} else {
421c82a330cSTomas Winkler 		rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
422a3fbfae8SJarkko Sakkinen 	}
4239deb0eb7SJason Gunthorpe 
4249deb0eb7SJason Gunthorpe 	return rc;
4259deb0eb7SJason Gunthorpe }
4269deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_suspend);
4279deb0eb7SJason Gunthorpe 
4289deb0eb7SJason Gunthorpe /*
4299deb0eb7SJason Gunthorpe  * Resume from a power safe. The BIOS already restored
4309deb0eb7SJason Gunthorpe  * the TPM state.
4319deb0eb7SJason Gunthorpe  */
4329deb0eb7SJason Gunthorpe int tpm_pm_resume(struct device *dev)
4339deb0eb7SJason Gunthorpe {
434ec03c50bSStefan Berger 	struct tpm_chip *chip = dev_get_drvdata(dev);
4359deb0eb7SJason Gunthorpe 
4369deb0eb7SJason Gunthorpe 	if (chip == NULL)
4379deb0eb7SJason Gunthorpe 		return -ENODEV;
4389deb0eb7SJason Gunthorpe 
4399deb0eb7SJason Gunthorpe 	return 0;
4409deb0eb7SJason Gunthorpe }
4419deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_resume);
4429deb0eb7SJason Gunthorpe 
4439deb0eb7SJason Gunthorpe /**
444aad887f6SJarkko Sakkinen  * tpm_get_random() - get random bytes from the TPM's RNG
445aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
4469deb0eb7SJason Gunthorpe  * @out:	destination buffer for the random bytes
4479deb0eb7SJason Gunthorpe  * @max:	the max number of bytes to write to @out
4489deb0eb7SJason Gunthorpe  *
4497aee9c52STomas Winkler  * Return: number of random bytes read or a negative error value.
4509deb0eb7SJason Gunthorpe  */
451aad887f6SJarkko Sakkinen int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
4529deb0eb7SJason Gunthorpe {
453433d390fSTomas Winkler 	int rc;
4549deb0eb7SJason Gunthorpe 
455433d390fSTomas Winkler 	if (!out || max > TPM_MAX_RNG_DATA)
4563e14d83eSJarkko Sakkinen 		return -EINVAL;
4573e14d83eSJarkko Sakkinen 
458fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
459aad887f6SJarkko Sakkinen 	if (!chip)
4609deb0eb7SJason Gunthorpe 		return -ENODEV;
4619deb0eb7SJason Gunthorpe 
462433d390fSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
463433d390fSTomas Winkler 		rc = tpm2_get_random(chip, out, max);
464433d390fSTomas Winkler 	else
465433d390fSTomas Winkler 		rc = tpm1_get_random(chip, out, max);
4669deb0eb7SJason Gunthorpe 
4674e26195fSJason Gunthorpe 	tpm_put_ops(chip);
468433d390fSTomas Winkler 	return rc;
4699deb0eb7SJason Gunthorpe }
4709deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_random);
4719deb0eb7SJason Gunthorpe 
472954650efSJarkko Sakkinen /**
473aad887f6SJarkko Sakkinen  * tpm_seal_trusted() - seal a trusted key payload
474aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
475954650efSJarkko Sakkinen  * @options:	authentication values and other options
476954650efSJarkko Sakkinen  * @payload:	the key data in clear and encrypted form
477954650efSJarkko Sakkinen  *
478aad887f6SJarkko Sakkinen  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
479aad887f6SJarkko Sakkinen  * the keyring subsystem.
480aad887f6SJarkko Sakkinen  *
481aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
482954650efSJarkko Sakkinen  */
483aad887f6SJarkko Sakkinen int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
484954650efSJarkko Sakkinen 		     struct trusted_key_options *options)
485954650efSJarkko Sakkinen {
486954650efSJarkko Sakkinen 	int rc;
487954650efSJarkko Sakkinen 
488fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
489aad887f6SJarkko Sakkinen 	if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
490954650efSJarkko Sakkinen 		return -ENODEV;
491954650efSJarkko Sakkinen 
492954650efSJarkko Sakkinen 	rc = tpm2_seal_trusted(chip, payload, options);
493954650efSJarkko Sakkinen 
4944e26195fSJason Gunthorpe 	tpm_put_ops(chip);
495954650efSJarkko Sakkinen 	return rc;
496954650efSJarkko Sakkinen }
497954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_seal_trusted);
498954650efSJarkko Sakkinen 
499954650efSJarkko Sakkinen /**
500954650efSJarkko Sakkinen  * tpm_unseal_trusted() - unseal a trusted key
501aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
502954650efSJarkko Sakkinen  * @options:	authentication values and other options
503954650efSJarkko Sakkinen  * @payload:	the key data in clear and encrypted form
504954650efSJarkko Sakkinen  *
505aad887f6SJarkko Sakkinen  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
506aad887f6SJarkko Sakkinen  * the keyring subsystem.
507aad887f6SJarkko Sakkinen  *
508aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
509954650efSJarkko Sakkinen  */
510aad887f6SJarkko Sakkinen int tpm_unseal_trusted(struct tpm_chip *chip,
511aad887f6SJarkko Sakkinen 		       struct trusted_key_payload *payload,
512954650efSJarkko Sakkinen 		       struct trusted_key_options *options)
513954650efSJarkko Sakkinen {
514954650efSJarkko Sakkinen 	int rc;
515954650efSJarkko Sakkinen 
516fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
517aad887f6SJarkko Sakkinen 	if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
518954650efSJarkko Sakkinen 		return -ENODEV;
519954650efSJarkko Sakkinen 
520954650efSJarkko Sakkinen 	rc = tpm2_unseal_trusted(chip, payload, options);
521954650efSJarkko Sakkinen 
5224e26195fSJason Gunthorpe 	tpm_put_ops(chip);
5234e26195fSJason Gunthorpe 
524954650efSJarkko Sakkinen 	return rc;
525954650efSJarkko Sakkinen }
526954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
527954650efSJarkko Sakkinen 
528313d21eeSJarkko Sakkinen static int __init tpm_init(void)
529313d21eeSJarkko Sakkinen {
530313d21eeSJarkko Sakkinen 	int rc;
531313d21eeSJarkko Sakkinen 
532313d21eeSJarkko Sakkinen 	tpm_class = class_create(THIS_MODULE, "tpm");
533313d21eeSJarkko Sakkinen 	if (IS_ERR(tpm_class)) {
534313d21eeSJarkko Sakkinen 		pr_err("couldn't create tpm class\n");
535313d21eeSJarkko Sakkinen 		return PTR_ERR(tpm_class);
536313d21eeSJarkko Sakkinen 	}
537313d21eeSJarkko Sakkinen 
538fdc915f7SJames Bottomley 	tpmrm_class = class_create(THIS_MODULE, "tpmrm");
539fdc915f7SJames Bottomley 	if (IS_ERR(tpmrm_class)) {
540fdc915f7SJames Bottomley 		pr_err("couldn't create tpmrm class\n");
5419e1b74a6STadeusz Struk 		rc = PTR_ERR(tpmrm_class);
5429e1b74a6STadeusz Struk 		goto out_destroy_tpm_class;
543fdc915f7SJames Bottomley 	}
544fdc915f7SJames Bottomley 
545fdc915f7SJames Bottomley 	rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
546313d21eeSJarkko Sakkinen 	if (rc < 0) {
547313d21eeSJarkko Sakkinen 		pr_err("tpm: failed to allocate char dev region\n");
5489e1b74a6STadeusz Struk 		goto out_destroy_tpmrm_class;
5499e1b74a6STadeusz Struk 	}
5509e1b74a6STadeusz Struk 
5519e1b74a6STadeusz Struk 	rc = tpm_dev_common_init();
5529e1b74a6STadeusz Struk 	if (rc) {
5539e1b74a6STadeusz Struk 		pr_err("tpm: failed to allocate char dev region\n");
5549e1b74a6STadeusz Struk 		goto out_unreg_chrdev;
555313d21eeSJarkko Sakkinen 	}
556313d21eeSJarkko Sakkinen 
557313d21eeSJarkko Sakkinen 	return 0;
5589e1b74a6STadeusz Struk 
5599e1b74a6STadeusz Struk out_unreg_chrdev:
5609e1b74a6STadeusz Struk 	unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
5619e1b74a6STadeusz Struk out_destroy_tpmrm_class:
5629e1b74a6STadeusz Struk 	class_destroy(tpmrm_class);
5639e1b74a6STadeusz Struk out_destroy_tpm_class:
5649e1b74a6STadeusz Struk 	class_destroy(tpm_class);
5659e1b74a6STadeusz Struk 
5669e1b74a6STadeusz Struk 	return rc;
567313d21eeSJarkko Sakkinen }
568313d21eeSJarkko Sakkinen 
569313d21eeSJarkko Sakkinen static void __exit tpm_exit(void)
570313d21eeSJarkko Sakkinen {
57115516788SStefan Berger 	idr_destroy(&dev_nums_idr);
572313d21eeSJarkko Sakkinen 	class_destroy(tpm_class);
573fdc915f7SJames Bottomley 	class_destroy(tpmrm_class);
574fdc915f7SJames Bottomley 	unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
5759e1b74a6STadeusz Struk 	tpm_dev_common_exit();
576313d21eeSJarkko Sakkinen }
577313d21eeSJarkko Sakkinen 
578313d21eeSJarkko Sakkinen subsys_initcall(tpm_init);
579313d21eeSJarkko Sakkinen module_exit(tpm_exit);
580313d21eeSJarkko Sakkinen 
5819deb0eb7SJason Gunthorpe MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
5829deb0eb7SJason Gunthorpe MODULE_DESCRIPTION("TPM Driver");
5839deb0eb7SJason Gunthorpe MODULE_VERSION("2.0");
5849deb0eb7SJason Gunthorpe MODULE_LICENSE("GPL");
585