tpm: Factor out common startup code
authorJason Gunthorpe <jgunthorpe@obsidianresearch.com>
Tue, 12 Jul 2016 17:41:49 +0000 (11:41 -0600)
committerJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Tue, 19 Jul 2016 14:43:38 +0000 (17:43 +0300)
The TCG standard startup sequence (get timeouts, tpm startup, etc) for
TPM and TPM2 chips is being open coded in many drivers, move it into
the core code.

tpm_tis and tpm_crb are used as the basis for the core code
implementation and the easy drivers are converted. In the process
several small drivers bugs relating to error handling this flow
are fixed.

For now the flag TPM_OPS_AUTO_STARTUP is optional to allow a staged
driver roll out, but ultimately all drivers should use this flow and
the flag removed. Some drivers still do not implement the startup
sequence at all and will need to be tested with it enabled.

Signed-off-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
Tested-by: Andrew Zamansky <andrew.zamansky@nuvoton.com>
Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
12 files changed:
drivers/char/tpm/st33zp24/st33zp24.c
drivers/char/tpm/tpm-chip.c
drivers/char/tpm/tpm-interface.c
drivers/char/tpm/tpm.h
drivers/char/tpm/tpm2-cmd.c
drivers/char/tpm/tpm_crb.c
drivers/char/tpm/tpm_i2c_atmel.c
drivers/char/tpm/tpm_i2c_infineon.c
drivers/char/tpm/tpm_i2c_nuvoton.c
drivers/char/tpm/tpm_tis_core.c
drivers/char/tpm/tpm_vtpm_proxy.c
include/linux/tpm.h

index a7c99a284724f3cf02dc3e2027fcdcbfe4d0c48e..c2ee30451e41d8632b01ae538f659aeb24a6e83d 100644 (file)
@@ -505,6 +505,7 @@ static bool st33zp24_req_canceled(struct tpm_chip *chip, u8 status)
 }
 
 static const struct tpm_class_ops st33zp24_tpm = {
+       .flags = TPM_OPS_AUTO_STARTUP,
        .send = st33zp24_send,
        .recv = st33zp24_recv,
        .cancel = st33zp24_cancel,
@@ -592,9 +593,6 @@ int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops,
                tpm_gen_interrupt(chip);
        }
 
-       tpm_get_timeouts(chip);
-       tpm_do_selftest(chip);
-
        return tpm_chip_register(chip);
 _tpm_clean_answer:
        dev_info(&chip->dev, "TPM initialization fail\n");
index 5a2f0439ef47ef8c35e16580271a0fd686d9af5a..e5950131bd907dbee3f4b4c3598c05572fcea416 100644 (file)
@@ -354,6 +354,15 @@ int tpm_chip_register(struct tpm_chip *chip)
 {
        int rc;
 
+       if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
+               if (chip->flags & TPM_CHIP_FLAG_TPM2)
+                       rc = tpm2_auto_startup(chip);
+               else
+                       rc = tpm1_auto_startup(chip);
+               if (rc)
+                       return rc;
+       }
+
        rc = tpm1_chip_register(chip);
        if (rc)
                return rc;
index 5e3c1b6848596f39837d408591bfa6ea749626b3..1abe2d7a2610708cc9f1101b83f248b6d3f058c0 100644 (file)
@@ -843,6 +843,33 @@ int tpm_do_selftest(struct tpm_chip *chip)
 }
 EXPORT_SYMBOL_GPL(tpm_do_selftest);
 
