18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2004 IBM Corporation
48c2ecf20Sopenharmony_ci * Copyright (C) 2014 Intel Corporation
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Authors:
78c2ecf20Sopenharmony_ci * Leendert van Doorn <leendert@watson.ibm.com>
88c2ecf20Sopenharmony_ci * Dave Safford <safford@watson.ibm.com>
98c2ecf20Sopenharmony_ci * Reiner Sailer <sailer@watson.ibm.com>
108c2ecf20Sopenharmony_ci * Kylene Hall <kjhall@us.ibm.com>
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Device driver for TCG/TCPA TPM (trusted platform module).
138c2ecf20Sopenharmony_ci * Specifications at www.trustedcomputinggroup.org
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/poll.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci#include <linux/mutex.h>
198c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
208c2ecf20Sopenharmony_ci#include <linux/freezer.h>
218c2ecf20Sopenharmony_ci#include <linux/tpm_eventlog.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include "tpm.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define TPM_MAX_ORDINAL 243
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/*
288c2ecf20Sopenharmony_ci * Array with one entry per ordinal defining the maximum amount
298c2ecf20Sopenharmony_ci * of time the chip could take to return the result.  The ordinal
308c2ecf20Sopenharmony_ci * designation of short, medium or long is defined in a table in
318c2ecf20Sopenharmony_ci * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
328c2ecf20Sopenharmony_ci * values of the SHORT, MEDIUM, and LONG durations are retrieved
338c2ecf20Sopenharmony_ci * from the chip during initialization with a call to tpm_get_timeouts.
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_cistatic const u8 tpm1_ordinal_duration[TPM_MAX_ORDINAL] = {
368c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 0 */
378c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
388c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
398c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
408c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
418c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 5 */
428c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
438c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
448c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
458c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
468c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 10 */
478c2ecf20Sopenharmony_ci	TPM_SHORT,
488c2ecf20Sopenharmony_ci	TPM_MEDIUM,
498c2ecf20Sopenharmony_ci	TPM_LONG,
508c2ecf20Sopenharmony_ci	TPM_LONG,
518c2ecf20Sopenharmony_ci	TPM_MEDIUM,		/* 15 */
528c2ecf20Sopenharmony_ci	TPM_SHORT,
538c2ecf20Sopenharmony_ci	TPM_SHORT,
548c2ecf20Sopenharmony_ci	TPM_MEDIUM,
558c2ecf20Sopenharmony_ci	TPM_LONG,
568c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 20 */
578c2ecf20Sopenharmony_ci	TPM_SHORT,
588c2ecf20Sopenharmony_ci	TPM_MEDIUM,
598c2ecf20Sopenharmony_ci	TPM_MEDIUM,
608c2ecf20Sopenharmony_ci	TPM_MEDIUM,
618c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 25 */
628c2ecf20Sopenharmony_ci	TPM_SHORT,
638c2ecf20Sopenharmony_ci	TPM_MEDIUM,
648c2ecf20Sopenharmony_ci	TPM_SHORT,
658c2ecf20Sopenharmony_ci	TPM_SHORT,
668c2ecf20Sopenharmony_ci	TPM_MEDIUM,		/* 30 */
678c2ecf20Sopenharmony_ci	TPM_LONG,
688c2ecf20Sopenharmony_ci	TPM_MEDIUM,
698c2ecf20Sopenharmony_ci	TPM_SHORT,
708c2ecf20Sopenharmony_ci	TPM_SHORT,
718c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 35 */
728c2ecf20Sopenharmony_ci	TPM_MEDIUM,
738c2ecf20Sopenharmony_ci	TPM_MEDIUM,
748c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
758c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
768c2ecf20Sopenharmony_ci	TPM_MEDIUM,		/* 40 */
778c2ecf20Sopenharmony_ci	TPM_LONG,
788c2ecf20Sopenharmony_ci	TPM_MEDIUM,
798c2ecf20Sopenharmony_ci	TPM_SHORT,
808c2ecf20Sopenharmony_ci	TPM_SHORT,
818c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 45 */
828c2ecf20Sopenharmony_ci	TPM_SHORT,
838c2ecf20Sopenharmony_ci	TPM_SHORT,
848c2ecf20Sopenharmony_ci	TPM_SHORT,
858c2ecf20Sopenharmony_ci	TPM_LONG,
868c2ecf20Sopenharmony_ci	TPM_MEDIUM,		/* 50 */
878c2ecf20Sopenharmony_ci	TPM_MEDIUM,
888c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
898c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
908c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
918c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 55 */
928c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
938c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
948c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
958c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
968c2ecf20Sopenharmony_ci	TPM_MEDIUM,		/* 60 */
978c2ecf20Sopenharmony_ci	TPM_MEDIUM,
988c2ecf20Sopenharmony_ci	TPM_MEDIUM,
998c2ecf20Sopenharmony_ci	TPM_SHORT,
1008c2ecf20Sopenharmony_ci	TPM_SHORT,
1018c2ecf20Sopenharmony_ci	TPM_MEDIUM,		/* 65 */
1028c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1038c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1048c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1058c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1068c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 70 */
1078c2ecf20Sopenharmony_ci	TPM_SHORT,
1088c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1098c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1108c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1118c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 75 */
1128c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1138c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1148c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1158c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1168c2ecf20Sopenharmony_ci	TPM_LONG,		/* 80 */
1178c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1188c2ecf20Sopenharmony_ci	TPM_MEDIUM,
1198c2ecf20Sopenharmony_ci	TPM_LONG,
1208c2ecf20Sopenharmony_ci	TPM_SHORT,
1218c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 85 */
1228c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1238c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1248c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1258c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1268c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 90 */
1278c2ecf20Sopenharmony_ci	TPM_SHORT,
1288c2ecf20Sopenharmony_ci	TPM_SHORT,
1298c2ecf20Sopenharmony_ci	TPM_SHORT,
1308c2ecf20Sopenharmony_ci	TPM_SHORT,
1318c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 95 */
1328c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1338c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1348c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1358c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1368c2ecf20Sopenharmony_ci	TPM_MEDIUM,		/* 100 */
1378c2ecf20Sopenharmony_ci	TPM_SHORT,
1388c2ecf20Sopenharmony_ci	TPM_SHORT,
1398c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1408c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1418c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 105 */
1428c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1438c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1448c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1458c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1468c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 110 */
1478c2ecf20Sopenharmony_ci	TPM_SHORT,
1488c2ecf20Sopenharmony_ci	TPM_SHORT,
1498c2ecf20Sopenharmony_ci	TPM_SHORT,
1508c2ecf20Sopenharmony_ci	TPM_SHORT,
1518c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 115 */
1528c2ecf20Sopenharmony_ci	TPM_SHORT,
1538c2ecf20Sopenharmony_ci	TPM_SHORT,
1548c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1558c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1568c2ecf20Sopenharmony_ci	TPM_LONG,		/* 120 */
1578c2ecf20Sopenharmony_ci	TPM_LONG,
1588c2ecf20Sopenharmony_ci	TPM_MEDIUM,
1598c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1608c2ecf20Sopenharmony_ci	TPM_SHORT,
1618c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 125 */
1628c2ecf20Sopenharmony_ci	TPM_SHORT,
1638c2ecf20Sopenharmony_ci	TPM_LONG,
1648c2ecf20Sopenharmony_ci	TPM_SHORT,
1658c2ecf20Sopenharmony_ci	TPM_SHORT,
1668c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 130 */
1678c2ecf20Sopenharmony_ci	TPM_MEDIUM,
1688c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1698c2ecf20Sopenharmony_ci	TPM_SHORT,
1708c2ecf20Sopenharmony_ci	TPM_MEDIUM,
1718c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 135 */
1728c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1738c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1748c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1758c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1768c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 140 */
1778c2ecf20Sopenharmony_ci	TPM_SHORT,
1788c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1798c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1808c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1818c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 145 */
1828c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1838c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1848c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1858c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1868c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 150 */
1878c2ecf20Sopenharmony_ci	TPM_MEDIUM,
1888c2ecf20Sopenharmony_ci	TPM_MEDIUM,
1898c2ecf20Sopenharmony_ci	TPM_SHORT,
1908c2ecf20Sopenharmony_ci	TPM_SHORT,
1918c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 155 */
1928c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1938c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1948c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1958c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
1968c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 160 */
1978c2ecf20Sopenharmony_ci	TPM_SHORT,
1988c2ecf20Sopenharmony_ci	TPM_SHORT,
1998c2ecf20Sopenharmony_ci	TPM_SHORT,
2008c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2018c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 165 */
2028c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2038c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2048c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2058c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2068c2ecf20Sopenharmony_ci	TPM_LONG,		/* 170 */
2078c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2088c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2098c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2108c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2118c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 175 */
2128c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2138c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2148c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2158c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2168c2ecf20Sopenharmony_ci	TPM_MEDIUM,		/* 180 */
2178c2ecf20Sopenharmony_ci	TPM_SHORT,
2188c2ecf20Sopenharmony_ci	TPM_MEDIUM,
2198c2ecf20Sopenharmony_ci	TPM_MEDIUM,
2208c2ecf20Sopenharmony_ci	TPM_MEDIUM,
2218c2ecf20Sopenharmony_ci	TPM_MEDIUM,		/* 185 */
2228c2ecf20Sopenharmony_ci	TPM_SHORT,
2238c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2248c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2258c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2268c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 190 */
2278c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2288c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2298c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2308c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2318c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 195 */
2328c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2338c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2348c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2358c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2368c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 200 */
2378c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2388c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2398c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2408c2ecf20Sopenharmony_ci	TPM_SHORT,
2418c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 205 */
2428c2ecf20Sopenharmony_ci	TPM_SHORT,
2438c2ecf20Sopenharmony_ci	TPM_SHORT,
2448c2ecf20Sopenharmony_ci	TPM_SHORT,
2458c2ecf20Sopenharmony_ci	TPM_SHORT,
2468c2ecf20Sopenharmony_ci	TPM_MEDIUM,		/* 210 */
2478c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2488c2ecf20Sopenharmony_ci	TPM_MEDIUM,
2498c2ecf20Sopenharmony_ci	TPM_MEDIUM,
2508c2ecf20Sopenharmony_ci	TPM_MEDIUM,
2518c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 215 */
2528c2ecf20Sopenharmony_ci	TPM_MEDIUM,
2538c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2548c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2558c2ecf20Sopenharmony_ci	TPM_SHORT,
2568c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 220 */
2578c2ecf20Sopenharmony_ci	TPM_SHORT,
2588c2ecf20Sopenharmony_ci	TPM_SHORT,
2598c2ecf20Sopenharmony_ci	TPM_SHORT,
2608c2ecf20Sopenharmony_ci	TPM_SHORT,
2618c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 225 */
2628c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2638c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2648c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2658c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2668c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 230 */
2678c2ecf20Sopenharmony_ci	TPM_LONG,
2688c2ecf20Sopenharmony_ci	TPM_MEDIUM,
2698c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2708c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2718c2ecf20Sopenharmony_ci	TPM_UNDEFINED,		/* 235 */
2728c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2738c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2748c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2758c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2768c2ecf20Sopenharmony_ci	TPM_SHORT,		/* 240 */
2778c2ecf20Sopenharmony_ci	TPM_UNDEFINED,
2788c2ecf20Sopenharmony_ci	TPM_MEDIUM,
2798c2ecf20Sopenharmony_ci};
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci/**
2828c2ecf20Sopenharmony_ci * tpm1_calc_ordinal_duration() - calculate the maximum command duration
2838c2ecf20Sopenharmony_ci * @chip:    TPM chip to use.
2848c2ecf20Sopenharmony_ci * @ordinal: TPM command ordinal.
2858c2ecf20Sopenharmony_ci *
2868c2ecf20Sopenharmony_ci * The function returns the maximum amount of time the chip could take
2878c2ecf20Sopenharmony_ci * to return the result for a particular ordinal in jiffies.
2888c2ecf20Sopenharmony_ci *
2898c2ecf20Sopenharmony_ci * Return: A maximal duration time for an ordinal in jiffies.
2908c2ecf20Sopenharmony_ci */
2918c2ecf20Sopenharmony_ciunsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	int duration_idx = TPM_UNDEFINED;
2948c2ecf20Sopenharmony_ci	int duration = 0;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	/*
2978c2ecf20Sopenharmony_ci	 * We only have a duration table for protected commands, where the upper
2988c2ecf20Sopenharmony_ci	 * 16 bits are 0. For the few other ordinals the fallback will be used.
2998c2ecf20Sopenharmony_ci	 */
3008c2ecf20Sopenharmony_ci	if (ordinal < TPM_MAX_ORDINAL)
3018c2ecf20Sopenharmony_ci		duration_idx = tpm1_ordinal_duration[ordinal];
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	if (duration_idx != TPM_UNDEFINED)
3048c2ecf20Sopenharmony_ci		duration = chip->duration[duration_idx];
3058c2ecf20Sopenharmony_ci	if (duration <= 0)
3068c2ecf20Sopenharmony_ci		return 2 * 60 * HZ;
3078c2ecf20Sopenharmony_ci	else
3088c2ecf20Sopenharmony_ci		return duration;
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci#define TPM_ORD_STARTUP 153
3128c2ecf20Sopenharmony_ci#define TPM_ST_CLEAR 1
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci/**
3158c2ecf20Sopenharmony_ci * tpm_startup() - turn on the TPM
3168c2ecf20Sopenharmony_ci * @chip: TPM chip to use
3178c2ecf20Sopenharmony_ci *
3188c2ecf20Sopenharmony_ci * Normally the firmware should start the TPM. This function is provided as a
3198c2ecf20Sopenharmony_ci * workaround if this does not happen. A legal case for this could be for
3208c2ecf20Sopenharmony_ci * example when a TPM emulator is used.
3218c2ecf20Sopenharmony_ci *
3228c2ecf20Sopenharmony_ci * Return: same as tpm_transmit_cmd()
3238c2ecf20Sopenharmony_ci */
3248c2ecf20Sopenharmony_cistatic int tpm1_startup(struct tpm_chip *chip)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	struct tpm_buf buf;
3278c2ecf20Sopenharmony_ci	int rc;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	dev_info(&chip->dev, "starting up the TPM manually\n");
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_STARTUP);
3328c2ecf20Sopenharmony_ci	if (rc < 0)
3338c2ecf20Sopenharmony_ci		return rc;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	tpm_buf_append_u16(&buf, TPM_ST_CLEAR);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM");
3388c2ecf20Sopenharmony_ci	tpm_buf_destroy(&buf);
3398c2ecf20Sopenharmony_ci	return rc;
3408c2ecf20Sopenharmony_ci}
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ciint tpm1_get_timeouts(struct tpm_chip *chip)
3438c2ecf20Sopenharmony_ci{
3448c2ecf20Sopenharmony_ci	cap_t cap;
3458c2ecf20Sopenharmony_ci	unsigned long timeout_old[4], timeout_chip[4], timeout_eff[4];
3468c2ecf20Sopenharmony_ci	unsigned long durations[3];
3478c2ecf20Sopenharmony_ci	ssize_t rc;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, NULL,
3508c2ecf20Sopenharmony_ci			 sizeof(cap.timeout));
3518c2ecf20Sopenharmony_ci	if (rc == TPM_ERR_INVALID_POSTINIT) {
3528c2ecf20Sopenharmony_ci		if (tpm1_startup(chip))
3538c2ecf20Sopenharmony_ci			return rc;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci		rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap,
3568c2ecf20Sopenharmony_ci				 "attempting to determine the timeouts",
3578c2ecf20Sopenharmony_ci				 sizeof(cap.timeout));
3588c2ecf20Sopenharmony_ci	}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	if (rc) {
3618c2ecf20Sopenharmony_ci		dev_err(&chip->dev, "A TPM error (%zd) occurred attempting to determine the timeouts\n",
3628c2ecf20Sopenharmony_ci			rc);
3638c2ecf20Sopenharmony_ci		return rc;
3648c2ecf20Sopenharmony_ci	}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	timeout_old[0] = jiffies_to_usecs(chip->timeout_a);
3678c2ecf20Sopenharmony_ci	timeout_old[1] = jiffies_to_usecs(chip->timeout_b);
3688c2ecf20Sopenharmony_ci	timeout_old[2] = jiffies_to_usecs(chip->timeout_c);
3698c2ecf20Sopenharmony_ci	timeout_old[3] = jiffies_to_usecs(chip->timeout_d);
3708c2ecf20Sopenharmony_ci	timeout_chip[0] = be32_to_cpu(cap.timeout.a);
3718c2ecf20Sopenharmony_ci	timeout_chip[1] = be32_to_cpu(cap.timeout.b);
3728c2ecf20Sopenharmony_ci	timeout_chip[2] = be32_to_cpu(cap.timeout.c);
3738c2ecf20Sopenharmony_ci	timeout_chip[3] = be32_to_cpu(cap.timeout.d);
3748c2ecf20Sopenharmony_ci	memcpy(timeout_eff, timeout_chip, sizeof(timeout_eff));
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	/*
3778c2ecf20Sopenharmony_ci	 * Provide ability for vendor overrides of timeout values in case
3788c2ecf20Sopenharmony_ci	 * of misreporting.
3798c2ecf20Sopenharmony_ci	 */
3808c2ecf20Sopenharmony_ci	if (chip->ops->update_timeouts)
3818c2ecf20Sopenharmony_ci		chip->ops->update_timeouts(chip, timeout_eff);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	if (!chip->timeout_adjusted) {
3848c2ecf20Sopenharmony_ci		/* Restore default if chip reported 0 */
3858c2ecf20Sopenharmony_ci		unsigned int i;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(timeout_eff); i++) {
3888c2ecf20Sopenharmony_ci			if (timeout_eff[i])
3898c2ecf20Sopenharmony_ci				continue;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci			timeout_eff[i] = timeout_old[i];
3928c2ecf20Sopenharmony_ci			chip->timeout_adjusted = true;
3938c2ecf20Sopenharmony_ci		}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci		if (timeout_eff[0] != 0 && timeout_eff[0] < 1000) {
3968c2ecf20Sopenharmony_ci			/* timeouts in msec rather usec */
3978c2ecf20Sopenharmony_ci			for (i = 0; i != ARRAY_SIZE(timeout_eff); i++)
3988c2ecf20Sopenharmony_ci				timeout_eff[i] *= 1000;
3998c2ecf20Sopenharmony_ci			chip->timeout_adjusted = true;
4008c2ecf20Sopenharmony_ci		}
4018c2ecf20Sopenharmony_ci	}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	/* Report adjusted timeouts */
4048c2ecf20Sopenharmony_ci	if (chip->timeout_adjusted) {
4058c2ecf20Sopenharmony_ci		dev_info(&chip->dev, HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
4068c2ecf20Sopenharmony_ci			 timeout_chip[0], timeout_eff[0],
4078c2ecf20Sopenharmony_ci			 timeout_chip[1], timeout_eff[1],
4088c2ecf20Sopenharmony_ci			 timeout_chip[2], timeout_eff[2],
4098c2ecf20Sopenharmony_ci			 timeout_chip[3], timeout_eff[3]);
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	chip->timeout_a = usecs_to_jiffies(timeout_eff[0]);
4138c2ecf20Sopenharmony_ci	chip->timeout_b = usecs_to_jiffies(timeout_eff[1]);
4148c2ecf20Sopenharmony_ci	chip->timeout_c = usecs_to_jiffies(timeout_eff[2]);
4158c2ecf20Sopenharmony_ci	chip->timeout_d = usecs_to_jiffies(timeout_eff[3]);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap,
4188c2ecf20Sopenharmony_ci			 "attempting to determine the durations",
4198c2ecf20Sopenharmony_ci			  sizeof(cap.duration));
4208c2ecf20Sopenharmony_ci	if (rc)
4218c2ecf20Sopenharmony_ci		return rc;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	chip->duration[TPM_SHORT] =
4248c2ecf20Sopenharmony_ci		usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short));
4258c2ecf20Sopenharmony_ci	chip->duration[TPM_MEDIUM] =
4268c2ecf20Sopenharmony_ci		usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium));
4278c2ecf20Sopenharmony_ci	chip->duration[TPM_LONG] =
4288c2ecf20Sopenharmony_ci		usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long));
4298c2ecf20Sopenharmony_ci	chip->duration[TPM_LONG_LONG] = 0; /* not used under 1.2 */
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	/*
4328c2ecf20Sopenharmony_ci	 * Provide the ability for vendor overrides of duration values in case
4338c2ecf20Sopenharmony_ci	 * of misreporting.
4348c2ecf20Sopenharmony_ci	 */
4358c2ecf20Sopenharmony_ci	if (chip->ops->update_durations)
4368c2ecf20Sopenharmony_ci		chip->ops->update_durations(chip, durations);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	if (chip->duration_adjusted) {
4398c2ecf20Sopenharmony_ci		dev_info(&chip->dev, HW_ERR "Adjusting reported durations.");
4408c2ecf20Sopenharmony_ci		chip->duration[TPM_SHORT] = durations[0];
4418c2ecf20Sopenharmony_ci		chip->duration[TPM_MEDIUM] = durations[1];
4428c2ecf20Sopenharmony_ci		chip->duration[TPM_LONG] = durations[2];
4438c2ecf20Sopenharmony_ci	}
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	/* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
4468c2ecf20Sopenharmony_ci	 * value wrong and apparently reports msecs rather than usecs. So we
4478c2ecf20Sopenharmony_ci	 * fix up the resulting too-small TPM_SHORT value to make things work.
4488c2ecf20Sopenharmony_ci	 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
4498c2ecf20Sopenharmony_ci	 */
4508c2ecf20Sopenharmony_ci	if (chip->duration[TPM_SHORT] < (HZ / 100)) {
4518c2ecf20Sopenharmony_ci		chip->duration[TPM_SHORT] = HZ;
4528c2ecf20Sopenharmony_ci		chip->duration[TPM_MEDIUM] *= 1000;
4538c2ecf20Sopenharmony_ci		chip->duration[TPM_LONG] *= 1000;
4548c2ecf20Sopenharmony_ci		chip->duration_adjusted = true;
4558c2ecf20Sopenharmony_ci		dev_info(&chip->dev, "Adjusting TPM timeout parameters.");
4568c2ecf20Sopenharmony_ci	}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
4598c2ecf20Sopenharmony_ci	return 0;
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci#define TPM_ORD_PCR_EXTEND 20
4638c2ecf20Sopenharmony_ciint tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
4648c2ecf20Sopenharmony_ci		    const char *log_msg)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	struct tpm_buf buf;
4678c2ecf20Sopenharmony_ci	int rc;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_EXTEND);
4708c2ecf20Sopenharmony_ci	if (rc)
4718c2ecf20Sopenharmony_ci		return rc;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	tpm_buf_append_u32(&buf, pcr_idx);
4748c2ecf20Sopenharmony_ci	tpm_buf_append(&buf, hash, TPM_DIGEST_SIZE);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	rc = tpm_transmit_cmd(chip, &buf, TPM_DIGEST_SIZE, log_msg);
4778c2ecf20Sopenharmony_ci	tpm_buf_destroy(&buf);
4788c2ecf20Sopenharmony_ci	return rc;
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci#define TPM_ORD_GET_CAP 101
4828c2ecf20Sopenharmony_cissize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
4838c2ecf20Sopenharmony_ci		    const char *desc, size_t min_cap_length)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	struct tpm_buf buf;
4868c2ecf20Sopenharmony_ci	int rc;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP);
4898c2ecf20Sopenharmony_ci	if (rc)
4908c2ecf20Sopenharmony_ci		return rc;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	if (subcap_id == TPM_CAP_VERSION_1_1 ||
4938c2ecf20Sopenharmony_ci	    subcap_id == TPM_CAP_VERSION_1_2) {
4948c2ecf20Sopenharmony_ci		tpm_buf_append_u32(&buf, subcap_id);
4958c2ecf20Sopenharmony_ci		tpm_buf_append_u32(&buf, 0);
4968c2ecf20Sopenharmony_ci	} else {
4978c2ecf20Sopenharmony_ci		if (subcap_id == TPM_CAP_FLAG_PERM ||
4988c2ecf20Sopenharmony_ci		    subcap_id == TPM_CAP_FLAG_VOL)
4998c2ecf20Sopenharmony_ci			tpm_buf_append_u32(&buf, TPM_CAP_FLAG);
5008c2ecf20Sopenharmony_ci		else
5018c2ecf20Sopenharmony_ci			tpm_buf_append_u32(&buf, TPM_CAP_PROP);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci		tpm_buf_append_u32(&buf, 4);
5048c2ecf20Sopenharmony_ci		tpm_buf_append_u32(&buf, subcap_id);
5058c2ecf20Sopenharmony_ci	}
5068c2ecf20Sopenharmony_ci	rc = tpm_transmit_cmd(chip, &buf, min_cap_length, desc);
5078c2ecf20Sopenharmony_ci	if (!rc)
5088c2ecf20Sopenharmony_ci		*cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
5098c2ecf20Sopenharmony_ci	tpm_buf_destroy(&buf);
5108c2ecf20Sopenharmony_ci	return rc;
5118c2ecf20Sopenharmony_ci}
5128c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(tpm1_getcap);
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci#define TPM_ORD_GET_RANDOM 70
5158c2ecf20Sopenharmony_cistruct tpm1_get_random_out {
5168c2ecf20Sopenharmony_ci	__be32 rng_data_len;
5178c2ecf20Sopenharmony_ci	u8 rng_data[TPM_MAX_RNG_DATA];
5188c2ecf20Sopenharmony_ci} __packed;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci/**
5218c2ecf20Sopenharmony_ci * tpm1_get_random() - get random bytes from the TPM's RNG
5228c2ecf20Sopenharmony_ci * @chip:	a &struct tpm_chip instance
5238c2ecf20Sopenharmony_ci * @dest:	destination buffer for the random bytes
5248c2ecf20Sopenharmony_ci * @max:	the maximum number of bytes to write to @dest
5258c2ecf20Sopenharmony_ci *
5268c2ecf20Sopenharmony_ci * Return:
5278c2ecf20Sopenharmony_ci * *  number of bytes read
5288c2ecf20Sopenharmony_ci * * -errno (positive TPM return codes are masked to -EIO)
5298c2ecf20Sopenharmony_ci */
5308c2ecf20Sopenharmony_ciint tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
5318c2ecf20Sopenharmony_ci{
5328c2ecf20Sopenharmony_ci	struct tpm1_get_random_out *out;
5338c2ecf20Sopenharmony_ci	u32 num_bytes =  min_t(u32, max, TPM_MAX_RNG_DATA);
5348c2ecf20Sopenharmony_ci	struct tpm_buf buf;
5358c2ecf20Sopenharmony_ci	u32 total = 0;
5368c2ecf20Sopenharmony_ci	int retries = 5;
5378c2ecf20Sopenharmony_ci	u32 recd;
5388c2ecf20Sopenharmony_ci	int rc;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM);
5418c2ecf20Sopenharmony_ci	if (rc)
5428c2ecf20Sopenharmony_ci		return rc;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	do {
5458c2ecf20Sopenharmony_ci		tpm_buf_append_u32(&buf, num_bytes);
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci		rc = tpm_transmit_cmd(chip, &buf, sizeof(out->rng_data_len),
5488c2ecf20Sopenharmony_ci				      "attempting get random");
5498c2ecf20Sopenharmony_ci		if (rc) {
5508c2ecf20Sopenharmony_ci			if (rc > 0)
5518c2ecf20Sopenharmony_ci				rc = -EIO;
5528c2ecf20Sopenharmony_ci			goto out;
5538c2ecf20Sopenharmony_ci		}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci		out = (struct tpm1_get_random_out *)&buf.data[TPM_HEADER_SIZE];
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci		recd = be32_to_cpu(out->rng_data_len);
5588c2ecf20Sopenharmony_ci		if (recd > num_bytes) {
5598c2ecf20Sopenharmony_ci			rc = -EFAULT;
5608c2ecf20Sopenharmony_ci			goto out;
5618c2ecf20Sopenharmony_ci		}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci		if (tpm_buf_length(&buf) < TPM_HEADER_SIZE +
5648c2ecf20Sopenharmony_ci					   sizeof(out->rng_data_len) + recd) {
5658c2ecf20Sopenharmony_ci			rc = -EFAULT;
5668c2ecf20Sopenharmony_ci			goto out;
5678c2ecf20Sopenharmony_ci		}
5688c2ecf20Sopenharmony_ci		memcpy(dest, out->rng_data, recd);
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci		dest += recd;
5718c2ecf20Sopenharmony_ci		total += recd;
5728c2ecf20Sopenharmony_ci		num_bytes -= recd;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci		tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM);
5758c2ecf20Sopenharmony_ci	} while (retries-- && total < max);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	rc = total ? (int)total : -EIO;
5788c2ecf20Sopenharmony_ciout:
5798c2ecf20Sopenharmony_ci	tpm_buf_destroy(&buf);
5808c2ecf20Sopenharmony_ci	return rc;
5818c2ecf20Sopenharmony_ci}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci#define TPM_ORD_PCRREAD 21
5848c2ecf20Sopenharmony_ciint tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
5858c2ecf20Sopenharmony_ci{
5868c2ecf20Sopenharmony_ci	struct tpm_buf buf;
5878c2ecf20Sopenharmony_ci	int rc;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCRREAD);
5908c2ecf20Sopenharmony_ci	if (rc)
5918c2ecf20Sopenharmony_ci		return rc;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	tpm_buf_append_u32(&buf, pcr_idx);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	rc = tpm_transmit_cmd(chip, &buf, TPM_DIGEST_SIZE,
5968c2ecf20Sopenharmony_ci			      "attempting to read a pcr value");
5978c2ecf20Sopenharmony_ci	if (rc)
5988c2ecf20Sopenharmony_ci		goto out;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	if (tpm_buf_length(&buf) < TPM_DIGEST_SIZE) {
6018c2ecf20Sopenharmony_ci		rc = -EFAULT;
6028c2ecf20Sopenharmony_ci		goto out;
6038c2ecf20Sopenharmony_ci	}
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	memcpy(res_buf, &buf.data[TPM_HEADER_SIZE], TPM_DIGEST_SIZE);
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ciout:
6088c2ecf20Sopenharmony_ci	tpm_buf_destroy(&buf);
6098c2ecf20Sopenharmony_ci	return rc;
6108c2ecf20Sopenharmony_ci}
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci#define TPM_ORD_CONTINUE_SELFTEST 83
6138c2ecf20Sopenharmony_ci/**
6148c2ecf20Sopenharmony_ci * tpm_continue_selftest() - run TPM's selftest
6158c2ecf20Sopenharmony_ci * @chip: TPM chip to use
6168c2ecf20Sopenharmony_ci *
6178c2ecf20Sopenharmony_ci * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
6188c2ecf20Sopenharmony_ci * a TPM error code.
6198c2ecf20Sopenharmony_ci */
6208c2ecf20Sopenharmony_cistatic int tpm1_continue_selftest(struct tpm_chip *chip)
6218c2ecf20Sopenharmony_ci{
6228c2ecf20Sopenharmony_ci	struct tpm_buf buf;
6238c2ecf20Sopenharmony_ci	int rc;
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_CONTINUE_SELFTEST);
6268c2ecf20Sopenharmony_ci	if (rc)
6278c2ecf20Sopenharmony_ci		return rc;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	rc = tpm_transmit_cmd(chip, &buf, 0, "continue selftest");
6308c2ecf20Sopenharmony_ci	tpm_buf_destroy(&buf);
6318c2ecf20Sopenharmony_ci	return rc;
6328c2ecf20Sopenharmony_ci}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci/**
6358c2ecf20Sopenharmony_ci * tpm1_do_selftest - have the TPM continue its selftest and wait until it
6368c2ecf20Sopenharmony_ci *                   can receive further commands
6378c2ecf20Sopenharmony_ci * @chip: TPM chip to use
6388c2ecf20Sopenharmony_ci *
6398c2ecf20Sopenharmony_ci * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
6408c2ecf20Sopenharmony_ci * a TPM error code.
6418c2ecf20Sopenharmony_ci */
6428c2ecf20Sopenharmony_ciint tpm1_do_selftest(struct tpm_chip *chip)
6438c2ecf20Sopenharmony_ci{
6448c2ecf20Sopenharmony_ci	int rc;
6458c2ecf20Sopenharmony_ci	unsigned int loops;
6468c2ecf20Sopenharmony_ci	unsigned int delay_msec = 100;
6478c2ecf20Sopenharmony_ci	unsigned long duration;
6488c2ecf20Sopenharmony_ci	u8 dummy[TPM_DIGEST_SIZE];
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	duration = tpm1_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	loops = jiffies_to_msecs(duration) / delay_msec;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	rc = tpm1_continue_selftest(chip);
6558c2ecf20Sopenharmony_ci	if (rc == TPM_ERR_INVALID_POSTINIT) {
6568c2ecf20Sopenharmony_ci		chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
6578c2ecf20Sopenharmony_ci		dev_info(&chip->dev, "TPM not ready (%d)\n", rc);
6588c2ecf20Sopenharmony_ci	}
6598c2ecf20Sopenharmony_ci	/* This may fail if there was no TPM driver during a suspend/resume
6608c2ecf20Sopenharmony_ci	 * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
6618c2ecf20Sopenharmony_ci	 */
6628c2ecf20Sopenharmony_ci	if (rc)
6638c2ecf20Sopenharmony_ci		return rc;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	do {
6668c2ecf20Sopenharmony_ci		/* Attempt to read a PCR value */
6678c2ecf20Sopenharmony_ci		rc = tpm1_pcr_read(chip, 0, dummy);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci		/* Some buggy TPMs will not respond to tpm_tis_ready() for
6708c2ecf20Sopenharmony_ci		 * around 300ms while the self test is ongoing, keep trying
6718c2ecf20Sopenharmony_ci		 * until the self test duration expires.
6728c2ecf20Sopenharmony_ci		 */
6738c2ecf20Sopenharmony_ci		if (rc == -ETIME) {
6748c2ecf20Sopenharmony_ci			dev_info(&chip->dev, HW_ERR "TPM command timed out during continue self test");
6758c2ecf20Sopenharmony_ci			tpm_msleep(delay_msec);
6768c2ecf20Sopenharmony_ci			continue;
6778c2ecf20Sopenharmony_ci		}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci		if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
6808c2ecf20Sopenharmony_ci			dev_info(&chip->dev, "TPM is disabled/deactivated (0x%X)\n",
6818c2ecf20Sopenharmony_ci				 rc);
6828c2ecf20Sopenharmony_ci			/* TPM is disabled and/or deactivated; driver can
6838c2ecf20Sopenharmony_ci			 * proceed and TPM does handle commands for
6848c2ecf20Sopenharmony_ci			 * suspend/resume correctly
6858c2ecf20Sopenharmony_ci			 */
6868c2ecf20Sopenharmony_ci			return 0;
6878c2ecf20Sopenharmony_ci		}
6888c2ecf20Sopenharmony_ci		if (rc != TPM_WARN_DOING_SELFTEST)
6898c2ecf20Sopenharmony_ci			return rc;
6908c2ecf20Sopenharmony_ci		tpm_msleep(delay_msec);
6918c2ecf20Sopenharmony_ci	} while (--loops > 0);
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	return rc;
6948c2ecf20Sopenharmony_ci}
6958c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(tpm1_do_selftest);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci/**
6988c2ecf20Sopenharmony_ci * tpm1_auto_startup - Perform the standard automatic TPM initialization
6998c2ecf20Sopenharmony_ci *                     sequence
7008c2ecf20Sopenharmony_ci * @chip: TPM chip to use
7018c2ecf20Sopenharmony_ci *
7028c2ecf20Sopenharmony_ci * Returns 0 on success, < 0 in case of fatal error.
7038c2ecf20Sopenharmony_ci */
7048c2ecf20Sopenharmony_ciint tpm1_auto_startup(struct tpm_chip *chip)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	int rc;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	rc = tpm1_get_timeouts(chip);
7098c2ecf20Sopenharmony_ci	if (rc)
7108c2ecf20Sopenharmony_ci		goto out;
7118c2ecf20Sopenharmony_ci	rc = tpm1_do_selftest(chip);
7128c2ecf20Sopenharmony_ci	if (rc) {
7138c2ecf20Sopenharmony_ci		dev_err(&chip->dev, "TPM self test failed\n");
7148c2ecf20Sopenharmony_ci		goto out;
7158c2ecf20Sopenharmony_ci	}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	return rc;
7188c2ecf20Sopenharmony_ciout:
7198c2ecf20Sopenharmony_ci	if (rc > 0)
7208c2ecf20Sopenharmony_ci		rc = -ENODEV;
7218c2ecf20Sopenharmony_ci	return rc;
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci#define TPM_ORD_SAVESTATE 152
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci/**
7278c2ecf20Sopenharmony_ci * tpm1_pm_suspend() - pm suspend handler
7288c2ecf20Sopenharmony_ci * @chip: TPM chip to use.
7298c2ecf20Sopenharmony_ci * @tpm_suspend_pcr: flush pcr for buggy TPM chips.
7308c2ecf20Sopenharmony_ci *
7318c2ecf20Sopenharmony_ci * The functions saves the TPM state to be restored on resume.
7328c2ecf20Sopenharmony_ci *
7338c2ecf20Sopenharmony_ci * Return:
7348c2ecf20Sopenharmony_ci * * 0 on success,
7358c2ecf20Sopenharmony_ci * * < 0 on error.
7368c2ecf20Sopenharmony_ci */
7378c2ecf20Sopenharmony_ciint tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)
7388c2ecf20Sopenharmony_ci{
7398c2ecf20Sopenharmony_ci	u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
7408c2ecf20Sopenharmony_ci	struct tpm_buf buf;
7418c2ecf20Sopenharmony_ci	unsigned int try;
7428c2ecf20Sopenharmony_ci	int rc;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	/* for buggy tpm, flush pcrs with extend to selected dummy */
7468c2ecf20Sopenharmony_ci	if (tpm_suspend_pcr)
7478c2ecf20Sopenharmony_ci		rc = tpm1_pcr_extend(chip, tpm_suspend_pcr, dummy_hash,
7488c2ecf20Sopenharmony_ci				     "extending dummy pcr before suspend");
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_SAVESTATE);
7518c2ecf20Sopenharmony_ci	if (rc)
7528c2ecf20Sopenharmony_ci		return rc;
7538c2ecf20Sopenharmony_ci	/* now do the actual savestate */
7548c2ecf20Sopenharmony_ci	for (try = 0; try < TPM_RETRY; try++) {
7558c2ecf20Sopenharmony_ci		rc = tpm_transmit_cmd(chip, &buf, 0, NULL);
7568c2ecf20Sopenharmony_ci		/*
7578c2ecf20Sopenharmony_ci		 * If the TPM indicates that it is too busy to respond to
7588c2ecf20Sopenharmony_ci		 * this command then retry before giving up.  It can take
7598c2ecf20Sopenharmony_ci		 * several seconds for this TPM to be ready.
7608c2ecf20Sopenharmony_ci		 *
7618c2ecf20Sopenharmony_ci		 * This can happen if the TPM has already been sent the
7628c2ecf20Sopenharmony_ci		 * SaveState command before the driver has loaded.  TCG 1.2
7638c2ecf20Sopenharmony_ci		 * specification states that any communication after SaveState
7648c2ecf20Sopenharmony_ci		 * may cause the TPM to invalidate previously saved state.
7658c2ecf20Sopenharmony_ci		 */
7668c2ecf20Sopenharmony_ci		if (rc != TPM_WARN_RETRY)
7678c2ecf20Sopenharmony_ci			break;
7688c2ecf20Sopenharmony_ci		tpm_msleep(TPM_TIMEOUT_RETRY);
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci		tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_SAVESTATE);
7718c2ecf20Sopenharmony_ci	}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	if (rc)
7748c2ecf20Sopenharmony_ci		dev_err(&chip->dev, "Error (%d) sending savestate before suspend\n",
7758c2ecf20Sopenharmony_ci			rc);
7768c2ecf20Sopenharmony_ci	else if (try > 0)
7778c2ecf20Sopenharmony_ci		dev_warn(&chip->dev, "TPM savestate took %dms\n",
7788c2ecf20Sopenharmony_ci			 try * TPM_TIMEOUT_RETRY);
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	tpm_buf_destroy(&buf);
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	return rc;
7838c2ecf20Sopenharmony_ci}
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci/**
7868c2ecf20Sopenharmony_ci * tpm1_get_pcr_allocation() - initialize the allocated bank
7878c2ecf20Sopenharmony_ci * @chip: TPM chip to use.
7888c2ecf20Sopenharmony_ci *
7898c2ecf20Sopenharmony_ci * The function initializes the SHA1 allocated bank to extend PCR
7908c2ecf20Sopenharmony_ci *
7918c2ecf20Sopenharmony_ci * Return:
7928c2ecf20Sopenharmony_ci * * 0 on success,
7938c2ecf20Sopenharmony_ci * * < 0 on error.
7948c2ecf20Sopenharmony_ci */
7958c2ecf20Sopenharmony_ciint tpm1_get_pcr_allocation(struct tpm_chip *chip)
7968c2ecf20Sopenharmony_ci{
7978c2ecf20Sopenharmony_ci	chip->allocated_banks = kcalloc(1, sizeof(*chip->allocated_banks),
7988c2ecf20Sopenharmony_ci					GFP_KERNEL);
7998c2ecf20Sopenharmony_ci	if (!chip->allocated_banks)
8008c2ecf20Sopenharmony_ci		return -ENOMEM;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	chip->allocated_banks[0].alg_id = TPM_ALG_SHA1;
8038c2ecf20Sopenharmony_ci	chip->allocated_banks[0].digest_size = hash_digest_size[HASH_ALGO_SHA1];
8048c2ecf20Sopenharmony_ci	chip->allocated_banks[0].crypto_id = HASH_ALGO_SHA1;
8058c2ecf20Sopenharmony_ci	chip->nr_allocated_banks = 1;
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	return 0;
8088c2ecf20Sopenharmony_ci}
809