From nobody Thu Feb 5 03:36:18 2026 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4f62v656Z4z6R736 for ; Thu, 05 Feb 2026 03:36:18 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R13" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4f62v63X1Gz3vs0 for ; Thu, 05 Feb 2026 03:36:18 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1770262578; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=i4o+W3rFQrp9Zoq2I1RWPelOM6Jcl2L/p5ywH+6EpyE=; b=Hpuli2f/M7gT27lj9RRs/F6xpQW0M442uvnHCDEFkBmzMEmOFlnKtfUlBS8EcuOVcicANu 6cdW9WwCbultix//FwNFquEmx8Z8DmuwX1NbHhpz1ypHEcEoKXcitB14+sa/8f52yIR8WV HxOrTQovV4NDdlm9t4MMu5VuwpW/jYSGsqG0i65pfttaSBrZM/kFjL3lLvdS8JYUFFcExm HjRJGCwlavGoW/1By0P/+C8cL2FzPn2Gz6CKKzX3xbJaVpxbct27fCl7fglYb8XTqFB2Nl YtDMeWEhZGfCdvv2P2JoiQoJ5hdeV+LT65dI9GUzM8uJr57/tBg1uikkoZgWjA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1770262578; a=rsa-sha256; cv=none; b=X+37noqw/H7k5FYjGyN0wypQGUMJ6ek3jJ6i2Dba4trBZ1yzzQAlVYnVTL41Mftsg/VpB3 cUWUbbsb21Lsm+uPtn1cxc4kXMnhhE0nzwIiQCAVnhVDyzHp5aGKU6wU8a1KinDBMwwiNz eDo0BYfjWNkdOQ6gr5uWURe2BFIh7BaOCyWKB9xTRzS82/x8NER8ovdvjKUHJ+DtRHyzvi QXvP+smxUfKRkTFHUFVbDxJRd6fzu2hepeBjrwk33rMf9DLoiLXACq/YpcWaPlJKxdec3y dck9CkpnnTi6TtozNJONzhXiKbgIa2fjZKvhHntpdj5cWCgbiNN7aMWCr/Zd5Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1770262578; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=i4o+W3rFQrp9Zoq2I1RWPelOM6Jcl2L/p5ywH+6EpyE=; b=wieboZDIOLP40veGYptK/kIsODhbtnxLi21M6TqfphSurk1gzHeXJjK44dRRSjGQrc5NuB ZmH/EvwzPaeO/bQQtHQSTK1cT1CyOvFpO5oSogTkseiodymrlXJDkmIDyeQACAeleNcB58 crq127mNvTpNG6aij+QbOQn/2JjRzm1zGwzVBZ8SaDz2aioIX60YiBmyEgN/tEP6RF//8S V2qwRlt3R371XgvZOex1g2rCE+gb7aj2woDtb1pgsP6L6QD+k0c5KYc6TDZgWcpSrCwHTC 8c5dW1qT79yKIASQMmWXTI/NLxJBSqg5aH3QaTusBLNBoC5Nf7HxDQBHvFH1Ow== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4f62v62v98z1R08 for ; Thu, 05 Feb 2026 03:36:18 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 4591c by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 05 Feb 2026 03:36:18 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: 38a4995eb52d - main - tpm20: fix suspend/resume and entropy harvesting List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 38a4995eb52db21116f8b37ed942e66a8c2f050f Auto-Submitted: auto-generated Date: Thu, 05 Feb 2026 03:36:18 +0000 Message-Id: <69841032.4591c.45547cc2@gitrepo.freebsd.org> The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=38a4995eb52db21116f8b37ed942e66a8c2f050f commit 38a4995eb52db21116f8b37ed942e66a8c2f050f Author: Kyle Evans AuthorDate: 2026-02-05 03:35:01 +0000 Commit: Kyle Evans CommitDate: 2026-02-05 03:35:10 +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. PR: 291067 Fixes: a2d5ed9442bb ("Introduce driver for TPM 2.0 in CRB and [...]") Fixes: 4ee7d3b0118c ("Allow using TPM as entropy source.") Co-authored-by: markj (D53835) Tested by: garga Differential Revision: https://reviews.freebsd.org/D55074 --- sys/dev/tpm/tpm20.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++ sys/dev/tpm/tpm20.h | 1 + sys/dev/tpm/tpm_crb.c | 1 + sys/dev/tpm/tpm_tis_core.c | 1 + 4 files changed, 68 insertions(+) diff --git a/sys/dev/tpm/tpm20.c b/sys/dev/tpm/tpm20.c index 6c587818058d..c521ca9dda9d 100644 --- a/sys/dev/tpm/tpm20.c +++ b/sys/dev/tpm/tpm20.c @@ -45,6 +45,7 @@ static void tpm20_discard_buffer(void *arg); #if defined TPM_HARVEST || defined RANDOM_ENABLE_TPM 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; @@ -242,9 +243,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)); } @@ -307,6 +330,43 @@ 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 TPM_TRANSMIT return and tpm's response */ + TPM_TRANSMIT(sc->dev, sizeof(startup_cmd)); + sc->pending_data_length = 0; + sc->total_length = 0; + + sx_xunlock(&sc->dev_lock); + + return (0); +} + static int tpm20_save_state(device_t dev, bool suspend) { @@ -331,8 +391,13 @@ 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 TPM_TRANSMIT return and tpm's response */ TPM_TRANSMIT(sc->dev, sizeof(save_cmd)); + sc->pending_data_length = 0; + sc->total_length = 0; sx_xunlock(&sc->dev_lock); diff --git a/sys/dev/tpm/tpm20.h b/sys/dev/tpm/tpm20.h index b2cfcd4f25bd..96b2920283b6 100644 --- a/sys/dev/tpm/tpm20.h +++ b/sys/dev/tpm/tpm20.h @@ -136,6 +136,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 26b53e7a621d..18414a6c799b 100644 --- a/sys/dev/tpm/tpm_crb.c +++ b/sys/dev/tpm/tpm_crb.c @@ -651,6 +651,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), DEVMETHOD(tpm_transmit, tpmcrb_transmit), {0, 0} }; diff --git a/sys/dev/tpm/tpm_tis_core.c b/sys/dev/tpm/tpm_tis_core.c index 4159de4daf3b..34ecfcc283b1 100644 --- a/sys/dev/tpm/tpm_tis_core.c +++ b/sys/dev/tpm/tpm_tis_core.c @@ -487,6 +487,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), DEVMETHOD(tpm_transmit, tpmtis_transmit), DEVMETHOD_END };