Skip to content

Commit 304ff67

Browse files
author
Jarkko Sakkinen
committed
tpm: clean up tpm_try_transmit() error handling flow
Move locking, locality handling and power management to tpm_transmit() in order to simplify the flow. 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 c4df71d commit 304ff67

File tree

3 files changed

+45
-52
lines changed

3 files changed

+45
-52
lines changed

drivers/char/tpm/tpm-interface.c

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space,
167167
ssize_t len = 0;
168168
u32 count, ordinal;
169169
unsigned long stop;
170-
bool need_locality;
171170

172171
rc = tpm_validate_command(chip, space, buf, bufsiz);
173172
if (rc == -EINVAL)
@@ -197,37 +196,16 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space,
197196
return -E2BIG;
198197
}
199198

200-
if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
201-
mutex_lock(&chip->tpm_mutex);
202-
203-
if (chip->ops->clk_enable != NULL)
204-
chip->ops->clk_enable(chip, true);
205-
206-
/* Store the decision as chip->locality will be changed. */
207-
need_locality = chip->locality == -1;
208-
209-
if (need_locality) {
210-
rc = tpm_request_locality(chip, flags);
211-
if (rc < 0) {
212-
need_locality = false;
213-
goto out_locality;
214-
}
215-
}
216-
217-
rc = tpm_cmd_ready(chip, flags);
218-
if (rc)
219-
goto out_locality;
220-
221199
rc = tpm2_prepare_space(chip, space, ordinal, buf);
222200
if (rc)
223-
goto out;
201+
return rc;
224202

225203
rc = chip->ops->send(chip, buf, count);
226204
if (rc < 0) {
227205
if (rc != -EPIPE)
228206
dev_err(&chip->dev,
229207
"%s: send(): error %d\n", __func__, rc);
230-
goto out;
208+
goto out_rc;
231209
}
232210

233211
/* A sanity check. send() should just return zero on success e.g.
@@ -252,7 +230,7 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space,
252230
if (chip->ops->req_canceled(chip, status)) {
253231
dev_err(&chip->dev, "Operation Canceled\n");
254232
rc = -ECANCELED;
255-
goto out;
233+
goto out_rc;
256234
}
257235

258236
tpm_msleep(TPM_TIMEOUT_POLL);
@@ -262,40 +240,20 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, struct tpm_space *space,
262240
chip->ops->cancel(chip);
263241
dev_err(&chip->dev, "Operation Timed out\n");
264242
rc = -ETIME;
265-
goto out;
243+
goto out_rc;
266244

267245
out_recv:
268246
len = chip->ops->recv(chip, buf, bufsiz);
269247
if (len < 0) {
270248
rc = len;
271-
dev_err(&chip->dev,
272-
"tpm_transmit: tpm_recv: error %d\n", rc);
273-
goto out;
274-
} else if (len < TPM_HEADER_SIZE) {
249+
dev_err(&chip->dev, "tpm_transmit: tpm_recv: error %d\n", rc);
250+
} else if (len < TPM_HEADER_SIZE || len != be32_to_cpu(header->length))
275251
rc = -EFAULT;
276-
goto out;
277-
}
278252

279-
if (len != be32_to_cpu(header->length)) {
280-
rc = -EFAULT;
281-
goto out;
282-
}
283-
284-
rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
253+
out_rc:
254+
if (!rc)
255+
rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
285256

286-
out:
287-
/* may fail but do not override previous error value in rc */
288-
tpm_go_idle(chip, flags);
289-
290-
out_locality:
291-
if (need_locality)
292-
tpm_relinquish_locality(chip, flags);
293-
294-
if (chip->ops->clk_enable != NULL)
295-
chip->ops->clk_enable(chip, false);
296-
297-
if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
298-
mutex_unlock(&chip->tpm_mutex);
299257
return rc ? rc : len;
300258
}
301259

@@ -325,6 +283,7 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
325283
/* space for header and handles */
326284
u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
327285
unsigned int delay_msec = TPM2_DURATION_SHORT;
286+
bool has_locality = false;
328287
u32 rc = 0;
329288
ssize_t ret;
330289
const size_t save_size = min(space ? sizeof(save) : TPM_HEADER_SIZE,
@@ -340,7 +299,40 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
340299
memcpy(save, buf, save_size);
341300

342301
for (;;) {
302+
if (!(flags & TPM_TRANSMIT_UNLOCKED) &&
303+
!(flags & TPM_TRANSMIT_NESTED))
304+
mutex_lock(&chip->tpm_mutex);
305+
306+
if (chip->ops->clk_enable != NULL)
307+
chip->ops->clk_enable(chip, true);
308+
309+
if (chip->locality == -1) {
310+
ret = tpm_request_locality(chip, flags);
311+
if (ret)
312+
goto out_locality;
313+
has_locality = true;
314+
}
315+
316+
ret = tpm_cmd_ready(chip, flags);
317+
if (ret)
318+
goto out_locality;
319+
343320
ret = tpm_try_transmit(chip, space, buf, bufsiz, flags);
321+
322+
/* This may fail but do not override ret. */
323+
tpm_go_idle(chip, flags);
324+
325+
out_locality:
326+
if (has_locality)
327+
tpm_relinquish_locality(chip, flags);
328+
329+
if (chip->ops->clk_enable != NULL)
330+
chip->ops->clk_enable(chip, false);
331+
332+
if (!(flags & TPM_TRANSMIT_UNLOCKED) &&
333+
!(flags & TPM_TRANSMIT_NESTED))
334+
mutex_unlock(&chip->tpm_mutex);
335+
344336
if (ret < 0)
345337
break;
346338
rc = be32_to_cpu(header->return_code);

drivers/char/tpm/tpm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ int tpm2_probe(struct tpm_chip *chip);
576576
int tpm2_find_cc(struct tpm_chip *chip, u32 cc);
577577
int tpm2_init_space(struct tpm_space *space);
578578
void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space);
579+
void tpm2_flush_space(struct tpm_chip *chip);
579580
int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u32 cc,
580581
u8 *cmd);
581582
int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,

drivers/char/tpm/tpm2-space.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf,
162162
return 0;
163163
}
164164

165-
static void tpm2_flush_space(struct tpm_chip *chip)
165+
void tpm2_flush_space(struct tpm_chip *chip)
166166
{
167167
struct tpm_space *space = &chip->work_space;
168168
int i;

0 commit comments

Comments
 (0)