xref: /openbmc/linux/drivers/char/tpm/tpm-interface.c (revision 7aee9c52d7ac90e19705bf3b429caa51f105174a)
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  */
419deb0eb7SJason Gunthorpe static int 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);
215877c57d0SJarkko Sakkinen 		if (rc < 0)
216877c57d0SJarkko Sakkinen 			goto out_no_locality;
217877c57d0SJarkko Sakkinen 	}
218877c57d0SJarkko Sakkinen 
219627448e8STomas Winkler 	rc = tpm_cmd_ready(chip, flags);
220627448e8STomas Winkler 	if (rc)
221627448e8STomas Winkler 		goto out;
222888d867dSTomas Winkler 
223745b361eSJarkko Sakkinen 	rc = tpm2_prepare_space(chip, space, ordinal, buf);
224745b361eSJarkko Sakkinen 	if (rc)
225745b361eSJarkko Sakkinen 		goto out;
226745b361eSJarkko Sakkinen 
22762c09e12SWinkler, Tomas 	rc = chip->ops->send(chip, buf, count);
2289deb0eb7SJason Gunthorpe 	if (rc < 0) {
229402149c6SStefan Berger 		if (rc != -EPIPE)
2308cfffc9dSJason Gunthorpe 			dev_err(&chip->dev,
231402149c6SStefan Berger 				"%s: tpm_send: error %d\n", __func__, rc);
2329deb0eb7SJason Gunthorpe 		goto out;
2339deb0eb7SJason Gunthorpe 	}
2349deb0eb7SJason Gunthorpe 
235570a3609SChristophe Ricard 	if (chip->flags & TPM_CHIP_FLAG_IRQ)
2369deb0eb7SJason Gunthorpe 		goto out_recv;
2379deb0eb7SJason Gunthorpe 
238d856c00fSTomas Winkler 	stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
2399deb0eb7SJason Gunthorpe 	do {
2405f82e9f0SJason Gunthorpe 		u8 status = chip->ops->status(chip);
2415f82e9f0SJason Gunthorpe 		if ((status & chip->ops->req_complete_mask) ==
2425f82e9f0SJason Gunthorpe 		    chip->ops->req_complete_val)
2439deb0eb7SJason Gunthorpe 			goto out_recv;
2449deb0eb7SJason Gunthorpe 
2455f82e9f0SJason Gunthorpe 		if (chip->ops->req_canceled(chip, status)) {
2468cfffc9dSJason Gunthorpe 			dev_err(&chip->dev, "Operation Canceled\n");
2479deb0eb7SJason Gunthorpe 			rc = -ECANCELED;
2489deb0eb7SJason Gunthorpe 			goto out;
2499deb0eb7SJason Gunthorpe 		}
2509deb0eb7SJason Gunthorpe 
25159f5a6b0SNayna Jain 		tpm_msleep(TPM_TIMEOUT_POLL);
2529deb0eb7SJason Gunthorpe 		rmb();
2539deb0eb7SJason Gunthorpe 	} while (time_before(jiffies, stop));
2549deb0eb7SJason Gunthorpe 
2555f82e9f0SJason Gunthorpe 	chip->ops->cancel(chip);
2568cfffc9dSJason Gunthorpe 	dev_err(&chip->dev, "Operation Timed out\n");
2579deb0eb7SJason Gunthorpe 	rc = -ETIME;
2589deb0eb7SJason Gunthorpe 	goto out;
2599deb0eb7SJason Gunthorpe 
2609deb0eb7SJason Gunthorpe out_recv:
26162c09e12SWinkler, Tomas 	len = chip->ops->recv(chip, buf, bufsiz);
262745b361eSJarkko Sakkinen 	if (len < 0) {
263745b361eSJarkko Sakkinen 		rc = len;
2648cfffc9dSJason Gunthorpe 		dev_err(&chip->dev,
265745b361eSJarkko Sakkinen 			"tpm_transmit: tpm_recv: error %d\n", rc);
266a147918eSJarkko Sakkinen 		goto out;
267745b361eSJarkko Sakkinen 	} else if (len < TPM_HEADER_SIZE) {
268a147918eSJarkko Sakkinen 		rc = -EFAULT;
269a147918eSJarkko Sakkinen 		goto out;
270a147918eSJarkko Sakkinen 	}
271a147918eSJarkko Sakkinen 
272745b361eSJarkko Sakkinen 	if (len != be32_to_cpu(header->length)) {
273745b361eSJarkko Sakkinen 		rc = -EFAULT;
274a147918eSJarkko Sakkinen 		goto out;
275745b361eSJarkko Sakkinen 	}
276745b361eSJarkko Sakkinen 
277745b361eSJarkko Sakkinen 	rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
278627448e8STomas Winkler 	if (rc)
279627448e8STomas Winkler 		dev_err(&chip->dev, "tpm2_commit_space: error %d\n", rc);
280a147918eSJarkko Sakkinen 
2819deb0eb7SJason Gunthorpe out:
282627448e8STomas Winkler 	rc = tpm_go_idle(chip, flags);
283627448e8STomas Winkler 	if (rc)
284627448e8STomas Winkler 		goto out;
285888d867dSTomas Winkler 
286888d867dSTomas Winkler 	if (need_locality)
287627448e8STomas Winkler 		tpm_relinquish_locality(chip, flags);
288888d867dSTomas Winkler 
289877c57d0SJarkko Sakkinen out_no_locality:
290b3e958ceSAzhar Shaikh 	if (chip->ops->clk_enable != NULL)
291b3e958ceSAzhar Shaikh 		chip->ops->clk_enable(chip, false);
292b3e958ceSAzhar Shaikh 
29358bac8ccSJarkko Sakkinen 	if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
2949deb0eb7SJason Gunthorpe 		mutex_unlock(&chip->tpm_mutex);
295745b361eSJarkko Sakkinen 	return rc ? rc : len;
2969deb0eb7SJason Gunthorpe }
2979deb0eb7SJason Gunthorpe 
298f865c196SWinkler, Tomas /**
299e2fb992dSJames Bottomley  * tpm_transmit - Internal kernel interface to transmit TPM commands.
300e2fb992dSJames Bottomley  *
301e2fb992dSJames Bottomley  * @chip: TPM chip to use
302e2fb992dSJames Bottomley  * @space: tpm space
303e2fb992dSJames Bottomley  * @buf: TPM command buffer
304e2fb992dSJames Bottomley  * @bufsiz: length of the TPM command buffer
305e2fb992dSJames Bottomley  * @flags: tpm transmit flags - bitmap
306e2fb992dSJames Bottomley  *
307e2fb992dSJames Bottomley  * A wrapper around tpm_try_transmit that handles TPM2_RC_RETRY
308e2fb992dSJames Bottomley  * returns from the TPM and retransmits the command after a delay up
309e2fb992dSJames Bottomley  * to a maximum wait of TPM2_DURATION_LONG.
310e2fb992dSJames Bottomley  *
311e2fb992dSJames Bottomley  * Note: TPM1 never returns TPM2_RC_RETRY so the retry logic is TPM2
312e2fb992dSJames Bottomley  * only
313e2fb992dSJames Bottomley  *
314e2fb992dSJames Bottomley  * Return:
315e2fb992dSJames Bottomley  *     the length of the return when the operation is successful.
316e2fb992dSJames Bottomley  *     A negative number for system errors (errno).
317e2fb992dSJames Bottomley  */
318e2fb992dSJames Bottomley ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
319e2fb992dSJames Bottomley 		     u8 *buf, size_t bufsiz, unsigned int flags)
320e2fb992dSJames Bottomley {
321e2fb992dSJames Bottomley 	struct tpm_output_header *header = (struct tpm_output_header *)buf;
322e2fb992dSJames Bottomley 	/* space for header and handles */
323e2fb992dSJames Bottomley 	u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
324e2fb992dSJames Bottomley 	unsigned int delay_msec = TPM2_DURATION_SHORT;
325e2fb992dSJames Bottomley 	u32 rc = 0;
326e2fb992dSJames Bottomley 	ssize_t ret;
327e2fb992dSJames Bottomley 	const size_t save_size = min(space ? sizeof(save) : TPM_HEADER_SIZE,
328e2fb992dSJames Bottomley 				     bufsiz);
3292be8ffedSJames Bottomley 	/* the command code is where the return code will be */
3302be8ffedSJames Bottomley 	u32 cc = be32_to_cpu(header->return_code);
331e2fb992dSJames Bottomley 
332e2fb992dSJames Bottomley 	/*
333e2fb992dSJames Bottomley 	 * Subtlety here: if we have a space, the handles will be
334e2fb992dSJames Bottomley 	 * transformed, so when we restore the header we also have to
335e2fb992dSJames Bottomley 	 * restore the handles.
336e2fb992dSJames Bottomley 	 */
337e2fb992dSJames Bottomley 	memcpy(save, buf, save_size);
338e2fb992dSJames Bottomley 
339e2fb992dSJames Bottomley 	for (;;) {
340e2fb992dSJames Bottomley 		ret = tpm_try_transmit(chip, space, buf, bufsiz, flags);
341e2fb992dSJames Bottomley 		if (ret < 0)
342e2fb992dSJames Bottomley 			break;
343e2fb992dSJames Bottomley 		rc = be32_to_cpu(header->return_code);
3442be8ffedSJames Bottomley 		if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
3452be8ffedSJames Bottomley 			break;
3462be8ffedSJames Bottomley 		/*
3472be8ffedSJames Bottomley 		 * return immediately if self test returns test
3482be8ffedSJames Bottomley 		 * still running to shorten boot time.
3492be8ffedSJames Bottomley 		 */
3502be8ffedSJames Bottomley 		if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
351e2fb992dSJames Bottomley 			break;
35292980756SNayna Jain 
353e2fb992dSJames Bottomley 		if (delay_msec > TPM2_DURATION_LONG) {
3542be8ffedSJames Bottomley 			if (rc == TPM2_RC_RETRY)
3552be8ffedSJames Bottomley 				dev_err(&chip->dev, "in retry loop\n");
3562be8ffedSJames Bottomley 			else
3572be8ffedSJames Bottomley 				dev_err(&chip->dev,
3582be8ffedSJames Bottomley 					"self test is still running\n");
359e2fb992dSJames Bottomley 			break;
360e2fb992dSJames Bottomley 		}
361e2fb992dSJames Bottomley 		tpm_msleep(delay_msec);
36292980756SNayna Jain 		delay_msec *= 2;
363e2fb992dSJames Bottomley 		memcpy(buf, save, save_size);
364e2fb992dSJames Bottomley 	}
365e2fb992dSJames Bottomley 	return ret;
366e2fb992dSJames Bottomley }
367e2fb992dSJames Bottomley /**
36865520d46SWinkler, Tomas  * tpm_transmit_cmd - send a tpm command to the device
369f865c196SWinkler, Tomas  *    The function extracts tpm out header return code
370f865c196SWinkler, Tomas  *
371f865c196SWinkler, Tomas  * @chip: TPM chip to use
37265520d46SWinkler, Tomas  * @space: tpm space
373c659af78SStefan Berger  * @buf: TPM command buffer
374c659af78SStefan Berger  * @bufsiz: length of the buffer
375c659af78SStefan Berger  * @min_rsp_body_length: minimum expected length of response body
376f865c196SWinkler, Tomas  * @flags: tpm transmit flags - bitmap
377f865c196SWinkler, Tomas  * @desc: command description used in the error message
378f865c196SWinkler, Tomas  *
379f865c196SWinkler, Tomas  * Return:
380f865c196SWinkler, Tomas  *     0 when the operation is successful.
381f865c196SWinkler, Tomas  *     A negative number for system errors (errno).
382f865c196SWinkler, Tomas  *     A positive number for a TPM error.
383f865c196SWinkler, Tomas  */
384745b361eSJarkko Sakkinen ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
38562c09e12SWinkler, Tomas 			 void *buf, size_t bufsiz,
386745b361eSJarkko Sakkinen 			 size_t min_rsp_body_length, unsigned int flags,
387745b361eSJarkko Sakkinen 			 const char *desc)
3889deb0eb7SJason Gunthorpe {
389a147918eSJarkko Sakkinen 	const struct tpm_output_header *header = buf;
3909deb0eb7SJason Gunthorpe 	int err;
391c659af78SStefan Berger 	ssize_t len;
3929deb0eb7SJason Gunthorpe 
39362c09e12SWinkler, Tomas 	len = tpm_transmit(chip, space, buf, bufsiz, 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
3990d6d0d62SJavier Martinez Canillas 	    && 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  */
457aad887f6SJarkko Sakkinen int tpm_pcr_read(struct tpm_chip *chip, int 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
468d4a31756STomas Winkler 		rc = tpm1_pcr_read_dev(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  */
487aad887f6SJarkko Sakkinen int tpm_pcr_extend(struct tpm_chip *chip, int 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 {
5309deb0eb7SJason Gunthorpe 	int rc;
5319deb0eb7SJason Gunthorpe 
532fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
533aad887f6SJarkko Sakkinen 	if (!chip)
5349deb0eb7SJason Gunthorpe 		return -ENODEV;
5359deb0eb7SJason Gunthorpe 
536745b361eSJarkko Sakkinen 	rc = tpm_transmit_cmd(chip, NULL, cmd, buflen, 0, 0,
537aad887f6SJarkko Sakkinen 			      "attempting to a send a command");
5384e26195fSJason Gunthorpe 	tpm_put_ops(chip);
5399deb0eb7SJason Gunthorpe 	return rc;
5409deb0eb7SJason Gunthorpe }
5419deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_send);
5429deb0eb7SJason Gunthorpe 
543b03c4370STomas Winkler int tpm_auto_startup(struct tpm_chip *chip)
544b03c4370STomas Winkler {
545b03c4370STomas Winkler 	int rc;
546b03c4370STomas Winkler 
547b03c4370STomas Winkler 	if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
548b03c4370STomas Winkler 		return 0;
549b03c4370STomas Winkler 
550b03c4370STomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
551b03c4370STomas Winkler 		rc = tpm2_auto_startup(chip);
552b03c4370STomas Winkler 	else
553b03c4370STomas Winkler 		rc = tpm1_auto_startup(chip);
554b03c4370STomas Winkler 
555b03c4370STomas Winkler 	return rc;
556b03c4370STomas Winkler }
557b03c4370STomas Winkler 
5589deb0eb7SJason Gunthorpe /*
5599deb0eb7SJason Gunthorpe  * We are about to suspend. Save the TPM state
5609deb0eb7SJason Gunthorpe  * so that it can be restored.
5619deb0eb7SJason Gunthorpe  */
5629deb0eb7SJason Gunthorpe int tpm_pm_suspend(struct device *dev)
5639deb0eb7SJason Gunthorpe {
564ec03c50bSStefan Berger 	struct tpm_chip *chip = dev_get_drvdata(dev);
565c82a330cSTomas Winkler 	int rc = 0;
5669deb0eb7SJason Gunthorpe 
567c82a330cSTomas Winkler 	if (!chip)
5689deb0eb7SJason Gunthorpe 		return -ENODEV;
5699deb0eb7SJason Gunthorpe 
570b5d0ebc9SEnric Balletbo i Serra 	if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
571b5d0ebc9SEnric Balletbo i Serra 		return 0;
572b5d0ebc9SEnric Balletbo i Serra 
573c82a330cSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
57474d6b3ceSJarkko Sakkinen 		tpm2_shutdown(chip, TPM2_SU_STATE);
575c82a330cSTomas Winkler 	else
576c82a330cSTomas Winkler 		rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
5779deb0eb7SJason Gunthorpe 
5789deb0eb7SJason Gunthorpe 	return rc;
5799deb0eb7SJason Gunthorpe }
5809deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_suspend);
5819deb0eb7SJason Gunthorpe 
5829deb0eb7SJason Gunthorpe /*
5839deb0eb7SJason Gunthorpe  * Resume from a power safe. The BIOS already restored
5849deb0eb7SJason Gunthorpe  * the TPM state.
5859deb0eb7SJason Gunthorpe  */
5869deb0eb7SJason Gunthorpe int tpm_pm_resume(struct device *dev)
5879deb0eb7SJason Gunthorpe {
588ec03c50bSStefan Berger 	struct tpm_chip *chip = dev_get_drvdata(dev);
5899deb0eb7SJason Gunthorpe 
5909deb0eb7SJason Gunthorpe 	if (chip == NULL)
5919deb0eb7SJason Gunthorpe 		return -ENODEV;
5929deb0eb7SJason Gunthorpe 
5939deb0eb7SJason Gunthorpe 	return 0;
5949deb0eb7SJason Gunthorpe }
5959deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_resume);
5969deb0eb7SJason Gunthorpe 
5979deb0eb7SJason Gunthorpe /**
598aad887f6SJarkko Sakkinen  * tpm_get_random() - get random bytes from the TPM's RNG
599aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
6009deb0eb7SJason Gunthorpe  * @out:	destination buffer for the random bytes
6019deb0eb7SJason Gunthorpe  * @max:	the max number of bytes to write to @out
6029deb0eb7SJason Gunthorpe  *
603*7aee9c52STomas Winkler  * Return: number of random bytes read or a negative error value.
6049deb0eb7SJason Gunthorpe  */
605aad887f6SJarkko Sakkinen int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
6069deb0eb7SJason Gunthorpe {
607433d390fSTomas Winkler 	int rc;
6089deb0eb7SJason Gunthorpe 
609433d390fSTomas Winkler 	if (!out || max > TPM_MAX_RNG_DATA)
6103e14d83eSJarkko Sakkinen 		return -EINVAL;
6113e14d83eSJarkko Sakkinen 
612fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
613aad887f6SJarkko Sakkinen 	if (!chip)
6149deb0eb7SJason Gunthorpe 		return -ENODEV;
6159deb0eb7SJason Gunthorpe 
616433d390fSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
617433d390fSTomas Winkler 		rc = tpm2_get_random(chip, out, max);
618433d390fSTomas Winkler 	else
619433d390fSTomas Winkler 		rc = tpm1_get_random(chip, out, max);
6209deb0eb7SJason Gunthorpe 
6214e26195fSJason Gunthorpe 	tpm_put_ops(chip);
622433d390fSTomas Winkler 	return rc;
6239deb0eb7SJason Gunthorpe }
6249deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_random);
6259deb0eb7SJason Gunthorpe 
626954650efSJarkko Sakkinen /**
627aad887f6SJarkko Sakkinen  * tpm_seal_trusted() - seal a trusted key payload
628aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
629954650efSJarkko Sakkinen  * @options:	authentication values and other options
630954650efSJarkko Sakkinen  * @payload:	the key data in clear and encrypted form
631954650efSJarkko Sakkinen  *
632aad887f6SJarkko Sakkinen  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
633aad887f6SJarkko Sakkinen  * the keyring subsystem.
634aad887f6SJarkko Sakkinen  *
635aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
636954650efSJarkko Sakkinen  */
637aad887f6SJarkko Sakkinen int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
638954650efSJarkko Sakkinen 		     struct trusted_key_options *options)
639954650efSJarkko Sakkinen {
640954650efSJarkko Sakkinen 	int rc;
641954650efSJarkko Sakkinen 
642fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
643aad887f6SJarkko Sakkinen 	if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
644954650efSJarkko Sakkinen 		return -ENODEV;
645954650efSJarkko Sakkinen 
646954650efSJarkko Sakkinen 	rc = tpm2_seal_trusted(chip, payload, options);
647954650efSJarkko Sakkinen 
6484e26195fSJason Gunthorpe 	tpm_put_ops(chip);
649954650efSJarkko Sakkinen 	return rc;
650954650efSJarkko Sakkinen }
651954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_seal_trusted);
652954650efSJarkko Sakkinen 
653954650efSJarkko Sakkinen /**
654954650efSJarkko Sakkinen  * tpm_unseal_trusted() - unseal a trusted key
655aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
656954650efSJarkko Sakkinen  * @options:	authentication values and other options
657954650efSJarkko Sakkinen  * @payload:	the key data in clear and encrypted form
658954650efSJarkko Sakkinen  *
659aad887f6SJarkko Sakkinen  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
660aad887f6SJarkko Sakkinen  * the keyring subsystem.
661aad887f6SJarkko Sakkinen  *
662aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
663954650efSJarkko Sakkinen  */
664aad887f6SJarkko Sakkinen int tpm_unseal_trusted(struct tpm_chip *chip,
665aad887f6SJarkko Sakkinen 		       struct trusted_key_payload *payload,
666954650efSJarkko Sakkinen 		       struct trusted_key_options *options)
667954650efSJarkko Sakkinen {
668954650efSJarkko Sakkinen 	int rc;
669954650efSJarkko Sakkinen 
670fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
671aad887f6SJarkko Sakkinen 	if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
672954650efSJarkko Sakkinen 		return -ENODEV;
673954650efSJarkko Sakkinen 
674954650efSJarkko Sakkinen 	rc = tpm2_unseal_trusted(chip, payload, options);
675954650efSJarkko Sakkinen 
6764e26195fSJason Gunthorpe 	tpm_put_ops(chip);
6774e26195fSJason Gunthorpe 
678954650efSJarkko Sakkinen 	return rc;
679954650efSJarkko Sakkinen }
680954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
681954650efSJarkko Sakkinen 
682313d21eeSJarkko Sakkinen static int __init tpm_init(void)
683313d21eeSJarkko Sakkinen {
684313d21eeSJarkko Sakkinen 	int rc;
685313d21eeSJarkko Sakkinen 
686313d21eeSJarkko Sakkinen 	tpm_class = class_create(THIS_MODULE, "tpm");
687313d21eeSJarkko Sakkinen 	if (IS_ERR(tpm_class)) {
688313d21eeSJarkko Sakkinen 		pr_err("couldn't create tpm class\n");
689313d21eeSJarkko Sakkinen 		return PTR_ERR(tpm_class);
690313d21eeSJarkko Sakkinen 	}
691313d21eeSJarkko Sakkinen 
692fdc915f7SJames Bottomley 	tpmrm_class = class_create(THIS_MODULE, "tpmrm");
693fdc915f7SJames Bottomley 	if (IS_ERR(tpmrm_class)) {
694fdc915f7SJames Bottomley 		pr_err("couldn't create tpmrm class\n");
6959e1b74a6STadeusz Struk 		rc = PTR_ERR(tpmrm_class);
6969e1b74a6STadeusz Struk 		goto out_destroy_tpm_class;
697fdc915f7SJames Bottomley 	}
698fdc915f7SJames Bottomley 
699fdc915f7SJames Bottomley 	rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
700313d21eeSJarkko Sakkinen 	if (rc < 0) {
701313d21eeSJarkko Sakkinen 		pr_err("tpm: failed to allocate char dev region\n");
7029e1b74a6STadeusz Struk 		goto out_destroy_tpmrm_class;
7039e1b74a6STadeusz Struk 	}
7049e1b74a6STadeusz Struk 
7059e1b74a6STadeusz Struk 	rc = tpm_dev_common_init();
7069e1b74a6STadeusz Struk 	if (rc) {
7079e1b74a6STadeusz Struk 		pr_err("tpm: failed to allocate char dev region\n");
7089e1b74a6STadeusz Struk 		goto out_unreg_chrdev;
709313d21eeSJarkko Sakkinen 	}
710313d21eeSJarkko Sakkinen 
711313d21eeSJarkko Sakkinen 	return 0;
7129e1b74a6STadeusz Struk 
7139e1b74a6STadeusz Struk out_unreg_chrdev:
7149e1b74a6STadeusz Struk 	unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
7159e1b74a6STadeusz Struk out_destroy_tpmrm_class:
7169e1b74a6STadeusz Struk 	class_destroy(tpmrm_class);
7179e1b74a6STadeusz Struk out_destroy_tpm_class:
7189e1b74a6STadeusz Struk 	class_destroy(tpm_class);
7199e1b74a6STadeusz Struk 
7209e1b74a6STadeusz Struk 	return rc;
721313d21eeSJarkko Sakkinen }
722313d21eeSJarkko Sakkinen 
723313d21eeSJarkko Sakkinen static void __exit tpm_exit(void)
724313d21eeSJarkko Sakkinen {
72515516788SStefan Berger 	idr_destroy(&dev_nums_idr);
726313d21eeSJarkko Sakkinen 	class_destroy(tpm_class);
727fdc915f7SJames Bottomley 	class_destroy(tpmrm_class);
728fdc915f7SJames Bottomley 	unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
7299e1b74a6STadeusz Struk 	tpm_dev_common_exit();
730313d21eeSJarkko Sakkinen }
731313d21eeSJarkko Sakkinen 
732313d21eeSJarkko Sakkinen subsys_initcall(tpm_init);
733313d21eeSJarkko Sakkinen module_exit(tpm_exit);
734313d21eeSJarkko Sakkinen 
7359deb0eb7SJason Gunthorpe MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
7369deb0eb7SJason Gunthorpe MODULE_DESCRIPTION("TPM Driver");
7379deb0eb7SJason Gunthorpe MODULE_VERSION("2.0");
7389deb0eb7SJason Gunthorpe MODULE_LICENSE("GPL");
739