+/**
+ * tpm1_auto_startup - Perform the standard automatic TPM initialization
+ *                     sequence
+ * @chip: TPM chip to use
+ *
+ * Returns 0 on success, < 0 in case of fatal error.
+ */
+int tpm1_auto_startup(struct tpm_chip *chip)
+{
+       int rc;
+
+       rc = tpm_get_timeouts(chip);
+       if (rc)
+               goto out;
+       rc = tpm_do_selftest(chip);
+       if (rc) {
+               dev_err(&chip->dev, "TPM self test failed\n");
+               goto out;
+       }
+
+       return rc;
+out:
+       if (rc > 0)
+               rc = -ENODEV;
+       return rc;
+}
+
 int tpm_send(u32 chip_num, void *cmd, size_t buflen)
 {
        struct tpm_chip *chip;
index 8890df2056352fadd5f6a192be8e0ede8b0cb8a7..3e32d5bd2dc63054d644558283f453ab72061130 100644 (file)
@@ -484,6 +484,7 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, void *cmd, int len,
                         const char *desc);
 extern int tpm_get_timeouts(struct tpm_chip *);
 extern void tpm_gen_interrupt(struct tpm_chip *);
+int tpm1_auto_startup(struct tpm_chip *chip);
 extern int tpm_do_selftest(struct tpm_chip *);
 extern unsigned long tpm_calc_ordinal_duration(struct tpm_chip *, u32);
 extern int tpm_pm_suspend(struct device *);
@@ -526,10 +527,9 @@ int tpm2_unseal_trusted(struct tpm_chip *chip,
 ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
                        u32 *value, const char *desc);
 
-extern int tpm2_startup(struct tpm_chip *chip, u16 startup_type);
+int tpm2_auto_startup(struct tpm_chip *chip);
 extern void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
 extern unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *, u32);
-extern int tpm2_do_selftest(struct tpm_chip *chip);
 extern int tpm2_gen_interrupt(struct tpm_chip *chip);
 extern int tpm2_probe(struct tpm_chip *chip);
 #endif
index a88b31e7fe72026b6d87457ce0542237a60e64da..08c7e23ed535bd665d3a9a6db68293d2ae93860b 100644 (file)
@@ -728,7 +728,7 @@ static const struct tpm_input_header tpm2_startup_header = {
  * returned it remarks a POSIX error code. If a positive number is returned
  * it remarks a TPM error.
  */
-int tpm2_startup(struct tpm_chip *chip, u16 startup_type)
+static int tpm2_startup(struct tpm_chip *chip, u16 startup_type)
 {
        struct tpm2_cmd cmd;
 
@@ -738,7 +738,6 @@ int tpm2_startup(struct tpm_chip *chip, u16 startup_type)
        return tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
                                "attempting to start the TPM");
 }
