Skip to content

Commit 3f06fd4

Browse files
jgunthorpeJiri Slaby
authored andcommitted
tpm: Provide a generic means to override the chip returned timeouts
commit 8e54caf upstream. Some Atmel TPMs provide completely wrong timeouts from their TPM_CAP_PROP_TIS_TIMEOUT query. This patch detects that and returns new correct values via a DID/VID table in the TIS driver. Tested on ARM using an AT97SC3204T FW version 37.16 [PHuewe: without this fix these 'broken' Atmel TPMs won't function on older kernels] Signed-off-by: "Berg, Christopher" <Christopher.Berg@atmel.com> Signed-off-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com> Signed-off-by: Peter Huewe <peterhuewe@gmx.de> [bwh: Backported to 3.10: - Adjust filename, context - s/chip->ops->/chip->vendor./] Signed-off-by: Ben Hutchings <ben@decadent.org.uk> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent a4ed6bc commit 3f06fd4

3 files changed

Lines changed: 75 additions & 21 deletions

File tree

‎drivers/char/tpm/tpm.c‎

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,10 @@ static int tpm_startup(struct tpm_chip *chip, __be16 startup_type)
533533
int tpm_get_timeouts(struct tpm_chip *chip)
534534
{
535535
struct tpm_cmd_t tpm_cmd;
536-
struct timeout_t *timeout_cap;
536+
unsigned long new_timeout[4];
537+
unsigned long old_timeout[4];
537538
struct duration_t *duration_cap;
538539
ssize_t rc;
539-
u32 timeout;
540-
unsigned int scale = 1;
541540

542541
tpm_cmd.header.in = tpm_getcap_header;
543542
tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
@@ -571,25 +570,46 @@ int tpm_get_timeouts(struct tpm_chip *chip)
571570
!= sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
572571
return -EINVAL;
573572

574-
timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
575-
/* Don't overwrite default if value is 0 */
576-
timeout = be32_to_cpu(timeout_cap->a);
577-
if (timeout && timeout < 1000) {
578-
/* timeouts in msec rather usec */
579-
scale = 1000;
580-
chip->vendor.timeout_adjusted = true;
573+
old_timeout[0] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.a);
574+
old_timeout[1] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.b);
575+
old_timeout[2] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.c);
576+
old_timeout[3] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.d);
577+
memcpy(new_timeout, old_timeout, sizeof(new_timeout));
578+
579+
/*
580+
* Provide ability for vendor overrides of timeout values in case
581+
* of misreporting.
582+
*/
583+
if (chip->vendor.update_timeouts != NULL)
584+
chip->vendor.timeout_adjusted =
585+
chip->vendor.update_timeouts(chip, new_timeout);
586+
587+
if (!chip->vendor.timeout_adjusted) {
588+
/* Don't overwrite default if value is 0 */
589+
if (new_timeout[0] != 0 && new_timeout[0] < 1000) {
590+
int i;
591+
592+
/* timeouts in msec rather usec */
593+
for (i = 0; i != ARRAY_SIZE(new_timeout); i++)
594+
new_timeout[i] *= 1000;
595+
chip->vendor.timeout_adjusted = true;
596+
}
581597
}
582-
if (timeout)
583-
chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
584-
timeout = be32_to_cpu(timeout_cap->b);
585-
if (timeout)
586-
chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
587-
timeout = be32_to_cpu(timeout_cap->c);
588-
if (timeout)
589-
chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
590-
timeout = be32_to_cpu(timeout_cap->d);
591-
if (timeout)
592-
chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
598+
599+
/* Report adjusted timeouts */
600+
if (chip->vendor.timeout_adjusted) {
601+
dev_info(chip->dev,
602+
HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
603+
old_timeout[0], new_timeout[0],
604+
old_timeout[1], new_timeout[1],
605+
old_timeout[2], new_timeout[2],
606+
old_timeout[3], new_timeout[3]);
607+
}
608+
609+
chip->vendor.timeout_a = usecs_to_jiffies(new_timeout[0]);
610+
chip->vendor.timeout_b = usecs_to_jiffies(new_timeout[1]);
611+
chip->vendor.timeout_c = usecs_to_jiffies(new_timeout[2]);
612+
chip->vendor.timeout_d = usecs_to_jiffies(new_timeout[3]);
593613

594614
duration:
595615
tpm_cmd.header.in = tpm_getcap_header;

‎drivers/char/tpm/tpm.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ struct tpm_vendor_specific {
9595
int (*send) (struct tpm_chip *, u8 *, size_t);
9696
void (*cancel) (struct tpm_chip *);
9797
u8 (*status) (struct tpm_chip *);
98+
bool (*update_timeouts)(struct tpm_chip *chip,
99+
unsigned long *timeout_cap);
100+
98101
void (*release) (struct device *);
99102
struct miscdevice miscdev;
100103
struct attribute_group *attr_group;

‎drivers/char/tpm/tpm_tis.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,36 @@ static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
373373
return rc;
374374
}
375375

376+
struct tis_vendor_timeout_override {
377+
u32 did_vid;
378+
unsigned long timeout_us[4];
379+
};
380+
381+
static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
382+
/* Atmel 3204 */
383+
{ 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
384+
(TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
385+
};
386+
387+
static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
388+
unsigned long *timeout_cap)
389+
{
390+
int i;
391+
u32 did_vid;
392+
393+
did_vid = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
394+
395+
for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
396+
if (vendor_timeout_overrides[i].did_vid != did_vid)
397+
continue;
398+
memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
399+
sizeof(vendor_timeout_overrides[i].timeout_us));
400+
return true;
401+
}
402+
403+
return false;
404+
}
405+
376406
/*
377407
* Early probing for iTPM with STS_DATA_EXPECT flaw.
378408
* Try sending command without itpm flag set and if that
@@ -475,6 +505,7 @@ static struct tpm_vendor_specific tpm_tis = {
475505
.recv = tpm_tis_recv,
476506
.send = tpm_tis_send,
477507
.cancel = tpm_tis_ready,
508+
.update_timeouts = tpm_tis_update_timeouts,
478509
.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
479510
.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
480511
.req_canceled = tpm_tis_req_canceled,

0 commit comments

Comments
 (0)