xref: /openbmc/linux/drivers/char/tpm/tpm1-cmd.c (revision 9fb29c73)
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  */
35 static 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  */
291 unsigned 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  */
324 static 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 
345 int 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
452 int 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
473 ssize_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 }
505 EXPORT_SYMBOL_GPL(tpm1_getcap);
506 
507 #define TPM_ORD_GET_RANDOM 70
508 struct 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  */
523 int 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;
569 out:
570 	tpm_buf_destroy(&buf);
571 	return rc;
572 }
573 
574 #define TPM_ORD_PCRREAD 21
575 int 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 
599 out:
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  */
612 static 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  */
637 int 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 }
690 EXPORT_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  */
699 int 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;
713 out:
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  */
732 int 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 
782