xref: /openbmc/linux/drivers/char/tpm/tpm-interface.c (revision 01f54664a4db0d612de0ece8e0022f21f9374e9b)
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 
65095531f8SJavier Martinez Canillas static int tpm_validate_command(struct tpm_chip *chip,
66745b361eSJarkko Sakkinen 				 struct tpm_space *space,
67745b361eSJarkko Sakkinen 				 const u8 *cmd,
6858472f5cSJarkko Sakkinen 				 size_t len)
6958472f5cSJarkko Sakkinen {
7058472f5cSJarkko Sakkinen 	const struct tpm_input_header *header = (const void *)cmd;
7158472f5cSJarkko Sakkinen 	int i;
7258472f5cSJarkko Sakkinen 	u32 cc;
7358472f5cSJarkko Sakkinen 	u32 attrs;
7458472f5cSJarkko Sakkinen 	unsigned int nr_handles;
7558472f5cSJarkko Sakkinen 
7658472f5cSJarkko Sakkinen 	if (len < TPM_HEADER_SIZE)
77095531f8SJavier Martinez Canillas 		return -EINVAL;
7858472f5cSJarkko Sakkinen 
79745b361eSJarkko Sakkinen 	if (!space)
80095531f8SJavier Martinez Canillas 		return 0;
81745b361eSJarkko Sakkinen 
8258472f5cSJarkko Sakkinen 	if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) {
8358472f5cSJarkko Sakkinen 		cc = be32_to_cpu(header->ordinal);
8458472f5cSJarkko Sakkinen 
8558472f5cSJarkko Sakkinen 		i = tpm2_find_cc(chip, cc);
8658472f5cSJarkko Sakkinen 		if (i < 0) {
8758472f5cSJarkko Sakkinen 			dev_dbg(&chip->dev, "0x%04X is an invalid command\n",
8858472f5cSJarkko Sakkinen 				cc);
89095531f8SJavier Martinez Canillas 			return -EOPNOTSUPP;
9058472f5cSJarkko Sakkinen 		}
9158472f5cSJarkko Sakkinen 
9258472f5cSJarkko Sakkinen 		attrs = chip->cc_attrs_tbl[i];
9358472f5cSJarkko Sakkinen 		nr_handles =
9458472f5cSJarkko Sakkinen 			4 * ((attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0));
9558472f5cSJarkko Sakkinen 		if (len < TPM_HEADER_SIZE + 4 * nr_handles)
9658472f5cSJarkko Sakkinen 			goto err_len;
9758472f5cSJarkko Sakkinen 	}
9858472f5cSJarkko Sakkinen 
99095531f8SJavier Martinez Canillas 	return 0;
10058472f5cSJarkko Sakkinen err_len:
10158472f5cSJarkko Sakkinen 	dev_dbg(&chip->dev,
10258472f5cSJarkko Sakkinen 		"%s: insufficient command length %zu", __func__, len);
103095531f8SJavier Martinez Canillas 	return -EINVAL;
10458472f5cSJarkko Sakkinen }
10558472f5cSJarkko Sakkinen 
106627448e8STomas Winkler static int tpm_request_locality(struct tpm_chip *chip, unsigned int flags)
107888d867dSTomas Winkler {
108888d867dSTomas Winkler 	int rc;
109888d867dSTomas Winkler 
11058bac8ccSJarkko Sakkinen 	if (flags & TPM_TRANSMIT_NESTED)
111627448e8STomas Winkler 		return 0;
112627448e8STomas Winkler 
113888d867dSTomas Winkler 	if (!chip->ops->request_locality)
114888d867dSTomas Winkler 		return 0;
115888d867dSTomas Winkler 
116888d867dSTomas Winkler 	rc = chip->ops->request_locality(chip, 0);
117888d867dSTomas Winkler 	if (rc < 0)
118888d867dSTomas Winkler 		return rc;
119888d867dSTomas Winkler 
120888d867dSTomas Winkler 	chip->locality = rc;
121888d867dSTomas Winkler 
122888d867dSTomas Winkler 	return 0;
123888d867dSTomas Winkler }
124888d867dSTomas Winkler 
125627448e8STomas Winkler static void tpm_relinquish_locality(struct tpm_chip *chip, unsigned int flags)
126888d867dSTomas Winkler {
127888d867dSTomas Winkler 	int rc;
128888d867dSTomas Winkler 
12958bac8ccSJarkko Sakkinen 	if (flags & TPM_TRANSMIT_NESTED)
130627448e8STomas Winkler 		return;
131627448e8STomas Winkler 
132888d867dSTomas Winkler 	if (!chip->ops->relinquish_locality)
133888d867dSTomas Winkler 		return;
134888d867dSTomas Winkler 
135888d867dSTomas Winkler 	rc = chip->ops->relinquish_locality(chip, chip->locality);
136888d867dSTomas Winkler 	if (rc)
137888d867dSTomas Winkler 		dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
138888d867dSTomas Winkler 
139888d867dSTomas Winkler 	chip->locality = -1;
140888d867dSTomas Winkler }
141888d867dSTomas Winkler 
142627448e8STomas Winkler static int tpm_cmd_ready(struct tpm_chip *chip, unsigned int flags)
143627448e8STomas Winkler {
14458bac8ccSJarkko Sakkinen 	if (flags & TPM_TRANSMIT_NESTED)
145627448e8STomas Winkler 		return 0;
146627448e8STomas Winkler 
147627448e8STomas Winkler 	if (!chip->ops->cmd_ready)
148627448e8STomas Winkler 		return 0;
149627448e8STomas Winkler 
150627448e8STomas Winkler 	return chip->ops->cmd_ready(chip);
151627448e8STomas Winkler }
152627448e8STomas Winkler 
153627448e8STomas Winkler static int tpm_go_idle(struct tpm_chip *chip, unsigned int flags)
154627448e8STomas Winkler {
15558bac8ccSJarkko Sakkinen 	if (flags & TPM_TRANSMIT_NESTED)
156627448e8STomas Winkler 		return 0;
157627448e8STomas Winkler 
158627448e8STomas Winkler 	if (!chip->ops->go_idle)
159627448e8STomas Winkler 		return 0;
160627448e8STomas Winkler 
161627448e8STomas Winkler 	return chip->ops->go_idle(chip);
162627448e8STomas Winkler }
163627448e8STomas Winkler 
164e2fb992dSJames Bottomley static ssize_t tpm_try_transmit(struct tpm_chip *chip,
165e2fb992dSJames Bottomley 				struct tpm_space *space,
166e2fb992dSJames Bottomley 				u8 *buf, size_t bufsiz,
167e2fb992dSJames Bottomley 				unsigned int flags)
1689deb0eb7SJason Gunthorpe {
169745b361eSJarkko Sakkinen 	struct tpm_output_header *header = (void *)buf;
170745b361eSJarkko Sakkinen 	int rc;
171745b361eSJarkko Sakkinen 	ssize_t len = 0;
1729deb0eb7SJason Gunthorpe 	u32 count, ordinal;
1739deb0eb7SJason Gunthorpe 	unsigned long stop;
174877c57d0SJarkko Sakkinen 	bool need_locality;
1759deb0eb7SJason Gunthorpe 
176095531f8SJavier Martinez Canillas 	rc = tpm_validate_command(chip, space, buf, bufsiz);
177095531f8SJavier Martinez Canillas 	if (rc == -EINVAL)
178095531f8SJavier Martinez Canillas 		return rc;
179095531f8SJavier Martinez Canillas 	/*
180095531f8SJavier Martinez Canillas 	 * If the command is not implemented by the TPM, synthesize a
181095531f8SJavier Martinez Canillas 	 * response with a TPM2_RC_COMMAND_CODE return for user-space.
182095531f8SJavier Martinez Canillas 	 */
183095531f8SJavier Martinez Canillas 	if (rc == -EOPNOTSUPP) {
184095531f8SJavier Martinez Canillas 		header->length = cpu_to_be32(sizeof(*header));
185095531f8SJavier Martinez Canillas 		header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
186095531f8SJavier Martinez Canillas 		header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
187095531f8SJavier Martinez Canillas 						  TSS2_RESMGR_TPM_RC_LAYER);
18836a11029SRicardo Schwarzmeier 		return sizeof(*header);
189095531f8SJavier Martinez Canillas 	}
190ebfd7532SJarkko Sakkinen 
1919deb0eb7SJason Gunthorpe 	if (bufsiz > TPM_BUFSIZE)
1929deb0eb7SJason Gunthorpe 		bufsiz = TPM_BUFSIZE;
1939deb0eb7SJason Gunthorpe 
1949deb0eb7SJason Gunthorpe 	count = be32_to_cpu(*((__be32 *) (buf + 2)));
1959deb0eb7SJason Gunthorpe 	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
1969deb0eb7SJason Gunthorpe 	if (count == 0)
1979deb0eb7SJason Gunthorpe 		return -ENODATA;
1989deb0eb7SJason Gunthorpe 	if (count > bufsiz) {
1998cfffc9dSJason Gunthorpe 		dev_err(&chip->dev,
2009deb0eb7SJason Gunthorpe 			"invalid count value %x %zx\n", count, bufsiz);
2019deb0eb7SJason Gunthorpe 		return -E2BIG;
2029deb0eb7SJason Gunthorpe 	}
2039deb0eb7SJason Gunthorpe 
20458bac8ccSJarkko Sakkinen 	if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
2059deb0eb7SJason Gunthorpe 		mutex_lock(&chip->tpm_mutex);
2069deb0eb7SJason Gunthorpe 
207b3e958ceSAzhar Shaikh 	if (chip->ops->clk_enable != NULL)
208b3e958ceSAzhar Shaikh 		chip->ops->clk_enable(chip, true);
209b3e958ceSAzhar Shaikh 
210877c57d0SJarkko Sakkinen 	/* Store the decision as chip->locality will be changed. */
211877c57d0SJarkko Sakkinen 	need_locality = chip->locality == -1;
212877c57d0SJarkko Sakkinen 
213627448e8STomas Winkler 	if (need_locality) {
214627448e8STomas Winkler 		rc = tpm_request_locality(chip, flags);
215*01f54664STomas Winkler 		if (rc < 0) {
216*01f54664STomas Winkler 			need_locality = false;
217*01f54664STomas Winkler 			goto out_locality;
218*01f54664STomas Winkler 		}
219877c57d0SJarkko Sakkinen 	}
220877c57d0SJarkko Sakkinen 
221627448e8STomas Winkler 	rc = tpm_cmd_ready(chip, flags);
222627448e8STomas Winkler 	if (rc)
223*01f54664STomas Winkler 		goto out_locality;
224888d867dSTomas Winkler 
225745b361eSJarkko Sakkinen 	rc = tpm2_prepare_space(chip, space, ordinal, buf);
226745b361eSJarkko Sakkinen 	if (rc)
227745b361eSJarkko Sakkinen 		goto out;
228745b361eSJarkko Sakkinen 
22962c09e12SWinkler, Tomas 	rc = chip->ops->send(chip, buf, count);
2309deb0eb7SJason Gunthorpe 	if (rc < 0) {
231402149c6SStefan Berger 		if (rc != -EPIPE)
2328cfffc9dSJason Gunthorpe 			dev_err(&chip->dev,
233402149c6SStefan Berger 				"%s: tpm_send: error %d\n", __func__, rc);
2349deb0eb7SJason Gunthorpe 		goto out;
2359deb0eb7SJason Gunthorpe 	}
2369deb0eb7SJason Gunthorpe 
237570a3609SChristophe Ricard 	if (chip->flags & TPM_CHIP_FLAG_IRQ)
2389deb0eb7SJason Gunthorpe 		goto out_recv;
2399deb0eb7SJason Gunthorpe 
240d856c00fSTomas Winkler 	stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
2419deb0eb7SJason Gunthorpe 	do {
2425f82e9f0SJason Gunthorpe 		u8 status = chip->ops->status(chip);
2435f82e9f0SJason Gunthorpe 		if ((status & chip->ops->req_complete_mask) ==
2445f82e9f0SJason Gunthorpe 		    chip->ops->req_complete_val)
2459deb0eb7SJason Gunthorpe 			goto out_recv;
2469deb0eb7SJason Gunthorpe 
2475f82e9f0SJason Gunthorpe 		if (chip->ops->req_canceled(chip, status)) {
2488cfffc9dSJason Gunthorpe 			dev_err(&chip->dev, "Operation Canceled\n");
2499deb0eb7SJason Gunthorpe 			rc = -ECANCELED;
2509deb0eb7SJason Gunthorpe 			goto out;
2519deb0eb7SJason Gunthorpe 		}
2529deb0eb7SJason Gunthorpe 
25359f5a6b0SNayna Jain 		tpm_msleep(TPM_TIMEOUT_POLL);
2549deb0eb7SJason Gunthorpe 		rmb();
2559deb0eb7SJason Gunthorpe 	} while (time_before(jiffies, stop));
2569deb0eb7SJason Gunthorpe 
2575f82e9f0SJason Gunthorpe 	chip->ops->cancel(chip);
2588cfffc9dSJason Gunthorpe 	dev_err(&chip->dev, "Operation Timed out\n");
2599deb0eb7SJason Gunthorpe 	rc = -ETIME;
2609deb0eb7SJason Gunthorpe 	goto out;
2619deb0eb7SJason Gunthorpe 
2629deb0eb7SJason Gunthorpe out_recv:
26362c09e12SWinkler, Tomas 	len = chip->ops->recv(chip, buf, bufsiz);
264745b361eSJarkko Sakkinen 	if (len < 0) {
265745b361eSJarkko Sakkinen 		rc = len;
2668cfffc9dSJason Gunthorpe 		dev_err(&chip->dev,
267745b361eSJarkko Sakkinen 			"tpm_transmit: tpm_recv: error %d\n", rc);
268a147918eSJarkko Sakkinen 		goto out;
269745b361eSJarkko Sakkinen 	} else if (len < TPM_HEADER_SIZE) {
270a147918eSJarkko Sakkinen 		rc = -EFAULT;
271a147918eSJarkko Sakkinen 		goto out;
272a147918eSJarkko Sakkinen 	}
273a147918eSJarkko Sakkinen 
274745b361eSJarkko Sakkinen 	if (len != be32_to_cpu(header->length)) {
275745b361eSJarkko Sakkinen 		rc = -EFAULT;
276a147918eSJarkko Sakkinen 		goto out;
277745b361eSJarkko Sakkinen 	}
278745b361eSJarkko Sakkinen 
279745b361eSJarkko Sakkinen 	rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
280627448e8STomas Winkler 	if (rc)
281627448e8STomas Winkler 		dev_err(&chip->dev, "tpm2_commit_space: error %d\n", rc);
282a147918eSJarkko Sakkinen 
2839deb0eb7SJason Gunthorpe out:
284*01f54664STomas Winkler 	/* may fail but do not override previous error value in rc */
285*01f54664STomas Winkler 	tpm_go_idle(chip, flags);
286888d867dSTomas Winkler 
287*01f54664STomas Winkler out_locality:
288888d867dSTomas Winkler 	if (need_locality)
289627448e8STomas Winkler 		tpm_relinquish_locality(chip, flags);
290888d867dSTomas Winkler 
291b3e958ceSAzhar Shaikh 	if (chip->ops->clk_enable != NULL)
292b3e958ceSAzhar Shaikh 		chip->ops->clk_enable(chip, false);
293b3e958ceSAzhar Shaikh 
29458bac8ccSJarkko Sakkinen 	if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
2959deb0eb7SJason Gunthorpe 		mutex_unlock(&chip->tpm_mutex);
296745b361eSJarkko Sakkinen 	return rc ? rc : len;
2979deb0eb7SJason Gunthorpe }
2989deb0eb7SJason Gunthorpe 
299f865c196SWinkler, Tomas /**
300e2fb992dSJames Bottomley  * tpm_transmit - Internal kernel interface to transmit TPM commands.
301e2fb992dSJames Bottomley  *
302e2fb992dSJames Bottomley  * @chip: TPM chip to use
303e2fb992dSJames Bottomley  * @space: tpm space
304e2fb992dSJames Bottomley  * @buf: TPM command buffer
305e2fb992dSJames Bottomley  * @bufsiz: length of the TPM command buffer
306e2fb992dSJames Bottomley  * @flags: tpm transmit flags - bitmap
307e2fb992dSJames Bottomley  *
308e2fb992dSJames Bottomley  * A wrapper around tpm_try_transmit that handles TPM2_RC_RETRY
309e2fb992dSJames Bottomley  * returns from the TPM and retransmits the command after a delay up
310e2fb992dSJames Bottomley  * to a maximum wait of TPM2_DURATION_LONG.
311e2fb992dSJames Bottomley  *
312e2fb992dSJames Bottomley  * Note: TPM1 never returns TPM2_RC_RETRY so the retry logic is TPM2
313e2fb992dSJames Bottomley  * only
314e2fb992dSJames Bottomley  *
315e2fb992dSJames Bottomley  * Return:
316e2fb992dSJames Bottomley  *     the length of the return when the operation is successful.
317e2fb992dSJames Bottomley  *     A negative number for system errors (errno).
318e2fb992dSJames Bottomley  */
319e2fb992dSJames Bottomley ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
320e2fb992dSJames Bottomley 		     u8 *buf, size_t bufsiz, unsigned int flags)
321e2fb992dSJames Bottomley {
322e2fb992dSJames Bottomley 	struct tpm_output_header *header = (struct tpm_output_header *)buf;
323e2fb992dSJames Bottomley 	/* space for header and handles */
324e2fb992dSJames Bottomley 	u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
325e2fb992dSJames Bottomley 	unsigned int delay_msec = TPM2_DURATION_SHORT;
326e2fb992dSJames Bottomley 	u32 rc = 0;
327e2fb992dSJames Bottomley 	ssize_t ret;
328e2fb992dSJames Bottomley 	const size_t save_size = min(space ? sizeof(save) : TPM_HEADER_SIZE,
329e2fb992dSJames Bottomley 				     bufsiz);
3302be8ffedSJames Bottomley 	/* the command code is where the return code will be */
3312be8ffedSJames Bottomley 	u32 cc = be32_to_cpu(header->return_code);
332e2fb992dSJames Bottomley 
333e2fb992dSJames Bottomley 	/*
334e2fb992dSJames Bottomley 	 * Subtlety here: if we have a space, the handles will be
335e2fb992dSJames Bottomley 	 * transformed, so when we restore the header we also have to
336e2fb992dSJames Bottomley 	 * restore the handles.
337e2fb992dSJames Bottomley 	 */
338e2fb992dSJames Bottomley 	memcpy(save, buf, save_size);
339e2fb992dSJames Bottomley 
340e2fb992dSJames Bottomley 	for (;;) {
341e2fb992dSJames Bottomley 		ret = tpm_try_transmit(chip, space, buf, bufsiz, flags);
342e2fb992dSJames Bottomley 		if (ret < 0)
343e2fb992dSJames Bottomley 			break;
344e2fb992dSJames Bottomley 		rc = be32_to_cpu(header->return_code);
3452be8ffedSJames Bottomley 		if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
3462be8ffedSJames Bottomley 			break;
3472be8ffedSJames Bottomley 		/*
3482be8ffedSJames Bottomley 		 * return immediately if self test returns test
3492be8ffedSJames Bottomley 		 * still running to shorten boot time.
3502be8ffedSJames Bottomley 		 */
3512be8ffedSJames Bottomley 		if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
352e2fb992dSJames Bottomley 			break;
35392980756SNayna Jain 
354e2fb992dSJames Bottomley 		if (delay_msec > TPM2_DURATION_LONG) {
3552be8ffedSJames Bottomley 			if (rc == TPM2_RC_RETRY)
3562be8ffedSJames Bottomley 				dev_err(&chip->dev, "in retry loop\n");
3572be8ffedSJames Bottomley 			else
3582be8ffedSJames Bottomley 				dev_err(&chip->dev,
3592be8ffedSJames Bottomley 					"self test is still running\n");
360e2fb992dSJames Bottomley 			break;
361e2fb992dSJames Bottomley 		}
362e2fb992dSJames Bottomley 		tpm_msleep(delay_msec);
36392980756SNayna Jain 		delay_msec *= 2;
364e2fb992dSJames Bottomley 		memcpy(buf, save, save_size);
365e2fb992dSJames Bottomley 	}
366e2fb992dSJames Bottomley 	return ret;
367e2fb992dSJames Bottomley }
368e2fb992dSJames Bottomley /**
36965520d46SWinkler, Tomas  * tpm_transmit_cmd - send a tpm command to the device
370f865c196SWinkler, Tomas  *    The function extracts tpm out header return code
371f865c196SWinkler, Tomas  *
372f865c196SWinkler, Tomas  * @chip: TPM chip to use
37365520d46SWinkler, Tomas  * @space: tpm space
374c659af78SStefan Berger  * @buf: TPM command buffer
375c659af78SStefan Berger  * @bufsiz: length of the buffer
376c659af78SStefan Berger  * @min_rsp_body_length: minimum expected length of response body
377f865c196SWinkler, Tomas  * @flags: tpm transmit flags - bitmap
378f865c196SWinkler, Tomas  * @desc: command description used in the error message
379f865c196SWinkler, Tomas  *
380f865c196SWinkler, Tomas  * Return:
381f865c196SWinkler, Tomas  *     0 when the operation is successful.
382f865c196SWinkler, Tomas  *     A negative number for system errors (errno).
383f865c196SWinkler, Tomas  *     A positive number for a TPM error.
384f865c196SWinkler, Tomas  */
385745b361eSJarkko Sakkinen ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
38662c09e12SWinkler, Tomas 			 void *buf, size_t bufsiz,
387745b361eSJarkko Sakkinen 			 size_t min_rsp_body_length, unsigned int flags,
388745b361eSJarkko Sakkinen 			 const char *desc)
3899deb0eb7SJason Gunthorpe {
390a147918eSJarkko Sakkinen 	const struct tpm_output_header *header = buf;
3919deb0eb7SJason Gunthorpe 	int err;
392c659af78SStefan Berger 	ssize_t len;
3939deb0eb7SJason Gunthorpe 
39462c09e12SWinkler, Tomas 	len = tpm_transmit(chip, space, buf, bufsiz, flags);
3959deb0eb7SJason Gunthorpe 	if (len <  0)
3969deb0eb7SJason Gunthorpe 		return len;
39787155b73SJarkko Sakkinen 
39887155b73SJarkko Sakkinen 	err = be32_to_cpu(header->return_code);
3990d6d0d62SJavier Martinez Canillas 	if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED
4000d6d0d62SJavier Martinez Canillas 	    && desc)
4018cfffc9dSJason Gunthorpe 		dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
40271ed848fSJarkko Sakkinen 			desc);
403c659af78SStefan Berger 	if (err)
4049deb0eb7SJason Gunthorpe 		return err;
405c659af78SStefan Berger 
406c659af78SStefan Berger 	if (len < min_rsp_body_length + TPM_HEADER_SIZE)
407c659af78SStefan Berger 		return -EFAULT;
408c659af78SStefan Berger 
409c659af78SStefan Berger 	return 0;
4109deb0eb7SJason Gunthorpe }
411be4c9acfSStefan Berger EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
4129deb0eb7SJason Gunthorpe 
4139deb0eb7SJason Gunthorpe int tpm_get_timeouts(struct tpm_chip *chip)
4149deb0eb7SJason Gunthorpe {
415d1d253cfSJason Gunthorpe 	if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
416d1d253cfSJason Gunthorpe 		return 0;
417d1d253cfSJason Gunthorpe 
41870a3199aSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
41970a3199aSTomas Winkler 		return tpm2_get_timeouts(chip);
42070a3199aSTomas Winkler 	else
42170a3199aSTomas Winkler 		return tpm1_get_timeouts(chip);
4229deb0eb7SJason Gunthorpe }
4239deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_timeouts);
4249deb0eb7SJason Gunthorpe 
4259deb0eb7SJason Gunthorpe /**
426aad887f6SJarkko Sakkinen  * tpm_is_tpm2 - do we a have a TPM2 chip?
427aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
428954650efSJarkko Sakkinen  *
429aad887f6SJarkko Sakkinen  * Return:
430aad887f6SJarkko Sakkinen  * 1 if we have a TPM2 chip.
431aad887f6SJarkko Sakkinen  * 0 if we don't have a TPM2 chip.
432aad887f6SJarkko Sakkinen  * A negative number for system errors (errno).
433954650efSJarkko Sakkinen  */
434aad887f6SJarkko Sakkinen int tpm_is_tpm2(struct tpm_chip *chip)
435954650efSJarkko Sakkinen {
436954650efSJarkko Sakkinen 	int rc;
437954650efSJarkko Sakkinen 
438fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
439aad887f6SJarkko Sakkinen 	if (!chip)
440954650efSJarkko Sakkinen 		return -ENODEV;
441954650efSJarkko Sakkinen 
442954650efSJarkko Sakkinen 	rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
443954650efSJarkko Sakkinen 
4444e26195fSJason Gunthorpe 	tpm_put_ops(chip);
445954650efSJarkko Sakkinen 
446954650efSJarkko Sakkinen 	return rc;
447954650efSJarkko Sakkinen }
448954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_is_tpm2);
449954650efSJarkko Sakkinen 
450954650efSJarkko Sakkinen /**
451aad887f6SJarkko Sakkinen  * tpm_pcr_read - read a PCR value from SHA1 bank
452aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
453aad887f6SJarkko Sakkinen  * @pcr_idx:	the PCR to be retrieved
454aad887f6SJarkko Sakkinen  * @res_buf:	the value of the PCR
4559deb0eb7SJason Gunthorpe  *
456aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
4579deb0eb7SJason Gunthorpe  */
45895adc6b4STomas Winkler int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
4599deb0eb7SJason Gunthorpe {
4609deb0eb7SJason Gunthorpe 	int rc;
4619deb0eb7SJason Gunthorpe 
462fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
463aad887f6SJarkko Sakkinen 	if (!chip)
4649deb0eb7SJason Gunthorpe 		return -ENODEV;
465d4a31756STomas Winkler 
4667a1d7e6dSJarkko Sakkinen 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
4677a1d7e6dSJarkko Sakkinen 		rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
4687a1d7e6dSJarkko Sakkinen 	else
469cfddcb05STomas Winkler 		rc = tpm1_pcr_read(chip, pcr_idx, res_buf);
470d4a31756STomas Winkler 
4714e26195fSJason Gunthorpe 	tpm_put_ops(chip);
4729deb0eb7SJason Gunthorpe 	return rc;
4739deb0eb7SJason Gunthorpe }
4749deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_read);
4759deb0eb7SJason Gunthorpe 
4769deb0eb7SJason Gunthorpe /**
477aad887f6SJarkko Sakkinen  * tpm_pcr_extend - extend a PCR value in SHA1 bank.
478aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
479aad887f6SJarkko Sakkinen  * @pcr_idx:	the PCR to be retrieved
480aad887f6SJarkko Sakkinen  * @hash:	the hash value used to extend the PCR value
4819deb0eb7SJason Gunthorpe  *
482aad887f6SJarkko Sakkinen  * Note: with TPM 2.0 extends also those banks with a known digest size to the
483aad887f6SJarkko Sakkinen  * cryto subsystem in order to prevent malicious use of those PCR banks. In the
484aad887f6SJarkko Sakkinen  * future we should dynamically determine digest sizes.
485aad887f6SJarkko Sakkinen  *
486aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
4879deb0eb7SJason Gunthorpe  */
48895adc6b4STomas Winkler int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash)
4899deb0eb7SJason Gunthorpe {
4909deb0eb7SJason Gunthorpe 	int rc;
491c1f92b4bSNayna Jain 	struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
492c1f92b4bSNayna Jain 	u32 count = 0;
493c1f92b4bSNayna Jain 	int i;
4949deb0eb7SJason Gunthorpe 
495fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
496aad887f6SJarkko Sakkinen 	if (!chip)
4979deb0eb7SJason Gunthorpe 		return -ENODEV;
4989deb0eb7SJason Gunthorpe 
4997a1d7e6dSJarkko Sakkinen 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
500c1f92b4bSNayna Jain 		memset(digest_list, 0, sizeof(digest_list));
501c1f92b4bSNayna Jain 
50270ea1636SDan Carpenter 		for (i = 0; i < ARRAY_SIZE(chip->active_banks) &&
50370ea1636SDan Carpenter 			    chip->active_banks[i] != TPM2_ALG_ERROR; i++) {
504c1f92b4bSNayna Jain 			digest_list[i].alg_id = chip->active_banks[i];
505c1f92b4bSNayna Jain 			memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
506c1f92b4bSNayna Jain 			count++;
507c1f92b4bSNayna Jain 		}
508c1f92b4bSNayna Jain 
509c1f92b4bSNayna Jain 		rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list);
5104e26195fSJason Gunthorpe 		tpm_put_ops(chip);
5117a1d7e6dSJarkko Sakkinen 		return rc;
5127a1d7e6dSJarkko Sakkinen 	}
5137a1d7e6dSJarkko Sakkinen 
514175d5b2aSRoberto Sassu 	rc = tpm1_pcr_extend(chip, pcr_idx, hash,
5159deb0eb7SJason Gunthorpe 			     "attempting extend a PCR value");
5164e26195fSJason Gunthorpe 	tpm_put_ops(chip);
5179deb0eb7SJason Gunthorpe 	return rc;
5189deb0eb7SJason Gunthorpe }
5199deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_extend);
5209deb0eb7SJason Gunthorpe 
5219deb0eb7SJason Gunthorpe /**
522aad887f6SJarkko Sakkinen  * tpm_send - send a TPM command
523aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
524aad887f6SJarkko Sakkinen  * @cmd:	a TPM command buffer
525aad887f6SJarkko Sakkinen  * @buflen:	the length of the TPM command buffer
526aad887f6SJarkko Sakkinen  *
527aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
528aad887f6SJarkko Sakkinen  */
529aad887f6SJarkko Sakkinen int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
5309deb0eb7SJason Gunthorpe {
5319deb0eb7SJason Gunthorpe 	int rc;
5329deb0eb7SJason Gunthorpe 
533fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
534aad887f6SJarkko Sakkinen 	if (!chip)
5359deb0eb7SJason Gunthorpe 		return -ENODEV;
5369deb0eb7SJason Gunthorpe 
537745b361eSJarkko Sakkinen 	rc = tpm_transmit_cmd(chip, NULL, cmd, buflen, 0, 0,
538aad887f6SJarkko Sakkinen 			      "attempting to a send a command");
5394e26195fSJason Gunthorpe 	tpm_put_ops(chip);
5409deb0eb7SJason Gunthorpe 	return rc;
5419deb0eb7SJason Gunthorpe }
5429deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_send);
5439deb0eb7SJason Gunthorpe 
544b03c4370STomas Winkler int tpm_auto_startup(struct tpm_chip *chip)
545b03c4370STomas Winkler {
546b03c4370STomas Winkler 	int rc;
547b03c4370STomas Winkler 
548b03c4370STomas Winkler 	if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
549b03c4370STomas Winkler 		return 0;
550b03c4370STomas Winkler 
551b03c4370STomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
552b03c4370STomas Winkler 		rc = tpm2_auto_startup(chip);
553b03c4370STomas Winkler 	else
554b03c4370STomas Winkler 		rc = tpm1_auto_startup(chip);
555b03c4370STomas Winkler 
556b03c4370STomas Winkler 	return rc;
557b03c4370STomas Winkler }
558b03c4370STomas Winkler 
5599deb0eb7SJason Gunthorpe /*
5609deb0eb7SJason Gunthorpe  * We are about to suspend. Save the TPM state
5619deb0eb7SJason Gunthorpe  * so that it can be restored.
5629deb0eb7SJason Gunthorpe  */
5639deb0eb7SJason Gunthorpe int tpm_pm_suspend(struct device *dev)
5649deb0eb7SJason Gunthorpe {
565ec03c50bSStefan Berger 	struct tpm_chip *chip = dev_get_drvdata(dev);
566c82a330cSTomas Winkler 	int rc = 0;
5679deb0eb7SJason Gunthorpe 
568c82a330cSTomas Winkler 	if (!chip)
5699deb0eb7SJason Gunthorpe 		return -ENODEV;
5709deb0eb7SJason Gunthorpe 
571b5d0ebc9SEnric Balletbo i Serra 	if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
572b5d0ebc9SEnric Balletbo i Serra 		return 0;
573b5d0ebc9SEnric Balletbo i Serra 
574c82a330cSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
57574d6b3ceSJarkko Sakkinen 		tpm2_shutdown(chip, TPM2_SU_STATE);
576c82a330cSTomas Winkler 	else
577c82a330cSTomas Winkler 		rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
5789deb0eb7SJason Gunthorpe 
5799deb0eb7SJason Gunthorpe 	return rc;
5809deb0eb7SJason Gunthorpe }
5819deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_suspend);
5829deb0eb7SJason Gunthorpe 
5839deb0eb7SJason Gunthorpe /*
5849deb0eb7SJason Gunthorpe  * Resume from a power safe. The BIOS already restored
5859deb0eb7SJason Gunthorpe  * the TPM state.
5869deb0eb7SJason Gunthorpe  */
5879deb0eb7SJason Gunthorpe int tpm_pm_resume(struct device *dev)
5889deb0eb7SJason Gunthorpe {
589ec03c50bSStefan Berger 	struct tpm_chip *chip = dev_get_drvdata(dev);
5909deb0eb7SJason Gunthorpe 
5919deb0eb7SJason Gunthorpe 	if (chip == NULL)
5929deb0eb7SJason Gunthorpe 		return -ENODEV;
5939deb0eb7SJason Gunthorpe 
5949deb0eb7SJason Gunthorpe 	return 0;
5959deb0eb7SJason Gunthorpe }
5969deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_resume);
5979deb0eb7SJason Gunthorpe 
5989deb0eb7SJason Gunthorpe /**
599aad887f6SJarkko Sakkinen  * tpm_get_random() - get random bytes from the TPM's RNG
600aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
6019deb0eb7SJason Gunthorpe  * @out:	destination buffer for the random bytes
6029deb0eb7SJason Gunthorpe  * @max:	the max number of bytes to write to @out
6039deb0eb7SJason Gunthorpe  *
6047aee9c52STomas Winkler  * Return: number of random bytes read or a negative error value.
6059deb0eb7SJason Gunthorpe  */
606aad887f6SJarkko Sakkinen int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
6079deb0eb7SJason Gunthorpe {
608433d390fSTomas Winkler 	int rc;
6099deb0eb7SJason Gunthorpe 
610433d390fSTomas Winkler 	if (!out || max > TPM_MAX_RNG_DATA)
6113e14d83eSJarkko Sakkinen 		return -EINVAL;
6123e14d83eSJarkko Sakkinen 
613fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
614aad887f6SJarkko Sakkinen 	if (!chip)
6159deb0eb7SJason Gunthorpe 		return -ENODEV;
6169deb0eb7SJason Gunthorpe 
617433d390fSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
618433d390fSTomas Winkler 		rc = tpm2_get_random(chip, out, max);
619433d390fSTomas Winkler 	else
620433d390fSTomas Winkler 		rc = tpm1_get_random(chip, out, max);
6219deb0eb7SJason Gunthorpe 
6224e26195fSJason Gunthorpe 	tpm_put_ops(chip);
623433d390fSTomas Winkler 	return rc;
6249deb0eb7SJason Gunthorpe }
6259deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_random);
6269deb0eb7SJason Gunthorpe 
627954650efSJarkko Sakkinen /**
628aad887f6SJarkko Sakkinen  * tpm_seal_trusted() - seal a trusted key payload
629aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
630954650efSJarkko Sakkinen  * @options:	authentication values and other options
631954650efSJarkko Sakkinen  * @payload:	the key data in clear and encrypted form
632954650efSJarkko Sakkinen  *
633aad887f6SJarkko Sakkinen  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
634aad887f6SJarkko Sakkinen  * the keyring subsystem.
635aad887f6SJarkko Sakkinen  *
636aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
637954650efSJarkko Sakkinen  */
638aad887f6SJarkko Sakkinen int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
639954650efSJarkko Sakkinen 		     struct trusted_key_options *options)
640954650efSJarkko Sakkinen {
641954650efSJarkko Sakkinen 	int rc;
642954650efSJarkko Sakkinen 
643fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
644aad887f6SJarkko Sakkinen 	if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
645954650efSJarkko Sakkinen 		return -ENODEV;
646954650efSJarkko Sakkinen 
647954650efSJarkko Sakkinen 	rc = tpm2_seal_trusted(chip, payload, options);
648954650efSJarkko Sakkinen 
6494e26195fSJason Gunthorpe 	tpm_put_ops(chip);
650954650efSJarkko Sakkinen 	return rc;
651954650efSJarkko Sakkinen }
652954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_seal_trusted);
653954650efSJarkko Sakkinen 
654954650efSJarkko Sakkinen /**
655954650efSJarkko Sakkinen  * tpm_unseal_trusted() - unseal a trusted key
656aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
657954650efSJarkko Sakkinen  * @options:	authentication values and other options
658954650efSJarkko Sakkinen  * @payload:	the key data in clear and encrypted form
659954650efSJarkko Sakkinen  *
660aad887f6SJarkko Sakkinen  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
661aad887f6SJarkko Sakkinen  * the keyring subsystem.
662aad887f6SJarkko Sakkinen  *
663aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
664954650efSJarkko Sakkinen  */
665aad887f6SJarkko Sakkinen int tpm_unseal_trusted(struct tpm_chip *chip,
666aad887f6SJarkko Sakkinen 		       struct trusted_key_payload *payload,
667954650efSJarkko Sakkinen 		       struct trusted_key_options *options)
668954650efSJarkko Sakkinen {
669954650efSJarkko Sakkinen 	int rc;
670954650efSJarkko Sakkinen 
671fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
672aad887f6SJarkko Sakkinen 	if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
673954650efSJarkko Sakkinen 		return -ENODEV;
674954650efSJarkko Sakkinen 
675954650efSJarkko Sakkinen 	rc = tpm2_unseal_trusted(chip, payload, options);
676954650efSJarkko Sakkinen 
6774e26195fSJason Gunthorpe 	tpm_put_ops(chip);
6784e26195fSJason Gunthorpe 
679954650efSJarkko Sakkinen 	return rc;
680954650efSJarkko Sakkinen }
681954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
682954650efSJarkko Sakkinen 
683313d21eeSJarkko Sakkinen static int __init tpm_init(void)
684313d21eeSJarkko Sakkinen {
685313d21eeSJarkko Sakkinen 	int rc;
686313d21eeSJarkko Sakkinen 
687313d21eeSJarkko Sakkinen 	tpm_class = class_create(THIS_MODULE, "tpm");
688313d21eeSJarkko Sakkinen 	if (IS_ERR(tpm_class)) {
689313d21eeSJarkko Sakkinen 		pr_err("couldn't create tpm class\n");
690313d21eeSJarkko Sakkinen 		return PTR_ERR(tpm_class);
691313d21eeSJarkko Sakkinen 	}
692313d21eeSJarkko Sakkinen 
693fdc915f7SJames Bottomley 	tpmrm_class = class_create(THIS_MODULE, "tpmrm");
694fdc915f7SJames Bottomley 	if (IS_ERR(tpmrm_class)) {
695fdc915f7SJames Bottomley 		pr_err("couldn't create tpmrm class\n");
6969e1b74a6STadeusz Struk 		rc = PTR_ERR(tpmrm_class);
6979e1b74a6STadeusz Struk 		goto out_destroy_tpm_class;
698fdc915f7SJames Bottomley 	}
699fdc915f7SJames Bottomley 
700fdc915f7SJames Bottomley 	rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
701313d21eeSJarkko Sakkinen 	if (rc < 0) {
702313d21eeSJarkko Sakkinen 		pr_err("tpm: failed to allocate char dev region\n");
7039e1b74a6STadeusz Struk 		goto out_destroy_tpmrm_class;
7049e1b74a6STadeusz Struk 	}
7059e1b74a6STadeusz Struk 
7069e1b74a6STadeusz Struk 	rc = tpm_dev_common_init();
7079e1b74a6STadeusz Struk 	if (rc) {
7089e1b74a6STadeusz Struk 		pr_err("tpm: failed to allocate char dev region\n");
7099e1b74a6STadeusz Struk 		goto out_unreg_chrdev;
710313d21eeSJarkko Sakkinen 	}
711313d21eeSJarkko Sakkinen 
712313d21eeSJarkko Sakkinen 	return 0;
7139e1b74a6STadeusz Struk 
7149e1b74a6STadeusz Struk out_unreg_chrdev:
7159e1b74a6STadeusz Struk 	unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
7169e1b74a6STadeusz Struk out_destroy_tpmrm_class:
7179e1b74a6STadeusz Struk 	class_destroy(tpmrm_class);
7189e1b74a6STadeusz Struk out_destroy_tpm_class:
7199e1b74a6STadeusz Struk 	class_destroy(tpm_class);
7209e1b74a6STadeusz Struk 
7219e1b74a6STadeusz Struk 	return rc;
722313d21eeSJarkko Sakkinen }
723313d21eeSJarkko Sakkinen 
724313d21eeSJarkko Sakkinen static void __exit tpm_exit(void)
725313d21eeSJarkko Sakkinen {
72615516788SStefan Berger 	idr_destroy(&dev_nums_idr);
727313d21eeSJarkko Sakkinen 	class_destroy(tpm_class);
728fdc915f7SJames Bottomley 	class_destroy(tpmrm_class);
729fdc915f7SJames Bottomley 	unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
7309e1b74a6STadeusz Struk 	tpm_dev_common_exit();
731313d21eeSJarkko Sakkinen }
732313d21eeSJarkko Sakkinen 
733313d21eeSJarkko Sakkinen subsys_initcall(tpm_init);
734313d21eeSJarkko Sakkinen module_exit(tpm_exit);
735313d21eeSJarkko Sakkinen 
7369deb0eb7SJason Gunthorpe MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
7379deb0eb7SJason Gunthorpe MODULE_DESCRIPTION("TPM Driver");
7389deb0eb7SJason Gunthorpe MODULE_VERSION("2.0");
7399deb0eb7SJason Gunthorpe MODULE_LICENSE("GPL");
740