git: f329d69b9176 - stable/14 - tpm20: fix suspend/resume and entropy harvesting

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Sun, 12 Apr 2026 13:44:25 UTC
The branch stable/14 has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=f329d69b9176a1d407276a010c2b87a07a2bbc5d

commit f329d69b9176a1d407276a010c2b87a07a2bbc5d
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-02-05 03:35:01 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-04-12 13:43:44 +0000

    tpm20: fix suspend/resume and entropy harvesting
    
    There were a few problem here:
      - TPM2_Shutdown results in a response that we need to either process
        or ignore, otherwise any tpm20_write or tpm20_harvest call will
        trivially hang on an `sc->pending_data_length != 0`
      - We should have a matching TPM2_Startup upon resume to restore any
        state that should have persisted
      - We must drain the harvest task before we suspend to avoid problems
        there
    
    This commit is sufficient to avoid breaking suspend/resume.
    
    Co-authored-by: markj
    Tested by:      garga
    
    (cherry picked from commit 38a4995eb52db21116f8b37ed942e66a8c2f050f)
---
 sys/dev/tpm/tpm20.c   | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++
 sys/dev/tpm/tpm20.h   |  1 +
 sys/dev/tpm/tpm_crb.c |  1 +
 sys/dev/tpm/tpm_tis.c |  1 +
 4 files changed, 66 insertions(+)

diff --git a/sys/dev/tpm/tpm20.c b/sys/dev/tpm/tpm20.c
index 393552172656..f2c63b9f0e93 100644
--- a/sys/dev/tpm/tpm20.c
+++ b/sys/dev/tpm/tpm20.c
@@ -45,6 +45,7 @@ static void tpm20_discard_buffer(void *arg);
 #ifdef TPM_HARVEST
 static void tpm20_harvest(void *arg, int unused);
 #endif
+static int  tpm20_restart(device_t dev, bool clear);
 static int  tpm20_save_state(device_t dev, bool suspend);
 
 static d_open_t		tpm20_open;
@@ -232,9 +233,31 @@ tpm20_release(struct tpm_sc *sc)
 		destroy_dev(sc->sc_cdev);
 }
 
+int
+tpm20_resume(device_t dev)
+{
+
+	tpm20_restart(dev, false);
+
+#if defined TPM_HARVEST || defined RANDOM_ENABLE_TPM
+	struct tpm_sc *sc;
+
+	sc = device_get_softc(dev);
+	taskqueue_enqueue_timeout(taskqueue_thread, &sc->harvest_task,
+	    hz * TPM_HARVEST_INTERVAL);
+#endif
+	return (0);
+}
+
 int
 tpm20_suspend(device_t dev)
 {
+#if defined TPM_HARVEST || defined RANDOM_ENABLE_TPM
+	struct tpm_sc *sc;
+
+	sc = device_get_softc(dev);
+	taskqueue_drain_timeout(taskqueue_thread, &sc->harvest_task);
+#endif
 	return (tpm20_save_state(dev, true));
 }
 
@@ -296,6 +319,42 @@ tpm20_harvest(void *arg, int unused)
 }
 #endif	/* TPM_HARVEST */
 
+static int
+tpm20_restart(device_t dev, bool clear)
+{
+	struct tpm_sc *sc;
+	uint8_t startup_cmd[] = {
+		0x80, 0x01,             /* TPM_ST_NO_SESSIONS tag*/
+		0x00, 0x00, 0x00, 0x0C, /* cmd length */
+		0x00, 0x00, 0x01, 0x44, /* cmd TPM_CC_Startup */
+		0x00, 0x01              /* TPM_SU_STATE */
+	};
+
+	sc = device_get_softc(dev);
+
+	/*
+	 * Inform the TPM whether we are resetting or resuming.
+	 */
+	if (clear)
+		startup_cmd[11] = 0; /* TPM_SU_CLEAR */
+
+	if (sc == NULL || sc->buf == NULL)
+		return (0);
+
+	sx_xlock(&sc->dev_lock);
+
+	MPASS(sc->pending_data_length == 0);
+	memcpy(sc->buf, startup_cmd, sizeof(startup_cmd));
+
+	/* XXX Ignoring both sc->transmit() return and tpm's response */
+	sc->transmit(sc, sizeof(startup_cmd));
+	sc->pending_data_length = 0;
+
+	sx_xunlock(&sc->dev_lock);
+
+	return (0);
+}
+
 static int
 tpm20_save_state(device_t dev, bool suspend)
 {
@@ -320,8 +379,12 @@ tpm20_save_state(device_t dev, bool suspend)
 
 	sx_xlock(&sc->dev_lock);
 
+	MPASS(sc->pending_data_length == 0);
 	memcpy(sc->buf, save_cmd, sizeof(save_cmd));
+
+	/* XXX Ignoring both sc->transmit return and tpm's response */
 	sc->transmit(sc, sizeof(save_cmd));
+	sc->pending_data_length = 0;
 
 	sx_xunlock(&sc->dev_lock);
 
diff --git a/sys/dev/tpm/tpm20.h b/sys/dev/tpm/tpm20.h
index 14f89de3e84e..1e663910cede 100644
--- a/sys/dev/tpm/tpm20.h
+++ b/sys/dev/tpm/tpm20.h
@@ -131,6 +131,7 @@ struct tpm_sc {
 };
 
 int tpm20_suspend(device_t dev);
+int tpm20_resume(device_t dev);
 int tpm20_shutdown(device_t dev);
 int32_t tpm20_get_timeout(uint32_t command);
 int tpm20_init(struct tpm_sc *sc);
diff --git a/sys/dev/tpm/tpm_crb.c b/sys/dev/tpm/tpm_crb.c
index bb4df82e613b..38640b010e18 100644
--- a/sys/dev/tpm/tpm_crb.c
+++ b/sys/dev/tpm/tpm_crb.c
@@ -411,6 +411,7 @@ static device_method_t	tpmcrb_methods[] = {
 	DEVMETHOD(device_detach,	tpmcrb_detach),
 	DEVMETHOD(device_shutdown,	tpm20_shutdown),
 	DEVMETHOD(device_suspend,	tpm20_suspend),
+	DEVMETHOD(device_resume,	tpm20_resume),
 	{0, 0}
 };
 
diff --git a/sys/dev/tpm/tpm_tis.c b/sys/dev/tpm/tpm_tis.c
index b97d7ab3f8e8..e51f5a656b05 100644
--- a/sys/dev/tpm/tpm_tis.c
+++ b/sys/dev/tpm/tpm_tis.c
@@ -510,6 +510,7 @@ static device_method_t tpmtis_methods[] = {
 	DEVMETHOD(device_detach,	tpmtis_detach),
 	DEVMETHOD(device_shutdown,	tpm20_shutdown),
 	DEVMETHOD(device_suspend,	tpm20_suspend),
+	DEVMETHOD(device_resume,	tpm20_resume),
 	{0, 0}
 };