summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2019-01-02 14:05:43 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2019-01-02 14:05:43 -0500
commit74673fc50babc9be22b32c4ce697fceb51c7671a (patch)
tree3ba181d4f4a0346eb500531f38c40538fcb72458
parent19f2e267a5d0d26282a64f8f788c482852c95324 (diff)
parent0db51ef26e87845d1748c363702585e2f0a06266 (diff)
Merge branch 'next-tpm' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security
Pull TPM updates from James Morris: - Support for partial reads of /dev/tpm0. - Clean up for TPM 1.x code: move the commands to tpm1-cmd.c and make everything to use the same data structure for building TPM commands i.e. struct tpm_buf. * 'next-tpm' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: (25 commits) tpm: add support for partial reads tpm: tpm_ibmvtpm: fix kdoc warnings tpm: fix kdoc for tpm2_flush_context_cmd() tpm: tpm_try_transmit() refactor error flow. tpm: use u32 instead of int for PCR index tpm1: reimplement tpm1_continue_selftest() using tpm_buf tpm1: reimplement SAVESTATE using tpm_buf tpm1: rename tpm1_pcr_read_dev to tpm1_pcr_read() tpm1: implement tpm1_pcr_read_dev() using tpm_buf structure tpm: tpm1: rewrite tpm1_get_random() using tpm_buf structure tpm: tpm-space.c remove unneeded semicolon tpm: tpm-interface.c drop unused macros tpm: add tpm_auto_startup() into tpm-interface.c tpm: factor out tpm_startup function tpm: factor out tpm 1.x pm suspend flow into tpm1-cmd.c tpm: move tpm 1.x selftest code from tpm-interface.c tpm1-cmd.c tpm: factor out tpm1_get_random into tpm1-cmd.c tpm: move tpm_getcap to tpm1-cmd.c tpm: move tpm1_pcr_extend to tpm1-cmd.c tpm: factor out tpm_get_timeouts() ...
-rw-r--r--drivers/char/tpm/Makefile16
-rw-r--r--drivers/char/tpm/st33zp24/st33zp24.c2
-rw-r--r--drivers/char/tpm/tpm-chip.c11
-rw-r--r--drivers/char/tpm/tpm-dev-common.c52
-rw-r--r--drivers/char/tpm/tpm-dev.h5
-rw-r--r--drivers/char/tpm/tpm-interface.c832
-rw-r--r--drivers/char/tpm/tpm-sysfs.c52
-rw-r--r--drivers/char/tpm/tpm.h97
-rw-r--r--drivers/char/tpm/tpm1-cmd.c781
-rw-r--r--drivers/char/tpm/tpm2-cmd.c308
-rw-r--r--drivers/char/tpm/tpm2-space.c2
-rw-r--r--drivers/char/tpm/tpm_i2c_nuvoton.c11
-rw-r--r--drivers/char/tpm/tpm_ibmvtpm.c6
-rw-r--r--drivers/char/tpm/tpm_tis_core.c10
-rw-r--r--include/linux/tpm.h11
-rw-r--r--security/integrity/ima/ima_crypto.c5
16 files changed, 1133 insertions, 1068 deletions
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index 4e9c33ca1f8f..a01c4cab902a 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -3,9 +3,19 @@
3# Makefile for the kernel tpm device drivers. 3# Makefile for the kernel tpm device drivers.
4# 4#
5obj-$(CONFIG_TCG_TPM) += tpm.o 5obj-$(CONFIG_TCG_TPM) += tpm.o
6tpm-y := tpm-interface.o tpm-dev.o tpm-sysfs.o tpm-chip.o tpm2-cmd.o \ 6tpm-y := tpm-chip.o
7 tpm-dev-common.o tpmrm-dev.o eventlog/common.o eventlog/tpm1.o \ 7tpm-y += tpm-dev-common.o
8 eventlog/tpm2.o tpm2-space.o 8tpm-y += tpm-dev.o
9tpm-y += tpm-interface.o
10tpm-y += tpm1-cmd.o
11tpm-y += tpm2-cmd.o
12tpm-y += tpmrm-dev.o
13tpm-y += tpm2-space.o
14tpm-y += tpm-sysfs.o
15tpm-y += eventlog/common.o
16tpm-y += eventlog/tpm1.o
17tpm-y += eventlog/tpm2.o
18
9tpm-$(CONFIG_ACPI) += tpm_ppi.o eventlog/acpi.o 19tpm-$(CONFIG_ACPI) += tpm_ppi.o eventlog/acpi.o
10tpm-$(CONFIG_EFI) += eventlog/efi.o 20tpm-$(CONFIG_EFI) += eventlog/efi.o
11tpm-$(CONFIG_OF) += eventlog/of.o 21tpm-$(CONFIG_OF) += eventlog/of.o
diff --git a/drivers/char/tpm/st33zp24/st33zp24.c b/drivers/char/tpm/st33zp24/st33zp24.c
index abd675bec88c..64dc560859f2 100644
--- a/drivers/char/tpm/st33zp24/st33zp24.c
+++ b/drivers/char/tpm/st33zp24/st33zp24.c
@@ -649,7 +649,7 @@ int st33zp24_pm_resume(struct device *dev)
649 } else { 649 } else {
650 ret = tpm_pm_resume(dev); 650 ret = tpm_pm_resume(dev);
651 if (!ret) 651 if (!ret)
652 tpm_do_selftest(chip); 652 tpm1_do_selftest(chip);
653 } 653 }
654 return ret; 654 return ret;
655} /* st33zp24_pm_resume() */ 655} /* st33zp24_pm_resume() */
diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index 46caadca916a..32db84683c40 100644
--- a/drivers/char/tpm/tpm-chip.c
+++ b/drivers/char/tpm/tpm-chip.c
@@ -451,14 +451,9 @@ int tpm_chip_register(struct tpm_chip *chip)
451{ 451{
452 int rc; 452 int rc;
453 453
454 if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) { 454 rc = tpm_auto_startup(chip);
455 if (chip->flags & TPM_CHIP_FLAG_TPM2) 455 if (rc)
456 rc = tpm2_auto_startup(chip); 456 return rc;
457 else
458 rc = tpm1_auto_startup(chip);
459 if (rc)
460 return rc;
461 }
462 457
463 tpm_sysfs_add_device(chip); 458 tpm_sysfs_add_device(chip);
464 459
diff --git a/drivers/char/tpm/tpm-dev-common.c b/drivers/char/tpm/tpm-dev-common.c
index 99b5133a9d05..5eecad233ea1 100644
--- a/drivers/char/tpm/tpm-dev-common.c
+++ b/drivers/char/tpm/tpm-dev-common.c
@@ -40,7 +40,7 @@ static void tpm_async_work(struct work_struct *work)
40 40
41 tpm_put_ops(priv->chip); 41 tpm_put_ops(priv->chip);
42 if (ret > 0) { 42 if (ret > 0) {
43 priv->data_pending = ret; 43 priv->response_length = ret;
44 mod_timer(&priv->user_read_timer, jiffies + (120 * HZ)); 44 mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
45 } 45 }
46 mutex_unlock(&priv->buffer_mutex); 46 mutex_unlock(&priv->buffer_mutex);
@@ -63,7 +63,8 @@ static void tpm_timeout_work(struct work_struct *work)
63 timeout_work); 63 timeout_work);
64 64
65 mutex_lock(&priv->buffer_mutex); 65 mutex_lock(&priv->buffer_mutex);
66 priv->data_pending = 0; 66 priv->response_read = true;
67 priv->response_length = 0;
67 memset(priv->data_buffer, 0, sizeof(priv->data_buffer)); 68 memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
68 mutex_unlock(&priv->buffer_mutex); 69 mutex_unlock(&priv->buffer_mutex);
69 wake_up_interruptible(&priv->async_wait); 70 wake_up_interruptible(&priv->async_wait);
@@ -74,6 +75,7 @@ void tpm_common_open(struct file *file, struct tpm_chip *chip,
74{ 75{
75 priv->chip = chip; 76 priv->chip = chip;
76 priv->space = space; 77 priv->space = space;
78 priv->response_read = true;
77 79
78 mutex_init(&priv->buffer_mutex); 80 mutex_init(&priv->buffer_mutex);
79 timer_setup(&priv->user_read_timer, user_reader_timeout, 0); 81 timer_setup(&priv->user_read_timer, user_reader_timeout, 0);
@@ -90,22 +92,35 @@ ssize_t tpm_common_read(struct file *file, char __user *buf,
90 ssize_t ret_size = 0; 92 ssize_t ret_size = 0;
91 int rc; 93 int rc;
92 94
93 del_singleshot_timer_sync(&priv->user_read_timer);
94 flush_work(&priv->timeout_work);
95 mutex_lock(&priv->buffer_mutex); 95 mutex_lock(&priv->buffer_mutex);
96 96
97 if (priv->data_pending) { 97 if (priv->response_length) {
98 ret_size = min_t(ssize_t, size, priv->data_pending); 98 priv->response_read = true;
99 if (ret_size > 0) { 99
100 rc = copy_to_user(buf, priv->data_buffer, ret_size); 100 ret_size = min_t(ssize_t, size, priv->response_length);
101 memset(priv->data_buffer, 0, priv->data_pending); 101 if (!ret_size) {
102 if (rc) 102 priv->response_length = 0;
103 ret_size = -EFAULT; 103 goto out;
104 } 104 }
105 105
106 priv->data_pending = 0; 106 rc = copy_to_user(buf, priv->data_buffer + *off, ret_size);
107 if (rc) {
108 memset(priv->data_buffer, 0, TPM_BUFSIZE);
109 priv->response_length = 0;
110 ret_size = -EFAULT;
111 } else {
112 memset(priv->data_buffer + *off, 0, ret_size);
113 priv->response_length -= ret_size;
114 *off += ret_size;
115 }
107 } 116 }
108 117
118out:
119 if (!priv->response_length) {
120 *off = 0;
121 del_singleshot_timer_sync(&priv->user_read_timer);
122 flush_work(&priv->timeout_work);
123 }
109 mutex_unlock(&priv->buffer_mutex); 124 mutex_unlock(&priv->buffer_mutex);
110 return ret_size; 125 return ret_size;
111} 126}
@@ -125,7 +140,8 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf,
125 * tpm_read or a user_read_timer timeout. This also prevents split 140 * tpm_read or a user_read_timer timeout. This also prevents split
126 * buffered writes from blocking here. 141 * buffered writes from blocking here.
127 */ 142 */
128 if (priv->data_pending != 0 || priv->command_enqueued) { 143 if ((!priv->response_read && priv->response_length) ||
144 priv->command_enqueued) {
129 ret = -EBUSY; 145 ret = -EBUSY;
130 goto out; 146 goto out;
131 } 147 }
@@ -150,6 +166,10 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf,
150 goto out; 166 goto out;
151 } 167 }
152 168
169 priv->response_length = 0;
170 priv->response_read = false;
171 *off = 0;
172
153 /* 173 /*
154 * If in nonblocking mode schedule an async job to send 174 * If in nonblocking mode schedule an async job to send
155 * the command return the size. 175 * the command return the size.
@@ -168,7 +188,7 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf,
168 tpm_put_ops(priv->chip); 188 tpm_put_ops(priv->chip);
169 189
170 if (ret > 0) { 190 if (ret > 0) {
171 priv->data_pending = ret; 191 priv->response_length = ret;
172 mod_timer(&priv->user_read_timer, jiffies + (120 * HZ)); 192 mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
173 ret = size; 193 ret = size;
174 } 194 }
@@ -184,7 +204,7 @@ __poll_t tpm_common_poll(struct file *file, poll_table *wait)
184 204
185 poll_wait(file, &priv->async_wait, wait); 205 poll_wait(file, &priv->async_wait, wait);
186 206
187 if (priv->data_pending) 207 if (!priv->response_read || priv->response_length)
188 mask = EPOLLIN | EPOLLRDNORM; 208 mask = EPOLLIN | EPOLLRDNORM;
189 else 209 else
190 mask = EPOLLOUT | EPOLLWRNORM; 210 mask = EPOLLOUT | EPOLLWRNORM;
@@ -201,7 +221,7 @@ void tpm_common_release(struct file *file, struct file_priv *priv)
201 del_singleshot_timer_sync(&priv->user_read_timer); 221 del_singleshot_timer_sync(&priv->user_read_timer);
202 flush_work(&priv->timeout_work); 222 flush_work(&priv->timeout_work);
203 file->private_data = NULL; 223 file->private_data = NULL;
204 priv->data_pending = 0; 224 priv->response_length = 0;
205} 225}
206 226
207int __init tpm_dev_common_init(void) 227int __init tpm_dev_common_init(void)
diff --git a/drivers/char/tpm/tpm-dev.h b/drivers/char/tpm/tpm-dev.h
index a126b575cb8c..1089fc0bb290 100644
--- a/drivers/char/tpm/tpm-dev.h
+++ b/drivers/char/tpm/tpm-dev.h
@@ -9,14 +9,13 @@ struct file_priv {
9 struct tpm_chip *chip; 9 struct tpm_chip *chip;
10 struct tpm_space *space; 10 struct tpm_space *space;
11 11
12 /* Holds the amount of data passed or an error code from async op */
13 ssize_t data_pending;
14 struct mutex buffer_mutex; 12 struct mutex buffer_mutex;
15
16 struct timer_list user_read_timer; /* user needs to claim result */ 13 struct timer_list user_read_timer; /* user needs to claim result */
17 struct work_struct timeout_work; 14 struct work_struct timeout_work;
18 struct work_struct async_work; 15 struct work_struct async_work;
19 wait_queue_head_t async_wait; 16 wait_queue_head_t async_wait;
17 size_t response_length;
18 bool response_read;
20 bool command_enqueued; 19 bool command_enqueued;
21 20
22 u8 data_buffer[TPM_BUFSIZE]; 21 u8 data_buffer[TPM_BUFSIZE];
diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index 129f640424b7..d9439f9abe78 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -33,297 +33,32 @@
33 33
34#include "tpm.h" 34#include "tpm.h"
35 35
36#define TPM_MAX_ORDINAL 243
37#define TSC_MAX_ORDINAL 12
38#define TPM_PROTECTED_COMMAND 0x00
39#define TPM_CONNECTION_COMMAND 0x40
40
41/* 36/*
42 * Bug workaround - some TPM's don't flush the most 37 * Bug workaround - some TPM's don't flush the most
43 * recently changed pcr on suspend, so force the flush 38 * recently changed pcr on suspend, so force the flush
44 * with an extend to the selected _unused_ non-volatile pcr. 39 * with an extend to the selected _unused_ non-volatile pcr.
45 */ 40 */
46static int tpm_suspend_pcr; 41static u32 tpm_suspend_pcr;
47module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644); 42module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
48MODULE_PARM_DESC(suspend_pcr, 43MODULE_PARM_DESC(suspend_pcr,
49 "PCR to use for dummy writes to facilitate flush on suspend."); 44 "PCR to use for dummy writes to facilitate flush on suspend.");
50 45
51/* 46/**
52 * Array with one entry per ordinal defining the maximum amount 47 * tpm_calc_ordinal_duration() - calculate the maximum command duration
53 * of time the chip could take to return the result. The ordinal 48 * @chip: TPM chip to use.
54 * designation of short, medium or long is defined in a table in 49 * @ordinal: TPM command ordinal.
55 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The 50 *
56 * values of the SHORT, MEDIUM, and LONG durations are retrieved 51 * The function returns the maximum amount of time the chip could take
57 * from the chip during initialization with a call to tpm_get_timeouts. 52 * to return the result for a particular ordinal in jiffies.
58 */ 53 *
59static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = { 54 * Return: A maximal duration time for an ordinal in jiffies.
60 TPM_UNDEFINED, /* 0 */
61 TPM_UNDEFINED,
62 TPM_UNDEFINED,
63 TPM_UNDEFINED,
64 TPM_UNDEFINED,
65 TPM_UNDEFINED, /* 5 */
66 TPM_UNDEFINED,
67 TPM_UNDEFINED,
68 TPM_UNDEFINED,
69 TPM_UNDEFINED,
70 TPM_SHORT, /* 10 */
71 TPM_SHORT,
72 TPM_MEDIUM,
73 TPM_LONG,
74 TPM_LONG,
75 TPM_MEDIUM, /* 15 */
76 TPM_SHORT,
77 TPM_SHORT,
78 TPM_MEDIUM,
79 TPM_LONG,
80 TPM_SHORT, /* 20 */
81 TPM_SHORT,
82 TPM_MEDIUM,
83 TPM_MEDIUM,
84 TPM_MEDIUM,
85 TPM_SHORT, /* 25 */
86 TPM_SHORT,
87 TPM_MEDIUM,
88 TPM_SHORT,
89 TPM_SHORT,
90 TPM_MEDIUM, /* 30 */
91 TPM_LONG,
92 TPM_MEDIUM,
93 TPM_SHORT,
94 TPM_SHORT,
95 TPM_SHORT, /* 35 */
96 TPM_MEDIUM,
97 TPM_MEDIUM,
98 TPM_UNDEFINED,
99 TPM_UNDEFINED,
100 TPM_MEDIUM, /* 40 */
101 TPM_LONG,
102 TPM_MEDIUM,
103 TPM_SHORT,
104 TPM_SHORT,
105 TPM_SHORT, /* 45 */
106 TPM_SHORT,
107 TPM_SHORT,
108 TPM_SHORT,
109 TPM_LONG,
110 TPM_MEDIUM, /* 50 */
111 TPM_MEDIUM,
112 TPM_UNDEFINED,
113 TPM_UNDEFINED,
114 TPM_UNDEFINED,
115 TPM_UNDEFINED, /* 55 */
116 TPM_UNDEFINED,
117 TPM_UNDEFINED,
118 TPM_UNDEFINED,
119 TPM_UNDEFINED,
120 TPM_MEDIUM, /* 60 */
121 TPM_MEDIUM,
122 TPM_MEDIUM,
123 TPM_SHORT,
124 TPM_SHORT,
125 TPM_MEDIUM, /* 65 */
126 TPM_UNDEFINED,
127 TPM_UNDEFINED,
128 TPM_UNDEFINED,
129 TPM_UNDEFINED,
130 TPM_SHORT, /* 70 */
131 TPM_SHORT,
132 TPM_UNDEFINED,
133 TPM_UNDEFINED,
134 TPM_UNDEFINED,
135 TPM_UNDEFINED, /* 75 */
136 TPM_UNDEFINED,
137 TPM_UNDEFINED,
138 TPM_UNDEFINED,
139 TPM_UNDEFINED,
140 TPM_LONG, /* 80 */
141 TPM_UNDEFINED,
142 TPM_MEDIUM,
143 TPM_LONG,
144 TPM_SHORT,
145 TPM_UNDEFINED, /* 85 */
146 TPM_UNDEFINED,
147 TPM_UNDEFINED,
148 TPM_UNDEFINED,
149 TPM_UNDEFINED,
150 TPM_SHORT, /* 90 */
151 TPM_SHORT,
152 TPM_SHORT,
153 TPM_SHORT,
154 TPM_SHORT,
155 TPM_UNDEFINED, /* 95 */
156 TPM_UNDEFINED,
157 TPM_UNDEFINED,
158 TPM_UNDEFINED,
159 TPM_UNDEFINED,
160 TPM_MEDIUM, /* 100 */
161 TPM_SHORT,
162 TPM_SHORT,
163 TPM_UNDEFINED,
164 TPM_UNDEFINED,
165 TPM_UNDEFINED, /* 105 */
166 TPM_UNDEFINED,
167 TPM_UNDEFINED,
168 TPM_UNDEFINED,
169 TPM_UNDEFINED,
170 TPM_SHORT, /* 110 */
171 TPM_SHORT,
172 TPM_SHORT,
173 TPM_SHORT,
174 TPM_SHORT,
175 TPM_SHORT, /* 115 */
176 TPM_SHORT,
177 TPM_SHORT,
178 TPM_UNDEFINED,
179 TPM_UNDEFINED,
180 TPM_LONG, /* 120 */
181 TPM_LONG,
182 TPM_MEDIUM,
183 TPM_UNDEFINED,
184 TPM_SHORT,
185 TPM_SHORT, /* 125 */
186 TPM_SHORT,
187 TPM_LONG,
188 TPM_SHORT,
189 TPM_SHORT,
190 TPM_SHORT, /* 130 */
191 TPM_MEDIUM,
192 TPM_UNDEFINED,
193 TPM_SHORT,
194 TPM_MEDIUM,
195 TPM_UNDEFINED, /* 135 */
196 TPM_UNDEFINED,
197 TPM_UNDEFINED,
198 TPM_UNDEFINED,
199 TPM_UNDEFINED,
200 TPM_SHORT, /* 140 */
201 TPM_SHORT,
202 TPM_UNDEFINED,
203 TPM_UNDEFINED,
204 TPM_UNDEFINED,
205 TPM_UNDEFINED, /* 145 */
206 TPM_UNDEFINED,
207 TPM_UNDEFINED,
208 TPM_UNDEFINED,
209 TPM_UNDEFINED,
210 TPM_SHORT, /* 150 */
211 TPM_MEDIUM,
212 TPM_MEDIUM,
213 TPM_SHORT,
214 TPM_SHORT,
215 TPM_UNDEFINED, /* 155 */
216 TPM_UNDEFINED,
217 TPM_UNDEFINED,
218 TPM_UNDEFINED,
219 TPM_UNDEFINED,
220 TPM_SHORT, /* 160 */
221 TPM_SHORT,
222 TPM_SHORT,
223 TPM_SHORT,
224 TPM_UNDEFINED,
225 TPM_UNDEFINED, /* 165 */
226 TPM_UNDEFINED,
227 TPM_UNDEFINED,
228 TPM_UNDEFINED,
229 TPM_UNDEFINED,
230 TPM_LONG, /* 170 */
231 TPM_UNDEFINED,
232 TPM_UNDEFINED,
233 TPM_UNDEFINED,
234 TPM_UNDEFINED,
235 TPM_UNDEFINED, /* 175 */
236 TPM_UNDEFINED,
237 TPM_UNDEFINED,
238 TPM_UNDEFINED,
239 TPM_UNDEFINED,
240 TPM_MEDIUM, /* 180 */
241 TPM_SHORT,
242 TPM_MEDIUM,
243 TPM_MEDIUM,
244 TPM_MEDIUM,
245 TPM_MEDIUM, /* 185 */
246 TPM_SHORT,
247 TPM_UNDEFINED,
248 TPM_UNDEFINED,
249 TPM_UNDEFINED,
250 TPM_UNDEFINED, /* 190 */
251 TPM_UNDEFINED,
252 TPM_UNDEFINED,
253 TPM_UNDEFINED,
254 TPM_UNDEFINED,
255 TPM_UNDEFINED, /* 195 */
256 TPM_UNDEFINED,
257 TPM_UNDEFINED,
258 TPM_UNDEFINED,
259 TPM_UNDEFINED,
260 TPM_SHORT, /* 200 */
261 TPM_UNDEFINED,
262 TPM_UNDEFINED,
263 TPM_UNDEFINED,
264 TPM_SHORT,
265 TPM_SHORT, /* 205 */
266 TPM_SHORT,
267 TPM_SHORT,
268 TPM_SHORT,
269 TPM_SHORT,
270 TPM_MEDIUM, /* 210 */
271 TPM_UNDEFINED,
272 TPM_MEDIUM,
273 TPM_MEDIUM,
274 TPM_MEDIUM,
275 TPM_UNDEFINED, /* 215 */
276 TPM_MEDIUM,
277 TPM_UNDEFINED,
278 TPM_UNDEFINED,
279 TPM_SHORT,
280 TPM_SHORT, /* 220 */
281 TPM_SHORT,
282 TPM_SHORT,
283 TPM_SHORT,
284 TPM_SHORT,
285 TPM_UNDEFINED, /* 225 */
286 TPM_UNDEFINED,
287 TPM_UNDEFINED,
288 TPM_UNDEFINED,
289 TPM_UNDEFINED,
290 TPM_SHORT, /* 230 */
291 TPM_LONG,
292 TPM_MEDIUM,
293 TPM_UNDEFINED,
294 TPM_UNDEFINED,
295 TPM_UNDEFINED, /* 235 */
296 TPM_UNDEFINED,
297 TPM_UNDEFINED,
298 TPM_UNDEFINED,
299 TPM_UNDEFINED,
300 TPM_SHORT, /* 240 */
301 TPM_UNDEFINED,
302 TPM_MEDIUM,
303};
304
305/*
306 * Returns max number of jiffies to wait
307 */ 55 */
308unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, 56unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
309 u32 ordinal)
310{ 57{
311 int duration_idx = TPM_UNDEFINED; 58 if (chip->flags & TPM_CHIP_FLAG_TPM2)
312 int duration = 0; 59 return tpm2_calc_ordinal_duration(chip, ordinal);
313
314 /*
315 * We only have a duration table for protected commands, where the upper
316 * 16 bits are 0. For the few other ordinals the fallback will be used.
317 */
318 if (ordinal < TPM_MAX_ORDINAL)
319 duration_idx = tpm_ordinal_duration[ordinal];
320
321 if (duration_idx != TPM_UNDEFINED)
322 duration = chip->duration[duration_idx];
323 if (duration <= 0)
324 return 2 * 60 * HZ;
325 else 60 else
326 return duration; 61 return tpm1_calc_ordinal_duration(chip, ordinal);
327} 62}
328EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration); 63EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
329 64
@@ -477,13 +212,15 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip,
477 212
478 if (need_locality) { 213 if (need_locality) {
479 rc = tpm_request_locality(chip, flags); 214 rc = tpm_request_locality(chip, flags);
480 if (rc < 0) 215 if (rc < 0) {
481 goto out_no_locality; 216 need_locality = false;
217 goto out_locality;
218 }
482 } 219 }
483 220
484 rc = tpm_cmd_ready(chip, flags); 221 rc = tpm_cmd_ready(chip, flags);
485 if (rc) 222 if (rc)
486 goto out; 223 goto out_locality;
487 224
488 rc = tpm2_prepare_space(chip, space, ordinal, buf); 225 rc = tpm2_prepare_space(chip, space, ordinal, buf);
489 if (rc) 226 if (rc)
@@ -500,10 +237,7 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip,
500 if (chip->flags & TPM_CHIP_FLAG_IRQ) 237 if (chip->flags & TPM_CHIP_FLAG_IRQ)
501 goto out_recv; 238 goto out_recv;
502 239
503 if (chip->flags & TPM_CHIP_FLAG_TPM2) 240 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
504 stop = jiffies + tpm2_calc_ordinal_duration(chip, ordinal);
505 else
506 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
507 do { 241 do {
508 u8 status = chip->ops->status(chip); 242 u8 status = chip->ops->status(chip);
509 if ((status & chip->ops->req_complete_mask) == 243 if ((status & chip->ops->req_complete_mask) ==
@@ -547,14 +281,13 @@ out_recv:
547 dev_err(&chip->dev, "tpm2_commit_space: error %d\n", rc); 281 dev_err(&chip->dev, "tpm2_commit_space: error %d\n", rc);
548 282
549out: 283out:
550 rc = tpm_go_idle(chip, flags); 284 /* may fail but do not override previous error value in rc */
551 if (rc) 285 tpm_go_idle(chip, flags);
552 goto out;
553 286
287out_locality:
554 if (need_locality) 288 if (need_locality)
555 tpm_relinquish_locality(chip, flags); 289 tpm_relinquish_locality(chip, flags);
556 290
557out_no_locality:
558 if (chip->ops->clk_enable != NULL) 291 if (chip->ops->clk_enable != NULL)
559 chip->ops->clk_enable(chip, false); 292 chip->ops->clk_enable(chip, false);
560 293
@@ -677,277 +410,18 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
677} 410}
678EXPORT_SYMBOL_GPL(tpm_transmit_cmd); 411EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
679 412
680#define TPM_ORD_STARTUP 153
681#define TPM_ST_CLEAR 1
682
683/**
684 * tpm_startup - turn on the TPM
685 * @chip: TPM chip to use
686 *
687 * Normally the firmware should start the TPM. This function is provided as a
688 * workaround if this does not happen. A legal case for this could be for
689 * example when a TPM emulator is used.
690 *
691 * Return: same as tpm_transmit_cmd()
692 */
693int tpm_startup(struct tpm_chip *chip)
694{
695 struct tpm_buf buf;
696 int rc;
697
698 dev_info(&chip->dev, "starting up the TPM manually\n");
699
700 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
701 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP);
702 if (rc < 0)
703 return rc;
704
705 tpm_buf_append_u16(&buf, TPM2_SU_CLEAR);
706 } else {
707 rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_STARTUP);
708 if (rc < 0)
709 return rc;
710
711 tpm_buf_append_u16(&buf, TPM_ST_CLEAR);
712 }
713
714 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
715 "attempting to start the TPM");
716
717 tpm_buf_destroy(&buf);
718 return rc;
719}
720
721#define TPM_DIGEST_SIZE 20
722#define TPM_RET_CODE_IDX 6
723#define TPM_INTERNAL_RESULT_SIZE 200
724#define TPM_ORD_GET_CAP 101
725#define TPM_ORD_GET_RANDOM 70
726
727static const struct tpm_input_header tpm_getcap_header = {
728 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
729 .length = cpu_to_be32(22),
730 .ordinal = cpu_to_be32(TPM_ORD_GET_CAP)
731};
732
733ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
734 const char *desc, size_t min_cap_length)
735{
736 struct tpm_buf buf;
737 int rc;
738
739 rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP);
740 if (rc)
741 return rc;
742
743 if (subcap_id == TPM_CAP_VERSION_1_1 ||
744 subcap_id == TPM_CAP_VERSION_1_2) {
745 tpm_buf_append_u32(&buf, subcap_id);
746 tpm_buf_append_u32(&buf, 0);
747 } else {
748 if (subcap_id == TPM_CAP_FLAG_PERM ||
749 subcap_id == TPM_CAP_FLAG_VOL)
750 tpm_buf_append_u32(&buf, TPM_CAP_FLAG);
751 else
752 tpm_buf_append_u32(&buf, TPM_CAP_PROP);
753
754 tpm_buf_append_u32(&buf, 4);
755 tpm_buf_append_u32(&buf, subcap_id);
756 }
757 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
758 min_cap_length, 0, desc);
759 if (!rc)
760 *cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
761
762 tpm_buf_destroy(&buf);
763 return rc;
764}
765EXPORT_SYMBOL_GPL(tpm_getcap);
766
767int tpm_get_timeouts(struct tpm_chip *chip) 413int tpm_get_timeouts(struct tpm_chip *chip)
768{ 414{
769 cap_t cap;
770 unsigned long timeout_old[4], timeout_chip[4], timeout_eff[4];
771 ssize_t rc;
772
773 if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS) 415 if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
774 return 0; 416 return 0;
775 417
776 if (chip->flags & TPM_CHIP_FLAG_TPM2) { 418 if (chip->flags & TPM_CHIP_FLAG_TPM2)
777 /* Fixed timeouts for TPM2 */ 419 return tpm2_get_timeouts(chip);
778 chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A); 420 else
779 chip->timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B); 421 return tpm1_get_timeouts(chip);
780 chip->timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
781 chip->timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
782 chip->duration[TPM_SHORT] =
783 msecs_to_jiffies(TPM2_DURATION_SHORT);
784 chip->duration[TPM_MEDIUM] =
785 msecs_to_jiffies(TPM2_DURATION_MEDIUM);
786 chip->duration[TPM_LONG] =
787 msecs_to_jiffies(TPM2_DURATION_LONG);
788 chip->duration[TPM_LONG_LONG] =
789 msecs_to_jiffies(TPM2_DURATION_LONG_LONG);
790
791 chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
792 return 0;
793 }
794
795 rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, NULL,
796 sizeof(cap.timeout));
797 if (rc == TPM_ERR_INVALID_POSTINIT) {
798 if (tpm_startup(chip))
799 return rc;
800
801 rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap,
802 "attempting to determine the timeouts",
803 sizeof(cap.timeout));
804 }
805
806 if (rc) {
807 dev_err(&chip->dev,
808 "A TPM error (%zd) occurred attempting to determine the timeouts\n",
809 rc);
810 return rc;
811 }
812
813 timeout_old[0] = jiffies_to_usecs(chip->timeout_a);
814 timeout_old[1] = jiffies_to_usecs(chip->timeout_b);
815 timeout_old[2] = jiffies_to_usecs(chip->timeout_c);
816 timeout_old[3] = jiffies_to_usecs(chip->timeout_d);
817 timeout_chip[0] = be32_to_cpu(cap.timeout.a);
818 timeout_chip[1] = be32_to_cpu(cap.timeout.b);
819 timeout_chip[2] = be32_to_cpu(cap.timeout.c);
820 timeout_chip[3] = be32_to_cpu(cap.timeout.d);
821 memcpy(timeout_eff, timeout_chip, sizeof(timeout_eff));
822
823 /*
824 * Provide ability for vendor overrides of timeout values in case
825 * of misreporting.
826 */
827 if (chip->ops->update_timeouts != NULL)
828 chip->timeout_adjusted =
829 chip->ops->update_timeouts(chip, timeout_eff);
830
831 if (!chip->timeout_adjusted) {
832 /* Restore default if chip reported 0 */
833 int i;
834
835 for (i = 0; i < ARRAY_SIZE(timeout_eff); i++) {
836 if (timeout_eff[i])
837 continue;
838
839 timeout_eff[i] = timeout_old[i];
840 chip->timeout_adjusted = true;
841 }
842
843 if (timeout_eff[0] != 0 && timeout_eff[0] < 1000) {
844 /* timeouts in msec rather usec */
845 for (i = 0; i != ARRAY_SIZE(timeout_eff); i++)
846 timeout_eff[i] *= 1000;
847 chip->timeout_adjusted = true;
848 }
849 }
850
851 /* Report adjusted timeouts */
852 if (chip->timeout_adjusted) {
853 dev_info(&chip->dev,
854 HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
855 timeout_chip[0], timeout_eff[0],
856 timeout_chip[1], timeout_eff[1],
857 timeout_chip[2], timeout_eff[2],
858 timeout_chip[3], timeout_eff[3]);
859 }
860
861 chip->timeout_a = usecs_to_jiffies(timeout_eff[0]);
862 chip->timeout_b = usecs_to_jiffies(timeout_eff[1]);
863 chip->timeout_c = usecs_to_jiffies(timeout_eff[2]);
864 chip->timeout_d = usecs_to_jiffies(timeout_eff[3]);
865
866 rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap,
867 "attempting to determine the durations",
868 sizeof(cap.duration));
869 if (rc)
870 return rc;
871
872 chip->duration[TPM_SHORT] =
873 usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short));
874 chip->duration[TPM_MEDIUM] =
875 usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium));
876 chip->duration[TPM_LONG] =
877 usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long));
878 chip->duration[TPM_LONG_LONG] = 0; /* not used under 1.2 */
879
880 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
881 * value wrong and apparently reports msecs rather than usecs. So we
882 * fix up the resulting too-small TPM_SHORT value to make things work.
883 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
884 */
885 if (chip->duration[TPM_SHORT] < (HZ / 100)) {
886 chip->duration[TPM_SHORT] = HZ;
887 chip->duration[TPM_MEDIUM] *= 1000;
888 chip->duration[TPM_LONG] *= 1000;
889 chip->duration_adjusted = true;
890 dev_info(&chip->dev, "Adjusting TPM timeout parameters.");
891 }
892
893 chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
894 return 0;
895} 422}
896EXPORT_SYMBOL_GPL(tpm_get_timeouts); 423EXPORT_SYMBOL_GPL(tpm_get_timeouts);
897 424
898#define TPM_ORD_CONTINUE_SELFTEST 83
899#define CONTINUE_SELFTEST_RESULT_SIZE 10
900
901static const struct tpm_input_header continue_selftest_header = {
902 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
903 .length = cpu_to_be32(10),
904 .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
905};
906
907/**
908 * tpm_continue_selftest -- run TPM's selftest
909 * @chip: TPM chip to use
910 *
911 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
912 * a TPM error code.
913 */
914static int tpm_continue_selftest(struct tpm_chip *chip)
915{
916 int rc;
917 struct tpm_cmd_t cmd;
918
919 cmd.header.in = continue_selftest_header;
920 rc = tpm_transmit_cmd(chip, NULL, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
921 0, 0, "continue selftest");
922 return rc;
923}
924
925#define TPM_ORDINAL_PCRREAD 21
926#define READ_PCR_RESULT_SIZE 30
927#define READ_PCR_RESULT_BODY_SIZE 20
928static const struct tpm_input_header pcrread_header = {
929 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
930 .length = cpu_to_be32(14),
931 .ordinal = cpu_to_be32(TPM_ORDINAL_PCRREAD)
932};
933
934int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
935{
936 int rc;
937 struct tpm_cmd_t cmd;
938
939 cmd.header.in = pcrread_header;
940 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
941 rc = tpm_transmit_cmd(chip, NULL, &cmd, READ_PCR_RESULT_SIZE,
942 READ_PCR_RESULT_BODY_SIZE, 0,
943 "attempting to read a pcr value");
944
945 if (rc == 0)
946 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
947 TPM_DIGEST_SIZE);
948 return rc;
949}
950
951/** 425/**
952 * tpm_is_tpm2 - do we a have a TPM2 chip? 426 * tpm_is_tpm2 - do we a have a TPM2 chip?
953 * @chip: a &struct tpm_chip instance, %NULL for the default chip 427 * @chip: a &struct tpm_chip instance, %NULL for the default chip
@@ -981,50 +455,24 @@ EXPORT_SYMBOL_GPL(tpm_is_tpm2);
981 * 455 *
982 * Return: same as with tpm_transmit_cmd() 456 * Return: same as with tpm_transmit_cmd()
983 */ 457 */
984int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) 458int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
985{ 459{
986 int rc; 460 int rc;
987 461
988 chip = tpm_find_get_ops(chip); 462 chip = tpm_find_get_ops(chip);
989 if (!chip) 463 if (!chip)
990 return -ENODEV; 464 return -ENODEV;
465
991 if (chip->flags & TPM_CHIP_FLAG_TPM2) 466 if (chip->flags & TPM_CHIP_FLAG_TPM2)
992 rc = tpm2_pcr_read(chip, pcr_idx, res_buf); 467 rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
993 else 468 else
994 rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf); 469 rc = tpm1_pcr_read(chip, pcr_idx, res_buf);
470
995 tpm_put_ops(chip); 471 tpm_put_ops(chip);
996 return rc; 472 return rc;
997} 473}
998EXPORT_SYMBOL_GPL(tpm_pcr_read); 474EXPORT_SYMBOL_GPL(tpm_pcr_read);
999 475
1000#define TPM_ORD_PCR_EXTEND 20
1001#define EXTEND_PCR_RESULT_SIZE 34
1002#define EXTEND_PCR_RESULT_BODY_SIZE 20
1003static const struct tpm_input_header pcrextend_header = {
1004 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
1005 .length = cpu_to_be32(34),
1006 .ordinal = cpu_to_be32(TPM_ORD_PCR_EXTEND)
1007};
1008
1009static int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
1010 char *log_msg)
1011{
1012 struct tpm_buf buf;
1013 int rc;
1014
1015 rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_EXTEND);
1016 if (rc)
1017 return rc;
1018
1019 tpm_buf_append_u32(&buf, pcr_idx);
1020 tpm_buf_append(&buf, hash, TPM_DIGEST_SIZE);
1021
1022 rc = tpm_transmit_cmd(chip, NULL, buf.data, EXTEND_PCR_RESULT_SIZE,
1023 EXTEND_PCR_RESULT_BODY_SIZE, 0, log_msg);
1024 tpm_buf_destroy(&buf);
1025 return rc;
1026}
1027
1028/** 476/**
1029 * tpm_pcr_extend - extend a PCR value in SHA1 bank. 477 * tpm_pcr_extend - extend a PCR value in SHA1 bank.
1030 * @chip: a &struct tpm_chip instance, %NULL for the default chip 478 * @chip: a &struct tpm_chip instance, %NULL for the default chip
@@ -1037,7 +485,7 @@ static int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
1037 * 485 *
1038 * Return: same as with tpm_transmit_cmd() 486 * Return: same as with tpm_transmit_cmd()
1039 */ 487 */
1040int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash) 488int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash)
1041{ 489{
1042 int rc; 490 int rc;
1043 struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)]; 491 struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
@@ -1071,97 +519,6 @@ int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
1071EXPORT_SYMBOL_GPL(tpm_pcr_extend); 519EXPORT_SYMBOL_GPL(tpm_pcr_extend);
1072 520
1073/** 521/**
1074 * tpm_do_selftest - have the TPM continue its selftest and wait until it
1075 * can receive further commands
1076 * @chip: TPM chip to use
1077 *
1078 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
1079 * a TPM error code.
1080 */
1081int tpm_do_selftest(struct tpm_chip *chip)
1082{
1083 int rc;
1084 unsigned int loops;
1085 unsigned int delay_msec = 100;
1086 unsigned long duration;
1087 u8 dummy[TPM_DIGEST_SIZE];
1088
1089 duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
1090
1091 loops = jiffies_to_msecs(duration) / delay_msec;
1092
1093 rc = tpm_continue_selftest(chip);
1094 if (rc == TPM_ERR_INVALID_POSTINIT) {
1095 chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
1096 dev_info(&chip->dev, "TPM not ready (%d)\n", rc);
1097 }
1098 /* This may fail if there was no TPM driver during a suspend/resume
1099 * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
1100 */
1101 if (rc)
1102 return rc;
1103
1104 do {
1105 /* Attempt to read a PCR value */
1106 rc = tpm_pcr_read_dev(chip, 0, dummy);
1107
1108 /* Some buggy TPMs will not respond to tpm_tis_ready() for
1109 * around 300ms while the self test is ongoing, keep trying
1110 * until the self test duration expires. */
1111 if (rc == -ETIME) {
1112 dev_info(
1113 &chip->dev, HW_ERR
1114 "TPM command timed out during continue self test");
1115 tpm_msleep(delay_msec);
1116 continue;
1117 }
1118
1119 if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
1120 dev_info(&chip->dev,
1121 "TPM is disabled/deactivated (0x%X)\n", rc);
1122 /* TPM is disabled and/or deactivated; driver can
1123 * proceed and TPM does handle commands for
1124 * suspend/resume correctly
1125 */
1126 return 0;
1127 }
1128 if (rc != TPM_WARN_DOING_SELFTEST)
1129 return rc;
1130 tpm_msleep(delay_msec);
1131 } while (--loops > 0);
1132
1133 return rc;
1134}
1135EXPORT_SYMBOL_GPL(tpm_do_selftest);
1136
1137/**
1138 * tpm1_auto_startup - Perform the standard automatic TPM initialization
1139 * sequence
1140 * @chip: TPM chip to use
1141 *
1142 * Returns 0 on success, < 0 in case of fatal error.
1143 */
1144int tpm1_auto_startup(struct tpm_chip *chip)
1145{
1146 int rc;
1147
1148 rc = tpm_get_timeouts(chip);
1149 if (rc)
1150 goto out;
1151 rc = tpm_do_selftest(chip);
1152 if (rc) {
1153 dev_err(&chip->dev, "TPM self test failed\n");
1154 goto out;
1155 }
1156
1157 return rc;
1158out:
1159 if (rc > 0)
1160 rc = -ENODEV;
1161 return rc;
1162}
1163
1164/**
1165 * tpm_send - send a TPM command 522 * tpm_send - send a TPM command
1166 * @chip: a &struct tpm_chip instance, %NULL for the default chip 523 * @chip: a &struct tpm_chip instance, %NULL for the default chip
1167 * @cmd: a TPM command buffer 524 * @cmd: a TPM command buffer
@@ -1184,14 +541,20 @@ int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
1184} 541}
1185EXPORT_SYMBOL_GPL(tpm_send); 542EXPORT_SYMBOL_GPL(tpm_send);
1186 543
1187#define TPM_ORD_SAVESTATE 152 544int tpm_auto_startup(struct tpm_chip *chip)
1188#define SAVESTATE_RESULT_SIZE 10 545{
546 int rc;
1189 547
1190static const struct tpm_input_header savestate_header = { 548 if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
1191 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND), 549 return 0;
1192 .length = cpu_to_be32(10), 550
1193 .ordinal = cpu_to_be32(TPM_ORD_SAVESTATE) 551 if (chip->flags & TPM_CHIP_FLAG_TPM2)
1194}; 552 rc = tpm2_auto_startup(chip);
553 else
554 rc = tpm1_auto_startup(chip);
555
556 return rc;
557}
1195 558
1196/* 559/*
1197 * We are about to suspend. Save the TPM state 560 * We are about to suspend. Save the TPM state
@@ -1200,54 +563,18 @@ static const struct tpm_input_header savestate_header = {
1200int tpm_pm_suspend(struct device *dev) 563int tpm_pm_suspend(struct device *dev)
1201{ 564{
1202 struct tpm_chip *chip = dev_get_drvdata(dev); 565 struct tpm_chip *chip = dev_get_drvdata(dev);
1203 struct tpm_cmd_t cmd; 566 int rc = 0;
1204 int rc, try;
1205 567
1206 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 }; 568 if (!chip)
1207
1208 if (chip == NULL)
1209 return -ENODEV; 569 return -ENODEV;
1210 570
1211 if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED) 571 if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
1212 return 0; 572 return 0;
1213 573
1214 if (chip->flags & TPM_CHIP_FLAG_TPM2) { 574 if (chip->flags & TPM_CHIP_FLAG_TPM2)
1215 tpm2_shutdown(chip, TPM2_SU_STATE); 575 tpm2_shutdown(chip, TPM2_SU_STATE);
1216 return 0; 576 else
1217 } 577 rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
1218
1219 /* for buggy tpm, flush pcrs with extend to selected dummy */
1220 if (tpm_suspend_pcr)
1221 rc = tpm1_pcr_extend(chip, tpm_suspend_pcr, dummy_hash,
1222 "extending dummy pcr before suspend");
1223
1224 /* now do the actual savestate */
1225 for (try = 0; try < TPM_RETRY; try++) {
1226 cmd.header.in = savestate_header;
1227 rc = tpm_transmit_cmd(chip, NULL, &cmd, SAVESTATE_RESULT_SIZE,
1228 0, 0, NULL);
1229
1230 /*
1231 * If the TPM indicates that it is too busy to respond to
1232 * this command then retry before giving up. It can take
1233 * several seconds for this TPM to be ready.
1234 *
1235 * This can happen if the TPM has already been sent the
1236 * SaveState command before the driver has loaded. TCG 1.2
1237 * specification states that any communication after SaveState
1238 * may cause the TPM to invalidate previously saved state.
1239 */
1240 if (rc != TPM_WARN_RETRY)
1241 break;
1242 tpm_msleep(TPM_TIMEOUT_RETRY);
1243 }
1244
1245 if (rc)
1246 dev_err(&chip->dev,
1247 "Error (%d) sending savestate before suspend\n", rc);
1248 else if (try > 0)
1249 dev_warn(&chip->dev, "TPM savestate took %dms\n",
1250 try * TPM_TIMEOUT_RETRY);
1251 578
1252 return rc; 579 return rc;
1253} 580}
@@ -1268,75 +595,32 @@ int tpm_pm_resume(struct device *dev)
1268} 595}
1269EXPORT_SYMBOL_GPL(tpm_pm_resume); 596EXPORT_SYMBOL_GPL(tpm_pm_resume);
1270 597
1271#define TPM_GETRANDOM_RESULT_SIZE 18
1272static const struct tpm_input_header tpm_getrandom_header = {
1273 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
1274 .length = cpu_to_be32(14),
1275 .ordinal = cpu_to_be32(TPM_ORD_GET_RANDOM)
1276};
1277
1278/** 598/**
1279 * tpm_get_random() - get random bytes from the TPM's RNG 599 * tpm_get_random() - get random bytes from the TPM's RNG
1280 * @chip: a &struct tpm_chip instance, %NULL for the default chip 600 * @chip: a &struct tpm_chip instance, %NULL for the default chip
1281 * @out: destination buffer for the random bytes 601 * @out: destination buffer for the random bytes
1282 * @max: the max number of bytes to write to @out 602 * @max: the max number of bytes to write to @out
1283 * 603 *
1284 * Return: same as with tpm_transmit_cmd() 604 * Return: number of random bytes read or a negative error value.
1285 */ 605 */
1286int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max) 606int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
1287{ 607{
1288 struct tpm_cmd_t tpm_cmd; 608 int rc;
1289 u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA), rlength;
1290 int err, total = 0, retries = 5;
1291 u8 *dest = out;
1292 609
1293 if (!out || !num_bytes || max > TPM_MAX_RNG_DATA) 610 if (!out || max > TPM_MAX_RNG_DATA)
1294 return -EINVAL; 611 return -EINVAL;
1295 612
1296 chip = tpm_find_get_ops(chip); 613 chip = tpm_find_get_ops(chip);
1297 if (!chip) 614 if (!chip)
1298 return -ENODEV; 615 return -ENODEV;
1299 616
1300 if (chip->flags & TPM_CHIP_FLAG_TPM2) { 617 if (chip->flags & TPM_CHIP_FLAG_TPM2)
1301 err = tpm2_get_random(chip, out, max); 618 rc = tpm2_get_random(chip, out, max);
1302 tpm_put_ops(chip); 619 else
1303 return err; 620 rc = tpm1_get_random(chip, out, max);
1304 }
1305
1306 do {
1307 tpm_cmd.header.in = tpm_getrandom_header;
1308 tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
1309
1310 err = tpm_transmit_cmd(chip, NULL, &tpm_cmd,
1311 TPM_GETRANDOM_RESULT_SIZE + num_bytes,
1312 offsetof(struct tpm_getrandom_out,
1313 rng_data),
1314 0, "attempting get random");
1315 if (err)
1316 break;
1317
1318 recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
1319 if (recd > num_bytes) {
1320 total = -EFAULT;
1321 break;
1322 }
1323
1324 rlength = be32_to_cpu(tpm_cmd.header.out.length);
1325 if (rlength < TPM_HEADER_SIZE +
1326 offsetof(struct tpm_getrandom_out, rng_data) +
1327 recd) {
1328 total = -EFAULT;
1329 break;
1330 }
1331 memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
1332
1333 dest += recd;
1334 total += recd;
1335 num_bytes -= recd;
1336 } while (retries-- && total < max);
1337 621
1338 tpm_put_ops(chip); 622 tpm_put_ops(chip);
1339 return total ? total : -EIO; 623 return rc;
1340} 624}
1341EXPORT_SYMBOL_GPL(tpm_get_random); 625EXPORT_SYMBOL_GPL(tpm_get_random);
1342 626
diff --git a/drivers/char/tpm/tpm-sysfs.c b/drivers/char/tpm/tpm-sysfs.c
index 83a77a445538..b88e08ec2c59 100644
--- a/drivers/char/tpm/tpm-sysfs.c
+++ b/drivers/char/tpm/tpm-sysfs.c
@@ -102,19 +102,19 @@ static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
102 cap_t cap; 102 cap_t cap;
103 u8 digest[TPM_DIGEST_SIZE]; 103 u8 digest[TPM_DIGEST_SIZE];
104 ssize_t rc; 104 ssize_t rc;
105 int i, j, num_pcrs; 105 u32 i, j, num_pcrs;
106 char *str = buf; 106 char *str = buf;
107 struct tpm_chip *chip = to_tpm_chip(dev); 107 struct tpm_chip *chip = to_tpm_chip(dev);
108 108
109 rc = tpm_getcap(chip, TPM_CAP_PROP_PCR, &cap, 109 rc = tpm1_getcap(chip, TPM_CAP_PROP_PCR, &cap,
110 "attempting to determine the number of PCRS", 110 "attempting to determine the number of PCRS",
111 sizeof(cap.num_pcrs)); 111 sizeof(cap.num_pcrs));
112 if (rc) 112 if (rc)
113 return 0; 113 return 0;
114 114
115 num_pcrs = be32_to_cpu(cap.num_pcrs); 115 num_pcrs = be32_to_cpu(cap.num_pcrs);
116 for (i = 0; i < num_pcrs; i++) { 116 for (i = 0; i < num_pcrs; i++) {
117 rc = tpm_pcr_read_dev(chip, i, digest); 117 rc = tpm1_pcr_read(chip, i, digest);
118 if (rc) 118 if (rc)
119 break; 119 break;
120 str += sprintf(str, "PCR-%02d: ", i); 120 str += sprintf(str, "PCR-%02d: ", i);
@@ -132,9 +132,9 @@ static ssize_t enabled_show(struct device *dev, struct device_attribute *attr,
132 cap_t cap; 132 cap_t cap;
133 ssize_t rc; 133 ssize_t rc;
134 134
135 rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap, 135 rc = tpm1_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
136 "attempting to determine the permanent enabled state", 136 "attempting to determine the permanent enabled state",
137 sizeof(cap.perm_flags)); 137 sizeof(cap.perm_flags));
138 if (rc) 138 if (rc)
139 return 0; 139 return 0;
140 140
@@ -149,9 +149,9 @@ static ssize_t active_show(struct device *dev, struct device_attribute *attr,
149 cap_t cap; 149 cap_t cap;
150 ssize_t rc; 150 ssize_t rc;
151 151
152 rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap, 152 rc = tpm1_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
153 "attempting to determine the permanent active state", 153 "attempting to determine the permanent active state",
154 sizeof(cap.perm_flags)); 154 sizeof(cap.perm_flags));
155 if (rc) 155 if (rc)
156 return 0; 156 return 0;
157 157
@@ -166,9 +166,9 @@ static ssize_t owned_show(struct device *dev, struct device_attribute *attr,
166 cap_t cap; 166 cap_t cap;
167 ssize_t rc; 167 ssize_t rc;
168 168
169 rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap, 169 rc = tpm1_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
170 "attempting to determine the owner state", 170 "attempting to determine the owner state",
171 sizeof(cap.owned)); 171 sizeof(cap.owned));
172 if (rc) 172 if (rc)
173 return 0; 173 return 0;
174 174
@@ -183,9 +183,9 @@ static ssize_t temp_deactivated_show(struct device *dev,
183 cap_t cap; 183 cap_t cap;
184 ssize_t rc; 184 ssize_t rc;
185 185
186 rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap, 186 rc = tpm1_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
187 "attempting to determine the temporary state", 187 "attempting to determine the temporary state",
188 sizeof(cap.stclear_flags)); 188 sizeof(cap.stclear_flags));
189 if (rc) 189 if (rc)
190 return 0; 190 return 0;
191 191
@@ -202,18 +202,18 @@ static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
202 ssize_t rc; 202 ssize_t rc;
203 char *str = buf; 203 char *str = buf;
204 204
205 rc = tpm_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap, 205 rc = tpm1_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
206 "attempting to determine the manufacturer", 206 "attempting to determine the manufacturer",
207 sizeof(cap.manufacturer_id)); 207 sizeof(cap.manufacturer_id));
208 if (rc) 208 if (rc)
209 return 0; 209 return 0;
210 str += sprintf(str, "Manufacturer: 0x%x\n", 210 str += sprintf(str, "Manufacturer: 0x%x\n",
211 be32_to_cpu(cap.manufacturer_id)); 211 be32_to_cpu(cap.manufacturer_id));
212 212
213 /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */ 213 /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
214 rc = tpm_getcap(chip, TPM_CAP_VERSION_1_2, &cap, 214 rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap,
215 "attempting to determine the 1.2 version", 215 "attempting to determine the 1.2 version",
216 sizeof(cap.tpm_version_1_2)); 216 sizeof(cap.tpm_version_1_2));
217 if (!rc) { 217 if (!rc) {
218 str += sprintf(str, 218 str += sprintf(str,
219 "TCG version: %d.%d\nFirmware version: %d.%d\n", 219 "TCG version: %d.%d\nFirmware version: %d.%d\n",
@@ -223,9 +223,9 @@ static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
223 cap.tpm_version_1_2.revMinor); 223 cap.tpm_version_1_2.revMinor);
224 } else { 224 } else {
225 /* Otherwise just use TPM_STRUCT_VER */ 225 /* Otherwise just use TPM_STRUCT_VER */
226 rc = tpm_getcap(chip, TPM_CAP_VERSION_1_1, &cap, 226 rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
227 "attempting to determine the 1.1 version", 227 "attempting to determine the 1.1 version",
228 sizeof(cap.tpm_version)); 228 sizeof(cap.tpm_version));
229 if (rc) 229 if (rc)
230 return 0; 230 return 0;
231 str += sprintf(str, 231 str += sprintf(str,
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index f20dc8ece348..f27d1f38a93d 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -134,22 +134,31 @@ enum tpm2_algorithms {
134}; 134};
135 135
136enum tpm2_command_codes { 136enum tpm2_command_codes {
137 TPM2_CC_FIRST = 0x011F, 137 TPM2_CC_FIRST = 0x011F,
138 TPM2_CC_CREATE_PRIMARY = 0x0131, 138 TPM2_CC_HIERARCHY_CONTROL = 0x0121,
139 TPM2_CC_SELF_TEST = 0x0143, 139 TPM2_CC_HIERARCHY_CHANGE_AUTH = 0x0129,
140 TPM2_CC_STARTUP = 0x0144, 140 TPM2_CC_CREATE_PRIMARY = 0x0131,
141 TPM2_CC_SHUTDOWN = 0x0145, 141 TPM2_CC_SEQUENCE_COMPLETE = 0x013E,
142 TPM2_CC_CREATE = 0x0153, 142 TPM2_CC_SELF_TEST = 0x0143,
143 TPM2_CC_LOAD = 0x0157, 143 TPM2_CC_STARTUP = 0x0144,
144 TPM2_CC_UNSEAL = 0x015E, 144 TPM2_CC_SHUTDOWN = 0x0145,
145 TPM2_CC_CONTEXT_LOAD = 0x0161, 145 TPM2_CC_NV_READ = 0x014E,
146 TPM2_CC_CONTEXT_SAVE = 0x0162, 146 TPM2_CC_CREATE = 0x0153,
147 TPM2_CC_FLUSH_CONTEXT = 0x0165, 147 TPM2_CC_LOAD = 0x0157,
148 TPM2_CC_GET_CAPABILITY = 0x017A, 148 TPM2_CC_SEQUENCE_UPDATE = 0x015C,
149 TPM2_CC_GET_RANDOM = 0x017B, 149 TPM2_CC_UNSEAL = 0x015E,
150 TPM2_CC_PCR_READ = 0x017E, 150 TPM2_CC_CONTEXT_LOAD = 0x0161,
151 TPM2_CC_PCR_EXTEND = 0x0182, 151 TPM2_CC_CONTEXT_SAVE = 0x0162,
152 TPM2_CC_LAST = 0x018F, 152 TPM2_CC_FLUSH_CONTEXT = 0x0165,
153 TPM2_CC_VERIFY_SIGNATURE = 0x0177,
154 TPM2_CC_GET_CAPABILITY = 0x017A,
155 TPM2_CC_GET_RANDOM = 0x017B,
156 TPM2_CC_PCR_READ = 0x017E,
157 TPM2_CC_PCR_EXTEND = 0x0182,
158 TPM2_CC_EVENT_SEQUENCE_COMPLETE = 0x0185,
159 TPM2_CC_HASH_SEQUENCE_START = 0x0186,
160 TPM2_CC_CREATE_LOADED = 0x0191,
161 TPM2_CC_LAST = 0x0193, /* Spec 1.36 */
153}; 162};
154 163
155enum tpm2_permanent_handles { 164enum tpm2_permanent_handles {
@@ -368,18 +377,6 @@ enum tpm_sub_capabilities {
368 TPM_CAP_PROP_TIS_DURATION = 0x120, 377 TPM_CAP_PROP_TIS_DURATION = 0x120,
369}; 378};
370 379
371typedef union {
372 struct tpm_input_header in;
373 struct tpm_output_header out;
374} tpm_cmd_header;
375
376struct tpm_pcrread_out {
377 u8 pcr_result[TPM_DIGEST_SIZE];
378} __packed;
379
380struct tpm_pcrread_in {
381 __be32 pcr_idx;
382} __packed;
383 380
384/* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18 381/* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
385 * bytes, but 128 is still a relatively large number of random bytes and 382 * bytes, but 128 is still a relatively large number of random bytes and
@@ -387,28 +384,6 @@ struct tpm_pcrread_in {
387 * compiler warnings about stack frame size. */ 384 * compiler warnings about stack frame size. */
388#define TPM_MAX_RNG_DATA 128 385#define TPM_MAX_RNG_DATA 128
389 386
390struct tpm_getrandom_out {
391 __be32 rng_data_len;
392 u8 rng_data[TPM_MAX_RNG_DATA];
393} __packed;
394
395struct tpm_getrandom_in {
396 __be32 num_bytes;
397} __packed;
398
399typedef union {
400 struct tpm_pcrread_in pcrread_in;
401 struct tpm_pcrread_out pcrread_out;
402 struct tpm_getrandom_in getrandom_in;
403 struct tpm_getrandom_out getrandom_out;
404} tpm_cmd_params;
405
406struct tpm_cmd_t {
407 tpm_cmd_header header;
408 tpm_cmd_params params;
409} __packed;
410
411
412/* A string buffer type for constructing TPM commands. This is based on the 387/* A string buffer type for constructing TPM commands. This is based on the
413 * ideas of string buffer code in security/keys/trusted.h but is heap based 388 * ideas of string buffer code in security/keys/trusted.h but is heap based
414 * in order to keep the stack usage minimal. 389 * in order to keep the stack usage minimal.
@@ -531,12 +506,20 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
531 void *buf, size_t bufsiz, 506 void *buf, size_t bufsiz,
532 size_t min_rsp_body_length, unsigned int flags, 507 size_t min_rsp_body_length, unsigned int flags,
533 const char *desc); 508 const char *desc);
534int tpm_startup(struct tpm_chip *chip);
535ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
536 const char *desc, size_t min_cap_length);
537int tpm_get_timeouts(struct tpm_chip *); 509int tpm_get_timeouts(struct tpm_chip *);
510int tpm_auto_startup(struct tpm_chip *chip);
511
512int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr);
538int tpm1_auto_startup(struct tpm_chip *chip); 513int tpm1_auto_startup(struct tpm_chip *chip);
539int tpm_do_selftest(struct tpm_chip *chip); 514int tpm1_do_selftest(struct tpm_chip *chip);
515int tpm1_get_timeouts(struct tpm_chip *chip);
516unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
517int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
518 const char *log_msg);
519int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
520ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
521 const char *desc, size_t min_cap_length);
522int tpm1_get_random(struct tpm_chip *chip, u8 *out, size_t max);
540unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal); 523unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
541int tpm_pm_suspend(struct device *dev); 524int tpm_pm_suspend(struct device *dev);
542int tpm_pm_resume(struct device *dev); 525int tpm_pm_resume(struct device *dev);
@@ -560,7 +543,6 @@ void tpm_chip_unregister(struct tpm_chip *chip);
560 543
561void tpm_sysfs_add_device(struct tpm_chip *chip); 544void tpm_sysfs_add_device(struct tpm_chip *chip);
562 545
563int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
564 546
565#ifdef CONFIG_ACPI 547#ifdef CONFIG_ACPI
566extern void tpm_add_ppi(struct tpm_chip *chip); 548extern void tpm_add_ppi(struct tpm_chip *chip);
@@ -575,8 +557,9 @@ static inline u32 tpm2_rc_value(u32 rc)
575 return (rc & BIT(7)) ? rc & 0xff : rc; 557 return (rc & BIT(7)) ? rc & 0xff : rc;
576} 558}
577 559
578int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf); 560int tpm2_get_timeouts(struct tpm_chip *chip);
579int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count, 561int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
562int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, u32 count,
580 struct tpm2_digest *digests); 563 struct tpm2_digest *digests);
581int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max); 564int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);
582void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle, 565void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c
new file mode 100644
index 000000000000..6f306338953b
--- /dev/null
+++ b/drivers/char/tpm/tpm1-cmd.c
@@ -0,0 +1,781 @@
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2004 IBM Corporation
4 * Copyright (C) 2014 Intel Corporation
5 *
6 * Authors:
7 * Leendert van Doorn <leendert@watson.ibm.com>
8 * Dave Safford <safford@watson.ibm.com>
9 * Reiner Sailer <sailer@watson.ibm.com>
10 * Kylene Hall <kjhall@us.ibm.com>
11 *
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
14 */
15
16#include <linux/poll.h>
17#include <linux/slab.h>
18#include <linux/mutex.h>
19#include <linux/spinlock.h>
20#include <linux/freezer.h>
21#include <linux/tpm_eventlog.h>
22
23#include "tpm.h"
24
25#define TPM_MAX_ORDINAL 243
26
27/*
28 * Array with one entry per ordinal defining the maximum amount
29 * of time the chip could take to return the result. The ordinal
30 * designation of short, medium or long is defined in a table in
31 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
32 * values of the SHORT, MEDIUM, and LONG durations are retrieved
33 * from the chip during initialization with a call to tpm_get_timeouts.
34 */
35static const u8 tpm1_ordinal_duration[TPM_MAX_ORDINAL] = {
36 TPM_UNDEFINED, /* 0 */
37 TPM_UNDEFINED,
38 TPM_UNDEFINED,
39 TPM_UNDEFINED,
40 TPM_UNDEFINED,
41 TPM_UNDEFINED, /* 5 */
42 TPM_UNDEFINED,
43 TPM_UNDEFINED,
44 TPM_UNDEFINED,
45 TPM_UNDEFINED,
46 TPM_SHORT, /* 10 */
47 TPM_SHORT,
48 TPM_MEDIUM,
49 TPM_LONG,
50 TPM_LONG,
51 TPM_MEDIUM, /* 15 */
52 TPM_SHORT,
53 TPM_SHORT,
54 TPM_MEDIUM,
55 TPM_LONG,
56 TPM_SHORT, /* 20 */
57 TPM_SHORT,
58 TPM_MEDIUM,
59 TPM_MEDIUM,
60 TPM_MEDIUM,
61 TPM_SHORT, /* 25 */
62 TPM_SHORT,
63 TPM_MEDIUM,
64 TPM_SHORT,
65 TPM_SHORT,
66 TPM_MEDIUM, /* 30 */
67 TPM_LONG,
68 TPM_MEDIUM,
69 TPM_SHORT,
70 TPM_SHORT,
71 TPM_SHORT, /* 35 */
72 TPM_MEDIUM,
73 TPM_MEDIUM,
74 TPM_UNDEFINED,
75 TPM_UNDEFINED,
76 TPM_MEDIUM, /* 40 */
77 TPM_LONG,
78 TPM_MEDIUM,
79 TPM_SHORT,
80 TPM_SHORT,
81 TPM_SHORT, /* 45 */
82 TPM_SHORT,
83 TPM_SHORT,
84 TPM_SHORT,
85 TPM_LONG,
86 TPM_MEDIUM, /* 50 */
87 TPM_MEDIUM,
88 TPM_UNDEFINED,
89 TPM_UNDEFINED,
90 TPM_UNDEFINED,
91 TPM_UNDEFINED, /* 55 */
92 TPM_UNDEFINED,
93 TPM_UNDEFINED,
94 TPM_UNDEFINED,
95 TPM_UNDEFINED,
96 TPM_MEDIUM, /* 60 */
97 TPM_MEDIUM,
98 TPM_MEDIUM,
99 TPM_SHORT,
100 TPM_SHORT,
101 TPM_MEDIUM, /* 65 */
102 TPM_UNDEFINED,
103 TPM_UNDEFINED,
104 TPM_UNDEFINED,
105 TPM_UNDEFINED,
106 TPM_SHORT, /* 70 */
107 TPM_SHORT,
108 TPM_UNDEFINED,
109 TPM_UNDEFINED,
110 TPM_UNDEFINED,
111 TPM_UNDEFINED, /* 75 */
112 TPM_UNDEFINED,
113 TPM_UNDEFINED,
114 TPM_UNDEFINED,
115 TPM_UNDEFINED,
116 TPM_LONG, /* 80 */
117 TPM_UNDEFINED,
118 TPM_MEDIUM,
119 TPM_LONG,
120 TPM_SHORT,
121 TPM_UNDEFINED, /* 85 */
122 TPM_UNDEFINED,
123 TPM_UNDEFINED,
124 TPM_UNDEFINED,
125 TPM_UNDEFINED,
126 TPM_SHORT, /* 90 */
127 TPM_SHORT,
128 TPM_SHORT,
129 TPM_SHORT,
130 TPM_SHORT,
131 TPM_UNDEFINED, /* 95 */
132 TPM_UNDEFINED,
133 TPM_UNDEFINED,
134 TPM_UNDEFINED,
135 TPM_UNDEFINED,
136 TPM_MEDIUM, /* 100 */
137 TPM_SHORT,
138 TPM_SHORT,
139 TPM_UNDEFINED,
140 TPM_UNDEFINED,
141 TPM_UNDEFINED, /* 105 */
142 TPM_UNDEFINED,
143 TPM_UNDEFINED,
144 TPM_UNDEFINED,
145 TPM_UNDEFINED,
146 TPM_SHORT, /* 110 */
147 TPM_SHORT,
148 TPM_SHORT,
149 TPM_SHORT,
150 TPM_SHORT,
151 TPM_SHORT, /* 115 */
152 TPM_SHORT,
153 TPM_SHORT,
154 TPM_UNDEFINED,
155 TPM_UNDEFINED,
156 TPM_LONG, /* 120 */
157 TPM_LONG,
158 TPM_MEDIUM,
159 TPM_UNDEFINED,
160 TPM_SHORT,
161 TPM_SHORT, /* 125 */
162 TPM_SHORT,
163 TPM_LONG,
164 TPM_SHORT,
165 TPM_SHORT,
166 TPM_SHORT, /* 130 */
167 TPM_MEDIUM,
168 TPM_UNDEFINED,
169 TPM_SHORT,
170 TPM_MEDIUM,
171 TPM_UNDEFINED, /* 135 */
172 TPM_UNDEFINED,
173 TPM_UNDEFINED,
174 TPM_UNDEFINED,
175 TPM_UNDEFINED,
176 TPM_SHORT, /* 140 */
177 TPM_SHORT,
178 TPM_UNDEFINED,
179 TPM_UNDEFINED,
180 TPM_UNDEFINED,
181 TPM_UNDEFINED, /* 145 */
182 TPM_UNDEFINED,
183 TPM_UNDEFINED,
184 TPM_UNDEFINED,
185 TPM_UNDEFINED,
186 TPM_SHORT, /* 150 */
187 TPM_MEDIUM,
188 TPM_MEDIUM,
189 TPM_SHORT,
190 TPM_SHORT,
191 TPM_UNDEFINED, /* 155 */
192 TPM_UNDEFINED,
193 TPM_UNDEFINED,
194 TPM_UNDEFINED,
195 TPM_UNDEFINED,
196 TPM_SHORT, /* 160 */
197 TPM_SHORT,
198 TPM_SHORT,
199 TPM_SHORT,
200 TPM_UNDEFINED,
201 TPM_UNDEFINED, /* 165 */
202 TPM_UNDEFINED,
203 TPM_UNDEFINED,
204 TPM_UNDEFINED,
205 TPM_UNDEFINED,
206 TPM_LONG, /* 170 */
207 TPM_UNDEFINED,
208 TPM_UNDEFINED,
209 TPM_UNDEFINED,
210 TPM_UNDEFINED,
211 TPM_UNDEFINED, /* 175 */
212 TPM_UNDEFINED,
213 TPM_UNDEFINED,
214 TPM_UNDEFINED,
215 TPM_UNDEFINED,
216 TPM_MEDIUM, /* 180 */
217 TPM_SHORT,
218 TPM_MEDIUM,
219 TPM_MEDIUM,
220 TPM_MEDIUM,
221 TPM_MEDIUM, /* 185 */
222 TPM_SHORT,
223 TPM_UNDEFINED,
224 TPM_UNDEFINED,
225 TPM_UNDEFINED,
226 TPM_UNDEFINED, /* 190 */
227 TPM_UNDEFINED,
228 TPM_UNDEFINED,
229 TPM_UNDEFINED,
230 TPM_UNDEFINED,
231 TPM_UNDEFINED, /* 195 */
232 TPM_UNDEFINED,
233 TPM_UNDEFINED,
234 TPM_UNDEFINED,
235 TPM_UNDEFINED,
236 TPM_SHORT, /* 200 */
237 TPM_UNDEFINED,
238 TPM_UNDEFINED,
239 TPM_UNDEFINED,
240 TPM_SHORT,
241 TPM_SHORT, /* 205 */
242 TPM_SHORT,
243 TPM_SHORT,
244 TPM_SHORT,
245 TPM_SHORT,
246 TPM_MEDIUM, /* 210 */
247 TPM_UNDEFINED,
248 TPM_MEDIUM,
249 TPM_MEDIUM,
250 TPM_MEDIUM,
251 TPM_UNDEFINED, /* 215 */
252 TPM_MEDIUM,
253 TPM_UNDEFINED,
254 TPM_UNDEFINED,
255 TPM_SHORT,
256 TPM_SHORT, /* 220 */
257 TPM_SHORT,
258 TPM_SHORT,
259 TPM_SHORT,
260 TPM_SHORT,
261 TPM_UNDEFINED, /* 225 */
262 TPM_UNDEFINED,
263 TPM_UNDEFINED,
264 TPM_UNDEFINED,
265 TPM_UNDEFINED,
266 TPM_SHORT, /* 230 */
267 TPM_LONG,
268 TPM_MEDIUM,
269 TPM_UNDEFINED,
270 TPM_UNDEFINED,
271 TPM_UNDEFINED, /* 235 */
272 TPM_UNDEFINED,
273 TPM_UNDEFINED,
274 TPM_UNDEFINED,
275 TPM_UNDEFINED,
276 TPM_SHORT, /* 240 */
277 TPM_UNDEFINED,
278 TPM_MEDIUM,
279};
280
281/**
282 * tpm1_calc_ordinal_duration() - calculate the maximum command duration
283 * @chip: TPM chip to use.
284 * @ordinal: TPM command ordinal.
285 *
286 * The function returns the maximum amount of time the chip could take
287 * to return the result for a particular ordinal in jiffies.
288 *
289 * Return: A maximal duration time for an ordinal in jiffies.
290 */
291unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
292{
293 int duration_idx = TPM_UNDEFINED;
294 int duration = 0;
295
296 /*
297 * We only have a duration table for protected commands, where the upper
298 * 16 bits are 0. For the few other ordinals the fallback will be used.
299 */
300 if (ordinal < TPM_MAX_ORDINAL)
301 duration_idx = tpm1_ordinal_duration[ordinal];
302
303 if (duration_idx != TPM_UNDEFINED)
304 duration = chip->duration[duration_idx];
305 if (duration <= 0)
306 return 2 * 60 * HZ;
307 else
308 return duration;
309}
310
311#define TPM_ORD_STARTUP 153
312#define TPM_ST_CLEAR 1
313
314/**
315 * tpm_startup() - turn on the TPM
316 * @chip: TPM chip to use
317 *
318 * Normally the firmware should start the TPM. This function is provided as a
319 * workaround if this does not happen. A legal case for this could be for
320 * example when a TPM emulator is used.
321 *
322 * Return: same as tpm_transmit_cmd()
323 */
324static int tpm1_startup(struct tpm_chip *chip)
325{
326 struct tpm_buf buf;
327 int rc;
328
329 dev_info(&chip->dev, "starting up the TPM manually\n");
330
331 rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_STARTUP);
332 if (rc < 0)
333 return rc;
334
335 tpm_buf_append_u16(&buf, TPM_ST_CLEAR);
336
337 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
338 "attempting to start the TPM");
339
340 tpm_buf_destroy(&buf);
341
342 return rc;
343}
344
345int tpm1_get_timeouts(struct tpm_chip *chip)
346{
347 cap_t cap;
348 unsigned long timeout_old[4], timeout_chip[4], timeout_eff[4];
349 ssize_t rc;
350
351 rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, NULL,
352 sizeof(cap.timeout));
353 if (rc == TPM_ERR_INVALID_POSTINIT) {
354 if (tpm1_startup(chip))
355 return rc;
356
357 rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap,
358 "attempting to determine the timeouts",
359 sizeof(cap.timeout));
360 }
361
362 if (rc) {
363 dev_err(&chip->dev, "A TPM error (%zd) occurred attempting to determine the timeouts\n",
364 rc);
365 return rc;
366 }
367
368 timeout_old[0] = jiffies_to_usecs(chip->timeout_a);
369 timeout_old[1] = jiffies_to_usecs(chip->timeout_b);
370 timeout_old[2] = jiffies_to_usecs(chip->timeout_c);
371 timeout_old[3] = jiffies_to_usecs(chip->timeout_d);
372 timeout_chip[0] = be32_to_cpu(cap.timeout.a);
373 timeout_chip[1] = be32_to_cpu(cap.timeout.b);
374 timeout_chip[2] = be32_to_cpu(cap.timeout.c);
375 timeout_chip[3] = be32_to_cpu(cap.timeout.d);
376 memcpy(timeout_eff, timeout_chip, sizeof(timeout_eff));
377
378 /*
379 * Provide ability for vendor overrides of timeout values in case
380 * of misreporting.
381 */
382 if (chip->ops->update_timeouts)
383 chip->timeout_adjusted =
384 chip->ops->update_timeouts(chip, timeout_eff);
385
386 if (!chip->timeout_adjusted) {
387 /* Restore default if chip reported 0 */
388 unsigned int i;
389
390 for (i = 0; i < ARRAY_SIZE(timeout_eff); i++) {
391 if (timeout_eff[i])
392 continue;
393
394 timeout_eff[i] = timeout_old[i];
395 chip->timeout_adjusted = true;
396 }
397
398 if (timeout_eff[0] != 0 && timeout_eff[0] < 1000) {
399 /* timeouts in msec rather usec */
400 for (i = 0; i != ARRAY_SIZE(timeout_eff); i++)
401 timeout_eff[i] *= 1000;
402 chip->timeout_adjusted = true;
403 }
404 }
405
406 /* Report adjusted timeouts */
407 if (chip->timeout_adjusted) {
408 dev_info(&chip->dev, HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
409 timeout_chip[0], timeout_eff[0],
410 timeout_chip[1], timeout_eff[1],
411 timeout_chip[2], timeout_eff[2],
412 timeout_chip[3], timeout_eff[3]);
413 }
414
415 chip->timeout_a = usecs_to_jiffies(timeout_eff[0]);
416 chip->timeout_b = usecs_to_jiffies(timeout_eff[1]);
417 chip->timeout_c = usecs_to_jiffies(timeout_eff[2]);
418 chip->timeout_d = usecs_to_jiffies(timeout_eff[3]);
419
420 rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap,
421 "attempting to determine the durations",
422 sizeof(cap.duration));
423 if (rc)
424 return rc;
425
426 chip->duration[TPM_SHORT] =
427 usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short));
428 chip->duration[TPM_MEDIUM] =
429 usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium));
430 chip->duration[TPM_LONG] =
431 usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long));
432 chip->duration[TPM_LONG_LONG] = 0; /* not used under 1.2 */
433
434 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
435 * value wrong and apparently reports msecs rather than usecs. So we
436 * fix up the resulting too-small TPM_SHORT value to make things work.
437 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
438 */
439 if (chip->duration[TPM_SHORT] < (HZ / 100)) {
440 chip->duration[TPM_SHORT] = HZ;
441 chip->duration[TPM_MEDIUM] *= 1000;
442 chip->duration[TPM_LONG] *= 1000;
443 chip->duration_adjusted = true;
444 dev_info(&chip->dev, "Adjusting TPM timeout parameters.");
445 }
446
447 chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
448 return 0;
449}
450
451#define TPM_ORD_PCR_EXTEND 20
452int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
453 const char *log_msg)
454{
455 struct tpm_buf buf;
456 int rc;
457
458 rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_EXTEND);
459 if (rc)
460 return rc;
461
462 tpm_buf_append_u32(&buf, pcr_idx);
463 tpm_buf_append(&buf, hash, TPM_DIGEST_SIZE);
464
465 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
466 TPM_DIGEST_SIZE, 0, log_msg);
467
468 tpm_buf_destroy(&buf);
469 return rc;
470}
471
472#define TPM_ORD_GET_CAP 101
473ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
474 const char *desc, size_t min_cap_length)
475{
476 struct tpm_buf buf;
477 int rc;
478
479 rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP);
480 if (rc)
481 return rc;
482
483 if (subcap_id == TPM_CAP_VERSION_1_1 ||
484 subcap_id == TPM_CAP_VERSION_1_2) {
485 tpm_buf_append_u32(&buf, subcap_id);
486 tpm_buf_append_u32(&buf, 0);
487 } else {
488 if (subcap_id == TPM_CAP_FLAG_PERM ||
489 subcap_id == TPM_CAP_FLAG_VOL)
490 tpm_buf_append_u32(&buf, TPM_CAP_FLAG);
491 else
492 tpm_buf_append_u32(&buf, TPM_CAP_PROP);
493
494 tpm_buf_append_u32(&buf, 4);
495 tpm_buf_append_u32(&buf, subcap_id);
496 }
497 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
498 min_cap_length, 0, desc);
499 if (!rc)
500 *cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
501
502 tpm_buf_destroy(&buf);
503 return rc;
504}
505EXPORT_SYMBOL_GPL(tpm1_getcap);
506
507#define TPM_ORD_GET_RANDOM 70
508struct tpm1_get_random_out {
509 __be32 rng_data_len;
510 u8 rng_data[TPM_MAX_RNG_DATA];
511} __packed;
512
513/**
514 * tpm1_get_random() - get random bytes from the TPM's RNG
515 * @chip: a &struct tpm_chip instance
516 * @dest: destination buffer for the random bytes
517 * @max: the maximum number of bytes to write to @dest
518 *
519 * Return:
520 * * number of bytes read
521 * * -errno or a TPM return code otherwise
522 */
523int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
524{
525 struct tpm1_get_random_out *out;
526 u32 num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
527 struct tpm_buf buf;
528 u32 total = 0;
529 int retries = 5;
530 u32 recd;
531 int rc;
532
533 rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM);
534 if (rc)
535 return rc;
536
537 do {
538 tpm_buf_append_u32(&buf, num_bytes);
539
540 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
541 sizeof(out->rng_data_len), 0,
542 "attempting get random");
543 if (rc)
544 goto out;
545
546 out = (struct tpm1_get_random_out *)&buf.data[TPM_HEADER_SIZE];
547
548 recd = be32_to_cpu(out->rng_data_len);
549 if (recd > num_bytes) {
550 rc = -EFAULT;
551 goto out;
552 }
553
554 if (tpm_buf_length(&buf) < TPM_HEADER_SIZE +
555 sizeof(out->rng_data_len) + recd) {
556 rc = -EFAULT;
557 goto out;
558 }
559 memcpy(dest, out->rng_data, recd);
560
561 dest += recd;
562 total += recd;
563 num_bytes -= recd;
564
565 tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM);
566 } while (retries-- && total < max);
567
568 rc = total ? (int)total : -EIO;
569out:
570 tpm_buf_destroy(&buf);
571 return rc;
572}
573
574#define TPM_ORD_PCRREAD 21
575int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
576{
577 struct tpm_buf buf;
578 int rc;
579
580 rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCRREAD);
581 if (rc)
582 return rc;
583
584 tpm_buf_append_u32(&buf, pcr_idx);
585
586 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
587 TPM_DIGEST_SIZE, 0,
588 "attempting to read a pcr value");
589 if (rc)
590 goto out;
591
592 if (tpm_buf_length(&buf) < TPM_DIGEST_SIZE) {
593 rc = -EFAULT;
594 goto out;
595 }
596
597 memcpy(res_buf, &buf.data[TPM_HEADER_SIZE], TPM_DIGEST_SIZE);
598
599out:
600 tpm_buf_destroy(&buf);
601 return rc;
602}
603
604#define TPM_ORD_CONTINUE_SELFTEST 83
605/**
606 * tpm_continue_selftest() - run TPM's selftest
607 * @chip: TPM chip to use
608 *
609 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
610 * a TPM error code.
611 */
612static int tpm1_continue_selftest(struct tpm_chip *chip)
613{
614 struct tpm_buf buf;
615 int rc;
616
617 rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_CONTINUE_SELFTEST);
618 if (rc)
619 return rc;
620
621 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
622 0, 0, "continue selftest");
623
624 tpm_buf_destroy(&buf);
625
626 return rc;
627}
628
629/**
630 * tpm1_do_selftest - have the TPM continue its selftest and wait until it
631 * can receive further commands
632 * @chip: TPM chip to use
633 *
634 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
635 * a TPM error code.
636 */
637int tpm1_do_selftest(struct tpm_chip *chip)
638{
639 int rc;
640 unsigned int loops;
641 unsigned int delay_msec = 100;
642 unsigned long duration;
643 u8 dummy[TPM_DIGEST_SIZE];
644
645 duration = tpm1_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
646
647 loops = jiffies_to_msecs(duration) / delay_msec;
648
649 rc = tpm1_continue_selftest(chip);
650 if (rc == TPM_ERR_INVALID_POSTINIT) {
651 chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
652 dev_info(&chip->dev, "TPM not ready (%d)\n", rc);
653 }
654 /* This may fail if there was no TPM driver during a suspend/resume
655 * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
656 */
657 if (rc)
658 return rc;
659
660 do {
661 /* Attempt to read a PCR value */
662 rc = tpm1_pcr_read(chip, 0, dummy);
663
664 /* Some buggy TPMs will not respond to tpm_tis_ready() for
665 * around 300ms while the self test is ongoing, keep trying
666 * until the self test duration expires.
667 */
668 if (rc == -ETIME) {
669 dev_info(&chip->dev, HW_ERR "TPM command timed out during continue self test");
670 tpm_msleep(delay_msec);
671 continue;
672 }
673
674 if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
675 dev_info(&chip->dev, "TPM is disabled/deactivated (0x%X)\n",
676 rc);
677 /* TPM is disabled and/or deactivated; driver can
678 * proceed and TPM does handle commands for
679 * suspend/resume correctly
680 */
681 return 0;
682 }
683 if (rc != TPM_WARN_DOING_SELFTEST)
684 return rc;
685 tpm_msleep(delay_msec);
686 } while (--loops > 0);
687
688 return rc;
689}
690EXPORT_SYMBOL_GPL(tpm1_do_selftest);
691
692/**
693 * tpm1_auto_startup - Perform the standard automatic TPM initialization
694 * sequence
695 * @chip: TPM chip to use
696 *
697 * Returns 0 on success, < 0 in case of fatal error.
698 */
699int tpm1_auto_startup(struct tpm_chip *chip)
700{
701 int rc;
702
703 rc = tpm1_get_timeouts(chip);
704 if (rc)
705 goto out;
706 rc = tpm1_do_selftest(chip);
707 if (rc) {
708 dev_err(&chip->dev, "TPM self test failed\n");
709 goto out;
710 }
711
712 return rc;
713out:
714 if (rc > 0)
715 rc = -ENODEV;
716 return rc;
717}
718
719#define TPM_ORD_SAVESTATE 152
720
721/**
722 * tpm1_pm_suspend() - pm suspend handler
723 * @chip: TPM chip to use.
724 * @tpm_suspend_pcr: flush pcr for buggy TPM chips.
725 *
726 * The functions saves the TPM state to be restored on resume.
727 *
728 * Return:
729 * * 0 on success,
730 * * < 0 on error.
731 */
732int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)
733{
734 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
735 struct tpm_buf buf;
736 unsigned int try;
737 int rc;
738
739
740 /* for buggy tpm, flush pcrs with extend to selected dummy */
741 if (tpm_suspend_pcr)
742 rc = tpm1_pcr_extend(chip, tpm_suspend_pcr, dummy_hash,
743 "extending dummy pcr before suspend");
744
745 rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_SAVESTATE);
746 if (rc)
747 return rc;
748 /* now do the actual savestate */
749 for (try = 0; try < TPM_RETRY; try++) {
750 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
751 0, 0, NULL);
752
753 /*
754 * If the TPM indicates that it is too busy to respond to
755 * this command then retry before giving up. It can take
756 * several seconds for this TPM to be ready.
757 *
758 * This can happen if the TPM has already been sent the
759 * SaveState command before the driver has loaded. TCG 1.2
760 * specification states that any communication after SaveState
761 * may cause the TPM to invalidate previously saved state.
762 */
763 if (rc != TPM_WARN_RETRY)
764 break;
765 tpm_msleep(TPM_TIMEOUT_RETRY);
766
767 tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_SAVESTATE);
768 }
769
770 if (rc)
771 dev_err(&chip->dev, "Error (%d) sending savestate before suspend\n",
772 rc);
773 else if (try > 0)
774 dev_warn(&chip->dev, "TPM savestate took %dms\n",
775 try * TPM_TIMEOUT_RETRY);
776
777 tpm_buf_destroy(&buf);
778
779 return rc;
780}
781
diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
index 3acf4fd4e5a5..a6bec13afa69 100644
--- a/drivers/char/tpm/tpm2-cmd.c
+++ b/drivers/char/tpm/tpm2-cmd.c
@@ -40,129 +40,121 @@ static struct tpm2_hash tpm2_hash_map[] = {
40 {HASH_ALGO_SM3_256, TPM2_ALG_SM3_256}, 40 {HASH_ALGO_SM3_256, TPM2_ALG_SM3_256},
41}; 41};
42 42
43/* 43int tpm2_get_timeouts(struct tpm_chip *chip)
44 * Array with one entry per ordinal defining the maximum amount 44{
45 * of time the chip could take to return the result. The values 45 /* Fixed timeouts for TPM2 */
46 * of the SHORT, MEDIUM, and LONG durations are taken from the 46 chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
47 * PC Client Profile (PTP) specification. 47 chip->timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
48 * LONG_LONG is for commands that generates keys which empirically 48 chip->timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
49 * takes longer time on some systems. 49 chip->timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
50
51 /* PTP spec timeouts */
52 chip->duration[TPM_SHORT] = msecs_to_jiffies(TPM2_DURATION_SHORT);
53 chip->duration[TPM_MEDIUM] = msecs_to_jiffies(TPM2_DURATION_MEDIUM);
54 chip->duration[TPM_LONG] = msecs_to_jiffies(TPM2_DURATION_LONG);
55
56 /* Key creation commands long timeouts */
57 chip->duration[TPM_LONG_LONG] =
58 msecs_to_jiffies(TPM2_DURATION_LONG_LONG);
59
60 chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
61
62 return 0;
63}
64
65/**
66 * tpm2_ordinal_duration_index() - returns an index to the chip duration table
67 * @ordinal: TPM command ordinal.
68 *
69 * The function returns an index to the chip duration table
70 * (enum tpm_duration), that describes the maximum amount of
71 * time the chip could take to return the result for a particular ordinal.
72 *
73 * The values of the MEDIUM, and LONG durations are taken
74 * from the PC Client Profile (PTP) specification (750, 2000 msec)
75 *
76 * LONG_LONG is for commands that generates keys which empirically takes
77 * a longer time on some systems.
78 *
79 * Return:
80 * * TPM_MEDIUM
81 * * TPM_LONG
82 * * TPM_LONG_LONG
83 * * TPM_UNDEFINED
50 */ 84 */
51static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = { 85static u8 tpm2_ordinal_duration_index(u32 ordinal)
52 TPM_UNDEFINED, /* 11F */ 86{
53 TPM_UNDEFINED, /* 120 */ 87 switch (ordinal) {
54 TPM_LONG, /* 121 */ 88 /* Startup */
55 TPM_UNDEFINED, /* 122 */ 89 case TPM2_CC_STARTUP: /* 144 */
56 TPM_UNDEFINED, /* 123 */ 90 return TPM_MEDIUM;
57 TPM_UNDEFINED, /* 124 */ 91
58 TPM_UNDEFINED, /* 125 */ 92 case TPM2_CC_SELF_TEST: /* 143 */
59 TPM_UNDEFINED, /* 126 */ 93 return TPM_LONG;
60 TPM_UNDEFINED, /* 127 */ 94
61 TPM_UNDEFINED, /* 128 */ 95 case TPM2_CC_GET_RANDOM: /* 17B */
62 TPM_LONG, /* 129 */ 96 return TPM_LONG;
63 TPM_UNDEFINED, /* 12a */ 97
64 TPM_UNDEFINED, /* 12b */ 98 case TPM2_CC_SEQUENCE_UPDATE: /* 15C */
65 TPM_UNDEFINED, /* 12c */ 99 return TPM_MEDIUM;
66 TPM_UNDEFINED, /* 12d */ 100 case TPM2_CC_SEQUENCE_COMPLETE: /* 13E */
67 TPM_UNDEFINED, /* 12e */ 101 return TPM_MEDIUM;
68 TPM_UNDEFINED, /* 12f */ 102 case TPM2_CC_EVENT_SEQUENCE_COMPLETE: /* 185 */
69 TPM_UNDEFINED, /* 130 */ 103 return TPM_MEDIUM;
70 TPM_LONG_LONG, /* 131 */ 104 case TPM2_CC_HASH_SEQUENCE_START: /* 186 */
71 TPM_UNDEFINED, /* 132 */ 105 return TPM_MEDIUM;
72 TPM_UNDEFINED, /* 133 */ 106
73 TPM_UNDEFINED, /* 134 */ 107 case TPM2_CC_VERIFY_SIGNATURE: /* 177 */
74 TPM_UNDEFINED, /* 135 */ 108 return TPM_LONG;
75 TPM_UNDEFINED, /* 136 */ 109
76 TPM_UNDEFINED, /* 137 */ 110 case TPM2_CC_PCR_EXTEND: /* 182 */
77 TPM_UNDEFINED, /* 138 */ 111 return TPM_MEDIUM;
78 TPM_UNDEFINED, /* 139 */ 112
79 TPM_UNDEFINED, /* 13a */ 113 case TPM2_CC_HIERARCHY_CONTROL: /* 121 */
80 TPM_UNDEFINED, /* 13b */ 114 return TPM_LONG;
81 TPM_UNDEFINED, /* 13c */ 115 case TPM2_CC_HIERARCHY_CHANGE_AUTH: /* 129 */
82 TPM_UNDEFINED, /* 13d */ 116 return TPM_LONG;
83 TPM_MEDIUM, /* 13e */ 117
84 TPM_UNDEFINED, /* 13f */ 118 case TPM2_CC_GET_CAPABILITY: /* 17A */
85 TPM_UNDEFINED, /* 140 */ 119 return TPM_MEDIUM;
86 TPM_UNDEFINED, /* 141 */ 120
87 TPM_UNDEFINED, /* 142 */ 121 case TPM2_CC_NV_READ: /* 14E */
88 TPM_LONG, /* 143 */ 122 return TPM_LONG;
89 TPM_MEDIUM, /* 144 */ 123
90 TPM_UNDEFINED, /* 145 */ 124 case TPM2_CC_CREATE_PRIMARY: /* 131 */
91 TPM_UNDEFINED, /* 146 */ 125 return TPM_LONG_LONG;
92 TPM_UNDEFINED, /* 147 */ 126 case TPM2_CC_CREATE: /* 153 */
93 TPM_UNDEFINED, /* 148 */ 127 return TPM_LONG_LONG;
94 TPM_UNDEFINED, /* 149 */ 128 case TPM2_CC_CREATE_LOADED: /* 191 */
95 TPM_UNDEFINED, /* 14a */ 129 return TPM_LONG_LONG;
96 TPM_UNDEFINED, /* 14b */ 130
97 TPM_UNDEFINED, /* 14c */ 131 default:
98 TPM_UNDEFINED, /* 14d */ 132 return TPM_UNDEFINED;
99 TPM_LONG, /* 14e */ 133 }
100 TPM_UNDEFINED, /* 14f */ 134}
101 TPM_UNDEFINED, /* 150 */ 135
102 TPM_UNDEFINED, /* 151 */ 136/**
103 TPM_UNDEFINED, /* 152 */ 137 * tpm2_calc_ordinal_duration() - calculate the maximum command duration
104 TPM_LONG_LONG, /* 153 */ 138 * @chip: TPM chip to use.
105 TPM_UNDEFINED, /* 154 */ 139 * @ordinal: TPM command ordinal.
106 TPM_UNDEFINED, /* 155 */ 140 *
107 TPM_UNDEFINED, /* 156 */ 141 * The function returns the maximum amount of time the chip could take
108 TPM_UNDEFINED, /* 157 */ 142 * to return the result for a particular ordinal in jiffies.
109 TPM_UNDEFINED, /* 158 */ 143 *
110 TPM_UNDEFINED, /* 159 */ 144 * Return: A maximal duration time for an ordinal in jiffies.
111 TPM_UNDEFINED, /* 15a */ 145 */
112 TPM_UNDEFINED, /* 15b */ 146unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
113 TPM_MEDIUM, /* 15c */ 147{
114 TPM_UNDEFINED, /* 15d */ 148 unsigned int index;
115 TPM_UNDEFINED, /* 15e */ 149
116 TPM_UNDEFINED, /* 15f */ 150 index = tpm2_ordinal_duration_index(ordinal);
117 TPM_UNDEFINED, /* 160 */ 151
118 TPM_UNDEFINED, /* 161 */ 152 if (index != TPM_UNDEFINED)
119 TPM_UNDEFINED, /* 162 */ 153 return chip->duration[index];
120 TPM_UNDEFINED, /* 163 */ 154 else
121 TPM_UNDEFINED, /* 164 */ 155 return msecs_to_jiffies(TPM2_DURATION_DEFAULT);
122 TPM_UNDEFINED, /* 165 */ 156}
123 TPM_UNDEFINED, /* 166 */ 157
124 TPM_UNDEFINED, /* 167 */
125 TPM_UNDEFINED, /* 168 */
126 TPM_UNDEFINED, /* 169 */
127 TPM_UNDEFINED, /* 16a */
128 TPM_UNDEFINED, /* 16b */
129 TPM_UNDEFINED, /* 16c */
130 TPM_UNDEFINED, /* 16d */
131 TPM_UNDEFINED, /* 16e */
132 TPM_UNDEFINED, /* 16f */
133 TPM_UNDEFINED, /* 170 */
134 TPM_UNDEFINED, /* 171 */
135 TPM_UNDEFINED, /* 172 */
136 TPM_UNDEFINED, /* 173 */
137 TPM_UNDEFINED, /* 174 */
138 TPM_UNDEFINED, /* 175 */
139 TPM_UNDEFINED, /* 176 */
140 TPM_LONG, /* 177 */
141 TPM_UNDEFINED, /* 178 */
142 TPM_UNDEFINED, /* 179 */
143 TPM_MEDIUM, /* 17a */
144 TPM_LONG, /* 17b */
145 TPM_UNDEFINED, /* 17c */
146 TPM_UNDEFINED, /* 17d */
147 TPM_UNDEFINED, /* 17e */
148 TPM_UNDEFINED, /* 17f */
149 TPM_UNDEFINED, /* 180 */
150 TPM_UNDEFINED, /* 181 */
151 TPM_MEDIUM, /* 182 */
152 TPM_UNDEFINED, /* 183 */
153 TPM_UNDEFINED, /* 184 */
154 TPM_MEDIUM, /* 185 */
155 TPM_MEDIUM, /* 186 */
156 TPM_UNDEFINED, /* 187 */
157 TPM_UNDEFINED, /* 188 */
158 TPM_UNDEFINED, /* 189 */
159 TPM_UNDEFINED, /* 18a */
160 TPM_UNDEFINED, /* 18b */
161 TPM_UNDEFINED, /* 18c */
162 TPM_UNDEFINED, /* 18d */
163 TPM_UNDEFINED, /* 18e */
164 TPM_UNDEFINED /* 18f */
165};
166 158
167struct tpm2_pcr_read_out { 159struct tpm2_pcr_read_out {
168 __be32 update_cnt; 160 __be32 update_cnt;
@@ -183,7 +175,7 @@ struct tpm2_pcr_read_out {
183 * 175 *
184 * Return: Same as with tpm_transmit_cmd. 176 * Return: Same as with tpm_transmit_cmd.
185 */ 177 */
186int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) 178int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
187{ 179{
188 int rc; 180 int rc;
189 struct tpm_buf buf; 181 struct tpm_buf buf;
@@ -233,7 +225,7 @@ struct tpm2_null_auth_area {
233 * 225 *
234 * Return: Same as with tpm_transmit_cmd. 226 * Return: Same as with tpm_transmit_cmd.
235 */ 227 */
236int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count, 228int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, u32 count,
237 struct tpm2_digest *digests) 229 struct tpm2_digest *digests)
238{ 230{
239 struct tpm_buf buf; 231 struct tpm_buf buf;
@@ -280,7 +272,6 @@ int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
280 return rc; 272 return rc;
281} 273}
282 274
283
284struct tpm2_get_random_out { 275struct tpm2_get_random_out {
285 __be16 size; 276 __be16 size;
286 u8 buffer[TPM_MAX_RNG_DATA]; 277 u8 buffer[TPM_MAX_RNG_DATA];
@@ -351,11 +342,10 @@ out:
351 342
352/** 343/**
353 * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command 344 * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
354 * @chip: TPM chip to use 345 * @chip: TPM chip to use
355 * @payload: the key data in clear and encrypted form 346 * @handle: context handle
356 * @options: authentication values and other options 347 * @flags: tpm transmit flags - bitmap
357 * 348 *
358 * Return: same as with tpm_transmit_cmd
359 */ 349 */
360void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle, 350void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
361 unsigned int flags) 351 unsigned int flags)
@@ -748,32 +738,6 @@ void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
748 tpm_buf_destroy(&buf); 738 tpm_buf_destroy(&buf);
749} 739}
750 740
751/*
752 * tpm2_calc_ordinal_duration() - maximum duration for a command
753 *
754 * @chip: TPM chip to use.
755 * @ordinal: command code number.
756 *
757 * Return: maximum duration for a command
758 */
759unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
760{
761 int index = TPM_UNDEFINED;
762 int duration = 0;
763
764 if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST)
765 index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST];
766
767 if (index != TPM_UNDEFINED)
768 duration = chip->duration[index];
769
770 if (duration <= 0)
771 duration = msecs_to_jiffies(TPM2_DURATION_DEFAULT);
772
773 return duration;
774}
775EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration);
776
777/** 741/**
778 * tpm2_do_selftest() - ensure that all self tests have passed 742 * tpm2_do_selftest() - ensure that all self tests have passed
779 * 743 *
@@ -983,6 +947,36 @@ out:
983} 947}
984 948
985/** 949/**
950 * tpm2_startup - turn on the TPM
951 * @chip: TPM chip to use
952 *
953 * Normally the firmware should start the TPM. This function is provided as a
954 * workaround if this does not happen. A legal case for this could be for
955 * example when a TPM emulator is used.
956 *
957 * Return: same as tpm_transmit_cmd()
958 */
959
960static int tpm2_startup(struct tpm_chip *chip)
961{
962 struct tpm_buf buf;
963 int rc;
964
965 dev_info(&chip->dev, "starting up the TPM manually\n");
966
967 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP);
968 if (rc < 0)
969 return rc;
970
971 tpm_buf_append_u16(&buf, TPM2_SU_CLEAR);
972 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
973 "attempting to start the TPM");
974 tpm_buf_destroy(&buf);
975
976 return rc;
977}
978
979/**
986 * tpm2_auto_startup - Perform the standard automatic TPM initialization 980 * tpm2_auto_startup - Perform the standard automatic TPM initialization
987 * sequence 981 * sequence
988 * @chip: TPM chip to use 982 * @chip: TPM chip to use
@@ -993,7 +987,7 @@ int tpm2_auto_startup(struct tpm_chip *chip)
993{ 987{
994 int rc; 988 int rc;
995 989
996 rc = tpm_get_timeouts(chip); 990 rc = tpm2_get_timeouts(chip);
997 if (rc) 991 if (rc)
998 goto out; 992 goto out;
999 993
@@ -1002,7 +996,7 @@ int tpm2_auto_startup(struct tpm_chip *chip)
1002 goto out; 996 goto out;
1003 997
1004 if (rc == TPM2_RC_INITIALIZE) { 998 if (rc == TPM2_RC_INITIALIZE) {
1005 rc = tpm_startup(chip); 999 rc = tpm2_startup(chip);
1006 if (rc) 1000 if (rc)
1007 goto out; 1001 goto out;
1008 1002
diff --git a/drivers/char/tpm/tpm2-space.c b/drivers/char/tpm/tpm2-space.c
index d2e101b32482..dcdfde3c253e 100644
--- a/drivers/char/tpm/tpm2-space.c
+++ b/drivers/char/tpm/tpm2-space.c
@@ -373,7 +373,7 @@ static int tpm2_map_response_header(struct tpm_chip *chip, u32 cc, u8 *rsp,
373 dev_err(&chip->dev, "%s: unknown handle 0x%08X\n", 373 dev_err(&chip->dev, "%s: unknown handle 0x%08X\n",
374 __func__, phandle); 374 __func__, phandle);
375 break; 375 break;
376 }; 376 }
377 377
378 return 0; 378 return 0;
379out_no_slots: 379out_no_slots:
diff --git a/drivers/char/tpm/tpm_i2c_nuvoton.c b/drivers/char/tpm/tpm_i2c_nuvoton.c
index caa86b19c76d..217f7f1cbde8 100644
--- a/drivers/char/tpm/tpm_i2c_nuvoton.c
+++ b/drivers/char/tpm/tpm_i2c_nuvoton.c
@@ -369,6 +369,7 @@ static int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len)
369 struct device *dev = chip->dev.parent; 369 struct device *dev = chip->dev.parent;
370 struct i2c_client *client = to_i2c_client(dev); 370 struct i2c_client *client = to_i2c_client(dev);
371 u32 ordinal; 371 u32 ordinal;
372 unsigned long duration;
372 size_t count = 0; 373 size_t count = 0;
373 int burst_count, bytes2write, retries, rc = -EIO; 374 int burst_count, bytes2write, retries, rc = -EIO;
374 375
@@ -455,12 +456,12 @@ static int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len)
455 return rc; 456 return rc;
456 } 457 }
457 ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); 458 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
458 rc = i2c_nuvoton_wait_for_data_avail(chip, 459 duration = tpm_calc_ordinal_duration(chip, ordinal);
459 tpm_calc_ordinal_duration(chip, 460
460 ordinal), 461 rc = i2c_nuvoton_wait_for_data_avail(chip, duration, &priv->read_queue);
461 &priv->read_queue);
462 if (rc) { 462 if (rc) {
463 dev_err(dev, "%s() timeout command duration\n", __func__); 463 dev_err(dev, "%s() timeout command duration %ld\n",
464 __func__, duration);
464 i2c_nuvoton_ready(chip); 465 i2c_nuvoton_ready(chip);
465 return rc; 466 return rc;
466 } 467 }
diff --git a/drivers/char/tpm/tpm_ibmvtpm.c b/drivers/char/tpm/tpm_ibmvtpm.c
index 25f6e2665385..07b5a487d0c8 100644
--- a/drivers/char/tpm/tpm_ibmvtpm.c
+++ b/drivers/char/tpm/tpm_ibmvtpm.c
@@ -39,8 +39,7 @@ static const struct vio_device_id tpm_ibmvtpm_device_table[] = {
39MODULE_DEVICE_TABLE(vio, tpm_ibmvtpm_device_table); 39MODULE_DEVICE_TABLE(vio, tpm_ibmvtpm_device_table);
40 40
41/** 41/**
42 * 42 * ibmvtpm_send_crq_word() - Send a CRQ request
43 * ibmvtpm_send_crq_word - Send a CRQ request
44 * @vdev: vio device struct 43 * @vdev: vio device struct
45 * @w1: pre-constructed first word of tpm crq (second word is reserved) 44 * @w1: pre-constructed first word of tpm crq (second word is reserved)
46 * 45 *
@@ -54,8 +53,7 @@ static int ibmvtpm_send_crq_word(struct vio_dev *vdev, u64 w1)
54} 53}
55 54
56/** 55/**
57 * 56 * ibmvtpm_send_crq() - Send a CRQ request
58 * ibmvtpm_send_crq - Send a CRQ request
59 * 57 *
60 * @vdev: vio device struct 58 * @vdev: vio device struct
61 * @valid: Valid field 59 * @valid: Valid field
diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c
index d2345d9fd7b5..bf7e49cfa643 100644
--- a/drivers/char/tpm/tpm_tis_core.c
+++ b/drivers/char/tpm/tpm_tis_core.c
@@ -473,11 +473,7 @@ static int tpm_tis_send_main(struct tpm_chip *chip, const u8 *buf, size_t len)
473 if (chip->flags & TPM_CHIP_FLAG_IRQ) { 473 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
474 ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); 474 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
475 475
476 if (chip->flags & TPM_CHIP_FLAG_TPM2) 476 dur = tpm_calc_ordinal_duration(chip, ordinal);
477 dur = tpm2_calc_ordinal_duration(chip, ordinal);
478 else
479 dur = tpm_calc_ordinal_duration(chip, ordinal);
480
481 if (wait_for_tpm_stat 477 if (wait_for_tpm_stat
482 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur, 478 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
483 &priv->read_queue, false) < 0) { 479 &priv->read_queue, false) < 0) {
@@ -668,7 +664,7 @@ static int tpm_tis_gen_interrupt(struct tpm_chip *chip)
668 if (chip->flags & TPM_CHIP_FLAG_TPM2) 664 if (chip->flags & TPM_CHIP_FLAG_TPM2)
669 return tpm2_get_tpm_pt(chip, 0x100, &cap2, desc); 665 return tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
670 else 666 else
671 return tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc, 667 return tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc,
672 0); 668 0);
673} 669}
674 670
@@ -1060,7 +1056,7 @@ int tpm_tis_resume(struct device *dev)
1060 * an error code but for unknown reason it isn't handled. 1056 * an error code but for unknown reason it isn't handled.
1061 */ 1057 */
1062 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) 1058 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
1063 tpm_do_selftest(chip); 1059 tpm1_do_selftest(chip);
1064 1060
1065 return 0; 1061 return 0;
1066} 1062}
diff --git a/include/linux/tpm.h b/include/linux/tpm.h
index 4609b94142d4..b49a55cf775f 100644
--- a/include/linux/tpm.h
+++ b/include/linux/tpm.h
@@ -53,8 +53,8 @@ struct tpm_class_ops {
53#if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE) 53#if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE)
54 54
55extern int tpm_is_tpm2(struct tpm_chip *chip); 55extern int tpm_is_tpm2(struct tpm_chip *chip);
56extern int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf); 56extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
57extern int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash); 57extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash);
58extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen); 58extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);
59extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max); 59extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
60extern int tpm_seal_trusted(struct tpm_chip *chip, 60extern int tpm_seal_trusted(struct tpm_chip *chip,
@@ -69,15 +69,18 @@ static inline int tpm_is_tpm2(struct tpm_chip *chip)
69{ 69{
70 return -ENODEV; 70 return -ENODEV;
71} 71}
72static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) 72
73static inline int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
73{ 74{
74 return -ENODEV; 75 return -ENODEV;
75} 76}
76static inline int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, 77
78static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
77 const u8 *hash) 79 const u8 *hash)
78{ 80{
79 return -ENODEV; 81 return -ENODEV;
80} 82}
83
81static inline int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen) 84static inline int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
82{ 85{
83 return -ENODEV; 86 return -ENODEV;
diff --git a/security/integrity/ima/ima_crypto.c b/security/integrity/ima/ima_crypto.c
index d9e7728027c6..acf2c7df7145 100644
--- a/security/integrity/ima/ima_crypto.c
+++ b/security/integrity/ima/ima_crypto.c
@@ -643,7 +643,7 @@ int ima_calc_buffer_hash(const void *buf, loff_t len,
643 return calc_buffer_shash(buf, len, hash); 643 return calc_buffer_shash(buf, len, hash);
644} 644}
645 645
646static void __init ima_pcrread(int idx, u8 *pcr) 646static void __init ima_pcrread(u32 idx, u8 *pcr)
647{ 647{
648 if (!ima_tpm_chip) 648 if (!ima_tpm_chip)
649 return; 649 return;
@@ -659,7 +659,8 @@ static int __init ima_calc_boot_aggregate_tfm(char *digest,
659 struct crypto_shash *tfm) 659 struct crypto_shash *tfm)
660{ 660{
661 u8 pcr_i[TPM_DIGEST_SIZE]; 661 u8 pcr_i[TPM_DIGEST_SIZE];
662 int rc, i; 662 int rc;
663 u32 i;
663 SHASH_DESC_ON_STACK(shash, tfm); 664 SHASH_DESC_ON_STACK(shash, tfm);
664 665
665 shash->tfm = tfm; 666 shash->tfm = tfm;