xref: /openbmc/linux/include/linux/tpm.h (revision 554b841d)
1b886d83cSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2659aaf2bSRajiv Andrade /*
3659aaf2bSRajiv Andrade  * Copyright (C) 2004,2007,2008 IBM Corporation
4659aaf2bSRajiv Andrade  *
5659aaf2bSRajiv Andrade  * Authors:
6659aaf2bSRajiv Andrade  * Leendert van Doorn <leendert@watson.ibm.com>
7659aaf2bSRajiv Andrade  * Dave Safford <safford@watson.ibm.com>
8659aaf2bSRajiv Andrade  * Reiner Sailer <sailer@watson.ibm.com>
9659aaf2bSRajiv Andrade  * Kylene Hall <kjhall@us.ibm.com>
10659aaf2bSRajiv Andrade  * Debora Velarde <dvelarde@us.ibm.com>
11659aaf2bSRajiv Andrade  *
12659aaf2bSRajiv Andrade  * Maintained by: <tpmdd_devel@lists.sourceforge.net>
13659aaf2bSRajiv Andrade  *
14659aaf2bSRajiv Andrade  * Device driver for TCG/TCPA TPM (trusted platform module).
15659aaf2bSRajiv Andrade  * Specifications at www.trustedcomputinggroup.org
16659aaf2bSRajiv Andrade  */
17659aaf2bSRajiv Andrade #ifndef __LINUX_TPM_H__
18659aaf2bSRajiv Andrade #define __LINUX_TPM_H__
19659aaf2bSRajiv Andrade 
20901615cbSRoberto Sassu #include <linux/hw_random.h>
21901615cbSRoberto Sassu #include <linux/acpi.h>
22901615cbSRoberto Sassu #include <linux/cdev.h>
23901615cbSRoberto Sassu #include <linux/fs.h>
2474edff2dSSumit Garg #include <linux/highmem.h>
25aa042475SRoberto Sassu #include <crypto/hash_info.h>
26aa042475SRoberto Sassu 
271c16c963SMimi Zohar #define TPM_DIGEST_SIZE 20	/* Max TPM v1.2 PCR size */
28aa042475SRoberto Sassu #define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
291c16c963SMimi Zohar 
3001ad1fa7SJason Gunthorpe struct tpm_chip;
31954650efSJarkko Sakkinen struct trusted_key_payload;
32954650efSJarkko Sakkinen struct trusted_key_options;
3301ad1fa7SJason Gunthorpe 
34aab73d95SJames Bottomley /* if you add a new hash to this, increment TPM_MAX_HASHES below */
35aa042475SRoberto Sassu enum tpm_algorithms {
36aa042475SRoberto Sassu 	TPM_ALG_ERROR		= 0x0000,
37aa042475SRoberto Sassu 	TPM_ALG_SHA1		= 0x0004,
38aa042475SRoberto Sassu 	TPM_ALG_KEYEDHASH	= 0x0008,
39aa042475SRoberto Sassu 	TPM_ALG_SHA256		= 0x000B,
40aa042475SRoberto Sassu 	TPM_ALG_SHA384		= 0x000C,
41aa042475SRoberto Sassu 	TPM_ALG_SHA512		= 0x000D,
42aa042475SRoberto Sassu 	TPM_ALG_NULL		= 0x0010,
43aa042475SRoberto Sassu 	TPM_ALG_SM3_256		= 0x0012,
44aa042475SRoberto Sassu };
45aa042475SRoberto Sassu 
46aab73d95SJames Bottomley /*
47aab73d95SJames Bottomley  * maximum number of hashing algorithms a TPM can have.  This is
48aab73d95SJames Bottomley  * basically a count of every hash in tpm_algorithms above
49aab73d95SJames Bottomley  */
50aab73d95SJames Bottomley #define TPM_MAX_HASHES	5
51aab73d95SJames Bottomley 
52aa042475SRoberto Sassu struct tpm_digest {
53aa042475SRoberto Sassu 	u16 alg_id;
54aa042475SRoberto Sassu 	u8 digest[TPM_MAX_DIGEST_SIZE];
55aa042475SRoberto Sassu } __packed;
56aa042475SRoberto Sassu 
57879b5892SRoberto Sassu struct tpm_bank_info {
58879b5892SRoberto Sassu 	u16 alg_id;
59879b5892SRoberto Sassu 	u16 digest_size;
60879b5892SRoberto Sassu 	u16 crypto_id;
61879b5892SRoberto Sassu };
62879b5892SRoberto Sassu 
63cae8b441SJason Gunthorpe enum TPM_OPS_FLAGS {
64cae8b441SJason Gunthorpe 	TPM_OPS_AUTO_STARTUP = BIT(0),
65cae8b441SJason Gunthorpe };
66cae8b441SJason Gunthorpe 
6701ad1fa7SJason Gunthorpe struct tpm_class_ops {
68cae8b441SJason Gunthorpe 	unsigned int flags;
6901ad1fa7SJason Gunthorpe 	const u8 req_complete_mask;
7001ad1fa7SJason Gunthorpe 	const u8 req_complete_val;
7101ad1fa7SJason Gunthorpe 	bool (*req_canceled)(struct tpm_chip *chip, u8 status);
7201ad1fa7SJason Gunthorpe 	int (*recv) (struct tpm_chip *chip, u8 *buf, size_t len);
7301ad1fa7SJason Gunthorpe 	int (*send) (struct tpm_chip *chip, u8 *buf, size_t len);
7401ad1fa7SJason Gunthorpe 	void (*cancel) (struct tpm_chip *chip);
7501ad1fa7SJason Gunthorpe 	u8 (*status) (struct tpm_chip *chip);
7636ce0897SJerry Snitselaar 	void (*update_timeouts)(struct tpm_chip *chip,
778e54caf4SJason Gunthorpe 				unsigned long *timeout_cap);
7815d0b22cSJerry Snitselaar 	void (*update_durations)(struct tpm_chip *chip,
7915d0b22cSJerry Snitselaar 				 unsigned long *duration_cap);
80627448e8STomas Winkler 	int (*go_idle)(struct tpm_chip *chip);
81627448e8STomas Winkler 	int (*cmd_ready)(struct tpm_chip *chip);
82877c57d0SJarkko Sakkinen 	int (*request_locality)(struct tpm_chip *chip, int loc);
83888d867dSTomas Winkler 	int (*relinquish_locality)(struct tpm_chip *chip, int loc);
84b3e958ceSAzhar Shaikh 	void (*clk_enable)(struct tpm_chip *chip, bool value);
8501ad1fa7SJason Gunthorpe };
8601ad1fa7SJason Gunthorpe 
87901615cbSRoberto Sassu #define TPM_NUM_EVENT_LOG_FILES		3
88901615cbSRoberto Sassu 
89901615cbSRoberto Sassu /* Indexes the duration array */
90901615cbSRoberto Sassu enum tpm_duration {
91901615cbSRoberto Sassu 	TPM_SHORT = 0,
92901615cbSRoberto Sassu 	TPM_MEDIUM = 1,
93901615cbSRoberto Sassu 	TPM_LONG = 2,
94901615cbSRoberto Sassu 	TPM_LONG_LONG = 3,
95901615cbSRoberto Sassu 	TPM_UNDEFINED,
96901615cbSRoberto Sassu 	TPM_NUM_DURATIONS = TPM_UNDEFINED,
97901615cbSRoberto Sassu };
98901615cbSRoberto Sassu 
99901615cbSRoberto Sassu #define TPM_PPI_VERSION_LEN		3
100901615cbSRoberto Sassu 
101901615cbSRoberto Sassu struct tpm_space {
102901615cbSRoberto Sassu 	u32 context_tbl[3];
103901615cbSRoberto Sassu 	u8 *context_buf;
104901615cbSRoberto Sassu 	u32 session_tbl[3];
105901615cbSRoberto Sassu 	u8 *session_buf;
1066c4e79d9SJarkko Sakkinen 	u32 buf_size;
107901615cbSRoberto Sassu };
108901615cbSRoberto Sassu 
109901615cbSRoberto Sassu struct tpm_bios_log {
110901615cbSRoberto Sassu 	void *bios_event_log;
111901615cbSRoberto Sassu 	void *bios_event_log_end;
112901615cbSRoberto Sassu };
113901615cbSRoberto Sassu 
114901615cbSRoberto Sassu struct tpm_chip_seqops {
115901615cbSRoberto Sassu 	struct tpm_chip *chip;
116901615cbSRoberto Sassu 	const struct seq_operations *seqops;
117901615cbSRoberto Sassu };
118901615cbSRoberto Sassu 
119901615cbSRoberto Sassu struct tpm_chip {
120901615cbSRoberto Sassu 	struct device dev;
121901615cbSRoberto Sassu 	struct device devs;
122901615cbSRoberto Sassu 	struct cdev cdev;
123901615cbSRoberto Sassu 	struct cdev cdevs;
124901615cbSRoberto Sassu 
125901615cbSRoberto Sassu 	/* A driver callback under ops cannot be run unless ops_sem is held
126901615cbSRoberto Sassu 	 * (sometimes implicitly, eg for the sysfs code). ops becomes null
127901615cbSRoberto Sassu 	 * when the driver is unregistered, see tpm_try_get_ops.
128901615cbSRoberto Sassu 	 */
129901615cbSRoberto Sassu 	struct rw_semaphore ops_sem;
130901615cbSRoberto Sassu 	const struct tpm_class_ops *ops;
131901615cbSRoberto Sassu 
132901615cbSRoberto Sassu 	struct tpm_bios_log log;
133901615cbSRoberto Sassu 	struct tpm_chip_seqops bin_log_seqops;
134901615cbSRoberto Sassu 	struct tpm_chip_seqops ascii_log_seqops;
135901615cbSRoberto Sassu 
136901615cbSRoberto Sassu 	unsigned int flags;
137901615cbSRoberto Sassu 
138901615cbSRoberto Sassu 	int dev_num;		/* /dev/tpm# */
139901615cbSRoberto Sassu 	unsigned long is_open;	/* only one allowed */
140901615cbSRoberto Sassu 
141901615cbSRoberto Sassu 	char hwrng_name[64];
142901615cbSRoberto Sassu 	struct hwrng hwrng;
143901615cbSRoberto Sassu 
144901615cbSRoberto Sassu 	struct mutex tpm_mutex;	/* tpm is processing */
145901615cbSRoberto Sassu 
146901615cbSRoberto Sassu 	unsigned long timeout_a; /* jiffies */
147901615cbSRoberto Sassu 	unsigned long timeout_b; /* jiffies */
148901615cbSRoberto Sassu 	unsigned long timeout_c; /* jiffies */
149901615cbSRoberto Sassu 	unsigned long timeout_d; /* jiffies */
150901615cbSRoberto Sassu 	bool timeout_adjusted;
151901615cbSRoberto Sassu 	unsigned long duration[TPM_NUM_DURATIONS]; /* jiffies */
152901615cbSRoberto Sassu 	bool duration_adjusted;
153901615cbSRoberto Sassu 
154901615cbSRoberto Sassu 	struct dentry *bios_dir[TPM_NUM_EVENT_LOG_FILES];
155901615cbSRoberto Sassu 
156aab73d95SJames Bottomley 	const struct attribute_group *groups[3 + TPM_MAX_HASHES];
157901615cbSRoberto Sassu 	unsigned int groups_cnt;
158901615cbSRoberto Sassu 
159901615cbSRoberto Sassu 	u32 nr_allocated_banks;
160901615cbSRoberto Sassu 	struct tpm_bank_info *allocated_banks;
161901615cbSRoberto Sassu #ifdef CONFIG_ACPI
162901615cbSRoberto Sassu 	acpi_handle acpi_dev_handle;
163901615cbSRoberto Sassu 	char ppi_version[TPM_PPI_VERSION_LEN + 1];
164901615cbSRoberto Sassu #endif /* CONFIG_ACPI */
165901615cbSRoberto Sassu 
166901615cbSRoberto Sassu 	struct tpm_space work_space;
167901615cbSRoberto Sassu 	u32 last_cc;
168901615cbSRoberto Sassu 	u32 nr_commands;
169901615cbSRoberto Sassu 	u32 *cc_attrs_tbl;
170901615cbSRoberto Sassu 
171901615cbSRoberto Sassu 	/* active locality */
172901615cbSRoberto Sassu 	int locality;
173901615cbSRoberto Sassu };
174901615cbSRoberto Sassu 
17574edff2dSSumit Garg #define TPM_HEADER_SIZE		10
17674edff2dSSumit Garg 
17774edff2dSSumit Garg enum tpm2_const {
17874edff2dSSumit Garg 	TPM2_PLATFORM_PCR       =     24,
17974edff2dSSumit Garg 	TPM2_PCR_SELECT_MIN     = ((TPM2_PLATFORM_PCR + 7) / 8),
18074edff2dSSumit Garg };
18174edff2dSSumit Garg 
18274edff2dSSumit Garg enum tpm2_timeouts {
18374edff2dSSumit Garg 	TPM2_TIMEOUT_A          =    750,
18474edff2dSSumit Garg 	TPM2_TIMEOUT_B          =   2000,
18574edff2dSSumit Garg 	TPM2_TIMEOUT_C          =    200,
18674edff2dSSumit Garg 	TPM2_TIMEOUT_D          =     30,
18774edff2dSSumit Garg 	TPM2_DURATION_SHORT     =     20,
18874edff2dSSumit Garg 	TPM2_DURATION_MEDIUM    =    750,
18974edff2dSSumit Garg 	TPM2_DURATION_LONG      =   2000,
19074edff2dSSumit Garg 	TPM2_DURATION_LONG_LONG = 300000,
19174edff2dSSumit Garg 	TPM2_DURATION_DEFAULT   = 120000,
19274edff2dSSumit Garg };
19374edff2dSSumit Garg 
19474edff2dSSumit Garg enum tpm2_structures {
19574edff2dSSumit Garg 	TPM2_ST_NO_SESSIONS	= 0x8001,
19674edff2dSSumit Garg 	TPM2_ST_SESSIONS	= 0x8002,
19774edff2dSSumit Garg };
19874edff2dSSumit Garg 
19974edff2dSSumit Garg /* Indicates from what layer of the software stack the error comes from */
20074edff2dSSumit Garg #define TSS2_RC_LAYER_SHIFT	 16
20174edff2dSSumit Garg #define TSS2_RESMGR_TPM_RC_LAYER (11 << TSS2_RC_LAYER_SHIFT)
20274edff2dSSumit Garg 
20374edff2dSSumit Garg enum tpm2_return_codes {
20474edff2dSSumit Garg 	TPM2_RC_SUCCESS		= 0x0000,
20574edff2dSSumit Garg 	TPM2_RC_HASH		= 0x0083, /* RC_FMT1 */
20674edff2dSSumit Garg 	TPM2_RC_HANDLE		= 0x008B,
20774edff2dSSumit Garg 	TPM2_RC_INITIALIZE	= 0x0100, /* RC_VER1 */
20874edff2dSSumit Garg 	TPM2_RC_FAILURE		= 0x0101,
20974edff2dSSumit Garg 	TPM2_RC_DISABLED	= 0x0120,
2100aa69878Saxelj 	TPM2_RC_UPGRADE		= 0x012D,
21174edff2dSSumit Garg 	TPM2_RC_COMMAND_CODE    = 0x0143,
21274edff2dSSumit Garg 	TPM2_RC_TESTING		= 0x090A, /* RC_WARN */
21374edff2dSSumit Garg 	TPM2_RC_REFERENCE_H0	= 0x0910,
21474edff2dSSumit Garg 	TPM2_RC_RETRY		= 0x0922,
21574edff2dSSumit Garg };
21674edff2dSSumit Garg 
21774edff2dSSumit Garg enum tpm2_command_codes {
21874edff2dSSumit Garg 	TPM2_CC_FIRST		        = 0x011F,
21974edff2dSSumit Garg 	TPM2_CC_HIERARCHY_CONTROL       = 0x0121,
22074edff2dSSumit Garg 	TPM2_CC_HIERARCHY_CHANGE_AUTH   = 0x0129,
22174edff2dSSumit Garg 	TPM2_CC_CREATE_PRIMARY          = 0x0131,
22274edff2dSSumit Garg 	TPM2_CC_SEQUENCE_COMPLETE       = 0x013E,
22374edff2dSSumit Garg 	TPM2_CC_SELF_TEST	        = 0x0143,
22474edff2dSSumit Garg 	TPM2_CC_STARTUP		        = 0x0144,
22574edff2dSSumit Garg 	TPM2_CC_SHUTDOWN	        = 0x0145,
22674edff2dSSumit Garg 	TPM2_CC_NV_READ                 = 0x014E,
22774edff2dSSumit Garg 	TPM2_CC_CREATE		        = 0x0153,
22874edff2dSSumit Garg 	TPM2_CC_LOAD		        = 0x0157,
22974edff2dSSumit Garg 	TPM2_CC_SEQUENCE_UPDATE         = 0x015C,
23074edff2dSSumit Garg 	TPM2_CC_UNSEAL		        = 0x015E,
23174edff2dSSumit Garg 	TPM2_CC_CONTEXT_LOAD	        = 0x0161,
23274edff2dSSumit Garg 	TPM2_CC_CONTEXT_SAVE	        = 0x0162,
23374edff2dSSumit Garg 	TPM2_CC_FLUSH_CONTEXT	        = 0x0165,
23474edff2dSSumit Garg 	TPM2_CC_VERIFY_SIGNATURE        = 0x0177,
23574edff2dSSumit Garg 	TPM2_CC_GET_CAPABILITY	        = 0x017A,
23674edff2dSSumit Garg 	TPM2_CC_GET_RANDOM	        = 0x017B,
23774edff2dSSumit Garg 	TPM2_CC_PCR_READ	        = 0x017E,
23874edff2dSSumit Garg 	TPM2_CC_PCR_EXTEND	        = 0x0182,
23974edff2dSSumit Garg 	TPM2_CC_EVENT_SEQUENCE_COMPLETE = 0x0185,
24074edff2dSSumit Garg 	TPM2_CC_HASH_SEQUENCE_START     = 0x0186,
24174edff2dSSumit Garg 	TPM2_CC_CREATE_LOADED           = 0x0191,
24274edff2dSSumit Garg 	TPM2_CC_LAST		        = 0x0193, /* Spec 1.36 */
24374edff2dSSumit Garg };
24474edff2dSSumit Garg 
24574edff2dSSumit Garg enum tpm2_permanent_handles {
24674edff2dSSumit Garg 	TPM2_RS_PW		= 0x40000009,
24774edff2dSSumit Garg };
24874edff2dSSumit Garg 
24974edff2dSSumit Garg enum tpm2_capabilities {
25074edff2dSSumit Garg 	TPM2_CAP_HANDLES	= 1,
25174edff2dSSumit Garg 	TPM2_CAP_COMMANDS	= 2,
25274edff2dSSumit Garg 	TPM2_CAP_PCRS		= 5,
25374edff2dSSumit Garg 	TPM2_CAP_TPM_PROPERTIES = 6,
25474edff2dSSumit Garg };
25574edff2dSSumit Garg 
25674edff2dSSumit Garg enum tpm2_properties {
25774edff2dSSumit Garg 	TPM_PT_TOTAL_COMMANDS	= 0x0129,
25874edff2dSSumit Garg };
25974edff2dSSumit Garg 
26074edff2dSSumit Garg enum tpm2_startup_types {
26174edff2dSSumit Garg 	TPM2_SU_CLEAR	= 0x0000,
26274edff2dSSumit Garg 	TPM2_SU_STATE	= 0x0001,
26374edff2dSSumit Garg };
26474edff2dSSumit Garg 
26574edff2dSSumit Garg enum tpm2_cc_attrs {
26674edff2dSSumit Garg 	TPM2_CC_ATTR_CHANDLES	= 25,
26774edff2dSSumit Garg 	TPM2_CC_ATTR_RHANDLE	= 28,
26885b93bbdSJulien Gomes 	TPM2_CC_ATTR_VENDOR	= 29,
26974edff2dSSumit Garg };
27074edff2dSSumit Garg 
27174edff2dSSumit Garg #define TPM_VID_INTEL    0x8086
27274edff2dSSumit Garg #define TPM_VID_WINBOND  0x1050
27374edff2dSSumit Garg #define TPM_VID_STM      0x104A
27479ca6f74SHao Wu #define TPM_VID_ATML     0x1114
27574edff2dSSumit Garg 
27674edff2dSSumit Garg enum tpm_chip_flags {
2770c8862deSJarkko Sakkinen 	TPM_CHIP_FLAG_BOOTSTRAPPED		= BIT(0),
27874edff2dSSumit Garg 	TPM_CHIP_FLAG_TPM2			= BIT(1),
27974edff2dSSumit Garg 	TPM_CHIP_FLAG_IRQ			= BIT(2),
28074edff2dSSumit Garg 	TPM_CHIP_FLAG_VIRTUAL			= BIT(3),
28174edff2dSSumit Garg 	TPM_CHIP_FLAG_HAVE_TIMEOUTS		= BIT(4),
28274edff2dSSumit Garg 	TPM_CHIP_FLAG_ALWAYS_POWERED		= BIT(5),
28374edff2dSSumit Garg 	TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED	= BIT(6),
2840aa69878Saxelj 	TPM_CHIP_FLAG_FIRMWARE_UPGRADE		= BIT(7),
28599d46450SJarkko Sakkinen 	TPM_CHIP_FLAG_SUSPENDED			= BIT(8),
286*554b841dSMario Limonciello 	TPM_CHIP_FLAG_HWRNG_DISABLED		= BIT(9),
28774edff2dSSumit Garg };
28874edff2dSSumit Garg 
28974edff2dSSumit Garg #define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
29074edff2dSSumit Garg 
29174edff2dSSumit Garg struct tpm_header {
29274edff2dSSumit Garg 	__be16 tag;
29374edff2dSSumit Garg 	__be32 length;
29474edff2dSSumit Garg 	union {
29574edff2dSSumit Garg 		__be32 ordinal;
29674edff2dSSumit Garg 		__be32 return_code;
29774edff2dSSumit Garg 	};
29874edff2dSSumit Garg } __packed;
29974edff2dSSumit Garg 
30074edff2dSSumit Garg /* A string buffer type for constructing TPM commands. This is based on the
30174edff2dSSumit Garg  * ideas of string buffer code in security/keys/trusted.h but is heap based
30274edff2dSSumit Garg  * in order to keep the stack usage minimal.
30374edff2dSSumit Garg  */
30474edff2dSSumit Garg 
30574edff2dSSumit Garg enum tpm_buf_flags {
30674edff2dSSumit Garg 	TPM_BUF_OVERFLOW	= BIT(0),
30774edff2dSSumit Garg };
30874edff2dSSumit Garg 
30974edff2dSSumit Garg struct tpm_buf {
31074edff2dSSumit Garg 	unsigned int flags;
31174edff2dSSumit Garg 	u8 *data;
31274edff2dSSumit Garg };
31374edff2dSSumit Garg 
3142e19e101SSumit Garg enum tpm2_object_attributes {
315e5fb5d2cSJames Bottomley 	TPM2_OA_FIXED_TPM		= BIT(1),
316e5fb5d2cSJames Bottomley 	TPM2_OA_FIXED_PARENT		= BIT(4),
3172e19e101SSumit Garg 	TPM2_OA_USER_WITH_AUTH		= BIT(6),
3182e19e101SSumit Garg };
3192e19e101SSumit Garg 
3202e19e101SSumit Garg enum tpm2_session_attributes {
3212e19e101SSumit Garg 	TPM2_SA_CONTINUE_SESSION	= BIT(0),
3222e19e101SSumit Garg };
3232e19e101SSumit Garg 
3242e19e101SSumit Garg struct tpm2_hash {
3252e19e101SSumit Garg 	unsigned int crypto_id;
3262e19e101SSumit Garg 	unsigned int tpm_id;
3272e19e101SSumit Garg };
3282e19e101SSumit Garg 
tpm_buf_reset(struct tpm_buf * buf,u16 tag,u32 ordinal)32974edff2dSSumit Garg static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
33074edff2dSSumit Garg {
33174edff2dSSumit Garg 	struct tpm_header *head = (struct tpm_header *)buf->data;
33274edff2dSSumit Garg 
33374edff2dSSumit Garg 	head->tag = cpu_to_be16(tag);
33474edff2dSSumit Garg 	head->length = cpu_to_be32(sizeof(*head));
33574edff2dSSumit Garg 	head->ordinal = cpu_to_be32(ordinal);
33674edff2dSSumit Garg }
33774edff2dSSumit Garg 
tpm_buf_init(struct tpm_buf * buf,u16 tag,u32 ordinal)33874edff2dSSumit Garg static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
33974edff2dSSumit Garg {
34074edff2dSSumit Garg 	buf->data = (u8 *)__get_free_page(GFP_KERNEL);
34174edff2dSSumit Garg 	if (!buf->data)
34274edff2dSSumit Garg 		return -ENOMEM;
34374edff2dSSumit Garg 
34474edff2dSSumit Garg 	buf->flags = 0;
34574edff2dSSumit Garg 	tpm_buf_reset(buf, tag, ordinal);
34674edff2dSSumit Garg 	return 0;
34774edff2dSSumit Garg }
34874edff2dSSumit Garg 
tpm_buf_destroy(struct tpm_buf * buf)34974edff2dSSumit Garg static inline void tpm_buf_destroy(struct tpm_buf *buf)
35074edff2dSSumit Garg {
35174edff2dSSumit Garg 	free_page((unsigned long)buf->data);
35274edff2dSSumit Garg }
35374edff2dSSumit Garg 
tpm_buf_length(struct tpm_buf * buf)35474edff2dSSumit Garg static inline u32 tpm_buf_length(struct tpm_buf *buf)
35574edff2dSSumit Garg {
35674edff2dSSumit Garg 	struct tpm_header *head = (struct tpm_header *)buf->data;
35774edff2dSSumit Garg 
35874edff2dSSumit Garg 	return be32_to_cpu(head->length);
35974edff2dSSumit Garg }
36074edff2dSSumit Garg 
tpm_buf_tag(struct tpm_buf * buf)36174edff2dSSumit Garg static inline u16 tpm_buf_tag(struct tpm_buf *buf)
36274edff2dSSumit Garg {
36374edff2dSSumit Garg 	struct tpm_header *head = (struct tpm_header *)buf->data;
36474edff2dSSumit Garg 
36574edff2dSSumit Garg 	return be16_to_cpu(head->tag);
36674edff2dSSumit Garg }
36774edff2dSSumit Garg 
tpm_buf_append(struct tpm_buf * buf,const unsigned char * new_data,unsigned int new_len)36874edff2dSSumit Garg static inline void tpm_buf_append(struct tpm_buf *buf,
36974edff2dSSumit Garg 				  const unsigned char *new_data,
37074edff2dSSumit Garg 				  unsigned int new_len)
37174edff2dSSumit Garg {
37274edff2dSSumit Garg 	struct tpm_header *head = (struct tpm_header *)buf->data;
37374edff2dSSumit Garg 	u32 len = tpm_buf_length(buf);
37474edff2dSSumit Garg 
37574edff2dSSumit Garg 	/* Return silently if overflow has already happened. */
37674edff2dSSumit Garg 	if (buf->flags & TPM_BUF_OVERFLOW)
37774edff2dSSumit Garg 		return;
37874edff2dSSumit Garg 
37974edff2dSSumit Garg 	if ((len + new_len) > PAGE_SIZE) {
38074edff2dSSumit Garg 		WARN(1, "tpm_buf: overflow\n");
38174edff2dSSumit Garg 		buf->flags |= TPM_BUF_OVERFLOW;
38274edff2dSSumit Garg 		return;
38374edff2dSSumit Garg 	}
38474edff2dSSumit Garg 
38574edff2dSSumit Garg 	memcpy(&buf->data[len], new_data, new_len);
38674edff2dSSumit Garg 	head->length = cpu_to_be32(len + new_len);
38774edff2dSSumit Garg }
38874edff2dSSumit Garg 
tpm_buf_append_u8(struct tpm_buf * buf,const u8 value)38974edff2dSSumit Garg static inline void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
39074edff2dSSumit Garg {
39174edff2dSSumit Garg 	tpm_buf_append(buf, &value, 1);
39274edff2dSSumit Garg }
39374edff2dSSumit Garg 
tpm_buf_append_u16(struct tpm_buf * buf,const u16 value)39474edff2dSSumit Garg static inline void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value)
39574edff2dSSumit Garg {
39674edff2dSSumit Garg 	__be16 value2 = cpu_to_be16(value);
39774edff2dSSumit Garg 
39874edff2dSSumit Garg 	tpm_buf_append(buf, (u8 *) &value2, 2);
39974edff2dSSumit Garg }
40074edff2dSSumit Garg 
tpm_buf_append_u32(struct tpm_buf * buf,const u32 value)40174edff2dSSumit Garg static inline void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
40274edff2dSSumit Garg {
40374edff2dSSumit Garg 	__be32 value2 = cpu_to_be32(value);
40474edff2dSSumit Garg 
40574edff2dSSumit Garg 	tpm_buf_append(buf, (u8 *) &value2, 4);
40674edff2dSSumit Garg }
40774edff2dSSumit Garg 
4080aa69878Saxelj /*
4090aa69878Saxelj  * Check if TPM device is in the firmware upgrade mode.
4100aa69878Saxelj  */
tpm_is_firmware_upgrade(struct tpm_chip * chip)4110aa69878Saxelj static inline bool tpm_is_firmware_upgrade(struct tpm_chip *chip)
4120aa69878Saxelj {
4130aa69878Saxelj 	return chip->flags & TPM_CHIP_FLAG_FIRMWARE_UPGRADE;
4140aa69878Saxelj }
4150aa69878Saxelj 
tpm2_rc_value(u32 rc)4162e19e101SSumit Garg static inline u32 tpm2_rc_value(u32 rc)
4172e19e101SSumit Garg {
4182e19e101SSumit Garg 	return (rc & BIT(7)) ? rc & 0xff : rc;
4192e19e101SSumit Garg }
4202e19e101SSumit Garg 
421ff76ec18SRandy Dunlap #if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE)
422659aaf2bSRajiv Andrade 
423aad887f6SJarkko Sakkinen extern int tpm_is_tpm2(struct tpm_chip *chip);
4248c657a05SJarkko Sakkinen extern __must_check int tpm_try_get_ops(struct tpm_chip *chip);
4258c657a05SJarkko Sakkinen extern void tpm_put_ops(struct tpm_chip *chip);
4268c657a05SJarkko Sakkinen extern ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
4278c657a05SJarkko Sakkinen 				size_t min_rsp_body_length, const char *desc);
428879b5892SRoberto Sassu extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
429879b5892SRoberto Sassu 			struct tpm_digest *digest);
4300b6cf6b9SRoberto Sassu extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
4310b6cf6b9SRoberto Sassu 			  struct tpm_digest *digests);
432aad887f6SJarkko Sakkinen extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);
433aad887f6SJarkko Sakkinen extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
434aaae8153SStefan Berger extern struct tpm_chip *tpm_default_chip(void);
43545477b3fSJames Bottomley void tpm2_flush_context(struct tpm_chip *chip, u32 handle);
436d6ba4521SMimi Zohar #else
tpm_is_tpm2(struct tpm_chip * chip)437aad887f6SJarkko Sakkinen static inline int tpm_is_tpm2(struct tpm_chip *chip)
438954650efSJarkko Sakkinen {
439954650efSJarkko Sakkinen 	return -ENODEV;
440954650efSJarkko Sakkinen }
tpm_pcr_read(struct tpm_chip * chip,int pcr_idx,struct tpm_digest * digest)441879b5892SRoberto Sassu static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,
442879b5892SRoberto Sassu 			       struct tpm_digest *digest)
443aad887f6SJarkko Sakkinen {
444d6ba4521SMimi Zohar 	return -ENODEV;
445d6ba4521SMimi Zohar }
44695adc6b4STomas Winkler 
tpm_pcr_extend(struct tpm_chip * chip,u32 pcr_idx,struct tpm_digest * digests)44795adc6b4STomas Winkler static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
4480b6cf6b9SRoberto Sassu 				 struct tpm_digest *digests)
449aad887f6SJarkko Sakkinen {
450d6ba4521SMimi Zohar 	return -ENODEV;
451d6ba4521SMimi Zohar }
45295adc6b4STomas Winkler 
tpm_send(struct tpm_chip * chip,void * cmd,size_t buflen)453aad887f6SJarkko Sakkinen static inline int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
454aad887f6SJarkko Sakkinen {
455c749ba91SMimi Zohar 	return -ENODEV;
456c749ba91SMimi Zohar }
tpm_get_random(struct tpm_chip * chip,u8 * data,size_t max)457aad887f6SJarkko Sakkinen static inline int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max)
458aad887f6SJarkko Sakkinen {
45941ab999cSKent Yoder 	return -ENODEV;
46041ab999cSKent Yoder }
461954650efSJarkko Sakkinen 
tpm_default_chip(void)462aaae8153SStefan Berger static inline struct tpm_chip *tpm_default_chip(void)
463aaae8153SStefan Berger {
464aaae8153SStefan Berger 	return NULL;
465aaae8153SStefan Berger }
466659aaf2bSRajiv Andrade #endif
467659aaf2bSRajiv Andrade #endif
468