-EXPORT_SYMBOL_GPL(tpm2_startup);
 
 #define TPM2_SHUTDOWN_IN_SIZE \
        (sizeof(struct tpm_input_header) + \
@@ -854,7 +853,7 @@ static int tpm2_start_selftest(struct tpm_chip *chip, bool full)
  * returned it remarks a POSIX error code. If a positive number is returned
  * it remarks a TPM error.
  */
-int tpm2_do_selftest(struct tpm_chip *chip)
+static int tpm2_do_selftest(struct tpm_chip *chip)
 {
        int rc;
        unsigned int loops;
@@ -894,7 +893,6 @@ int tpm2_do_selftest(struct tpm_chip *chip)
 
        return rc;
 }
-EXPORT_SYMBOL_GPL(tpm2_do_selftest);
 
 /**
  * tpm2_gen_interrupt() - generate an interrupt
@@ -942,3 +940,43 @@ int tpm2_probe(struct tpm_chip *chip)
        return 0;
 }
 EXPORT_SYMBOL_GPL(tpm2_probe);
+
+/**
+ * tpm2_auto_startup - Perform the standard automatic TPM initialization
+ *                     sequence
+ * @chip: TPM chip to use
+ *
+ * Returns 0 on success, < 0 in case of fatal error.
+ */
+int tpm2_auto_startup(struct tpm_chip *chip)
+{
+       int rc;
+
+       rc = tpm_get_timeouts(chip);
+       if (rc)
+               goto out;
+
+       rc = tpm2_do_selftest(chip);
+       if (rc != TPM2_RC_INITIALIZE) {
+               dev_err(&chip->dev, "TPM self test failed\n");
+               goto out;
+       }
+
+       if (rc == TPM2_RC_INITIALIZE) {
+               rc = tpm2_startup(chip, TPM2_SU_CLEAR);
+               if (rc)
+                       goto out;
+
+               rc = tpm2_do_selftest(chip);
+               if (rc) {
+                       dev_err(&chip->dev, "TPM self test failed\n");
+                       goto out;
+               }
+       }
+
+       return rc;
+out:
+       if (rc > 0)
+               rc = -ENODEV;
+       return rc;
+}
index 1b8e1b51bfc06a47f77eadd6ebc953cfa9f8b678..018c382554ba2cefcf8cd11fff52ec9634c35495 100644 (file)
@@ -188,6 +188,7 @@ static bool crb_req_canceled(struct tpm_chip *chip, u8 status)
 }
 
 static const struct tpm_class_ops tpm_crb = {
+       .flags = TPM_OPS_AUTO_STARTUP,
        .status = crb_status,
        .recv = crb_recv,
        .send = crb_send,
@@ -200,7 +201,6 @@ static const struct tpm_class_ops tpm_crb = {
 static int crb_init(struct acpi_device *device, struct crb_priv *priv)
 {
        struct tpm_chip *chip;
-       int rc;
 
        chip = tpmm_chip_alloc(&device->dev, &tpm_crb);
        if (IS_ERR(chip))
@@ -210,14 +210,6 @@ static int crb_init(struct acpi_device *device, struct crb_priv *priv)
        chip->acpi_dev_handle = device->handle;
        chip->flags = TPM_CHIP_FLAG_TPM2;
 
-       rc = tpm_get_timeouts(chip);
-       if (rc)
-               return rc;
-
-       rc = tpm2_do_selftest(chip);
-       if (rc)
-               return rc;
-
        return tpm_chip_register(chip);
 }
 
index c37aa7259f76aab40adaceb4e1fe0a6727fce041..95ce2e9ccdc6e2ec40c7d52b0f35d17ba260001e 100644 (file)
@@ -141,6 +141,7 @@ static bool i2c_atmel_req_canceled(struct tpm_chip *chip, u8 status)
 }
 
 static const struct tpm_class_ops i2c_atmel = {
+       .flags = TPM_OPS_AUTO_STARTUP,
        .status = i2c_atmel_read_status,
        .recv = i2c_atmel_recv,
        .send = i2c_atmel_send,
@@ -179,11 +180,6 @@ static int i2c_atmel_probe(struct i2c_client *client,
        /* There is no known way to probe for this device, and all version
         * information seems to be read via TPM commands. Thus we rely on the
         * TPM startup process in the common code to detect the device. */
-       if (tpm_get_timeouts(chip))
-               return -ENODEV;
-
-       if (tpm_do_selftest(chip))
-               return -ENODEV;
 
        return tpm_chip_register(chip);
 }
index a426b6f67d2e0d356d13f48ea341a0e63c202c50..62ee44e57ddc4478d41d9b2082d0e4e27df57b04 100644 (file)
@@ -567,6 +567,7 @@ static bool tpm_tis_i2c_req_canceled(struct tpm_chip *chip, u8 status)
 }
 
 static const struct tpm_class_ops tpm_tis_i2c = {
+       .flags = TPM_OPS_AUTO_STARTUP,
        .status = tpm_tis_i2c_status,
        .recv = tpm_tis_i2c_recv,
        .send = tpm_tis_i2c_send,
@@ -619,9 +620,6 @@ static int tpm_tis_i2c_init(struct device *dev)
 
        tpm_dev.chip = chip;
 
-       tpm_get_timeouts(chip);
-       tpm_do_selftest(chip);
-
        return tpm_chip_register(chip);
 out_release:
        release_locality(chip, tpm_dev.locality, 1);
index 82669823433637698dbbacebeb61af5dced7483d..6e404e0211ddd2b76e4e65a2059a936ca37225aa 100644 (file)
@@ -461,6 +461,7 @@ static bool i2c_nuvoton_req_canceled(struct tpm_chip *chip, u8 status)
 }
 
 static const struct tpm_class_ops tpm_i2c = {
+       .flags = TPM_OPS_AUTO_STARTUP,
        .status = i2c_nuvoton_read_status,
        .recv = i2c_nuvoton_recv,
        .send = i2c_nuvoton_send,
@@ -607,12 +608,6 @@ static int i2c_nuvoton_probe(struct i2c_client *client,
                }
        }
 
-       if (tpm_get_timeouts(chip))
-               return -ENODEV;
-
-       if (tpm_do_selftest(chip))
-               return -ENODEV;
-
        return tpm_chip_register(chip);
 }
 
