xref: /openbmc/linux/drivers/char/tpm/tpm-interface.c (revision b34b77a99b1a4ccccb54f2c4c6ef982d6b008c15)
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 
65*b34b77a9SJarkko Sakkinen static int tpm_validate_command(struct tpm_chip *chip, struct tpm_space *space,
66*b34b77a9SJarkko Sakkinen 				const void *cmd, size_t len)
6758472f5cSJarkko Sakkinen {
68*b34b77a9SJarkko Sakkinen 	const struct tpm_header *header = cmd;
6958472f5cSJarkko Sakkinen 	int i;
7058472f5cSJarkko Sakkinen 	u32 cc;
7158472f5cSJarkko Sakkinen 	u32 attrs;
7258472f5cSJarkko Sakkinen 	unsigned int nr_handles;
7358472f5cSJarkko Sakkinen 
7458472f5cSJarkko Sakkinen 	if (len < TPM_HEADER_SIZE)
75095531f8SJavier Martinez Canillas 		return -EINVAL;
7658472f5cSJarkko Sakkinen 
77745b361eSJarkko Sakkinen 	if (!space)
78095531f8SJavier Martinez Canillas 		return 0;
79745b361eSJarkko Sakkinen 
8058472f5cSJarkko Sakkinen 	if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) {
8158472f5cSJarkko Sakkinen 		cc = be32_to_cpu(header->ordinal);
8258472f5cSJarkko Sakkinen 
8358472f5cSJarkko Sakkinen 		i = tpm2_find_cc(chip, cc);
8458472f5cSJarkko Sakkinen 		if (i < 0) {
8558472f5cSJarkko Sakkinen 			dev_dbg(&chip->dev, "0x%04X is an invalid command\n",
8658472f5cSJarkko Sakkinen 				cc);
87095531f8SJavier Martinez Canillas 			return -EOPNOTSUPP;
8858472f5cSJarkko Sakkinen 		}
8958472f5cSJarkko Sakkinen 
9058472f5cSJarkko Sakkinen 		attrs = chip->cc_attrs_tbl[i];
9158472f5cSJarkko Sakkinen 		nr_handles =
9258472f5cSJarkko Sakkinen 			4 * ((attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0));
9358472f5cSJarkko Sakkinen 		if (len < TPM_HEADER_SIZE + 4 * nr_handles)
9458472f5cSJarkko Sakkinen 			goto err_len;
9558472f5cSJarkko Sakkinen 	}
9658472f5cSJarkko Sakkinen 
97095531f8SJavier Martinez Canillas 	return 0;
9858472f5cSJarkko Sakkinen err_len:
9958472f5cSJarkko Sakkinen 	dev_dbg(&chip->dev,
10058472f5cSJarkko Sakkinen 		"%s: insufficient command length %zu", __func__, len);
101095531f8SJavier Martinez Canillas 	return -EINVAL;
10258472f5cSJarkko Sakkinen }
10358472f5cSJarkko Sakkinen 
104627448e8STomas Winkler static int tpm_request_locality(struct tpm_chip *chip, unsigned int flags)
105888d867dSTomas Winkler {
106888d867dSTomas Winkler 	int rc;
107888d867dSTomas Winkler 
10858bac8ccSJarkko Sakkinen 	if (flags & TPM_TRANSMIT_NESTED)
109627448e8STomas Winkler 		return 0;
110627448e8STomas Winkler 
111888d867dSTomas Winkler 	if (!chip->ops->request_locality)
112888d867dSTomas Winkler 		return 0;
113888d867dSTomas Winkler 
114888d867dSTomas Winkler 	rc = chip->ops->request_locality(chip, 0);
115888d867dSTomas Winkler 	if (rc < 0)
116888d867dSTomas Winkler 		return rc;
117888d867dSTomas Winkler 
118888d867dSTomas Winkler 	chip->locality = rc;
119888d867dSTomas Winkler 
120888d867dSTomas Winkler 	return 0;
121888d867dSTomas Winkler }
122888d867dSTomas Winkler 
123627448e8STomas Winkler static void tpm_relinquish_locality(struct tpm_chip *chip, unsigned int flags)
124888d867dSTomas Winkler {
125888d867dSTomas Winkler 	int rc;
126888d867dSTomas Winkler 
12758bac8ccSJarkko Sakkinen 	if (flags & TPM_TRANSMIT_NESTED)
128627448e8STomas Winkler 		return;
129627448e8STomas Winkler 
130888d867dSTomas Winkler 	if (!chip->ops->relinquish_locality)
131888d867dSTomas Winkler 		return;
132888d867dSTomas Winkler 
133888d867dSTomas Winkler 	rc = chip->ops->relinquish_locality(chip, chip->locality);
134888d867dSTomas Winkler 	if (rc)
135888d867dSTomas Winkler 		dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
136888d867dSTomas Winkler 
137888d867dSTomas Winkler 	chip->locality = -1;
138888d867dSTomas Winkler }
139888d867dSTomas Winkler 
140627448e8STomas Winkler static int tpm_cmd_ready(struct tpm_chip *chip, unsigned int flags)
141627448e8STomas Winkler {
14258bac8ccSJarkko Sakkinen 	if (flags & TPM_TRANSMIT_NESTED)
143627448e8STomas Winkler 		return 0;
144627448e8STomas Winkler 
145627448e8STomas Winkler 	if (!chip->ops->cmd_ready)
146627448e8STomas Winkler 		return 0;
147627448e8STomas Winkler 
148627448e8STomas Winkler 	return chip->ops->cmd_ready(chip);
149627448e8STomas Winkler }
150627448e8STomas Winkler 
151627448e8STomas Winkler static int tpm_go_idle(struct tpm_chip *chip, unsigned int flags)
152627448e8STomas Winkler {
15358bac8ccSJarkko Sakkinen 	if (flags & TPM_TRANSMIT_NESTED)
154627448e8STomas Winkler 		return 0;
155627448e8STomas Winkler 
156627448e8STomas Winkler 	if (!chip->ops->go_idle)
157627448e8STomas Winkler 		return 0;
158627448e8STomas Winkler 
159627448e8STomas Winkler 	return chip->ops->go_idle(chip);
160627448e8STomas Winkler }
161627448e8STomas Winkler 
162*b34b77a9SJarkko Sakkinen static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space,
163*b34b77a9SJarkko Sakkinen 				void *buf, size_t bufsiz, unsigned int flags)
1649deb0eb7SJason Gunthorpe {
165*b34b77a9SJarkko Sakkinen 	struct tpm_header *header = buf;
166745b361eSJarkko Sakkinen 	int rc;
167745b361eSJarkko Sakkinen 	ssize_t len = 0;
1689deb0eb7SJason Gunthorpe 	u32 count, ordinal;
1699deb0eb7SJason Gunthorpe 	unsigned long stop;
170877c57d0SJarkko Sakkinen 	bool need_locality;
1719deb0eb7SJason Gunthorpe 
172095531f8SJavier Martinez Canillas 	rc = tpm_validate_command(chip, space, buf, bufsiz);
173095531f8SJavier Martinez Canillas 	if (rc == -EINVAL)
174095531f8SJavier Martinez Canillas 		return rc;
175095531f8SJavier Martinez Canillas 	/*
176095531f8SJavier Martinez Canillas 	 * If the command is not implemented by the TPM, synthesize a
177095531f8SJavier Martinez Canillas 	 * response with a TPM2_RC_COMMAND_CODE return for user-space.
178095531f8SJavier Martinez Canillas 	 */
179095531f8SJavier Martinez Canillas 	if (rc == -EOPNOTSUPP) {
180095531f8SJavier Martinez Canillas 		header->length = cpu_to_be32(sizeof(*header));
181095531f8SJavier Martinez Canillas 		header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
182095531f8SJavier Martinez Canillas 		header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
183095531f8SJavier Martinez Canillas 						  TSS2_RESMGR_TPM_RC_LAYER);
18436a11029SRicardo Schwarzmeier 		return sizeof(*header);
185095531f8SJavier Martinez Canillas 	}
186ebfd7532SJarkko Sakkinen 
1879deb0eb7SJason Gunthorpe 	if (bufsiz > TPM_BUFSIZE)
1889deb0eb7SJason Gunthorpe 		bufsiz = TPM_BUFSIZE;
1899deb0eb7SJason Gunthorpe 
1909deb0eb7SJason Gunthorpe 	count = be32_to_cpu(*((__be32 *) (buf + 2)));
1919deb0eb7SJason Gunthorpe 	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
1929deb0eb7SJason Gunthorpe 	if (count == 0)
1939deb0eb7SJason Gunthorpe 		return -ENODATA;
1949deb0eb7SJason Gunthorpe 	if (count > bufsiz) {
1958cfffc9dSJason Gunthorpe 		dev_err(&chip->dev,
1969deb0eb7SJason Gunthorpe 			"invalid count value %x %zx\n", count, bufsiz);
1979deb0eb7SJason Gunthorpe 		return -E2BIG;
1989deb0eb7SJason Gunthorpe 	}
1999deb0eb7SJason Gunthorpe 
20058bac8ccSJarkko Sakkinen 	if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
2019deb0eb7SJason Gunthorpe 		mutex_lock(&chip->tpm_mutex);
2029deb0eb7SJason Gunthorpe 
203b3e958ceSAzhar Shaikh 	if (chip->ops->clk_enable != NULL)
204b3e958ceSAzhar Shaikh 		chip->ops->clk_enable(chip, true);
205b3e958ceSAzhar Shaikh 
206877c57d0SJarkko Sakkinen 	/* Store the decision as chip->locality will be changed. */
207877c57d0SJarkko Sakkinen 	need_locality = chip->locality == -1;
208877c57d0SJarkko Sakkinen 
209627448e8STomas Winkler 	if (need_locality) {
210627448e8STomas Winkler 		rc = tpm_request_locality(chip, flags);
21101f54664STomas Winkler 		if (rc < 0) {
21201f54664STomas Winkler 			need_locality = false;
21301f54664STomas Winkler 			goto out_locality;
21401f54664STomas Winkler 		}
215877c57d0SJarkko Sakkinen 	}
216877c57d0SJarkko Sakkinen 
217627448e8STomas Winkler 	rc = tpm_cmd_ready(chip, flags);
218627448e8STomas Winkler 	if (rc)
21901f54664STomas Winkler 		goto out_locality;
220888d867dSTomas Winkler 
221745b361eSJarkko Sakkinen 	rc = tpm2_prepare_space(chip, space, ordinal, buf);
222745b361eSJarkko Sakkinen 	if (rc)
223745b361eSJarkko Sakkinen 		goto out;
224745b361eSJarkko Sakkinen 
22562c09e12SWinkler, Tomas 	rc = chip->ops->send(chip, buf, count);
2269deb0eb7SJason Gunthorpe 	if (rc < 0) {
227402149c6SStefan Berger 		if (rc != -EPIPE)
2288cfffc9dSJason Gunthorpe 			dev_err(&chip->dev,
229f5595f5bSJarkko Sakkinen 				"%s: send(): error %d\n", __func__, rc);
2309deb0eb7SJason Gunthorpe 		goto out;
2319deb0eb7SJason Gunthorpe 	}
2329deb0eb7SJason Gunthorpe 
233f5595f5bSJarkko Sakkinen 	/* A sanity check. send() should just return zero on success e.g.
234f5595f5bSJarkko Sakkinen 	 * not the command length.
235f5595f5bSJarkko Sakkinen 	 */
236f5595f5bSJarkko Sakkinen 	if (rc > 0) {
237f5595f5bSJarkko Sakkinen 		dev_warn(&chip->dev,
238f5595f5bSJarkko Sakkinen 			 "%s: send(): invalid value %d\n", __func__, rc);
239f5595f5bSJarkko Sakkinen 		rc = 0;
240f5595f5bSJarkko Sakkinen 	}
241f5595f5bSJarkko Sakkinen 
242570a3609SChristophe Ricard 	if (chip->flags & TPM_CHIP_FLAG_IRQ)
2439deb0eb7SJason Gunthorpe 		goto out_recv;
2449deb0eb7SJason Gunthorpe 
245d856c00fSTomas Winkler 	stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
2469deb0eb7SJason Gunthorpe 	do {
2475f82e9f0SJason Gunthorpe 		u8 status = chip->ops->status(chip);
2485f82e9f0SJason Gunthorpe 		if ((status & chip->ops->req_complete_mask) ==
2495f82e9f0SJason Gunthorpe 		    chip->ops->req_complete_val)
2509deb0eb7SJason Gunthorpe 			goto out_recv;
2519deb0eb7SJason Gunthorpe 
2525f82e9f0SJason Gunthorpe 		if (chip->ops->req_canceled(chip, status)) {
2538cfffc9dSJason Gunthorpe 			dev_err(&chip->dev, "Operation Canceled\n");
2549deb0eb7SJason Gunthorpe 			rc = -ECANCELED;
2559deb0eb7SJason Gunthorpe 			goto out;
2569deb0eb7SJason Gunthorpe 		}
2579deb0eb7SJason Gunthorpe 
25859f5a6b0SNayna Jain 		tpm_msleep(TPM_TIMEOUT_POLL);
2599deb0eb7SJason Gunthorpe 		rmb();
2609deb0eb7SJason Gunthorpe 	} while (time_before(jiffies, stop));
2619deb0eb7SJason Gunthorpe 
2625f82e9f0SJason Gunthorpe 	chip->ops->cancel(chip);
2638cfffc9dSJason Gunthorpe 	dev_err(&chip->dev, "Operation Timed out\n");
2649deb0eb7SJason Gunthorpe 	rc = -ETIME;
2659deb0eb7SJason Gunthorpe 	goto out;
2669deb0eb7SJason Gunthorpe 
2679deb0eb7SJason Gunthorpe out_recv:
26862c09e12SWinkler, Tomas 	len = chip->ops->recv(chip, buf, bufsiz);
269745b361eSJarkko Sakkinen 	if (len < 0) {
270745b361eSJarkko Sakkinen 		rc = len;
2718cfffc9dSJason Gunthorpe 		dev_err(&chip->dev,
272745b361eSJarkko Sakkinen 			"tpm_transmit: tpm_recv: error %d\n", rc);
273a147918eSJarkko Sakkinen 		goto out;
274745b361eSJarkko Sakkinen 	} else if (len < TPM_HEADER_SIZE) {
275a147918eSJarkko Sakkinen 		rc = -EFAULT;
276a147918eSJarkko Sakkinen 		goto out;
277a147918eSJarkko Sakkinen 	}
278a147918eSJarkko Sakkinen 
279745b361eSJarkko Sakkinen 	if (len != be32_to_cpu(header->length)) {
280745b361eSJarkko Sakkinen 		rc = -EFAULT;
281a147918eSJarkko Sakkinen 		goto out;
282745b361eSJarkko Sakkinen 	}
283745b361eSJarkko Sakkinen 
284745b361eSJarkko Sakkinen 	rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
285a147918eSJarkko Sakkinen 
2869deb0eb7SJason Gunthorpe out:
28701f54664STomas Winkler 	/* may fail but do not override previous error value in rc */
28801f54664STomas Winkler 	tpm_go_idle(chip, flags);
289888d867dSTomas Winkler 
29001f54664STomas Winkler out_locality:
291888d867dSTomas Winkler 	if (need_locality)
292627448e8STomas Winkler 		tpm_relinquish_locality(chip, flags);
293888d867dSTomas Winkler 
294b3e958ceSAzhar Shaikh 	if (chip->ops->clk_enable != NULL)
295b3e958ceSAzhar Shaikh 		chip->ops->clk_enable(chip, false);
296b3e958ceSAzhar Shaikh 
29758bac8ccSJarkko Sakkinen 	if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
2989deb0eb7SJason Gunthorpe 		mutex_unlock(&chip->tpm_mutex);
299745b361eSJarkko Sakkinen 	return rc ? rc : len;
3009deb0eb7SJason Gunthorpe }
3019deb0eb7SJason Gunthorpe 
302f865c196SWinkler, Tomas /**
303e2fb992dSJames Bottomley  * tpm_transmit - Internal kernel interface to transmit TPM commands.
304412eb585SJarkko Sakkinen  * @chip:	a TPM chip to use
305412eb585SJarkko Sakkinen  * @space:	a TPM space
306412eb585SJarkko Sakkinen  * @buf:	a TPM command buffer
307e2fb992dSJames Bottomley  * @bufsiz:	length of the TPM command buffer
308412eb585SJarkko Sakkinen  * @flags:	TPM transmit flags
309e2fb992dSJames Bottomley  *
310412eb585SJarkko Sakkinen  * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from
311412eb585SJarkko Sakkinen  * the TPM and retransmits the command after a delay up to a maximum wait of
312412eb585SJarkko Sakkinen  * TPM2_DURATION_LONG.
313e2fb992dSJames Bottomley  *
314412eb585SJarkko Sakkinen  * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0
315412eb585SJarkko Sakkinen  * only.
316e2fb992dSJames Bottomley  *
317e2fb992dSJames Bottomley  * Return:
318412eb585SJarkko Sakkinen  * * The response length	- OK
319412eb585SJarkko Sakkinen  * * -errno			- A system error
320e2fb992dSJames Bottomley  */
321e2fb992dSJames Bottomley ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
322e2fb992dSJames Bottomley 		     u8 *buf, size_t bufsiz, unsigned int flags)
323e2fb992dSJames Bottomley {
324*b34b77a9SJarkko Sakkinen 	struct tpm_header *header = (struct tpm_header *)buf;
325e2fb992dSJames Bottomley 	/* space for header and handles */
326e2fb992dSJames Bottomley 	u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
327e2fb992dSJames Bottomley 	unsigned int delay_msec = TPM2_DURATION_SHORT;
328e2fb992dSJames Bottomley 	u32 rc = 0;
329e2fb992dSJames Bottomley 	ssize_t ret;
330e2fb992dSJames Bottomley 	const size_t save_size = min(space ? sizeof(save) : TPM_HEADER_SIZE,
331e2fb992dSJames Bottomley 				     bufsiz);
3322be8ffedSJames Bottomley 	/* the command code is where the return code will be */
3332be8ffedSJames Bottomley 	u32 cc = be32_to_cpu(header->return_code);
334e2fb992dSJames Bottomley 
335e2fb992dSJames Bottomley 	/*
336e2fb992dSJames Bottomley 	 * Subtlety here: if we have a space, the handles will be
337e2fb992dSJames Bottomley 	 * transformed, so when we restore the header we also have to
338e2fb992dSJames Bottomley 	 * restore the handles.
339e2fb992dSJames Bottomley 	 */
340e2fb992dSJames Bottomley 	memcpy(save, buf, save_size);
341e2fb992dSJames Bottomley 
342e2fb992dSJames Bottomley 	for (;;) {
343e2fb992dSJames Bottomley 		ret = tpm_try_transmit(chip, space, buf, bufsiz, flags);
344e2fb992dSJames Bottomley 		if (ret < 0)
345e2fb992dSJames Bottomley 			break;
346e2fb992dSJames Bottomley 		rc = be32_to_cpu(header->return_code);
3472be8ffedSJames Bottomley 		if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
3482be8ffedSJames Bottomley 			break;
3492be8ffedSJames Bottomley 		/*
3502be8ffedSJames Bottomley 		 * return immediately if self test returns test
3512be8ffedSJames Bottomley 		 * still running to shorten boot time.
3522be8ffedSJames Bottomley 		 */
3532be8ffedSJames Bottomley 		if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
354e2fb992dSJames Bottomley 			break;
35592980756SNayna Jain 
356e2fb992dSJames Bottomley 		if (delay_msec > TPM2_DURATION_LONG) {
3572be8ffedSJames Bottomley 			if (rc == TPM2_RC_RETRY)
3582be8ffedSJames Bottomley 				dev_err(&chip->dev, "in retry loop\n");
3592be8ffedSJames Bottomley 			else
3602be8ffedSJames Bottomley 				dev_err(&chip->dev,
3612be8ffedSJames Bottomley 					"self test is still running\n");
362e2fb992dSJames Bottomley 			break;
363e2fb992dSJames Bottomley 		}
364e2fb992dSJames Bottomley 		tpm_msleep(delay_msec);
36592980756SNayna Jain 		delay_msec *= 2;
366e2fb992dSJames Bottomley 		memcpy(buf, save, save_size);
367e2fb992dSJames Bottomley 	}
368e2fb992dSJames Bottomley 	return ret;
369e2fb992dSJames Bottomley }
370412eb585SJarkko Sakkinen 
371e2fb992dSJames Bottomley /**
37265520d46SWinkler, Tomas  * tpm_transmit_cmd - send a tpm command to the device
373412eb585SJarkko Sakkinen  * @chip:			a TPM chip to use
374412eb585SJarkko Sakkinen  * @space:			a TPM space
375412eb585SJarkko Sakkinen  * @buf:			a TPM command buffer
376c659af78SStefan Berger  * @min_rsp_body_length:	minimum expected length of response body
377412eb585SJarkko Sakkinen  * @flags:			TPM transmit flags
378f865c196SWinkler, Tomas  * @desc:			command description used in the error message
379f865c196SWinkler, Tomas  *
380f865c196SWinkler, Tomas  * Return:
381412eb585SJarkko Sakkinen  * * 0		- OK
382412eb585SJarkko Sakkinen  * * -errno	- A system error
383412eb585SJarkko Sakkinen  * * TPM_RC	- A TPM error
384f865c196SWinkler, Tomas  */
385745b361eSJarkko Sakkinen ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
386412eb585SJarkko Sakkinen 			 struct tpm_buf *buf, size_t min_rsp_body_length,
387412eb585SJarkko Sakkinen 			 unsigned int flags, const char *desc)
3889deb0eb7SJason Gunthorpe {
389*b34b77a9SJarkko Sakkinen 	const struct tpm_header *header = (struct tpm_header *)buf->data;
3909deb0eb7SJason Gunthorpe 	int err;
391c659af78SStefan Berger 	ssize_t len;
3929deb0eb7SJason Gunthorpe 
393412eb585SJarkko Sakkinen 	len = tpm_transmit(chip, space, buf->data, PAGE_SIZE, flags);
3949deb0eb7SJason Gunthorpe 	if (len <  0)
3959deb0eb7SJason Gunthorpe 		return len;
39687155b73SJarkko Sakkinen 
39787155b73SJarkko Sakkinen 	err = be32_to_cpu(header->return_code);
3980d6d0d62SJavier Martinez Canillas 	if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED
39908a8112aSJerry Snitselaar 	    && err != TPM2_RC_TESTING && desc)
4008cfffc9dSJason Gunthorpe 		dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
40171ed848fSJarkko Sakkinen 			desc);
402c659af78SStefan Berger 	if (err)
4039deb0eb7SJason Gunthorpe 		return err;
404c659af78SStefan Berger 
405c659af78SStefan Berger 	if (len < min_rsp_body_length + TPM_HEADER_SIZE)
406c659af78SStefan Berger 		return -EFAULT;
407c659af78SStefan Berger 
408c659af78SStefan Berger 	return 0;
4099deb0eb7SJason Gunthorpe }
410be4c9acfSStefan Berger EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
4119deb0eb7SJason Gunthorpe 
4129deb0eb7SJason Gunthorpe int tpm_get_timeouts(struct tpm_chip *chip)
4139deb0eb7SJason Gunthorpe {
414d1d253cfSJason Gunthorpe 	if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
415d1d253cfSJason Gunthorpe 		return 0;
416d1d253cfSJason Gunthorpe 
41770a3199aSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
41870a3199aSTomas Winkler 		return tpm2_get_timeouts(chip);
41970a3199aSTomas Winkler 	else
42070a3199aSTomas Winkler 		return tpm1_get_timeouts(chip);
4219deb0eb7SJason Gunthorpe }
4229deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_timeouts);
4239deb0eb7SJason Gunthorpe 
4249deb0eb7SJason Gunthorpe /**
425aad887f6SJarkko Sakkinen  * tpm_is_tpm2 - do we a have a TPM2 chip?
426aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
427954650efSJarkko Sakkinen  *
428aad887f6SJarkko Sakkinen  * Return:
429aad887f6SJarkko Sakkinen  * 1 if we have a TPM2 chip.
430aad887f6SJarkko Sakkinen  * 0 if we don't have a TPM2 chip.
431aad887f6SJarkko Sakkinen  * A negative number for system errors (errno).
432954650efSJarkko Sakkinen  */
433aad887f6SJarkko Sakkinen int tpm_is_tpm2(struct tpm_chip *chip)
434954650efSJarkko Sakkinen {
435954650efSJarkko Sakkinen 	int rc;
436954650efSJarkko Sakkinen 
437fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
438aad887f6SJarkko Sakkinen 	if (!chip)
439954650efSJarkko Sakkinen 		return -ENODEV;
440954650efSJarkko Sakkinen 
441954650efSJarkko Sakkinen 	rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
442954650efSJarkko Sakkinen 
4434e26195fSJason Gunthorpe 	tpm_put_ops(chip);
444954650efSJarkko Sakkinen 
445954650efSJarkko Sakkinen 	return rc;
446954650efSJarkko Sakkinen }
447954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_is_tpm2);
448954650efSJarkko Sakkinen 
449954650efSJarkko Sakkinen /**
450aad887f6SJarkko Sakkinen  * tpm_pcr_read - read a PCR value from SHA1 bank
451aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
452aad887f6SJarkko Sakkinen  * @pcr_idx:	the PCR to be retrieved
453aad887f6SJarkko Sakkinen  * @res_buf:	the value of the PCR
4549deb0eb7SJason Gunthorpe  *
455aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
4569deb0eb7SJason Gunthorpe  */
45795adc6b4STomas Winkler int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
4589deb0eb7SJason Gunthorpe {
4599deb0eb7SJason Gunthorpe 	int rc;
4609deb0eb7SJason Gunthorpe 
461fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
462aad887f6SJarkko Sakkinen 	if (!chip)
4639deb0eb7SJason Gunthorpe 		return -ENODEV;
464d4a31756STomas Winkler 
4657a1d7e6dSJarkko Sakkinen 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
4667a1d7e6dSJarkko Sakkinen 		rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
4677a1d7e6dSJarkko Sakkinen 	else
468cfddcb05STomas Winkler 		rc = tpm1_pcr_read(chip, pcr_idx, res_buf);
469d4a31756STomas Winkler 
4704e26195fSJason Gunthorpe 	tpm_put_ops(chip);
4719deb0eb7SJason Gunthorpe 	return rc;
4729deb0eb7SJason Gunthorpe }
4739deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_read);
4749deb0eb7SJason Gunthorpe 
4759deb0eb7SJason Gunthorpe /**
476aad887f6SJarkko Sakkinen  * tpm_pcr_extend - extend a PCR value in SHA1 bank.
477aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
478aad887f6SJarkko Sakkinen  * @pcr_idx:	the PCR to be retrieved
479aad887f6SJarkko Sakkinen  * @hash:	the hash value used to extend the PCR value
4809deb0eb7SJason Gunthorpe  *
481aad887f6SJarkko Sakkinen  * Note: with TPM 2.0 extends also those banks with a known digest size to the
482aad887f6SJarkko Sakkinen  * cryto subsystem in order to prevent malicious use of those PCR banks. In the
483aad887f6SJarkko Sakkinen  * future we should dynamically determine digest sizes.
484aad887f6SJarkko Sakkinen  *
485aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
4869deb0eb7SJason Gunthorpe  */
48795adc6b4STomas Winkler int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash)
4889deb0eb7SJason Gunthorpe {
4899deb0eb7SJason Gunthorpe 	int rc;
490c1f92b4bSNayna Jain 	struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
491c1f92b4bSNayna Jain 	u32 count = 0;
492c1f92b4bSNayna Jain 	int i;
4939deb0eb7SJason Gunthorpe 
494fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
495aad887f6SJarkko Sakkinen 	if (!chip)
4969deb0eb7SJason Gunthorpe 		return -ENODEV;
4979deb0eb7SJason Gunthorpe 
4987a1d7e6dSJarkko Sakkinen 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
499c1f92b4bSNayna Jain 		memset(digest_list, 0, sizeof(digest_list));
500c1f92b4bSNayna Jain 
50170ea1636SDan Carpenter 		for (i = 0; i < ARRAY_SIZE(chip->active_banks) &&
50270ea1636SDan Carpenter 			    chip->active_banks[i] != TPM2_ALG_ERROR; i++) {
503c1f92b4bSNayna Jain 			digest_list[i].alg_id = chip->active_banks[i];
504c1f92b4bSNayna Jain 			memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
505c1f92b4bSNayna Jain 			count++;
506c1f92b4bSNayna Jain 		}
507c1f92b4bSNayna Jain 
508c1f92b4bSNayna Jain 		rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list);
5094e26195fSJason Gunthorpe 		tpm_put_ops(chip);
5107a1d7e6dSJarkko Sakkinen 		return rc;
5117a1d7e6dSJarkko Sakkinen 	}
5127a1d7e6dSJarkko Sakkinen 
513175d5b2aSRoberto Sassu 	rc = tpm1_pcr_extend(chip, pcr_idx, hash,
5149deb0eb7SJason Gunthorpe 			     "attempting extend a PCR value");
5154e26195fSJason Gunthorpe 	tpm_put_ops(chip);
5169deb0eb7SJason Gunthorpe 	return rc;
5179deb0eb7SJason Gunthorpe }
5189deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_extend);
5199deb0eb7SJason Gunthorpe 
5209deb0eb7SJason Gunthorpe /**
521aad887f6SJarkko Sakkinen  * tpm_send - send a TPM command
522aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
523aad887f6SJarkko Sakkinen  * @cmd:	a TPM command buffer
524aad887f6SJarkko Sakkinen  * @buflen:	the length of the TPM command buffer
525aad887f6SJarkko Sakkinen  *
526aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
527aad887f6SJarkko Sakkinen  */
528aad887f6SJarkko Sakkinen int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
5299deb0eb7SJason Gunthorpe {
530412eb585SJarkko Sakkinen 	struct tpm_buf buf;
5319deb0eb7SJason Gunthorpe 	int rc;
5329deb0eb7SJason Gunthorpe 
533fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
534aad887f6SJarkko Sakkinen 	if (!chip)
5359deb0eb7SJason Gunthorpe 		return -ENODEV;
5369deb0eb7SJason Gunthorpe 
537412eb585SJarkko Sakkinen 	rc = tpm_buf_init(&buf, 0, 0);
538412eb585SJarkko Sakkinen 	if (rc)
539412eb585SJarkko Sakkinen 		goto out;
540412eb585SJarkko Sakkinen 
541412eb585SJarkko Sakkinen 	memcpy(buf.data, cmd, buflen);
542412eb585SJarkko Sakkinen 	rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0,
543aad887f6SJarkko Sakkinen 			      "attempting to a send a command");
544412eb585SJarkko Sakkinen 	tpm_buf_destroy(&buf);
545412eb585SJarkko Sakkinen out:
5464e26195fSJason Gunthorpe 	tpm_put_ops(chip);
5479deb0eb7SJason Gunthorpe 	return rc;
5489deb0eb7SJason Gunthorpe }
5499deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_send);
5509deb0eb7SJason Gunthorpe 
551b03c4370STomas Winkler int tpm_auto_startup(struct tpm_chip *chip)
552b03c4370STomas Winkler {
553b03c4370STomas Winkler 	int rc;
554b03c4370STomas Winkler 
555b03c4370STomas Winkler 	if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
556b03c4370STomas Winkler 		return 0;
557b03c4370STomas Winkler 
558b03c4370STomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
559b03c4370STomas Winkler 		rc = tpm2_auto_startup(chip);
560b03c4370STomas Winkler 	else
561b03c4370STomas Winkler 		rc = tpm1_auto_startup(chip);
562b03c4370STomas Winkler 
563b03c4370STomas Winkler 	return rc;
564b03c4370STomas Winkler }
565b03c4370STomas Winkler 
5669deb0eb7SJason Gunthorpe /*
5679deb0eb7SJason Gunthorpe  * We are about to suspend. Save the TPM state
5689deb0eb7SJason Gunthorpe  * so that it can be restored.
5699deb0eb7SJason Gunthorpe  */
5709deb0eb7SJason Gunthorpe int tpm_pm_suspend(struct device *dev)
5719deb0eb7SJason Gunthorpe {
572ec03c50bSStefan Berger 	struct tpm_chip *chip = dev_get_drvdata(dev);
573c82a330cSTomas Winkler 	int rc = 0;
5749deb0eb7SJason Gunthorpe 
575c82a330cSTomas Winkler 	if (!chip)
5769deb0eb7SJason Gunthorpe 		return -ENODEV;
5779deb0eb7SJason Gunthorpe 
578b5d0ebc9SEnric Balletbo i Serra 	if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
579b5d0ebc9SEnric Balletbo i Serra 		return 0;
580b5d0ebc9SEnric Balletbo i Serra 
581c82a330cSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
58274d6b3ceSJarkko Sakkinen 		tpm2_shutdown(chip, TPM2_SU_STATE);
583c82a330cSTomas Winkler 	else
584c82a330cSTomas Winkler 		rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
5859deb0eb7SJason Gunthorpe 
5869deb0eb7SJason Gunthorpe 	return rc;
5879deb0eb7SJason Gunthorpe }
5889deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_suspend);
5899deb0eb7SJason Gunthorpe 
5909deb0eb7SJason Gunthorpe /*
5919deb0eb7SJason Gunthorpe  * Resume from a power safe. The BIOS already restored
5929deb0eb7SJason Gunthorpe  * the TPM state.
5939deb0eb7SJason Gunthorpe  */
5949deb0eb7SJason Gunthorpe int tpm_pm_resume(struct device *dev)
5959deb0eb7SJason Gunthorpe {
596ec03c50bSStefan Berger 	struct tpm_chip *chip = dev_get_drvdata(dev);
5979deb0eb7SJason Gunthorpe 
5989deb0eb7SJason Gunthorpe 	if (chip == NULL)
5999deb0eb7SJason Gunthorpe 		return -ENODEV;
6009deb0eb7SJason Gunthorpe 
6019deb0eb7SJason Gunthorpe 	return 0;
6029deb0eb7SJason Gunthorpe }
6039deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_resume);
6049deb0eb7SJason Gunthorpe 
6059deb0eb7SJason Gunthorpe /**
606aad887f6SJarkko Sakkinen  * tpm_get_random() - get random bytes from the TPM's RNG
607aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
6089deb0eb7SJason Gunthorpe  * @out:	destination buffer for the random bytes
6099deb0eb7SJason Gunthorpe  * @max:	the max number of bytes to write to @out
6109deb0eb7SJason Gunthorpe  *
6117aee9c52STomas Winkler  * Return: number of random bytes read or a negative error value.
6129deb0eb7SJason Gunthorpe  */
613aad887f6SJarkko Sakkinen int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
6149deb0eb7SJason Gunthorpe {
615433d390fSTomas Winkler 	int rc;
6169deb0eb7SJason Gunthorpe 
617433d390fSTomas Winkler 	if (!out || max > TPM_MAX_RNG_DATA)
6183e14d83eSJarkko Sakkinen 		return -EINVAL;
6193e14d83eSJarkko Sakkinen 
620fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
621aad887f6SJarkko Sakkinen 	if (!chip)
6229deb0eb7SJason Gunthorpe 		return -ENODEV;
6239deb0eb7SJason Gunthorpe 
624433d390fSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
625433d390fSTomas Winkler 		rc = tpm2_get_random(chip, out, max);
626433d390fSTomas Winkler 	else
627433d390fSTomas Winkler 		rc = tpm1_get_random(chip, out, max);
6289deb0eb7SJason Gunthorpe 
6294e26195fSJason Gunthorpe 	tpm_put_ops(chip);
630433d390fSTomas Winkler 	return rc;
6319deb0eb7SJason Gunthorpe }
6329deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_random);
6339deb0eb7SJason Gunthorpe 
634954650efSJarkko Sakkinen /**
635aad887f6SJarkko Sakkinen  * tpm_seal_trusted() - seal a trusted key payload
636aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
637954650efSJarkko Sakkinen  * @options:	authentication values and other options
638954650efSJarkko Sakkinen  * @payload:	the key data in clear and encrypted form
639954650efSJarkko Sakkinen  *
640aad887f6SJarkko Sakkinen  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
641aad887f6SJarkko Sakkinen  * the keyring subsystem.
642aad887f6SJarkko Sakkinen  *
643aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
644954650efSJarkko Sakkinen  */
645aad887f6SJarkko Sakkinen int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
646954650efSJarkko Sakkinen 		     struct trusted_key_options *options)
647954650efSJarkko Sakkinen {
648954650efSJarkko Sakkinen 	int rc;
649954650efSJarkko Sakkinen 
650fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
651aad887f6SJarkko Sakkinen 	if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
652954650efSJarkko Sakkinen 		return -ENODEV;
653954650efSJarkko Sakkinen 
654954650efSJarkko Sakkinen 	rc = tpm2_seal_trusted(chip, payload, options);
655954650efSJarkko Sakkinen 
6564e26195fSJason Gunthorpe 	tpm_put_ops(chip);
657954650efSJarkko Sakkinen 	return rc;
658954650efSJarkko Sakkinen }
659954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_seal_trusted);
660954650efSJarkko Sakkinen 
661954650efSJarkko Sakkinen /**
662954650efSJarkko Sakkinen  * tpm_unseal_trusted() - unseal a trusted key
663aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
664954650efSJarkko Sakkinen  * @options:	authentication values and other options
665954650efSJarkko Sakkinen  * @payload:	the key data in clear and encrypted form
666954650efSJarkko Sakkinen  *
667aad887f6SJarkko Sakkinen  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
668aad887f6SJarkko Sakkinen  * the keyring subsystem.
669aad887f6SJarkko Sakkinen  *
670aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
671954650efSJarkko Sakkinen  */
672aad887f6SJarkko Sakkinen int tpm_unseal_trusted(struct tpm_chip *chip,
673aad887f6SJarkko Sakkinen 		       struct trusted_key_payload *payload,
674954650efSJarkko Sakkinen 		       struct trusted_key_options *options)
675954650efSJarkko Sakkinen {
676954650efSJarkko Sakkinen 	int rc;
677954650efSJarkko Sakkinen 
678fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
679aad887f6SJarkko Sakkinen 	if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
680954650efSJarkko Sakkinen 		return -ENODEV;
681954650efSJarkko Sakkinen 
682954650efSJarkko Sakkinen 	rc = tpm2_unseal_trusted(chip, payload, options);
683954650efSJarkko Sakkinen 
6844e26195fSJason Gunthorpe 	tpm_put_ops(chip);
6854e26195fSJason Gunthorpe 
686954650efSJarkko Sakkinen 	return rc;
687954650efSJarkko Sakkinen }
688954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
689954650efSJarkko Sakkinen 
690313d21eeSJarkko Sakkinen static int __init tpm_init(void)
691313d21eeSJarkko Sakkinen {
692313d21eeSJarkko Sakkinen 	int rc;
693313d21eeSJarkko Sakkinen 
694313d21eeSJarkko Sakkinen 	tpm_class = class_create(THIS_MODULE, "tpm");
695313d21eeSJarkko Sakkinen 	if (IS_ERR(tpm_class)) {
696313d21eeSJarkko Sakkinen 		pr_err("couldn't create tpm class\n");
697313d21eeSJarkko Sakkinen 		return PTR_ERR(tpm_class);
698313d21eeSJarkko Sakkinen 	}
699313d21eeSJarkko Sakkinen 
700fdc915f7SJames Bottomley 	tpmrm_class = class_create(THIS_MODULE, "tpmrm");
701fdc915f7SJames Bottomley 	if (IS_ERR(tpmrm_class)) {
702fdc915f7SJames Bottomley 		pr_err("couldn't create tpmrm class\n");
7039e1b74a6STadeusz Struk 		rc = PTR_ERR(tpmrm_class);
7049e1b74a6STadeusz Struk 		goto out_destroy_tpm_class;
705fdc915f7SJames Bottomley 	}
706fdc915f7SJames Bottomley 
707fdc915f7SJames Bottomley 	rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
708313d21eeSJarkko Sakkinen 	if (rc < 0) {
709313d21eeSJarkko Sakkinen 		pr_err("tpm: failed to allocate char dev region\n");
7109e1b74a6STadeusz Struk 		goto out_destroy_tpmrm_class;
7119e1b74a6STadeusz Struk 	}
7129e1b74a6STadeusz Struk 
7139e1b74a6STadeusz Struk 	rc = tpm_dev_common_init();
7149e1b74a6STadeusz Struk 	if (rc) {
7159e1b74a6STadeusz Struk 		pr_err("tpm: failed to allocate char dev region\n");
7169e1b74a6STadeusz Struk 		goto out_unreg_chrdev;
717313d21eeSJarkko Sakkinen 	}
718313d21eeSJarkko Sakkinen 
719313d21eeSJarkko Sakkinen 	return 0;
7209e1b74a6STadeusz Struk 
7219e1b74a6STadeusz Struk out_unreg_chrdev:
7229e1b74a6STadeusz Struk 	unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
7239e1b74a6STadeusz Struk out_destroy_tpmrm_class:
7249e1b74a6STadeusz Struk 	class_destroy(tpmrm_class);
7259e1b74a6STadeusz Struk out_destroy_tpm_class:
7269e1b74a6STadeusz Struk 	class_destroy(tpm_class);
7279e1b74a6STadeusz Struk 
7289e1b74a6STadeusz Struk 	return rc;
729313d21eeSJarkko Sakkinen }
730313d21eeSJarkko Sakkinen 
731313d21eeSJarkko Sakkinen static void __exit tpm_exit(void)
732313d21eeSJarkko Sakkinen {
73315516788SStefan Berger 	idr_destroy(&dev_nums_idr);
734313d21eeSJarkko Sakkinen 	class_destroy(tpm_class);
735fdc915f7SJames Bottomley 	class_destroy(tpmrm_class);
736fdc915f7SJames Bottomley 	unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
7379e1b74a6STadeusz Struk 	tpm_dev_common_exit();
738313d21eeSJarkko Sakkinen }
739313d21eeSJarkko Sakkinen 
740313d21eeSJarkko Sakkinen subsys_initcall(tpm_init);
741313d21eeSJarkko Sakkinen module_exit(tpm_exit);
742313d21eeSJarkko Sakkinen 
7439deb0eb7SJason Gunthorpe MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
7449deb0eb7SJason Gunthorpe MODULE_DESCRIPTION("TPM Driver");
7459deb0eb7SJason Gunthorpe MODULE_VERSION("2.0");
7469deb0eb7SJason Gunthorpe MODULE_LICENSE("GPL");
747