Skip to content

Commit 29b47ce

Browse files
author
Jarkko Sakkinen
committed
tpm: move TPM space code out of tpm_transmit()
Prepare and commit TPM space before and after calling tpm_transmit() instead of doing that inside tpm_transmit(). After this change we can remove TPM_TRANSMIT_NESTED flag from tpm2_prepare_space() and tpm2_commit_space() and replace it with TPM_TRANSMIT_UNLOCKED. Cc: James Bottomley <James.Bottomley@HansenPartnership.com> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Reviewed-by: Stefan Berger <stefanb@linux.ibm.com> Tested-by: Stefan Berger <stefanb@linux.ibm.com> Reviewed-by: Jerry Snitselaar <jsnitsel@redhat.com> Reviewed-by: James Bottomley <James.Bottomley@HansenPartnership.com> Tested-by: Alexander Steffen <Alexander.Steffen@infineon.com>
1 parent c3465a3 commit 29b47ce

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

drivers/char/tpm/tpm-dev-common.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,35 @@ static DEFINE_MUTEX(tpm_dev_wq_lock);
3030
static ssize_t tpm_dev_transmit(struct tpm_chip *chip, struct tpm_space *space,
3131
u8 *buf, size_t bufsiz)
3232
{
33-
ssize_t ret;
33+
struct tpm_header *header = (void *)buf;
34+
ssize_t ret, len;
3435

3536
mutex_lock(&chip->tpm_mutex);
36-
ret = tpm_transmit(chip, space, buf, bufsiz, TPM_TRANSMIT_UNLOCKED);
37+
ret = tpm2_prepare_space(chip, space, buf, bufsiz);
38+
/* If the command is not implemented by the TPM, synthesize a
39+
* response with a TPM2_RC_COMMAND_CODE return for user-space.
40+
*/
41+
if (ret == -EOPNOTSUPP) {
42+
header->length = cpu_to_be32(sizeof(*header));
43+
header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
44+
header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
45+
TSS2_RESMGR_TPM_RC_LAYER);
46+
ret = sizeof(*header);
47+
}
48+
if (ret)
49+
goto out_lock;
50+
51+
len = tpm_transmit(chip, space, buf, bufsiz, TPM_TRANSMIT_UNLOCKED);
52+
if (len < 0)
53+
ret = len;
54+
55+
if (!ret)
56+
ret = tpm2_commit_space(chip, space, buf, &len);
57+
58+
out_lock:
3759
mutex_unlock(&chip->tpm_mutex);
3860

39-
return ret;
61+
return ret ? ret : len;
4062
}
4163

4264
static void tpm_dev_async_work(struct work_struct *work)

drivers/char/tpm/tpm-interface.c

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -145,27 +145,12 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space,
145145
return -E2BIG;
146146
}
147147

148-
rc = tpm2_prepare_space(chip, space, buf, bufsiz);
149-
/*
150-
* If the command is not implemented by the TPM, synthesize a
151-
* response with a TPM2_RC_COMMAND_CODE return for user-space.
152-
*/
153-
if (rc == -EOPNOTSUPP) {
154-
header->length = cpu_to_be32(sizeof(*header));
155-
header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
156-
header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
157-
TSS2_RESMGR_TPM_RC_LAYER);
158-
return sizeof(*header);
159-
}
160-
if (rc)
161-
return rc;
162-
163148
rc = chip->ops->send(chip, buf, count);
164149
if (rc < 0) {
165150
if (rc != -EPIPE)
166151
dev_err(&chip->dev,
167152
"%s: send(): error %d\n", __func__, rc);
168-
goto out_rc;
153+
return rc;
169154
}
170155

