^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2004 IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2014 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Leendert van Doorn <leendert@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Dave Safford <safford@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Reiner Sailer <sailer@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Kylene Hall <kjhall@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Maintained by: <tpmdd-devel@lists.sourceforge.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * TPM chip management routines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/freezer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/tpm_eventlog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/hw_random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "tpm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) DEFINE_IDR(dev_nums_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static DEFINE_MUTEX(idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct class *tpm_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct class *tpmrm_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) dev_t tpm_devt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int tpm_request_locality(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (!chip->ops->request_locality)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) rc = chip->ops->request_locality(chip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) chip->locality = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static void tpm_relinquish_locality(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (!chip->ops->relinquish_locality)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) rc = chip->ops->relinquish_locality(chip, chip->locality);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) chip->locality = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int tpm_cmd_ready(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (!chip->ops->cmd_ready)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return chip->ops->cmd_ready(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static int tpm_go_idle(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (!chip->ops->go_idle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return chip->ops->go_idle(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static void tpm_clk_enable(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (chip->ops->clk_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) chip->ops->clk_enable(chip, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static void tpm_clk_disable(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (chip->ops->clk_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) chip->ops->clk_enable(chip, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * tpm_chip_start() - power on the TPM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * @chip: a TPM chip to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * * The response length - OK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * * -errno - A system error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int tpm_chip_start(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) tpm_clk_enable(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (chip->locality == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ret = tpm_request_locality(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) tpm_clk_disable(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ret = tpm_cmd_ready(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) tpm_relinquish_locality(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) tpm_clk_disable(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) EXPORT_SYMBOL_GPL(tpm_chip_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * tpm_chip_stop() - power off the TPM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * @chip: a TPM chip to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * * The response length - OK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * * -errno - A system error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) void tpm_chip_stop(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) tpm_go_idle(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) tpm_relinquish_locality(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) tpm_clk_disable(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) EXPORT_SYMBOL_GPL(tpm_chip_stop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * tpm_try_get_ops() - Get a ref to the tpm_chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * @chip: Chip to ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * The caller must already have some kind of locking to ensure that chip is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * valid. This function will lock the chip so that the ops member can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * accessed safely. The locking prevents tpm_chip_unregister from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * completing, so it should not be held for long periods.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * Returns -ERRNO if the chip could not be got.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int tpm_try_get_ops(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) get_device(&chip->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) down_read(&chip->ops_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!chip->ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) goto out_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) mutex_lock(&chip->tpm_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) rc = tpm_chip_start(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) goto out_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) out_lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) mutex_unlock(&chip->tpm_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) out_ops:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) up_read(&chip->ops_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) put_device(&chip->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) EXPORT_SYMBOL_GPL(tpm_try_get_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * tpm_put_ops() - Release a ref to the tpm_chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * @chip: Chip to put
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * be kfree'd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) void tpm_put_ops(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) tpm_chip_stop(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) mutex_unlock(&chip->tpm_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) up_read(&chip->ops_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) put_device(&chip->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) EXPORT_SYMBOL_GPL(tpm_put_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * tpm_default_chip() - find a TPM chip and get a reference to it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct tpm_chip *tpm_default_chip(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct tpm_chip *chip, *res = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int chip_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) int chip_prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) mutex_lock(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) chip_prev = chip_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) chip = idr_get_next(&dev_nums_idr, &chip_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (chip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) get_device(&chip->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) res = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) } while (chip_prev != chip_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) mutex_unlock(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) EXPORT_SYMBOL_GPL(tpm_default_chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * tpm_find_get_ops() - find and reserve a TPM chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * @chip: a &struct tpm_chip instance, %NULL for the default chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * Finds a TPM chip and reserves its class device and operations. The chip must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * be released with tpm_put_ops() after use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * This function is for internal use only. It supports existing TPM callers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * by accepting NULL, but those callers should be converted to pass in a chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * A reserved &struct tpm_chip instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * %NULL if a chip is not found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * %NULL if the chip is not available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (chip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (!tpm_try_get_ops(chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) chip = tpm_default_chip();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) rc = tpm_try_get_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /* release additional reference we got from tpm_default_chip() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) put_device(&chip->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * tpm_dev_release() - free chip memory and the device number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * @dev: the character device for the TPM chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * This is used as the release function for the character device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static void tpm_dev_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) mutex_lock(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) idr_remove(&dev_nums_idr, chip->dev_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) mutex_unlock(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) kfree(chip->log.bios_event_log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) kfree(chip->work_space.context_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) kfree(chip->work_space.session_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) kfree(chip->allocated_banks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) kfree(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * tpm_class_shutdown() - prepare the TPM device for loss of power.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * @dev: device to which the chip is associated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * Issues a TPM2_Shutdown command prior to loss of power, as required by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * TPM 2.0 spec. Then, calls bus- and device- specific shutdown code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * Return: always 0 (i.e. success)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static int tpm_class_shutdown(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) down_write(&chip->ops_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (chip->flags & TPM_CHIP_FLAG_TPM2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (!tpm_chip_start(chip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) tpm2_shutdown(chip, TPM2_SU_CLEAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) tpm_chip_stop(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) chip->ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) up_write(&chip->ops_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * tpm_chip_alloc() - allocate a new struct tpm_chip instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * @pdev: device to which the chip is associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * At this point pdev mst be initialized, but does not have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * be registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * @ops: struct tpm_class_ops instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * Allocates a new struct tpm_chip instance and assigns a free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * device number for it. Must be paired with put_device(&chip->dev).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct tpm_chip *tpm_chip_alloc(struct device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) const struct tpm_class_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) struct tpm_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) chip = kzalloc(sizeof(*chip), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (chip == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) mutex_init(&chip->tpm_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) init_rwsem(&chip->ops_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) chip->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) mutex_lock(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) mutex_unlock(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) dev_err(pdev, "No available tpm device numbers\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) kfree(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) chip->dev_num = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) device_initialize(&chip->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) chip->dev.class = tpm_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) chip->dev.class->shutdown_pre = tpm_class_shutdown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) chip->dev.release = tpm_dev_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) chip->dev.parent = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) chip->dev.groups = chip->groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (chip->dev_num == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (!pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) cdev_init(&chip->cdev, &tpm_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) chip->cdev.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) chip->locality = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) put_device(&chip->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) EXPORT_SYMBOL_GPL(tpm_chip_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * @pdev: parent device to which the chip is associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * @ops: struct tpm_class_ops instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * Same as tpm_chip_alloc except devm is used to do the put_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) const struct tpm_class_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) struct tpm_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) chip = tpm_chip_alloc(pdev, ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (IS_ERR(chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) rc = devm_add_action_or_reset(pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) (void (*)(void *)) put_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) &chip->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) dev_set_drvdata(pdev, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) static int tpm_add_char_device(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) rc = cdev_device_add(&chip->cdev, &chip->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) dev_err(&chip->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) dev_name(&chip->dev), MAJOR(chip->dev.devt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) MINOR(chip->dev.devt), rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (chip->flags & TPM_CHIP_FLAG_TPM2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) rc = tpm_devs_add(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) goto err_del_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) /* Make the chip available. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) mutex_lock(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) idr_replace(&dev_nums_idr, chip, chip->dev_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) mutex_unlock(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) err_del_cdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) cdev_device_del(&chip->cdev, &chip->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) static void tpm_del_char_device(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) cdev_device_del(&chip->cdev, &chip->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) /* Make the chip unavailable. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) mutex_lock(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) idr_replace(&dev_nums_idr, NULL, chip->dev_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) mutex_unlock(&idr_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /* Make the driver uncallable. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) down_write(&chip->ops_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (chip->flags & TPM_CHIP_FLAG_TPM2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (!tpm_chip_start(chip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) tpm2_shutdown(chip, TPM2_SU_CLEAR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) tpm_chip_stop(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) chip->ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) up_write(&chip->ops_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) struct attribute **i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) for (i = chip->groups[0]->attrs; *i != NULL; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) /* For compatibility with legacy sysfs paths we provide symlinks from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * parent dev directory to selected names within the tpm chip directory. Old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * kernel versions created these files directly under the parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) struct attribute **i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) rc = compat_only_sysfs_link_entry_to_kobj(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (rc && rc != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) /* All the names from tpm-sysfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) rc = compat_only_sysfs_link_entry_to_kobj(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) tpm_del_legacy_sysfs(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) return tpm_get_random(chip, data, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) static int tpm_add_hwrng(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) "tpm-rng-%d", chip->dev_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) chip->hwrng.name = chip->hwrng_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) chip->hwrng.read = tpm_hwrng_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return hwrng_register(&chip->hwrng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) static int tpm_get_pcr_allocation(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) tpm2_get_pcr_allocation(chip) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) tpm1_get_pcr_allocation(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (rc > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * tpm_chip_register() - create a character device for the TPM chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * @chip: TPM chip to use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * Creates a character device for the TPM chip and adds sysfs attributes for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * the device. As the last step this function adds the chip to the list of TPM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) * chips available for in-kernel use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * This function should be only called after the chip initialization is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) int tpm_chip_register(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) rc = tpm_chip_start(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) rc = tpm_auto_startup(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) tpm_chip_stop(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) rc = tpm_get_pcr_allocation(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) tpm_chip_stop(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) tpm_sysfs_add_device(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) tpm_bios_log_setup(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) tpm_add_ppi(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) rc = tpm_add_hwrng(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) goto out_ppi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) rc = tpm_add_char_device(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) goto out_hwrng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) rc = tpm_add_legacy_sysfs(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) tpm_chip_unregister(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) out_hwrng:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) hwrng_unregister(&chip->hwrng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) out_ppi:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) tpm_bios_log_teardown(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) EXPORT_SYMBOL_GPL(tpm_chip_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) * tpm_chip_unregister() - release the TPM driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) * @chip: TPM chip to use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) * Takes the chip first away from the list of available TPM chips and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) * cleans up all the resources reserved by tpm_chip_register().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) * Once this function returns the driver call backs in 'op's will not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * running and will no longer start.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) * NOTE: This function should be only called before deinitializing chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) void tpm_chip_unregister(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) tpm_del_legacy_sysfs(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) hwrng_unregister(&chip->hwrng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) tpm_bios_log_teardown(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (chip->flags & TPM_CHIP_FLAG_TPM2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) tpm_devs_remove(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) tpm_del_char_device(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) EXPORT_SYMBOL_GPL(tpm_chip_unregister);