index fb8c3de557469088a2b41cc4066a7746eb305a75..d66f51b3648e9d9113fb448074bacf0ca09dfcf5 100644 (file)
@@ -638,6 +638,7 @@ void tpm_tis_remove(struct tpm_chip *chip)
 EXPORT_SYMBOL_GPL(tpm_tis_remove);
 
 static const struct tpm_class_ops tpm_tis = {
+       .flags = TPM_OPS_AUTO_STARTUP,
        .status = tpm_tis_status,
        .recv = tpm_tis_recv,
        .send = tpm_tis_send,
@@ -773,29 +774,6 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
                }
        }
 
-       if (chip->flags & TPM_CHIP_FLAG_TPM2) {
-               rc = tpm2_do_selftest(chip);
-               if (rc == TPM2_RC_INITIALIZE) {
-                       dev_warn(dev, "Firmware has not started TPM\n");
-                       rc  = tpm2_startup(chip, TPM2_SU_CLEAR);
-                       if (!rc)
-                               rc = tpm2_do_selftest(chip);
-               }
-
-               if (rc) {
-                       dev_err(dev, "TPM self test failed\n");
-                       if (rc > 0)
-                               rc = -ENODEV;
-                       goto out_err;
-               }
-       } else {
-               if (tpm_do_selftest(chip)) {
-                       dev_err(dev, "TPM self test failed\n");
-                       rc = -ENODEV;
-                       goto out_err;
-               }
-       }
-
        return tpm_chip_register(chip);
 out_err:
        tpm_tis_remove(chip);
index 86e27e823d4dab2016712efe7612c49ce02979a8..9a940332c15758ae3f818301eea526a79d4dff9d 100644 (file)
@@ -346,6 +346,7 @@ static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip  *chip, u8 status)
 }
 
 static const struct tpm_class_ops vtpm_proxy_tpm_ops = {
+       .flags = TPM_OPS_AUTO_STARTUP,
        .recv = vtpm_proxy_tpm_op_recv,
        .send = vtpm_proxy_tpm_op_send,
        .cancel = vtpm_proxy_tpm_op_cancel,
@@ -366,14 +367,6 @@ static void vtpm_proxy_work(struct work_struct *work)
                                                   work);
        int rc;
 
-       if (proxy_dev->flags & VTPM_PROXY_FLAG_TPM2)
-               rc = tpm2_startup(proxy_dev->chip, TPM2_SU_CLEAR);
-       else
-               rc = tpm_get_timeouts(proxy_dev->chip);
-
-       if (rc)
-               goto err;
-
        rc = tpm_chip_register(proxy_dev->chip);
        if (rc)
                goto err;
index 706e63eea0800123d50dae79cfae327f6dacaa51..da158f06e0b2fec35f10e2faea57d806696e4d20 100644 (file)
@@ -33,7 +33,12 @@ struct tpm_chip;
 struct trusted_key_payload;
 struct trusted_key_options;
 
+enum TPM_OPS_FLAGS {
+       TPM_OPS_AUTO_STARTUP = BIT(0),
+};
+
 struct tpm_class_ops {
+       unsigned int flags;
        const u8 req_complete_mask;
        const u8 req_complete_val;
        bool (*req_canceled)(struct tpm_chip *chip, u8 status);