171156
/* A sanity check. send() should just return zero on success e.g.
@@ -189,8 +174,7 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space,
189174

190175
if (chip->ops->req_canceled(chip, status)) {
191176
dev_err(&chip->dev, "Operation Canceled\n");
192-
rc = -ECANCELED;
193-
goto out_rc;
177+
return -ECANCELED;
194178
}
195179

196180
tpm_msleep(TPM_TIMEOUT_POLL);
@@ -199,8 +183,7 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space,
199183

200184
chip->ops->cancel(chip);
201185
dev_err(&chip->dev, "Operation Timed out\n");
202-
rc = -ETIME;
203-
goto out_rc;
186+
return -ETIME;
204187

205188
out_recv:
206189
len = chip->ops->recv(chip, buf, bufsiz);
@@ -210,10 +193,6 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space,
210193
} else if (len < TPM_HEADER_SIZE || len != be32_to_cpu(header->length))
211194
rc = -EFAULT;
212195

213-
out_rc:
214-
if (!rc)
215-
rc = tpm2_commit_space(chip, space, buf, &len);
216-
217196
return rc ? rc : len;
218197
}
219198

drivers/char/tpm/tpm2-space.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static void tpm2_flush_sessions(struct tpm_chip *chip, struct tpm_space *space)
3939
for (i = 0; i < ARRAY_SIZE(space->session_tbl); i++) {
4040
if (space->session_tbl[i])
4141
tpm2_flush_context_cmd(chip, space->session_tbl[i],
42-
TPM_TRANSMIT_NESTED);
42+
TPM_TRANSMIT_UNLOCKED);
4343
}
4444
}
4545

@@ -84,7 +84,7 @@ static int tpm2_load_context(struct tpm_chip *chip, u8 *buf,
8484
tpm_buf_append(&tbuf, &buf[*offset], body_size);
8585

8686
rc = tpm_transmit_cmd(chip, NULL, &tbuf, 4,
87-
TPM_TRANSMIT_NESTED, NULL);
87+
TPM_TRANSMIT_UNLOCKED, NULL);
8888
if (rc < 0) {
8989
dev_warn(&chip->dev, "%s: failed with a system error %d\n",
9090
__func__, rc);
@@ -133,7 +133,7 @@ static int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf,
133133
tpm_buf_append_u32(&tbuf, handle);
134134

135135
rc = tpm_transmit_cmd(chip, NULL, &tbuf, 0,
136-
TPM_TRANSMIT_NESTED, NULL);
136+
TPM_TRANSMIT_UNLOCKED, NULL);
137137
if (rc < 0) {
138138
dev_warn(&chip->dev, "%s: failed with a system error %d\n",
139139
__func__, rc);
@@ -170,7 +170,7 @@ void tpm2_flush_space(struct tpm_chip *chip)
170170
for (i = 0; i < ARRAY_SIZE(space->context_tbl); i++)
171171
if (space->context_tbl[i] && ~space->context_tbl[i])
172172
tpm2_flush_context_cmd(chip, space->context_tbl[i],
173-
TPM_TRANSMIT_NESTED);
173+
TPM_TRANSMIT_UNLOCKED);
174174

175175
tpm2_flush_sessions(chip, space);
176176
}
@@ -418,7 +418,7 @@ static int tpm2_map_response_header(struct tpm_chip *chip, u32 cc, u8 *rsp,
418418

419419
return 0;
420420
out_no_slots:
421-
tpm2_flush_context_cmd(chip, phandle, TPM_TRANSMIT_NESTED);
421+
tpm2_flush_context_cmd(chip, phandle, TPM_TRANSMIT_UNLOCKED);
422422
dev_warn(&chip->dev, "%s: out of slots for 0x%08X\n", __func__,
423423
phandle);
424424
return -ENOMEM;
@@ -506,7 +506,7 @@ static int tpm2_save_space(struct tpm_chip *chip)
506506
return rc;
507507

508508
tpm2_flush_context_cmd(chip, space->context_tbl[i],
509-
TPM_TRANSMIT_NESTED);
509+
TPM_TRANSMIT_UNLOCKED);
510510
space->context_tbl[i] = ~0;
511511
}
512512

0 commit comments

Comments
 (0)