tpm: Fix suspend regression
[cascardo/linux.git] / drivers / char / tpm / tpm-interface.c
index e2fa89c..5e3c1b6 100644 (file)
@@ -319,7 +319,7 @@ unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
                duration_idx = tpm_ordinal_duration[ordinal];
 
        if (duration_idx != TPM_UNDEFINED)
-               duration = chip->vendor.duration[duration_idx];
+               duration = chip->duration[duration_idx];
        if (duration <= 0)
                return 2 * 60 * HZ;
        else
@@ -345,7 +345,7 @@ ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
        if (count == 0)
                return -ENODATA;
        if (count > bufsiz) {
-               dev_err(chip->pdev,
+               dev_err(&chip->dev,
                        "invalid count value %x %zx\n", count, bufsiz);
                return -E2BIG;
        }
@@ -354,12 +354,12 @@ ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
 
        rc = chip->ops->send(chip, (u8 *) buf, count);
        if (rc < 0) {
-               dev_err(chip->pdev,
+               dev_err(&chip->dev,
                        "tpm_transmit: tpm_send: error %zd\n", rc);
                goto out;
        }
 
-       if (chip->vendor.irq)
+       if (chip->flags & TPM_CHIP_FLAG_IRQ)
                goto out_recv;
 
        if (chip->flags & TPM_CHIP_FLAG_TPM2)
@@ -373,7 +373,7 @@ ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
                        goto out_recv;
 
                if (chip->ops->req_canceled(chip, status)) {
-                       dev_err(chip->pdev, "Operation Canceled\n");
+                       dev_err(&chip->dev, "Operation Canceled\n");
                        rc = -ECANCELED;
                        goto out;
                }
@@ -383,14 +383,14 @@ ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
        } while (time_before(jiffies, stop));
 
        chip->ops->cancel(chip);
-       dev_err(chip->pdev, "Operation Timed out\n");
+       dev_err(&chip->dev, "Operation Timed out\n");
        rc = -ETIME;
        goto out;
 
 out_recv:
        rc = chip->ops->recv(chip, (u8 *) buf, bufsiz);
        if (rc < 0)
-               dev_err(chip->pdev,
+               dev_err(&chip->dev,
                        "tpm_transmit: tpm_recv: error %zd\n", rc);
 out:
        mutex_unlock(&chip->tpm_mutex);
@@ -416,7 +416,7 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, void *cmd,
 
        err = be32_to_cpu(header->return_code);
        if (err != 0 && desc)
-               dev_err(chip->pdev, "A TPM error (%d) occurred %s\n", err,
+               dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
                        desc);
 
        return err;
@@ -432,12 +432,11 @@ static const struct tpm_input_header tpm_getcap_header = {
        .ordinal = TPM_ORD_GET_CAP
 };
 
-ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
+ssize_t tpm_getcap(struct tpm_chip *chip, __be32 subcap_id, cap_t *cap,
                   const char *desc)
 {
        struct tpm_cmd_t tpm_cmd;
        int rc;
-       struct tpm_chip *chip = dev_get_drvdata(dev);
 
        tpm_cmd.header.in = tpm_getcap_header;
        if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
@@ -505,15 +504,15 @@ int tpm_get_timeouts(struct tpm_chip *chip)
 
        if (chip->flags & TPM_CHIP_FLAG_TPM2) {
                /* Fixed timeouts for TPM2 */
-               chip->vendor.timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
-               chip->vendor.timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
-               chip->vendor.timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
-               chip->vendor.timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
-               chip->vendor.duration[TPM_SHORT] =
+               chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
+               chip->timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
+               chip->timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
+               chip->timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
+               chip->duration[TPM_SHORT] =
                    msecs_to_jiffies(TPM2_DURATION_SHORT);
-               chip->vendor.duration[TPM_MEDIUM] =
+               chip->duration[TPM_MEDIUM] =
                    msecs_to_jiffies(TPM2_DURATION_MEDIUM);
-               chip->vendor.duration[TPM_LONG] =
+               chip->duration[TPM_LONG] =
                    msecs_to_jiffies(TPM2_DURATION_LONG);
                return 0;
        }
@@ -527,7 +526,7 @@ int tpm_get_timeouts(struct tpm_chip *chip)
        if (rc == TPM_ERR_INVALID_POSTINIT) {
                /* The TPM is not started, we are the first to talk to it.
                   Execute a startup command. */
-               dev_info(chip->pdev, "Issuing TPM_STARTUP");
+               dev_info(&chip->dev, "Issuing TPM_STARTUP");
                if (tpm_startup(chip, TPM_ST_CLEAR))
                        return rc;
 
@@ -539,7 +538,7 @@ int tpm_get_timeouts(struct tpm_chip *chip)
                                  NULL);
        }
        if (rc) {
-               dev_err(chip->pdev,
+               dev_err(&chip->dev,
                        "A TPM error (%zd) occurred attempting to determine the timeouts\n",
                        rc);
                goto duration;
@@ -561,10 +560,10 @@ int tpm_get_timeouts(struct tpm_chip *chip)
         * of misreporting.
         */
        if (chip->ops->update_timeouts != NULL)
-               chip->vendor.timeout_adjusted =
+               chip->timeout_adjusted =
                        chip->ops->update_timeouts(chip, new_timeout);
 
-       if (!chip->vendor.timeout_adjusted) {
+       if (!chip->timeout_adjusted) {
                /* Don't overwrite default if value is 0 */
                if (new_timeout[0] != 0 && new_timeout[0] < 1000) {
                        int i;
@@ -572,13 +571,13 @@ int tpm_get_timeouts(struct tpm_chip *chip)
                        /* timeouts in msec rather usec */
                        for (i = 0; i != ARRAY_SIZE(new_timeout); i++)
                                new_timeout[i] *= 1000;
-                       chip->vendor.timeout_adjusted = true;
+                       chip->timeout_adjusted = true;
                }
        }
 
        /* Report adjusted timeouts */
-       if (chip->vendor.timeout_adjusted) {
-               dev_info(chip->pdev,
+       if (chip->timeout_adjusted) {
+               dev_info(&chip->dev,
                         HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
                         old_timeout[0], new_timeout[0],
                         old_timeout[1], new_timeout[1],
@@ -586,10 +585,10 @@ int tpm_get_timeouts(struct tpm_chip *chip)
                         old_timeout[3], new_timeout[3]);
        }
 
-       chip->vendor.timeout_a = usecs_to_jiffies(new_timeout[0]);
-       chip->vendor.timeout_b = usecs_to_jiffies(new_timeout[1]);
-       chip->vendor.timeout_c = usecs_to_jiffies(new_timeout[2]);
-       chip->vendor.timeout_d = usecs_to_jiffies(new_timeout[3]);
+       chip->timeout_a = usecs_to_jiffies(new_timeout[0]);
+       chip->timeout_b = usecs_to_jiffies(new_timeout[1]);
+       chip->timeout_c = usecs_to_jiffies(new_timeout[2]);
+       chip->timeout_d = usecs_to_jiffies(new_timeout[3]);
 
 duration:
        tpm_cmd.header.in = tpm_getcap_header;
@@ -608,11 +607,11 @@ duration:
                return -EINVAL;
 
        duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
-       chip->vendor.duration[TPM_SHORT] =
+       chip->duration[TPM_SHORT] =
            usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
-       chip->vendor.duration[TPM_MEDIUM] =
+       chip->duration[TPM_MEDIUM] =
            usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
-       chip->vendor.duration[TPM_LONG] =
+       chip->duration[TPM_LONG] =
            usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
 
        /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
@@ -620,12 +619,12 @@ duration:
         * fix up the resulting too-small TPM_SHORT value to make things work.
         * We also scale the TPM_MEDIUM and -_LONG values by 1000.
         */
-       if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
-               chip->vendor.duration[TPM_SHORT] = HZ;
-               chip->vendor.duration[TPM_MEDIUM] *= 1000;
-               chip->vendor.duration[TPM_LONG] *= 1000;
-               chip->vendor.duration_adjusted = true;
-               dev_info(chip->pdev, "Adjusting TPM timeout parameters.");
+       if (chip->duration[TPM_SHORT] < (HZ / 100)) {
+               chip->duration[TPM_SHORT] = HZ;
+               chip->duration[TPM_MEDIUM] *= 1000;
+               chip->duration[TPM_LONG] *= 1000;
+               chip->duration_adjusted = true;
+               dev_info(&chip->dev, "Adjusting TPM timeout parameters.");
        }
        return 0;
 }
@@ -700,7 +699,7 @@ int tpm_is_tpm2(u32 chip_num)
 
        rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
 
-       tpm_chip_put(chip);
+       tpm_put_ops(chip);
 
        return rc;
 }
@@ -729,7 +728,7 @@ int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
                rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
        else
                rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
-       tpm_chip_put(chip);
+       tpm_put_ops(chip);
        return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_pcr_read);
@@ -764,7 +763,7 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
 
        if (chip->flags & TPM_CHIP_FLAG_TPM2) {
                rc = tpm2_pcr_extend(chip, pcr_idx, hash);
-               tpm_chip_put(chip);
+               tpm_put_ops(chip);
                return rc;
        }
 
@@ -774,7 +773,7 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
        rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
                              "attempting extend a PCR value");
 
-       tpm_chip_put(chip);
+       tpm_put_ops(chip);
        return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
@@ -815,7 +814,9 @@ int tpm_do_selftest(struct tpm_chip *chip)
                 * around 300ms while the self test is ongoing, keep trying
                 * until the self test duration expires. */
                if (rc == -ETIME) {
-                       dev_info(chip->pdev, HW_ERR "TPM command timed out during continue self test");
+                       dev_info(
+                           &chip->dev, HW_ERR
+                           "TPM command timed out during continue self test");
                        msleep(delay_msec);
                        continue;
                }
@@ -825,7 +826,7 @@ int tpm_do_selftest(struct tpm_chip *chip)
 
                rc = be32_to_cpu(cmd.header.out.return_code);
                if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
-                       dev_info(chip->pdev,
+                       dev_info(&chip->dev,
                                 "TPM is disabled/deactivated (0x%X)\n", rc);
                        /* TPM is disabled and/or deactivated; driver can
                         * proceed and TPM does handle commands for
@@ -853,7 +854,7 @@ int tpm_send(u32 chip_num, void *cmd, size_t buflen)
 
        rc = tpm_transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
 
-       tpm_chip_put(chip);
+       tpm_put_ops(chip);
        return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_send);
@@ -888,7 +889,7 @@ int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
 
        stop = jiffies + timeout;
 
-       if (chip->vendor.irq) {
+       if (chip->flags & TPM_CHIP_FLAG_IRQ) {
 again:
                timeout = stop - jiffies;
                if ((long)timeout <= 0)
@@ -978,10 +979,10 @@ int tpm_pm_suspend(struct device *dev)
        }
 
        if (rc)
-               dev_err(chip->pdev,
+               dev_err(&chip->dev,
                        "Error (%d) sending savestate before suspend\n", rc);
        else if (try > 0)
-               dev_warn(chip->pdev, "TPM savestate took %dms\n",
+               dev_warn(&chip->dev, "TPM savestate took %dms\n",
                         try * TPM_TIMEOUT_RETRY);
 
        return rc;
@@ -1035,7 +1036,7 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
 
        if (chip->flags & TPM_CHIP_FLAG_TPM2) {
                err = tpm2_get_random(chip, out, max);
-               tpm_chip_put(chip);
+               tpm_put_ops(chip);
                return err;
        }
 
@@ -1057,7 +1058,7 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
                num_bytes -= recd;
        } while (retries-- && total < max);
 
-       tpm_chip_put(chip);
+       tpm_put_ops(chip);
        return total ? total : -EIO;
 }
 EXPORT_SYMBOL_GPL(tpm_get_random);
@@ -1083,7 +1084,7 @@ int tpm_seal_trusted(u32 chip_num, struct trusted_key_payload *payload,
 
        rc = tpm2_seal_trusted(chip, payload, options);
 
-       tpm_chip_put(chip);
+       tpm_put_ops(chip);
        return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_seal_trusted);
@@ -1109,7 +1110,8 @@ int tpm_unseal_trusted(u32 chip_num, struct trusted_key_payload *payload,
 
        rc = tpm2_unseal_trusted(chip, payload, options);
 
-       tpm_chip_put(chip);
+       tpm_put_ops(chip);
+
        return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
@@ -1136,6 +1138,7 @@ static int __init tpm_init(void)
 
 static void __exit tpm_exit(void)
 {
+       idr_destroy(&dev_nums_idr);
        class_destroy(tpm_class);
        unregister_chrdev_region(tpm_devt, TPM_NUM_DEVICES);
 }