Commit cacc6e22 authored by Mario Limonciello's avatar Mario Limonciello Committed by Linus Torvalds
Browse files

tpm: Add a helper for checking hwrng enabled



The same checks are repeated in three places to decide whether to use
hwrng.  Consolidate these into a helper.

Also this fixes a case that one of them was missing a check in the
cleanup path.

Fixes: 554b841d ("tpm: Disable RNG for all AMD fTPMs")
Signed-off-by: default avatarMario Limonciello <mario.limonciello@amd.com>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 22883973
Loading
Loading
Loading
Loading
+14 −5
Original line number Diff line number Diff line
@@ -521,10 +521,20 @@ static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
	return tpm_get_random(chip, data, max);
}

static bool tpm_is_hwrng_enabled(struct tpm_chip *chip)
{
	if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
		return false;
	if (tpm_is_firmware_upgrade(chip))
		return false;
	if (chip->flags & TPM_CHIP_FLAG_HWRNG_DISABLED)
		return false;
	return true;
}

static int tpm_add_hwrng(struct tpm_chip *chip)
{
	if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM) || tpm_is_firmware_upgrade(chip) ||
	    chip->flags & TPM_CHIP_FLAG_HWRNG_DISABLED)
	if (!tpm_is_hwrng_enabled(chip))
		return 0;

	snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
@@ -629,7 +639,7 @@ int tpm_chip_register(struct tpm_chip *chip)
	return 0;

out_hwrng:
	if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip))
	if (tpm_is_hwrng_enabled(chip))
		hwrng_unregister(&chip->hwrng);
out_ppi:
	tpm_bios_log_teardown(chip);
@@ -654,8 +664,7 @@ EXPORT_SYMBOL_GPL(tpm_chip_register);
void tpm_chip_unregister(struct tpm_chip *chip)
{
	tpm_del_legacy_sysfs(chip);
	if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip) &&
	    !(chip->flags & TPM_CHIP_FLAG_HWRNG_DISABLED))
	if (tpm_is_hwrng_enabled(chip))
		hwrng_unregister(&chip->hwrng);
	tpm_bios_log_teardown(chip);
	if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip))