xref: /openbmc/linux/drivers/char/tpm/tpm-interface.c (revision f5595f5baa30e009bf54d0d7653a9a0cc465be60)
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);
21501f54664STomas Winkler 		if (rc < 0) {
21601f54664STomas Winkler 			need_locality = false;
21701f54664STomas Winkler 			goto out_locality;
21801f54664STomas Winkler 		}
219877c57d0SJarkko Sakkinen 	}
220877c57d0SJarkko Sakkinen 
221627448e8STomas Winkler 	rc = tpm_cmd_ready(chip, flags);
222627448e8STomas Winkler 	if (rc)
22301f54664STomas 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,
233*f5595f5bSJarkko Sakkinen 				"%s: send(): error %d\n", __func__, rc);
2349deb0eb7SJason Gunthorpe 		goto out;
2359deb0eb7SJason Gunthorpe 	}
2369deb0eb7SJason Gunthorpe 
237*f5595f5bSJarkko Sakkinen 	/* A sanity check. send() should just return zero on success e.g.
238*f5595f5bSJarkko Sakkinen 	 * not the command length.
239*f5595f5bSJarkko Sakkinen 	 */
240*f5595f5bSJarkko Sakkinen 	if (rc > 0) {
241*f5595f5bSJarkko Sakkinen 		dev_warn(&chip->dev,
242*f5595f5bSJarkko Sakkinen 			 "%s: send(): invalid value %d\n", __func__, rc);
243*f5595f5bSJarkko Sakkinen 		rc = 0;
244*f5595f5bSJarkko Sakkinen 	}
245*f5595f5bSJarkko Sakkinen 
246570a3609SChristophe Ricard 	if (chip->flags & TPM_CHIP_FLAG_IRQ)
2479deb0eb7SJason Gunthorpe 		goto out_recv;
2489deb0eb7SJason Gunthorpe 
249d856c00fSTomas Winkler 	stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
2509deb0eb7SJason Gunthorpe 	do {
2515f82e9f0SJason Gunthorpe 		u8 status = chip->ops->status(chip);
2525f82e9f0SJason Gunthorpe 		if ((status & chip->ops->req_complete_mask) ==
2535f82e9f0SJason Gunthorpe 		    chip->ops->req_complete_val)
2549deb0eb7SJason Gunthorpe 			goto out_recv;
2559deb0eb7SJason Gunthorpe 
2565f82e9f0SJason Gunthorpe 		if (chip->ops->req_canceled(chip, status)) {
2578cfffc9dSJason Gunthorpe 			dev_err(&chip->dev, "Operation Canceled\n");
2589deb0eb7SJason Gunthorpe 			rc = -ECANCELED;
2599deb0eb7SJason Gunthorpe 			goto out;
2609deb0eb7SJason Gunthorpe 		}
2619deb0eb7SJason Gunthorpe 
26259f5a6b0SNayna Jain 		tpm_msleep(TPM_TIMEOUT_POLL);
2639deb0eb7SJason Gunthorpe 		rmb();
2649deb0eb7SJason Gunthorpe 	} while (time_before(jiffies, stop));
2659deb0eb7SJason Gunthorpe 
2665f82e9f0SJason Gunthorpe 	chip->ops->cancel(chip);
2678cfffc9dSJason Gunthorpe 	dev_err(&chip->dev, "Operation Timed out\n");
2689deb0eb7SJason Gunthorpe 	rc = -ETIME;
2699deb0eb7SJason Gunthorpe 	goto out;
2709deb0eb7SJason Gunthorpe 
2719deb0eb7SJason Gunthorpe out_recv:
27262c09e12SWinkler, Tomas 	len = chip->ops->recv(chip, buf, bufsiz);
273745b361eSJarkko Sakkinen 	if (len < 0) {
274745b361eSJarkko Sakkinen 		rc = len;
2758cfffc9dSJason Gunthorpe 		dev_err(&chip->dev,
276745b361eSJarkko Sakkinen 			"tpm_transmit: tpm_recv: error %d\n", rc);
277a147918eSJarkko Sakkinen 		goto out;
278745b361eSJarkko Sakkinen 	} else if (len < TPM_HEADER_SIZE) {
279a147918eSJarkko Sakkinen 		rc = -EFAULT;
280a147918eSJarkko Sakkinen 		goto out;
281a147918eSJarkko Sakkinen 	}
282a147918eSJarkko Sakkinen 
283745b361eSJarkko Sakkinen 	if (len != be32_to_cpu(header->length)) {
284745b361eSJarkko Sakkinen 		rc = -EFAULT;
285a147918eSJarkko Sakkinen 		goto out;
286745b361eSJarkko Sakkinen 	}
287745b361eSJarkko Sakkinen 
288745b361eSJarkko Sakkinen 	rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
289627448e8STomas Winkler 	if (rc)
290627448e8STomas Winkler 		dev_err(&chip->dev, "tpm2_commit_space: error %d\n", rc);
291a147918eSJarkko Sakkinen 
2929deb0eb7SJason Gunthorpe out:
29301f54664STomas Winkler 	/* may fail but do not override previous error value in rc */
29401f54664STomas Winkler 	tpm_go_idle(chip, flags);
295888d867dSTomas Winkler 
29601f54664STomas Winkler out_locality:
297888d867dSTomas Winkler 	if (need_locality)
298627448e8STomas Winkler 		tpm_relinquish_locality(chip, flags);
299888d867dSTomas Winkler 
300b3e958ceSAzhar Shaikh 	if (chip->ops->clk_enable != NULL)
301b3e958ceSAzhar Shaikh 		chip->ops->clk_enable(chip, false);
302b3e958ceSAzhar Shaikh 
30358bac8ccSJarkko Sakkinen 	if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
3049deb0eb7SJason Gunthorpe 		mutex_unlock(&chip->tpm_mutex);
305745b361eSJarkko Sakkinen 	return rc ? rc : len;
3069deb0eb7SJason Gunthorpe }
3079deb0eb7SJason Gunthorpe 
308f865c196SWinkler, Tomas /**
309e2fb992dSJames Bottomley  * tpm_transmit - Internal kernel interface to transmit TPM commands.
310e2fb992dSJames Bottomley  *
311e2fb992dSJames Bottomley  * @chip: TPM chip to use
312e2fb992dSJames Bottomley  * @space: tpm space
313e2fb992dSJames Bottomley  * @buf: TPM command buffer
314e2fb992dSJames Bottomley  * @bufsiz: length of the TPM command buffer
315e2fb992dSJames Bottomley  * @flags: tpm transmit flags - bitmap
316e2fb992dSJames Bottomley  *
317e2fb992dSJames Bottomley  * A wrapper around tpm_try_transmit that handles TPM2_RC_RETRY
318e2fb992dSJames Bottomley  * returns from the TPM and retransmits the command after a delay up
319e2fb992dSJames Bottomley  * to a maximum wait of TPM2_DURATION_LONG.
320e2fb992dSJames Bottomley  *
321e2fb992dSJames Bottomley  * Note: TPM1 never returns TPM2_RC_RETRY so the retry logic is TPM2
322e2fb992dSJames Bottomley  * only
323e2fb992dSJames Bottomley  *
324e2fb992dSJames Bottomley  * Return:
325e2fb992dSJames Bottomley  *     the length of the return when the operation is successful.
326e2fb992dSJames Bottomley  *     A negative number for system errors (errno).
327e2fb992dSJames Bottomley  */
328e2fb992dSJames Bottomley ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
329e2fb992dSJames Bottomley 		     u8 *buf, size_t bufsiz, unsigned int flags)
330e2fb992dSJames Bottomley {
331e2fb992dSJames Bottomley 	struct tpm_output_header *header = (struct tpm_output_header *)buf;
332e2fb992dSJames Bottomley 	/* space for header and handles */
333e2fb992dSJames Bottomley 	u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
334e2fb992dSJames Bottomley 	unsigned int delay_msec = TPM2_DURATION_SHORT;
335e2fb992dSJames Bottomley 	u32 rc = 0;
336e2fb992dSJames Bottomley 	ssize_t ret;
337e2fb992dSJames Bottomley 	const size_t save_size = min(space ? sizeof(save) : TPM_HEADER_SIZE,
338e2fb992dSJames Bottomley 				     bufsiz);
3392be8ffedSJames Bottomley 	/* the command code is where the return code will be */
3402be8ffedSJames Bottomley 	u32 cc = be32_to_cpu(header->return_code);
341e2fb992dSJames Bottomley 
342e2fb992dSJames Bottomley 	/*
343e2fb992dSJames Bottomley 	 * Subtlety here: if we have a space, the handles will be
344e2fb992dSJames Bottomley 	 * transformed, so when we restore the header we also have to
345e2fb992dSJames Bottomley 	 * restore the handles.
346e2fb992dSJames Bottomley 	 */
347e2fb992dSJames Bottomley 	memcpy(save, buf, save_size);
348e2fb992dSJames Bottomley 
349e2fb992dSJames Bottomley 	for (;;) {
350e2fb992dSJames Bottomley 		ret = tpm_try_transmit(chip, space, buf, bufsiz, flags);
351e2fb992dSJames Bottomley 		if (ret < 0)
352e2fb992dSJames Bottomley 			break;
353e2fb992dSJames Bottomley 		rc = be32_to_cpu(header->return_code);
3542be8ffedSJames Bottomley 		if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
3552be8ffedSJames Bottomley 			break;
3562be8ffedSJames Bottomley 		/*
3572be8ffedSJames Bottomley 		 * return immediately if self test returns test
3582be8ffedSJames Bottomley 		 * still running to shorten boot time.
3592be8ffedSJames Bottomley 		 */
3602be8ffedSJames Bottomley 		if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
361e2fb992dSJames Bottomley 			break;
36292980756SNayna Jain 
363e2fb992dSJames Bottomley 		if (delay_msec > TPM2_DURATION_LONG) {
3642be8ffedSJames Bottomley 			if (rc == TPM2_RC_RETRY)
3652be8ffedSJames Bottomley 				dev_err(&chip->dev, "in retry loop\n");
3662be8ffedSJames Bottomley 			else
3672be8ffedSJames Bottomley 				dev_err(&chip->dev,
3682be8ffedSJames Bottomley 					"self test is still running\n");
369e2fb992dSJames Bottomley 			break;
370e2fb992dSJames Bottomley 		}
371e2fb992dSJames Bottomley 		tpm_msleep(delay_msec);
37292980756SNayna Jain 		delay_msec *= 2;
373e2fb992dSJames Bottomley 		memcpy(buf, save, save_size);
374e2fb992dSJames Bottomley 	}
375e2fb992dSJames Bottomley 	return ret;
376e2fb992dSJames Bottomley }
377e2fb992dSJames Bottomley /**
37865520d46SWinkler, Tomas  * tpm_transmit_cmd - send a tpm command to the device
379f865c196SWinkler, Tomas  *    The function extracts tpm out header return code
380f865c196SWinkler, Tomas  *
381f865c196SWinkler, Tomas  * @chip: TPM chip to use
38265520d46SWinkler, Tomas  * @space: tpm space
383c659af78SStefan Berger  * @buf: TPM command buffer
384c659af78SStefan Berger  * @bufsiz: length of the buffer
385c659af78SStefan Berger  * @min_rsp_body_length: minimum expected length of response body
386f865c196SWinkler, Tomas  * @flags: tpm transmit flags - bitmap
387f865c196SWinkler, Tomas  * @desc: command description used in the error message
388f865c196SWinkler, Tomas  *
389f865c196SWinkler, Tomas  * Return:
390f865c196SWinkler, Tomas  *     0 when the operation is successful.
391f865c196SWinkler, Tomas  *     A negative number for system errors (errno).
392f865c196SWinkler, Tomas  *     A positive number for a TPM error.
393f865c196SWinkler, Tomas  */
394745b361eSJarkko Sakkinen ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
39562c09e12SWinkler, Tomas 			 void *buf, size_t bufsiz,
396745b361eSJarkko Sakkinen 			 size_t min_rsp_body_length, unsigned int flags,
397745b361eSJarkko Sakkinen 			 const char *desc)
3989deb0eb7SJason Gunthorpe {
399a147918eSJarkko Sakkinen 	const struct tpm_output_header *header = buf;
4009deb0eb7SJason Gunthorpe 	int err;
401c659af78SStefan Berger 	ssize_t len;
4029deb0eb7SJason Gunthorpe 
40362c09e12SWinkler, Tomas 	len = tpm_transmit(chip, space, buf, bufsiz, flags);
4049deb0eb7SJason Gunthorpe 	if (len <  0)
4059deb0eb7SJason Gunthorpe 		return len;
40687155b73SJarkko Sakkinen 
40787155b73SJarkko Sakkinen 	err = be32_to_cpu(header->return_code);
4080d6d0d62SJavier Martinez Canillas 	if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED
4090d6d0d62SJavier Martinez Canillas 	    && desc)
4108cfffc9dSJason Gunthorpe 		dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
41171ed848fSJarkko Sakkinen 			desc);
412c659af78SStefan Berger 	if (err)
4139deb0eb7SJason Gunthorpe 		return err;
414c659af78SStefan Berger 
415c659af78SStefan Berger 	if (len < min_rsp_body_length + TPM_HEADER_SIZE)
416c659af78SStefan Berger 		return -EFAULT;
417c659af78SStefan Berger 
418c659af78SStefan Berger 	return 0;
4199deb0eb7SJason Gunthorpe }
420be4c9acfSStefan Berger EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
4219deb0eb7SJason Gunthorpe 
4229deb0eb7SJason Gunthorpe int tpm_get_timeouts(struct tpm_chip *chip)
4239deb0eb7SJason Gunthorpe {
424d1d253cfSJason Gunthorpe 	if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
425d1d253cfSJason Gunthorpe 		return 0;
426d1d253cfSJason Gunthorpe 
42770a3199aSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
42870a3199aSTomas Winkler 		return tpm2_get_timeouts(chip);
42970a3199aSTomas Winkler 	else
43070a3199aSTomas Winkler 		return tpm1_get_timeouts(chip);
4319deb0eb7SJason Gunthorpe }
4329deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_timeouts);
4339deb0eb7SJason Gunthorpe 
4349deb0eb7SJason Gunthorpe /**
435aad887f6SJarkko Sakkinen  * tpm_is_tpm2 - do we a have a TPM2 chip?
436aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
437954650efSJarkko Sakkinen  *
438aad887f6SJarkko Sakkinen  * Return:
439aad887f6SJarkko Sakkinen  * 1 if we have a TPM2 chip.
440aad887f6SJarkko Sakkinen  * 0 if we don't have a TPM2 chip.
441aad887f6SJarkko Sakkinen  * A negative number for system errors (errno).
442954650efSJarkko Sakkinen  */
443aad887f6SJarkko Sakkinen int tpm_is_tpm2(struct tpm_chip *chip)
444954650efSJarkko Sakkinen {
445954650efSJarkko Sakkinen 	int rc;
446954650efSJarkko Sakkinen 
447fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
448aad887f6SJarkko Sakkinen 	if (!chip)
449954650efSJarkko Sakkinen 		return -ENODEV;
450954650efSJarkko Sakkinen 
451954650efSJarkko Sakkinen 	rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
452954650efSJarkko Sakkinen 
4534e26195fSJason Gunthorpe 	tpm_put_ops(chip);
454954650efSJarkko Sakkinen 
455954650efSJarkko Sakkinen 	return rc;
456954650efSJarkko Sakkinen }
457954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_is_tpm2);
458954650efSJarkko Sakkinen 
459954650efSJarkko Sakkinen /**
460aad887f6SJarkko Sakkinen  * tpm_pcr_read - read a PCR value from SHA1 bank
461aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
462aad887f6SJarkko Sakkinen  * @pcr_idx:	the PCR to be retrieved
463aad887f6SJarkko Sakkinen  * @res_buf:	the value of the PCR
4649deb0eb7SJason Gunthorpe  *
465aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
4669deb0eb7SJason Gunthorpe  */
46795adc6b4STomas Winkler int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
4689deb0eb7SJason Gunthorpe {
4699deb0eb7SJason Gunthorpe 	int rc;
4709deb0eb7SJason Gunthorpe 
471fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
472aad887f6SJarkko Sakkinen 	if (!chip)
4739deb0eb7SJason Gunthorpe 		return -ENODEV;
474d4a31756STomas Winkler 
4757a1d7e6dSJarkko Sakkinen 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
4767a1d7e6dSJarkko Sakkinen 		rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
4777a1d7e6dSJarkko Sakkinen 	else
478cfddcb05STomas Winkler 		rc = tpm1_pcr_read(chip, pcr_idx, res_buf);
479d4a31756STomas Winkler 
4804e26195fSJason Gunthorpe 	tpm_put_ops(chip);
4819deb0eb7SJason Gunthorpe 	return rc;
4829deb0eb7SJason Gunthorpe }
4839deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_read);
4849deb0eb7SJason Gunthorpe 
4859deb0eb7SJason Gunthorpe /**
486aad887f6SJarkko Sakkinen  * tpm_pcr_extend - extend a PCR value in SHA1 bank.
487aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
488aad887f6SJarkko Sakkinen  * @pcr_idx:	the PCR to be retrieved
489aad887f6SJarkko Sakkinen  * @hash:	the hash value used to extend the PCR value
4909deb0eb7SJason Gunthorpe  *
491aad887f6SJarkko Sakkinen  * Note: with TPM 2.0 extends also those banks with a known digest size to the
492aad887f6SJarkko Sakkinen  * cryto subsystem in order to prevent malicious use of those PCR banks. In the
493aad887f6SJarkko Sakkinen  * future we should dynamically determine digest sizes.
494aad887f6SJarkko Sakkinen  *
495aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
4969deb0eb7SJason Gunthorpe  */
49795adc6b4STomas Winkler int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash)
4989deb0eb7SJason Gunthorpe {
4999deb0eb7SJason Gunthorpe 	int rc;
500c1f92b4bSNayna Jain 	struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
501c1f92b4bSNayna Jain 	u32 count = 0;
502c1f92b4bSNayna Jain 	int i;
5039deb0eb7SJason Gunthorpe 
504fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
505aad887f6SJarkko Sakkinen 	if (!chip)
5069deb0eb7SJason Gunthorpe 		return -ENODEV;
5079deb0eb7SJason Gunthorpe 
5087a1d7e6dSJarkko Sakkinen 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
509c1f92b4bSNayna Jain 		memset(digest_list, 0, sizeof(digest_list));
510c1f92b4bSNayna Jain 
51170ea1636SDan Carpenter 		for (i = 0; i < ARRAY_SIZE(chip->active_banks) &&
51270ea1636SDan Carpenter 			    chip->active_banks[i] != TPM2_ALG_ERROR; i++) {
513c1f92b4bSNayna Jain 			digest_list[i].alg_id = chip->active_banks[i];
514c1f92b4bSNayna Jain 			memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
515c1f92b4bSNayna Jain 			count++;
516c1f92b4bSNayna Jain 		}
517c1f92b4bSNayna Jain 
518c1f92b4bSNayna Jain 		rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list);
5194e26195fSJason Gunthorpe 		tpm_put_ops(chip);
5207a1d7e6dSJarkko Sakkinen 		return rc;
5217a1d7e6dSJarkko Sakkinen 	}
5227a1d7e6dSJarkko Sakkinen 
523175d5b2aSRoberto Sassu 	rc = tpm1_pcr_extend(chip, pcr_idx, hash,
5249deb0eb7SJason Gunthorpe 			     "attempting extend a PCR value");
5254e26195fSJason Gunthorpe 	tpm_put_ops(chip);
5269deb0eb7SJason Gunthorpe 	return rc;
5279deb0eb7SJason Gunthorpe }
5289deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_extend);
5299deb0eb7SJason Gunthorpe 
5309deb0eb7SJason Gunthorpe /**
531aad887f6SJarkko Sakkinen  * tpm_send - send a TPM command
532aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
533aad887f6SJarkko Sakkinen  * @cmd:	a TPM command buffer
534aad887f6SJarkko Sakkinen  * @buflen:	the length of the TPM command buffer
535aad887f6SJarkko Sakkinen  *
536aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
537aad887f6SJarkko Sakkinen  */
538aad887f6SJarkko Sakkinen int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
5399deb0eb7SJason Gunthorpe {
5409deb0eb7SJason Gunthorpe 	int rc;
5419deb0eb7SJason Gunthorpe 
542fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
543aad887f6SJarkko Sakkinen 	if (!chip)
5449deb0eb7SJason Gunthorpe 		return -ENODEV;
5459deb0eb7SJason Gunthorpe 
546745b361eSJarkko Sakkinen 	rc = tpm_transmit_cmd(chip, NULL, cmd, buflen, 0, 0,
547aad887f6SJarkko Sakkinen 			      "attempting to a send a command");
5484e26195fSJason Gunthorpe 	tpm_put_ops(chip);
5499deb0eb7SJason Gunthorpe 	return rc;
5509deb0eb7SJason Gunthorpe }
5519deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_send);
5529deb0eb7SJason Gunthorpe 
553b03c4370STomas Winkler int tpm_auto_startup(struct tpm_chip *chip)
554b03c4370STomas Winkler {
555b03c4370STomas Winkler 	int rc;
556b03c4370STomas Winkler 
557b03c4370STomas Winkler 	if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
558b03c4370STomas Winkler 		return 0;
559b03c4370STomas Winkler 
560b03c4370STomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
561b03c4370STomas Winkler 		rc = tpm2_auto_startup(chip);
562b03c4370STomas Winkler 	else
563b03c4370STomas Winkler 		rc = tpm1_auto_startup(chip);
564b03c4370STomas Winkler 
565b03c4370STomas Winkler 	return rc;
566b03c4370STomas Winkler }
567b03c4370STomas Winkler 
5689deb0eb7SJason Gunthorpe /*
5699deb0eb7SJason Gunthorpe  * We are about to suspend. Save the TPM state
5709deb0eb7SJason Gunthorpe  * so that it can be restored.
5719deb0eb7SJason Gunthorpe  */
5729deb0eb7SJason Gunthorpe int tpm_pm_suspend(struct device *dev)
5739deb0eb7SJason Gunthorpe {
574ec03c50bSStefan Berger 	struct tpm_chip *chip = dev_get_drvdata(dev);
575c82a330cSTomas Winkler 	int rc = 0;
5769deb0eb7SJason Gunthorpe 
577c82a330cSTomas Winkler 	if (!chip)
5789deb0eb7SJason Gunthorpe 		return -ENODEV;
5799deb0eb7SJason Gunthorpe 
580b5d0ebc9SEnric Balletbo i Serra 	if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
581b5d0ebc9SEnric Balletbo i Serra 		return 0;
582b5d0ebc9SEnric Balletbo i Serra 
583c82a330cSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
58474d6b3ceSJarkko Sakkinen 		tpm2_shutdown(chip, TPM2_SU_STATE);
585c82a330cSTomas Winkler 	else
586c82a330cSTomas Winkler 		rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
5879deb0eb7SJason Gunthorpe 
5889deb0eb7SJason Gunthorpe 	return rc;
5899deb0eb7SJason Gunthorpe }
5909deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_suspend);
5919deb0eb7SJason Gunthorpe 
5929deb0eb7SJason Gunthorpe /*
5939deb0eb7SJason Gunthorpe  * Resume from a power safe. The BIOS already restored
5949deb0eb7SJason Gunthorpe  * the TPM state.
5959deb0eb7SJason Gunthorpe  */
5969deb0eb7SJason Gunthorpe int tpm_pm_resume(struct device *dev)
5979deb0eb7SJason Gunthorpe {
598ec03c50bSStefan Berger 	struct tpm_chip *chip = dev_get_drvdata(dev);
5999deb0eb7SJason Gunthorpe 
6009deb0eb7SJason Gunthorpe 	if (chip == NULL)
6019deb0eb7SJason Gunthorpe 		return -ENODEV;
6029deb0eb7SJason Gunthorpe 
6039deb0eb7SJason Gunthorpe 	return 0;
6049deb0eb7SJason Gunthorpe }
6059deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_resume);
6069deb0eb7SJason Gunthorpe 
6079deb0eb7SJason Gunthorpe /**
608aad887f6SJarkko Sakkinen  * tpm_get_random() - get random bytes from the TPM's RNG
609aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
6109deb0eb7SJason Gunthorpe  * @out:	destination buffer for the random bytes
6119deb0eb7SJason Gunthorpe  * @max:	the max number of bytes to write to @out
6129deb0eb7SJason Gunthorpe  *
6137aee9c52STomas Winkler  * Return: number of random bytes read or a negative error value.
6149deb0eb7SJason Gunthorpe  */
615aad887f6SJarkko Sakkinen int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
6169deb0eb7SJason Gunthorpe {
617433d390fSTomas Winkler 	int rc;
6189deb0eb7SJason Gunthorpe 
619433d390fSTomas Winkler 	if (!out || max > TPM_MAX_RNG_DATA)
6203e14d83eSJarkko Sakkinen 		return -EINVAL;
6213e14d83eSJarkko Sakkinen 
622fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
623aad887f6SJarkko Sakkinen 	if (!chip)
6249deb0eb7SJason Gunthorpe 		return -ENODEV;
6259deb0eb7SJason Gunthorpe 
626433d390fSTomas Winkler 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
627433d390fSTomas Winkler 		rc = tpm2_get_random(chip, out, max);
628433d390fSTomas Winkler 	else
629433d390fSTomas Winkler 		rc = tpm1_get_random(chip, out, max);
6309deb0eb7SJason Gunthorpe 
6314e26195fSJason Gunthorpe 	tpm_put_ops(chip);
632433d390fSTomas Winkler 	return rc;
6339deb0eb7SJason Gunthorpe }
6349deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_random);
6359deb0eb7SJason Gunthorpe 
636954650efSJarkko Sakkinen /**
637aad887f6SJarkko Sakkinen  * tpm_seal_trusted() - seal a trusted key payload
638aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
639954650efSJarkko Sakkinen  * @options:	authentication values and other options
640954650efSJarkko Sakkinen  * @payload:	the key data in clear and encrypted form
641954650efSJarkko Sakkinen  *
642aad887f6SJarkko Sakkinen  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
643aad887f6SJarkko Sakkinen  * the keyring subsystem.
644aad887f6SJarkko Sakkinen  *
645aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
646954650efSJarkko Sakkinen  */
647aad887f6SJarkko Sakkinen int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
648954650efSJarkko Sakkinen 		     struct trusted_key_options *options)
649954650efSJarkko Sakkinen {
650954650efSJarkko Sakkinen 	int rc;
651954650efSJarkko Sakkinen 
652fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
653aad887f6SJarkko Sakkinen 	if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
654954650efSJarkko Sakkinen 		return -ENODEV;
655954650efSJarkko Sakkinen 
656954650efSJarkko Sakkinen 	rc = tpm2_seal_trusted(chip, payload, options);
657954650efSJarkko Sakkinen 
6584e26195fSJason Gunthorpe 	tpm_put_ops(chip);
659954650efSJarkko Sakkinen 	return rc;
660954650efSJarkko Sakkinen }
661954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_seal_trusted);
662954650efSJarkko Sakkinen 
663954650efSJarkko Sakkinen /**
664954650efSJarkko Sakkinen  * tpm_unseal_trusted() - unseal a trusted key
665aad887f6SJarkko Sakkinen  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
666954650efSJarkko Sakkinen  * @options:	authentication values and other options
667954650efSJarkko Sakkinen  * @payload:	the key data in clear and encrypted form
668954650efSJarkko Sakkinen  *
669aad887f6SJarkko Sakkinen  * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
670aad887f6SJarkko Sakkinen  * the keyring subsystem.
671aad887f6SJarkko Sakkinen  *
672aad887f6SJarkko Sakkinen  * Return: same as with tpm_transmit_cmd()
673954650efSJarkko Sakkinen  */
674aad887f6SJarkko Sakkinen int tpm_unseal_trusted(struct tpm_chip *chip,
675aad887f6SJarkko Sakkinen 		       struct trusted_key_payload *payload,
676954650efSJarkko Sakkinen 		       struct trusted_key_options *options)
677954650efSJarkko Sakkinen {
678954650efSJarkko Sakkinen 	int rc;
679954650efSJarkko Sakkinen 
680fc1d52b7SStefan Berger 	chip = tpm_find_get_ops(chip);
681aad887f6SJarkko Sakkinen 	if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
682954650efSJarkko Sakkinen 		return -ENODEV;
683954650efSJarkko Sakkinen 
684954650efSJarkko Sakkinen 	rc = tpm2_unseal_trusted(chip, payload, options);
685954650efSJarkko Sakkinen 
6864e26195fSJason Gunthorpe 	tpm_put_ops(chip);
6874e26195fSJason Gunthorpe 
688954650efSJarkko Sakkinen 	return rc;
689954650efSJarkko Sakkinen }
690954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
691954650efSJarkko Sakkinen 
692313d21eeSJarkko Sakkinen static int __init tpm_init(void)
693313d21eeSJarkko Sakkinen {
694313d21eeSJarkko Sakkinen 	int rc;
695313d21eeSJarkko Sakkinen 
696313d21eeSJarkko Sakkinen 	tpm_class = class_create(THIS_MODULE, "tpm");
697313d21eeSJarkko Sakkinen 	if (IS_ERR(tpm_class)) {
698313d21eeSJarkko Sakkinen 		pr_err("couldn't create tpm class\n");
699313d21eeSJarkko Sakkinen 		return PTR_ERR(tpm_class);
700313d21eeSJarkko Sakkinen 	}
701313d21eeSJarkko Sakkinen 
702fdc915f7SJames Bottomley 	tpmrm_class = class_create(THIS_MODULE, "tpmrm");
703fdc915f7SJames Bottomley 	if (IS_ERR(tpmrm_class)) {
704fdc915f7SJames Bottomley 		pr_err("couldn't create tpmrm class\n");
7059e1b74a6STadeusz Struk 		rc = PTR_ERR(tpmrm_class);
7069e1b74a6STadeusz Struk 		goto out_destroy_tpm_class;
707fdc915f7SJames Bottomley 	}
708fdc915f7SJames Bottomley 
709fdc915f7SJames Bottomley 	rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
710313d21eeSJarkko Sakkinen 	if (rc < 0) {
711313d21eeSJarkko Sakkinen 		pr_err("tpm: failed to allocate char dev region\n");
7129e1b74a6STadeusz Struk 		goto out_destroy_tpmrm_class;
7139e1b74a6STadeusz Struk 	}
7149e1b74a6STadeusz Struk 
7159e1b74a6STadeusz Struk 	rc = tpm_dev_common_init();
7169e1b74a6STadeusz Struk 	if (rc) {
7179e1b74a6STadeusz Struk 		pr_err("tpm: failed to allocate char dev region\n");
7189e1b74a6STadeusz Struk 		goto out_unreg_chrdev;
719313d21eeSJarkko Sakkinen 	}
720313d21eeSJarkko Sakkinen 
721313d21eeSJarkko Sakkinen 	return 0;
7229e1b74a6STadeusz Struk 
7239e1b74a6STadeusz Struk out_unreg_chrdev:
7249e1b74a6STadeusz Struk 	unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
7259e1b74a6STadeusz Struk out_destroy_tpmrm_class:
7269e1b74a6STadeusz Struk 	class_destroy(tpmrm_class);
7279e1b74a6STadeusz Struk out_destroy_tpm_class:
7289e1b74a6STadeusz Struk 	class_destroy(tpm_class);
7299e1b74a6STadeusz Struk 
7309e1b74a6STadeusz Struk 	return rc;
731313d21eeSJarkko Sakkinen }
732313d21eeSJarkko Sakkinen 
733313d21eeSJarkko Sakkinen static void __exit tpm_exit(void)
734313d21eeSJarkko Sakkinen {
73515516788SStefan Berger 	idr_destroy(&dev_nums_idr);
736313d21eeSJarkko Sakkinen 	class_destroy(tpm_class);
737fdc915f7SJames Bottomley 	class_destroy(tpmrm_class);
738fdc915f7SJames Bottomley 	unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
7399e1b74a6STadeusz Struk 	tpm_dev_common_exit();
740313d21eeSJarkko Sakkinen }
741313d21eeSJarkko Sakkinen 
742313d21eeSJarkko Sakkinen subsys_initcall(tpm_init);
743313d21eeSJarkko Sakkinen module_exit(tpm_exit);
744313d21eeSJarkko Sakkinen 
7459deb0eb7SJason Gunthorpe MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
7469deb0eb7SJason Gunthorpe MODULE_DESCRIPTION("TPM Driver");
7479deb0eb7SJason Gunthorpe MODULE_VERSION("2.0");
7489deb0eb7SJason Gunthorpe MODULE_LICENSE("GPL");
749