Skip to content

Commit c82a330

Browse files
Tomas WinklerJarkko Sakkinen
authored andcommitted
tpm: factor out tpm 1.x pm suspend flow into tpm1-cmd.c
Factor out TPM 1.x suspend flow from tpm-interface.c into a new function tpm1_pm_suspend() in tpm1-cmd.c Signed-off-by: Tomas Winkler <tomas.winkler@intel.com> Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Tested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
1 parent d4a3175 commit c82a330

3 files changed

Lines changed: 68 additions & 50 deletions

File tree

‎drivers/char/tpm/tpm-interface.c‎

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -585,70 +585,25 @@ int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
585585
}
586586
EXPORT_SYMBOL_GPL(tpm_send);
587587

588-
#define TPM_ORD_SAVESTATE 152
589-
#define SAVESTATE_RESULT_SIZE 10
590-
591-
static const struct tpm_input_header savestate_header = {
592-
.tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
593-
.length = cpu_to_be32(10),
594-
.ordinal = cpu_to_be32(TPM_ORD_SAVESTATE)
595-
};
596-
597588
/*
598589
* We are about to suspend. Save the TPM state
599590
* so that it can be restored.
600591
*/
601592
int tpm_pm_suspend(struct device *dev)
602593
{
603594
struct tpm_chip *chip = dev_get_drvdata(dev);
604-
struct tpm_cmd_t cmd;
605-
int rc, try;
606-
607-
u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
595+
int rc = 0;
608596

609-
if (chip == NULL)
597+
if (!chip)
610598
return -ENODEV;
611599

612600
if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
613601
return 0;
614602

615-
if (chip->flags & TPM_CHIP_FLAG_TPM2) {
603+
if (chip->flags & TPM_CHIP_FLAG_TPM2)
616604
tpm2_shutdown(chip, TPM2_SU_STATE);
617-
return 0;
618-
}
619-
620-
/* for buggy tpm, flush pcrs with extend to selected dummy */
621-
if (tpm_suspend_pcr)
622-
rc = tpm1_pcr_extend(chip, tpm_suspend_pcr, dummy_hash,
623-
"extending dummy pcr before suspend");
624-
625-
/* now do the actual savestate */
626-
for (try = 0; try < TPM_RETRY; try++) {
627-
cmd.header.in = savestate_header;
628-
rc = tpm_transmit_cmd(chip, NULL, &cmd, SAVESTATE_RESULT_SIZE,
629-
0, 0, NULL);
630-
631-
/*
632-
* If the TPM indicates that it is too busy to respond to
633-
* this command then retry before giving up. It can take
634-
* several seconds for this TPM to be ready.
635-
*
636-
* This can happen if the TPM has already been sent the
637-
* SaveState command before the driver has loaded. TCG 1.2
638-
* specification states that any communication after SaveState
639-
* may cause the TPM to invalidate previously saved state.
640-
*/
641-
if (rc != TPM_WARN_RETRY)
642-
break;
643-
tpm_msleep(TPM_TIMEOUT_RETRY);
644-
}
645-
646-
if (rc)
647-
dev_err(&chip->dev,
648-
"Error (%d) sending savestate before suspend\n", rc);
649-
else if (try > 0)
650-
dev_warn(&chip->dev, "TPM savestate took %dms\n",
651-
try * TPM_TIMEOUT_RETRY);
605+
else
606+
rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
652607

653608
return rc;
654609
}

‎drivers/char/tpm/tpm.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
542542
const char *desc);
543543
int tpm_startup(struct tpm_chip *chip);
544544
int tpm_get_timeouts(struct tpm_chip *);
545+
546+
int tpm1_pm_suspend(struct tpm_chip *chip, int tpm_suspend_pcr);
545547
int tpm1_auto_startup(struct tpm_chip *chip);
546548
int tpm1_do_selftest(struct tpm_chip *chip);
547549
int tpm1_get_timeouts(struct tpm_chip *chip);

‎drivers/char/tpm/tpm1-cmd.c‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,3 +666,64 @@ int tpm1_auto_startup(struct tpm_chip *chip)
666666
rc = -ENODEV;
667667
return rc;
668668
}
669+
670+
#define TPM_ORD_SAVESTATE 152
671+
#define SAVESTATE_RESULT_SIZE 10
672+
static const struct tpm_input_header savestate_header = {
673+
.tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
674+
.length = cpu_to_be32(10),
675+
.ordinal = cpu_to_be32(TPM_ORD_SAVESTATE)
676+
};
677+
678+
/**
679+
* tpm1_pm_suspend() - pm suspend handler
680+
* @chip: TPM chip to use.
681+
* @tpm_suspend_pcr: flush pcr for buggy TPM chips.
682+
*
683+
* The functions saves the TPM state to be restored on resume.
684+
*
685+
* Return:
686+
* * 0 on success,
687+
* * < 0 on error.
688+
*/
689+
int tpm1_pm_suspend(struct tpm_chip *chip, int tpm_suspend_pcr)
690+
{
691+
u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
692+
struct tpm_cmd_t cmd;
693+
int rc, try;
694+
695+
/* for buggy tpm, flush pcrs with extend to selected dummy */
696+
if (tpm_suspend_pcr)
697+
rc = tpm1_pcr_extend(chip, tpm_suspend_pcr, dummy_hash,
698+
"extending dummy pcr before suspend");
699+
700+
/* now do the actual savestate */
701+
for (try = 0; try < TPM_RETRY; try++) {
702+
cmd.header.in = savestate_header;
703+
rc = tpm_transmit_cmd(chip, NULL, &cmd, SAVESTATE_RESULT_SIZE,
704+
0, 0, NULL);
705+
706+
/*
707+
* If the TPM indicates that it is too busy to respond to
708+
* this command then retry before giving up. It can take
709+
* several seconds for this TPM to be ready.
710+
*
711+
* This can happen if the TPM has already been sent the
712+
* SaveState command before the driver has loaded. TCG 1.2
713+
* specification states that any communication after SaveState
714+
* may cause the TPM to invalidate previously saved state.
715+
*/
716+
if (rc != TPM_WARN_RETRY)
717+
break;
718+
tpm_msleep(TPM_TIMEOUT_RETRY);
719+
}
720+
721+
if (rc)
722+
dev_err(&chip->dev, "Error (%d) sending savestate before suspend\n",
723+
rc);
724+
else if (try > 0)
725+
dev_warn(&chip->dev, "TPM savestate took %dms\n",
726+
try * TPM_TIMEOUT_RETRY);
727+
728+
return rc;
729+
}

0 commit comments

Comments
 (0)