blob: 77fdc988c610e24364f5a408432244c2061780a1 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-only */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright (C) 2004,2007,2008 IBM Corporation
4 *
5 * Authors:
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Dave Safford <safford@watson.ibm.com>
8 * Reiner Sailer <sailer@watson.ibm.com>
9 * Kylene Hall <kjhall@us.ibm.com>
10 * Debora Velarde <dvelarde@us.ibm.com>
11 *
12 * Maintained by: <tpmdd_devel@lists.sourceforge.net>
13 *
14 * Device driver for TCG/TCPA TPM (trusted platform module).
15 * Specifications at www.trustedcomputinggroup.org
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016 */
17#ifndef __LINUX_TPM_H__
18#define __LINUX_TPM_H__
19
David Brazdil0f672f62019-12-10 10:32:29 +000020#include <linux/hw_random.h>
21#include <linux/acpi.h>
22#include <linux/cdev.h>
23#include <linux/fs.h>
24#include <crypto/hash_info.h>
25
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026#define TPM_DIGEST_SIZE 20 /* Max TPM v1.2 PCR size */
David Brazdil0f672f62019-12-10 10:32:29 +000027#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028
29struct tpm_chip;
30struct trusted_key_payload;
31struct trusted_key_options;
32
David Brazdil0f672f62019-12-10 10:32:29 +000033enum tpm_algorithms {
34 TPM_ALG_ERROR = 0x0000,
35 TPM_ALG_SHA1 = 0x0004,
36 TPM_ALG_KEYEDHASH = 0x0008,
37 TPM_ALG_SHA256 = 0x000B,
38 TPM_ALG_SHA384 = 0x000C,
39 TPM_ALG_SHA512 = 0x000D,
40 TPM_ALG_NULL = 0x0010,
41 TPM_ALG_SM3_256 = 0x0012,
42};
43
44struct tpm_digest {
45 u16 alg_id;
46 u8 digest[TPM_MAX_DIGEST_SIZE];
47} __packed;
48
49struct tpm_bank_info {
50 u16 alg_id;
51 u16 digest_size;
52 u16 crypto_id;
53};
54
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055enum TPM_OPS_FLAGS {
56 TPM_OPS_AUTO_STARTUP = BIT(0),
57};
58
59struct tpm_class_ops {
60 unsigned int flags;
61 const u8 req_complete_mask;
62 const u8 req_complete_val;
63 bool (*req_canceled)(struct tpm_chip *chip, u8 status);
64 int (*recv) (struct tpm_chip *chip, u8 *buf, size_t len);
65 int (*send) (struct tpm_chip *chip, u8 *buf, size_t len);
66 void (*cancel) (struct tpm_chip *chip);
67 u8 (*status) (struct tpm_chip *chip);
David Brazdil0f672f62019-12-10 10:32:29 +000068 void (*update_timeouts)(struct tpm_chip *chip,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000069 unsigned long *timeout_cap);
70 int (*go_idle)(struct tpm_chip *chip);
71 int (*cmd_ready)(struct tpm_chip *chip);
72 int (*request_locality)(struct tpm_chip *chip, int loc);
73 int (*relinquish_locality)(struct tpm_chip *chip, int loc);
74 void (*clk_enable)(struct tpm_chip *chip, bool value);
75};
76
David Brazdil0f672f62019-12-10 10:32:29 +000077#define TPM_NUM_EVENT_LOG_FILES 3
78
79/* Indexes the duration array */
80enum tpm_duration {
81 TPM_SHORT = 0,
82 TPM_MEDIUM = 1,
83 TPM_LONG = 2,
84 TPM_LONG_LONG = 3,
85 TPM_UNDEFINED,
86 TPM_NUM_DURATIONS = TPM_UNDEFINED,
87};
88
89#define TPM_PPI_VERSION_LEN 3
90
91struct tpm_space {
92 u32 context_tbl[3];
93 u8 *context_buf;
94 u32 session_tbl[3];
95 u8 *session_buf;
Olivier Deprez0e641232021-09-23 10:07:05 +020096 u32 buf_size;
David Brazdil0f672f62019-12-10 10:32:29 +000097};
98
99struct tpm_bios_log {
100 void *bios_event_log;
101 void *bios_event_log_end;
102};
103
104struct tpm_chip_seqops {
105 struct tpm_chip *chip;
106 const struct seq_operations *seqops;
107};
108
109struct tpm_chip {
110 struct device dev;
111 struct device devs;
112 struct cdev cdev;
113 struct cdev cdevs;
114
115 /* A driver callback under ops cannot be run unless ops_sem is held
116 * (sometimes implicitly, eg for the sysfs code). ops becomes null
117 * when the driver is unregistered, see tpm_try_get_ops.
118 */
119 struct rw_semaphore ops_sem;
120 const struct tpm_class_ops *ops;
121
122 struct tpm_bios_log log;
123 struct tpm_chip_seqops bin_log_seqops;
124 struct tpm_chip_seqops ascii_log_seqops;
125
126 unsigned int flags;
127
128 int dev_num; /* /dev/tpm# */
129 unsigned long is_open; /* only one allowed */
130
131 char hwrng_name[64];
132 struct hwrng hwrng;
133
134 struct mutex tpm_mutex; /* tpm is processing */
135
136 unsigned long timeout_a; /* jiffies */
137 unsigned long timeout_b; /* jiffies */
138 unsigned long timeout_c; /* jiffies */
139 unsigned long timeout_d; /* jiffies */
140 bool timeout_adjusted;
141 unsigned long duration[TPM_NUM_DURATIONS]; /* jiffies */
142 bool duration_adjusted;
143
144 struct dentry *bios_dir[TPM_NUM_EVENT_LOG_FILES];
145
146 const struct attribute_group *groups[3];
147 unsigned int groups_cnt;
148
149 u32 nr_allocated_banks;
150 struct tpm_bank_info *allocated_banks;
151#ifdef CONFIG_ACPI
152 acpi_handle acpi_dev_handle;
153 char ppi_version[TPM_PPI_VERSION_LEN + 1];
154#endif /* CONFIG_ACPI */
155
156 struct tpm_space work_space;
157 u32 last_cc;
158 u32 nr_commands;
159 u32 *cc_attrs_tbl;
160
161 /* active locality */
162 int locality;
163};
164
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000165#if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE)
166
167extern int tpm_is_tpm2(struct tpm_chip *chip);
David Brazdil0f672f62019-12-10 10:32:29 +0000168extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
169 struct tpm_digest *digest);
170extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
171 struct tpm_digest *digests);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000172extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);
173extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
174extern int tpm_seal_trusted(struct tpm_chip *chip,
175 struct trusted_key_payload *payload,
176 struct trusted_key_options *options);
177extern int tpm_unseal_trusted(struct tpm_chip *chip,
178 struct trusted_key_payload *payload,
179 struct trusted_key_options *options);
180extern struct tpm_chip *tpm_default_chip(void);
181#else
182static inline int tpm_is_tpm2(struct tpm_chip *chip)
183{
184 return -ENODEV;
185}
David Brazdil0f672f62019-12-10 10:32:29 +0000186
187static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,
188 struct tpm_digest *digest)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000189{
190 return -ENODEV;
191}
David Brazdil0f672f62019-12-10 10:32:29 +0000192
193static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
194 struct tpm_digest *digests)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000195{
196 return -ENODEV;
197}
David Brazdil0f672f62019-12-10 10:32:29 +0000198
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000199static inline int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
200{
201 return -ENODEV;
202}
203static inline int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max)
204{
205 return -ENODEV;
206}
207
208static inline int tpm_seal_trusted(struct tpm_chip *chip,
209 struct trusted_key_payload *payload,
210 struct trusted_key_options *options)
211{
212 return -ENODEV;
213}
214static inline int tpm_unseal_trusted(struct tpm_chip *chip,
215 struct trusted_key_payload *payload,
216 struct trusted_key_options *options)
217{
218 return -ENODEV;
219}
220static inline struct tpm_chip *tpm_default_chip(void)
221{
222 return NULL;
223}
224#endif
225#endif