Received: by 2002:a4a:301c:0:0:0:0:0 with SMTP id q28-v6csp1020027oof; Tue, 25 Sep 2018 07:21:37 -0700 (PDT) X-Google-Smtp-Source: ACcGV6215i3p6TlGC30WjrADYD3MCVGcf18132NerHzvbDqe8FgYGNny0IuPLSpKrMO0ogYiWecj X-Received: by 2002:a62:280a:: with SMTP id o10-v6mr1386860pfo.129.1537885297451; Tue, 25 Sep 2018 07:21:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537885297; cv=none; d=google.com; s=arc-20160816; b=twvKEp5CINj6IsvsnTqL3btUWyiwIRTmTvA0dBtWhBTZh1S7WG1+69zmyaYc1E3NrI FLGoqJ7sMpvd9kRdd3jcn28jseG0WNCUxARsun22TtLPWoDiHDA8uXR5Mbbh5yeiJvPS SHs5lRjpW9jxn8jCE8Oxz3vvIxMjpB+HTn73+qn/to0wAyJRepJstLEp0/loTBfffAGJ C/fms9hbXZQbH50Uw69KWEx+NWkdjZ88Kq9fiAz67Gwpb4hWxqQzcr99p+q4KWlnHccp 0xJ2xfx0hp0KxSi6di/Z4gEjYzep9iw6jr+ulsLs9qidsNGbkGvjdzR/pdlTp0I/91BK t7vQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :organization:references:in-reply-to:date:cc:to:from:subject :message-id; bh=JAlSSR0e5IdQVSd0g7h5wJNEccM4hatzJkgCFjCG2AQ=; b=nek6oocP0iNFt47qA5cky848bZ+otPnMwk71Jubm7RwMCtfZiG2VBz/TAX4zz0daet VTQ36aLL3/TAaRoIYptEQwNu2jEhFyrNUO5VFb1LEbiUXspKiFFaoCzdTMrqwgQbLzkz KpQTNuOBiXN0+ctyaURYVdMM5mR6wkxQqYd42WRy2C8qtSGCmovIIjVVIySzLoOXZuNu dxaVR0oU6S+LU8jm1q/cgmoc+mqPId5u8s+vh2wxvr1NHdNt7wm15afmeXTTTThbj1Mj o+4KIkFYMRznXp4mvREGWzpSgr4ShFyZ/7rNCfO41czlV5F5UHxYkQzk03B4M+1KyI76 ROYg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u70-v6si2391396pgd.296.2018.09.25.07.21.20; Tue, 25 Sep 2018 07:21:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729343AbeIYU0y (ORCPT + 99 others); Tue, 25 Sep 2018 16:26:54 -0400 Received: from mga18.intel.com ([134.134.136.126]:21702 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729267AbeIYU0y (ORCPT ); Tue, 25 Sep 2018 16:26:54 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Sep 2018 07:19:10 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,302,1534834800"; d="scan'208";a="75789970" Received: from thomasvo-mobl2.ger.corp.intel.com ([10.252.53.212]) by orsmga007.jf.intel.com with ESMTP; 25 Sep 2018 07:19:05 -0700 Message-ID: <28104c678b1c29b3851aab15149be1beed600711.camel@linux.intel.com> Subject: Re: [PATCH v4 05/21] tpm: factor out tpm_get_timeouts() From: Jarkko Sakkinen To: Tomas Winkler , Jason Gunthorpe Cc: Alexander Usyskin , Tadeusz Struk , linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Date: Tue, 25 Sep 2018 17:19:04 +0300 In-Reply-To: <20180921135820.20549-6-tomas.winkler@intel.com> References: <20180921135820.20549-1-tomas.winkler@intel.com> <20180921135820.20549-6-tomas.winkler@intel.com> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.28.1-2 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2018-09-21 at 16:58 +0300, Tomas Winkler wrote: > Factor out tpm_get_timeouts() into tpm2_get_timeouts() > and tpm1_get_timeouts() > > Signed-off-by: Tomas Winkler > --- > V2-V3: Rebase. > V4: Fix the commit message. > > drivers/char/tpm/tpm-interface.c | 127 ++---------------------------------- > --- > drivers/char/tpm/tpm.h | 5 +- > drivers/char/tpm/tpm1-cmd.c | 107 +++++++++++++++++++++++++++++++++ > drivers/char/tpm/tpm2-cmd.c | 22 +++++++ > 4 files changed, 137 insertions(+), 124 deletions(-) > > diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm- > interface.c > index a7529844c60e..bd8942e56aa7 100644 > --- a/drivers/char/tpm/tpm-interface.c > +++ b/drivers/char/tpm/tpm-interface.c > @@ -502,132 +502,13 @@ EXPORT_SYMBOL_GPL(tpm_getcap); > > int tpm_get_timeouts(struct tpm_chip *chip) > { > - cap_t cap; > - unsigned long timeout_old[4], timeout_chip[4], timeout_eff[4]; > - ssize_t rc; > - > if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS) > return 0; > > - if (chip->flags & TPM_CHIP_FLAG_TPM2) { > - /* Fixed timeouts for TPM2 */ > - 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->duration[TPM_MEDIUM] = > - msecs_to_jiffies(TPM2_DURATION_MEDIUM); > - chip->duration[TPM_LONG] = > - msecs_to_jiffies(TPM2_DURATION_LONG); > - chip->duration[TPM_LONG_LONG] = > - msecs_to_jiffies(TPM2_DURATION_LONG_LONG); > - > - chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS; > - return 0; > - } > - > - rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, NULL, > - sizeof(cap.timeout)); > - if (rc == TPM_ERR_INVALID_POSTINIT) { > - if (tpm_startup(chip)) > - return rc; > - > - rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, > - "attempting to determine the timeouts", > - sizeof(cap.timeout)); > - } > - > - if (rc) { > - dev_err(&chip->dev, > - "A TPM error (%zd) occurred attempting to determine > the timeouts\n", > - rc); > - return rc; > - } > - > - timeout_old[0] = jiffies_to_usecs(chip->timeout_a); > - timeout_old[1] = jiffies_to_usecs(chip->timeout_b); > - timeout_old[2] = jiffies_to_usecs(chip->timeout_c); > - timeout_old[3] = jiffies_to_usecs(chip->timeout_d); > - timeout_chip[0] = be32_to_cpu(cap.timeout.a); > - timeout_chip[1] = be32_to_cpu(cap.timeout.b); > - timeout_chip[2] = be32_to_cpu(cap.timeout.c); > - timeout_chip[3] = be32_to_cpu(cap.timeout.d); > - memcpy(timeout_eff, timeout_chip, sizeof(timeout_eff)); > - > - /* > - * Provide ability for vendor overrides of timeout values in case > - * of misreporting. > - */ > - if (chip->ops->update_timeouts != NULL) > - chip->timeout_adjusted = > - chip->ops->update_timeouts(chip, timeout_eff); > - > - if (!chip->timeout_adjusted) { > - /* Restore default if chip reported 0 */ > - int i; > - > - for (i = 0; i < ARRAY_SIZE(timeout_eff); i++) { > - if (timeout_eff[i]) > - continue; > - > - timeout_eff[i] = timeout_old[i]; > - chip->timeout_adjusted = true; > - } > - > - if (timeout_eff[0] != 0 && timeout_eff[0] < 1000) { > - /* timeouts in msec rather usec */ > - for (i = 0; i != ARRAY_SIZE(timeout_eff); i++) > - timeout_eff[i] *= 1000; > - chip->timeout_adjusted = true; > - } > - } > - > - /* Report adjusted timeouts */ > - 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", > - timeout_chip[0], timeout_eff[0], > - timeout_chip[1], timeout_eff[1], > - timeout_chip[2], timeout_eff[2], > - timeout_chip[3], timeout_eff[3]); > - } > - > - chip->timeout_a = usecs_to_jiffies(timeout_eff[0]); > - chip->timeout_b = usecs_to_jiffies(timeout_eff[1]); > - chip->timeout_c = usecs_to_jiffies(timeout_eff[2]); > - chip->timeout_d = usecs_to_jiffies(timeout_eff[3]); > - > - rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap, > - "attempting to determine the durations", > - sizeof(cap.duration)); > - if (rc) > - return rc; > - > - chip->duration[TPM_SHORT] = > - usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short)); > - chip->duration[TPM_MEDIUM] = > - usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium)); > - chip->duration[TPM_LONG] = > - usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long)); > - chip->duration[TPM_LONG_LONG] = 0; /* not used under 1.2 */ > - > - /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the > above > - * value wrong and apparently reports msecs rather than usecs. So we > - * 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->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."); > - } > - > - chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS; > - return 0; > + if (chip->flags & TPM_CHIP_FLAG_TPM2) > + return tpm2_get_timeouts(chip); > + else > + return tpm1_get_timeouts(chip); > } > EXPORT_SYMBOL_GPL(tpm_get_timeouts); > > diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h > index 73511cd89bef..a97d72fcda5b 100644 > --- a/drivers/char/tpm/tpm.h > +++ b/drivers/char/tpm/tpm.h > @@ -544,8 +544,10 @@ int tpm_startup(struct tpm_chip *chip); > ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap, > const char *desc, size_t min_cap_length); > int tpm_get_timeouts(struct tpm_chip *); > -int tpm1_auto_startup(struct tpm_chip *chip); > int tpm_do_selftest(struct tpm_chip *chip); > + > +int tpm1_auto_startup(struct tpm_chip *chip); > +int tpm1_get_timeouts(struct tpm_chip *chip); > unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal); > unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal); > int tpm_pm_suspend(struct device *dev); > @@ -585,6 +587,7 @@ static inline u32 tpm2_rc_value(u32 rc) > return (rc & BIT(7)) ? rc & 0xff : rc; > } > > +int tpm2_get_timeouts(struct tpm_chip *chip); > int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf); > int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count, > struct tpm2_digest *digests); > diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c > index f8d85fda3b9b..12974aa4ea3e 100644 > --- a/drivers/char/tpm/tpm1-cmd.c > +++ b/drivers/char/tpm/tpm1-cmd.c > @@ -307,3 +307,110 @@ unsigned long tpm1_calc_ordinal_duration(struct tpm_chip > *chip, u32 ordinal) > else > return duration; > } > + > +int tpm1_get_timeouts(struct tpm_chip *chip) > +{ > + cap_t cap; > + unsigned long timeout_old[4], timeout_chip[4], timeout_eff[4]; > + ssize_t rc; > + > + rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, NULL, > + sizeof(cap.timeout)); > + if (rc == TPM_ERR_INVALID_POSTINIT) { > + if (tpm_startup(chip)) > + return rc; > + > + rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, > + "attempting to determine the timeouts", > + sizeof(cap.timeout)); > + } > + > + if (rc) { > + dev_err(&chip->dev, > + "A TPM error (%zd) occurred attempting to determine > the timeouts\n", > + rc); > + return rc; > + } > + > + timeout_old[0] = jiffies_to_usecs(chip->timeout_a); > + timeout_old[1] = jiffies_to_usecs(chip->timeout_b); > + timeout_old[2] = jiffies_to_usecs(chip->timeout_c); > + timeout_old[3] = jiffies_to_usecs(chip->timeout_d); > + timeout_chip[0] = be32_to_cpu(cap.timeout.a); > + timeout_chip[1] = be32_to_cpu(cap.timeout.b); > + timeout_chip[2] = be32_to_cpu(cap.timeout.c); > + timeout_chip[3] = be32_to_cpu(cap.timeout.d); > + memcpy(timeout_eff, timeout_chip, sizeof(timeout_eff)); > + > + /* > + * Provide ability for vendor overrides of timeout values in case > + * of misreporting. > + */ > + if (chip->ops->update_timeouts) > + chip->timeout_adjusted = > + chip->ops->update_timeouts(chip, timeout_eff); > + > + if (!chip->timeout_adjusted) { > + /* Restore default if chip reported 0 */ > + unsigned int i; > + > + for (i = 0; i < ARRAY_SIZE(timeout_eff); i++) { > + if (timeout_eff[i]) > + continue; > + > + timeout_eff[i] = timeout_old[i]; > + chip->timeout_adjusted = true; > + } > + > + if (timeout_eff[0] != 0 && timeout_eff[0] < 1000) { > + /* timeouts in msec rather usec */ > + for (i = 0; i != ARRAY_SIZE(timeout_eff); i++) > + timeout_eff[i] *= 1000; > + chip->timeout_adjusted = true; > + } > + } > + > + /* Report adjusted timeouts */ > + 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", > + timeout_chip[0], timeout_eff[0], > + timeout_chip[1], timeout_eff[1], > + timeout_chip[2], timeout_eff[2], > + timeout_chip[3], timeout_eff[3]); > + } > + > + chip->timeout_a = usecs_to_jiffies(timeout_eff[0]); > + chip->timeout_b = usecs_to_jiffies(timeout_eff[1]); > + chip->timeout_c = usecs_to_jiffies(timeout_eff[2]); > + chip->timeout_d = usecs_to_jiffies(timeout_eff[3]); > + > + rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap, > + "attempting to determine the durations", > + sizeof(cap.duration)); > + if (rc) > + return rc; > + > + chip->duration[TPM_SHORT] = > + usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short)); > + chip->duration[TPM_MEDIUM] = > + usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium)); > + chip->duration[TPM_LONG] = > + usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long)); > + chip->duration[TPM_LONG_LONG] = 0; /* not used under 1.2 */ > + > + /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the > above > + * value wrong and apparently reports msecs rather than usecs. So we > + * 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->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."); > + } > + > + chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS; > + return 0; > +} > diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c > index 238755b8fb01..65ce463a6f92 100644 > --- a/drivers/char/tpm/tpm2-cmd.c > +++ b/drivers/char/tpm/tpm2-cmd.c > @@ -40,6 +40,28 @@ static struct tpm2_hash tpm2_hash_map[] = { > {HASH_ALGO_SM3_256, TPM2_ALG_SM3_256}, > }; > > +int tpm2_get_timeouts(struct tpm_chip *chip) > +{ > + /* Fixed timeouts for TPM2 */ > + 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); > + > + /* PTP spec timeouts */ > + chip->duration[TPM_SHORT] = msecs_to_jiffies(TPM2_DURATION_SHORT); > + chip->duration[TPM_MEDIUM] = msecs_to_jiffies(TPM2_DURATION_MEDIUM); > + chip->duration[TPM_LONG] = msecs_to_jiffies(TPM2_DURATION_LONG); > + > + /* Key creation commands long timeouts */ > + chip->duration[TPM_LONG_LONG] = > + msecs_to_jiffies(TPM2_DURATION_LONG_LONG); > + > + chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS; > + > + return 0; > +} > + > /** > * tpm2_ordinal_duration_index() - returns an index to the chip duration > * table (enum tpm_duration), that describes the maximum amount of Reviewed-by: Jarkko Sakkinen Tested-by: Jarkko Sakkinen /Jarkko