blob: 1b5436b213a2d1c6194f423dbaecedd939d0dbf1 [file] [log] [blame]
Rajiv Andrade659aaf22009-02-02 15:23:44 -02001/*
2 * Copyright (C) 2004,2007,2008 IBM Corporation
3 *
4 * Authors:
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
9 * Debora Velarde <dvelarde@us.ibm.com>
10 *
11 * Maintained by: <tpmdd_devel@lists.sourceforge.net>
12 *
13 * Device driver for TCG/TCPA TPM (trusted platform module).
14 * Specifications at www.trustedcomputinggroup.org
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation, version 2 of the
19 * License.
20 *
21 */
22#ifndef __LINUX_TPM_H__
23#define __LINUX_TPM_H__
24
Roberto Sassu901615c2019-02-06 17:24:50 +010025#include <linux/hw_random.h>
26#include <linux/acpi.h>
27#include <linux/cdev.h>
28#include <linux/fs.h>
Roberto Sassuaa042472019-02-06 17:24:48 +010029#include <crypto/hash_info.h>
30
Mimi Zohar1c16c962013-05-21 10:40:47 -040031#define TPM_DIGEST_SIZE 20 /* Max TPM v1.2 PCR size */
Roberto Sassuaa042472019-02-06 17:24:48 +010032#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
Mimi Zohar1c16c962013-05-21 10:40:47 -040033
Jason Gunthorpe01ad1fa2013-11-26 13:30:43 -070034struct tpm_chip;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +030035struct trusted_key_payload;
36struct trusted_key_options;
Jason Gunthorpe01ad1fa2013-11-26 13:30:43 -070037
Roberto Sassuaa042472019-02-06 17:24:48 +010038enum tpm_algorithms {
39 TPM_ALG_ERROR = 0x0000,
40 TPM_ALG_SHA1 = 0x0004,
41 TPM_ALG_KEYEDHASH = 0x0008,
42 TPM_ALG_SHA256 = 0x000B,
43 TPM_ALG_SHA384 = 0x000C,
44 TPM_ALG_SHA512 = 0x000D,
45 TPM_ALG_NULL = 0x0010,
46 TPM_ALG_SM3_256 = 0x0012,
47};
48
49struct tpm_digest {
50 u16 alg_id;
51 u8 digest[TPM_MAX_DIGEST_SIZE];
52} __packed;
53
Roberto Sassu879b5892019-02-06 17:24:49 +010054struct tpm_bank_info {
55 u16 alg_id;
56 u16 digest_size;
57 u16 crypto_id;
58};
59
Jason Gunthorpecae8b442016-07-12 11:41:49 -060060enum TPM_OPS_FLAGS {
61 TPM_OPS_AUTO_STARTUP = BIT(0),
62};
63
Jason Gunthorpe01ad1fa2013-11-26 13:30:43 -070064struct tpm_class_ops {
Jason Gunthorpecae8b442016-07-12 11:41:49 -060065 unsigned int flags;
Jason Gunthorpe01ad1fa2013-11-26 13:30:43 -070066 const u8 req_complete_mask;
67 const u8 req_complete_val;
68 bool (*req_canceled)(struct tpm_chip *chip, u8 status);
69 int (*recv) (struct tpm_chip *chip, u8 *buf, size_t len);
70 int (*send) (struct tpm_chip *chip, u8 *buf, size_t len);
71 void (*cancel) (struct tpm_chip *chip);
72 u8 (*status) (struct tpm_chip *chip);
Jerry Snitselaar36ce0892019-01-30 15:06:58 -070073 void (*update_timeouts)(struct tpm_chip *chip,
Jason Gunthorpe8e54caf2014-05-21 18:26:44 -060074 unsigned long *timeout_cap);
Tomas Winkler627448e2018-06-28 18:13:33 +030075 int (*go_idle)(struct tpm_chip *chip);
76 int (*cmd_ready)(struct tpm_chip *chip);
Jarkko Sakkinen877c57d2017-03-24 11:45:49 +020077 int (*request_locality)(struct tpm_chip *chip, int loc);
Tomas Winkler888d8672018-03-05 13:34:49 +020078 int (*relinquish_locality)(struct tpm_chip *chip, int loc);
Azhar Shaikhb3e958c2017-12-22 12:13:44 -080079 void (*clk_enable)(struct tpm_chip *chip, bool value);
Jason Gunthorpe01ad1fa2013-11-26 13:30:43 -070080};
81
Roberto Sassu901615c2019-02-06 17:24:50 +010082#define TPM_NUM_EVENT_LOG_FILES 3
83
84/* Indexes the duration array */
85enum tpm_duration {
86 TPM_SHORT = 0,
87 TPM_MEDIUM = 1,
88 TPM_LONG = 2,
89 TPM_LONG_LONG = 3,
90 TPM_UNDEFINED,
91 TPM_NUM_DURATIONS = TPM_UNDEFINED,
92};
93
94#define TPM_PPI_VERSION_LEN 3
95
96struct tpm_space {
97 u32 context_tbl[3];
98 u8 *context_buf;
99 u32 session_tbl[3];
100 u8 *session_buf;
101};
102
103struct tpm_bios_log {
104 void *bios_event_log;
105 void *bios_event_log_end;
106};
107
108struct tpm_chip_seqops {
109 struct tpm_chip *chip;
110 const struct seq_operations *seqops;
111};
112
113struct tpm_chip {
114 struct device dev;
115 struct device devs;
116 struct cdev cdev;
117 struct cdev cdevs;
118
119 /* A driver callback under ops cannot be run unless ops_sem is held
120 * (sometimes implicitly, eg for the sysfs code). ops becomes null
121 * when the driver is unregistered, see tpm_try_get_ops.
122 */
123 struct rw_semaphore ops_sem;
124 const struct tpm_class_ops *ops;
125
126 struct tpm_bios_log log;
127 struct tpm_chip_seqops bin_log_seqops;
128 struct tpm_chip_seqops ascii_log_seqops;
129
130 unsigned int flags;
131
132 int dev_num; /* /dev/tpm# */
133 unsigned long is_open; /* only one allowed */
134
135 char hwrng_name[64];
136 struct hwrng hwrng;
137
138 struct mutex tpm_mutex; /* tpm is processing */
139
140 unsigned long timeout_a; /* jiffies */
141 unsigned long timeout_b; /* jiffies */
142 unsigned long timeout_c; /* jiffies */
143 unsigned long timeout_d; /* jiffies */
144 bool timeout_adjusted;
145 unsigned long duration[TPM_NUM_DURATIONS]; /* jiffies */
146 bool duration_adjusted;
147
148 struct dentry *bios_dir[TPM_NUM_EVENT_LOG_FILES];
149
150 const struct attribute_group *groups[3];
151 unsigned int groups_cnt;
152
153 u32 nr_allocated_banks;
154 struct tpm_bank_info *allocated_banks;
155#ifdef CONFIG_ACPI
156 acpi_handle acpi_dev_handle;
157 char ppi_version[TPM_PPI_VERSION_LEN + 1];
158#endif /* CONFIG_ACPI */
159
160 struct tpm_space work_space;
161 u32 last_cc;
162 u32 nr_commands;
163 u32 *cc_attrs_tbl;
164
165 /* active locality */
166 int locality;
167};
168
Randy Dunlapff76ec12009-10-28 12:26:39 -0700169#if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE)
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200170
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200171extern int tpm_is_tpm2(struct tpm_chip *chip);
Roberto Sassu879b5892019-02-06 17:24:49 +0100172extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
173 struct tpm_digest *digest);
Roberto Sassu0b6cf6b2019-02-06 17:24:52 +0100174extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
175 struct tpm_digest *digests);
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200176extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);
177extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
178extern int tpm_seal_trusted(struct tpm_chip *chip,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300179 struct trusted_key_payload *payload,
180 struct trusted_key_options *options);
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200181extern int tpm_unseal_trusted(struct tpm_chip *chip,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300182 struct trusted_key_payload *payload,
183 struct trusted_key_options *options);
Stefan Bergeraaae8152018-06-26 15:09:30 -0400184extern struct tpm_chip *tpm_default_chip(void);
Mimi Zohard6ba4522009-10-26 09:26:18 -0400185#else
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200186static inline int tpm_is_tpm2(struct tpm_chip *chip)
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300187{
188 return -ENODEV;
189}
Tomas Winkler95adc6b2018-10-19 21:23:07 +0300190
Roberto Sassu879b5892019-02-06 17:24:49 +0100191static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,
192 struct tpm_digest *digest)
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200193{
Mimi Zohard6ba4522009-10-26 09:26:18 -0400194 return -ENODEV;
195}
Tomas Winkler95adc6b2018-10-19 21:23:07 +0300196
197static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
Roberto Sassu0b6cf6b2019-02-06 17:24:52 +0100198 struct tpm_digest *digests)
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200199{
Mimi Zohard6ba4522009-10-26 09:26:18 -0400200 return -ENODEV;
201}
Tomas Winkler95adc6b2018-10-19 21:23:07 +0300202
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200203static inline int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
204{
Mimi Zoharc749ba92010-11-23 18:54:16 -0500205 return -ENODEV;
206}
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200207static inline int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max)
208{
Kent Yoder41ab9992012-06-07 13:47:14 -0500209 return -ENODEV;
210}
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300211
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200212static inline int tpm_seal_trusted(struct tpm_chip *chip,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300213 struct trusted_key_payload *payload,
214 struct trusted_key_options *options)
215{
216 return -ENODEV;
217}
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200218static inline int tpm_unseal_trusted(struct tpm_chip *chip,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300219 struct trusted_key_payload *payload,
220 struct trusted_key_options *options)
221{
222 return -ENODEV;
223}
Stefan Bergeraaae8152018-06-26 15:09:30 -0400224static inline struct tpm_chip *tpm_default_chip(void)
225{
226 return NULL;
227}
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200228#endif
229#endif