Skip to contentSkip to conversation

TPM 2.0 support

PATCH Superseded Jarkko Sakkinen 2 participants 17 messages Oct 7, 2014 – Oct 9, 2014

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover TPM 2.0 support

This patch set enables TPM2 protocol and provides drivers for FIFO and CRB interfaces.

Major changes since v1:

- Improved struct tpm_chip life-cycle by taking advantage of devres API.
- Refined sysfs attributes as simple key-values thereby not repeating mistakes in TPM1 sysfs attributes.
- Documented functions in tpm-chip.c and tpm2-cmd.c.
- Documented sysfs attributes.

Jarkko Sakkinen (6):
tpm: merge duplicate transmit_cmd() functions
tpm: two-phase chip management functions
tpm: clean up tpm_tis driver life-cycle
tpm: TPM 2.0 commands
tpm: TPM 2.0 sysfs attributes
tpm: TPM 2.0 CRB Interface

Will Arthur (1):
tpm: TPM 2.0 FIFO Interface

 Documentation/ABI/stable/sysfs-class-tpm2 |  69 ++++
 drivers/char/tpm/Kconfig                  |   9 +
 drivers/char/tpm/Makefile                 |   3 +-
 drivers/char/tpm/tpm-chip.c               | 184 ++++++++++
 drivers/char/tpm/tpm-dev.c                |   4 +-
 drivers/char/tpm/tpm-interface.c          | 201 ++++-------
 drivers/char/tpm/tpm-sysfs.c              |  23 +-
 drivers/char/tpm/tpm.h                    | 103 +++++-
 drivers/char/tpm/tpm2-cmd.c               | 543 ++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm2-sysfs.c             | 314 +++++++++++++++++
 drivers/char/tpm/tpm_crb.c                | 329 ++++++++++++++++++
 drivers/char/tpm/tpm_i2c_atmel.c          |   5 -
 drivers/char/tpm/tpm_i2c_infineon.c       |  10 -
 drivers/char/tpm/tpm_i2c_nuvoton.c        |   5 -
 drivers/char/tpm/tpm_infineon.c           |   1 -
 drivers/char/tpm/tpm_tis.c                | 142 +++++---
 16 files changed, 1696 insertions(+), 249 deletions(-)
 create mode 100644 Documentation/ABI/stable/sysfs-class-tpm2
 create mode 100644 drivers/char/tpm/tpm-chip.c
 create mode 100644 drivers/char/tpm/tpm2-cmd.c
 create mode 100644 drivers/char/tpm/tpm2-sysfs.c
 create mode 100644 drivers/char/tpm/tpm_crb.c
Signature · 1 line
2.1.0
#1 lore raw

PATCH v2 1/7 tpm: merge duplicate transmit_cmd() functions

Merged transmit_cmd() functions in tpm-interface.c and tpm-sysfs.c. Added "tpm_" prefix for consistency sake. Changed cmd parameter as opaque. This enables to use separate command structures for TPM1 and TPM2 commands instead of putting everything to struct tpm_cmd_t.

---
 drivers/char/tpm/tpm-dev.c       |  4 +--
 drivers/char/tpm/tpm-interface.c | 53 +++++++++++++++++++++-------------------
 drivers/char/tpm/tpm-sysfs.c     | 23 ++---------------
 drivers/char/tpm/tpm.h           |  5 ++--
 4 files changed, 34 insertions(+), 51 deletions(-)
drivers/char/tpm/tpm-dev.c +2 -2
index d9b774e..bd79d33 100644
@@ -140,8 +140,8 @@ static ssize_t tpm_write(struct file *file, const char __user *buf,
 	}
 
 	/* atomic tpm command send and result receive */
-	out_size = tpm_transmit(priv->chip, priv->data_buffer,
-				sizeof(priv->data_buffer));
+	out_size = tpm_transmit_cmd(priv->chip, priv->data_buffer,
+				    sizeof(priv->data_buffer), NULL);
 	if (out_size < 0) {
 		mutex_unlock(&priv->buffer_mutex);
 		return out_size;
drivers/char/tpm/tpm-interface.c +28 -25

149 lines not on this page · raw message

drivers/char/tpm/tpm-sysfs.c +2 -21
index 01730a2..8ecb052 100644
@@ -20,25 +20,6 @@
 #include <linux/device.h>
 #include "tpm.h"
 
-/* XXX for now this helper is duplicated in tpm-interface.c */
-static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
-			    int len, const char *desc)
-{
-	int err;
-
-	len = tpm_transmit(chip, (u8 *) cmd, len);
-	if (len <  0)
-		return len;
-	else if (len < TPM_HEADER_SIZE)
-		return -EFAULT;
-
-	err = be32_to_cpu(cmd->header.out.return_code);
-	if (err != 0 && desc)
-		dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
-
-	return err;
-}
-
 #define READ_PUBEK_RESULT_SIZE 314
 #define TPM_ORD_READPUBEK cpu_to_be32(124)
 static struct tpm_input_header tpm_readpubek_header = {
@@ -58,8 +39,8 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
 	struct tpm_chip *chip = dev_get_drvdata(dev);
 
 	tpm_cmd.header.in = tpm_readpubek_header;
-	err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
-			   "attempting to read the PUBEK");
+	err = tpm_transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
+			       "attempting to read the PUBEK");
 	if (err)
 		goto out;
 
drivers/char/tpm/tpm.h +2 -3
index e4d0888..12326e1 100644
@@ -314,9 +314,8 @@ struct tpm_cmd_t {
 } __packed;
 
 ssize_t	tpm_getcap(struct device *, __be32, cap_t *, const char *);
-
-ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
-		     size_t bufsiz);
+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 *);
 extern int tpm_do_selftest(struct tpm_chip *);
Signature · 1 line
2.1.0
#2 lore raw

PATCH v2 2/7 tpm: two-phase chip management functions

In reply to Jarkko Sakkinen

Added tpm_chip_alloc() and tpm_chip_register() where tpm_chip_alloc() reserves memory resources and tpm_chip_register() initializes the device driver. This way it is possible to alter struct tpm_chip attributes before passing it to tpm_chip_register().

The framework takes care of freeing struct tpm_chip by using devres API. The broken release callback has been wiped.

tpm_register_hardware() and tpm_remove_hardware are still available as a wrapper in order to make this change less intrusive. However, device drivers should eventually move to the tpm_chip_* functions.

---
 drivers/char/tpm/Makefile           |   2 +-
 drivers/char/tpm/tpm-chip.c         | 178 ++++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm-interface.c    | 124 ++-----------------------
 drivers/char/tpm/tpm.h              |   8 +-
 drivers/char/tpm/tpm_i2c_atmel.c    |   5 -
 drivers/char/tpm/tpm_i2c_infineon.c |  10 --
 drivers/char/tpm/tpm_i2c_nuvoton.c  |   5 -
 drivers/char/tpm/tpm_infineon.c     |   1 -
 drivers/char/tpm/tpm_tis.c          |   5 +-
 9 files changed, 194 insertions(+), 144 deletions(-)
 create mode 100644 drivers/char/tpm/tpm-chip.c
drivers/char/tpm/Makefile +1 -1
index 4d85dd6..837da04 100644
@@ -2,7 +2,7 @@
 # Makefile for the kernel tpm device drivers.
 #
 obj-$(CONFIG_TCG_TPM) += tpm.o
-tpm-y := tpm-interface.o tpm-dev.o tpm-sysfs.o
+tpm-y := tpm-interface.o tpm-dev.o tpm-sysfs.o tpm-chip.o
 tpm-$(CONFIG_ACPI) += tpm_ppi.o
 
 ifdef CONFIG_ACPI
drivers/char/tpm/tpm-chip.c new file +178 -0

178 lines not on this page · raw message

drivers/char/tpm/tpm-interface.c +8 -116

164 lines not on this page · raw message

drivers/char/tpm/tpm.h +6 -2
index 12326e1..5eb89897 100644
@@ -110,7 +110,6 @@ struct tpm_chip {
 	struct dentry **bios_dir;
 
 	struct list_head list;
-	void (*release) (struct device *);
 };
 
 #define to_tpm_chip(n) container_of(n, struct tpm_chip, vendor)
@@ -322,13 +321,18 @@ extern int tpm_do_selftest(struct tpm_chip *);
 extern unsigned long tpm_calc_ordinal_duration(struct tpm_chip *, u32);
 extern struct tpm_chip* tpm_register_hardware(struct device *,
 					      const struct tpm_class_ops *ops);
-extern void tpm_dev_vendor_release(struct tpm_chip *);
 extern void tpm_remove_hardware(struct device *);
 extern int tpm_pm_suspend(struct device *);
 extern int tpm_pm_resume(struct device *);
 extern int wait_for_tpm_stat(struct tpm_chip *, u8, unsigned long,
 			     wait_queue_head_t *, bool);
 
+struct tpm_chip *tpm_chip_find_get(int chip_num);
+extern struct tpm_chip *tpm_chip_alloc(struct device *dev,
+				       const struct tpm_class_ops *ops);
+extern int tpm_chip_register(struct tpm_chip *chip);
+extern void tpm_chip_unregister(struct tpm_chip *chip);
+
 int tpm_dev_add_device(struct tpm_chip *chip);
 void tpm_dev_del_device(struct tpm_chip *chip);
 int tpm_sysfs_add_device(struct tpm_chip *chip);
drivers/char/tpm/tpm_i2c_atmel.c +0 -5
index 7727292..1b52045 100644
@@ -192,7 +192,6 @@ static int i2c_atmel_probe(struct i2c_client *client,
 	return 0;
 
 out_err:
-	tpm_dev_vendor_release(chip);
 	tpm_remove_hardware(chip->dev);
 	return rc;
 }
@@ -200,12 +199,8 @@ out_err:
 static int i2c_atmel_remove(struct i2c_client *client)
 {
 	struct device *dev = &(client->dev);
-	struct tpm_chip *chip = dev_get_drvdata(dev);
 
-	if (chip)
-		tpm_dev_vendor_release(chip);
 	tpm_remove_hardware(dev);
-	kfree(chip);
 	return 0;
 }
 
drivers/char/tpm/tpm_i2c_infineon.c +0 -10
index 472af4b..9d9834d 100644
@@ -634,15 +634,10 @@ out_release:
 	release_locality(chip, chip->vendor.locality, 1);
 
 out_vendor:
-	/* close file handles */
-	tpm_dev_vendor_release(chip);
-
 	/* remove hardware */
 	tpm_remove_hardware(chip->dev);
 
 	/* reset these pointers, otherwise we oops */
-	chip->dev->release = NULL;
-	chip->release = NULL;
 	tpm_dev.client = NULL;
 out_err:
 	return rc;
@@ -714,15 +709,10 @@ static int tpm_tis_i2c_remove(struct i2c_client *client)
 	struct tpm_chip *chip = tpm_dev.chip;
 	release_locality(chip, chip->vendor.locality, 1);
 
-	/* close file handles */
-	tpm_dev_vendor_release(chip);
-
 	/* remove hardware */
 	tpm_remove_hardware(chip->dev);
 
 	/* reset these pointers, otherwise we oops */
-	chip->dev->release = NULL;
-	chip->release = NULL;
 	tpm_dev.client = NULL;
 
 	return 0;
drivers/char/tpm/tpm_i2c_nuvoton.c +0 -5
index 7b158ef..3a2caec 100644
@@ -615,7 +615,6 @@ static int i2c_nuvoton_probe(struct i2c_client *client,
 	return 0;
 
 out_err:
-	tpm_dev_vendor_release(chip);
 	tpm_remove_hardware(chip->dev);
 	return rc;
 }
@@ -623,12 +622,8 @@ out_err:
 static int i2c_nuvoton_remove(struct i2c_client *client)
 {
 	struct device *dev = &(client->dev);
-	struct tpm_chip *chip = dev_get_drvdata(dev);
 
-	if (chip)
-		tpm_dev_vendor_release(chip);
 	tpm_remove_hardware(dev);
-	kfree(chip);
 	return 0;
 }
 
drivers/char/tpm/tpm_infineon.c +0 -1
index dc0a255..0a72840 100644
@@ -581,7 +581,6 @@ static void tpm_inf_pnp_remove(struct pnp_dev *dev)
 			iounmap(tpm_dev.mem_base);
 			release_mem_region(tpm_dev.map_base, tpm_dev.map_size);
 		}
-		tpm_dev_vendor_release(chip);
 		tpm_remove_hardware(chip->dev);
 	}
 }
drivers/char/tpm/tpm_tis.c +1 -4
index 2c46734..a2780df 100644
@@ -811,10 +811,7 @@ MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
 static void tpm_tis_pnp_remove(struct pnp_dev *dev)
 {
 	struct tpm_chip *chip = pnp_get_drvdata(dev);
-
-	tpm_dev_vendor_release(chip);
-
-	kfree(chip);
+	tpm_remove_hardware(chip->dev);
 }
 
 
Signature · 1 line
2.1.0
#3 lore raw

Re: [tpmdd-devel] [PATCH v2 2/7] tpm: two-phase chip management functions

Quoted 4 lines · Jarkko Sakkinen

Added tpm_chip_alloc() and tpm_chip_register() where tpm_chip_alloc() reserves memory resources and tpm_chip_register() initializes the device driver. This way it is possible to alter struct tpm_chip attributes before passing it to tpm_chip_register().

This looks broadly reasonable to me

Please add a note to the commit that this is known to still have problems with resource reference counting, but they are less severe than what existed before, and this is only an interm step.

Quoted 2 lines

+/**
+ * tpm_chip_alloc() - allocate a new struct tpm_chip instance

This is using devm so it should be called 'tpmm_chip_alloc()' for clarity

I know that was there before, but it sure is racy:

Quoted 1 line
drivers/char/tpm/tpm-chip.c new file +1 -0
+	chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);

[..]

drivers/char/tpm/tpm-chip.c line 104

Quoted 1 line
drivers/char/tpm/tpm-chip.c new file +1 -0
+	set_bit(chip->dev_num, dev_mask);

Someday it should use IDR.

Quoted 9 lines
drivers/char/tpm/tpm-interface.c +0 -5
@@ -896,18 +872,7 @@ void tpm_remove_hardware(struct device *dev)
 		return;
 	}
 
-	spin_lock(&driver_lock);
-	list_del_rcu(&chip->list);
-	spin_unlock(&driver_lock);
-	synchronize_rcu();
-

drivers/char/tpm/tpm-interface.c void tpm_remove_hardware(struct device *dev) · line 877

Quoted 10 lines
drivers/char/tpm/tpm-interface.c +1 -7
-	tpm_dev_del_device(chip);
-	tpm_sysfs_del_device(chip);
-	tpm_remove_ppi(&dev->kobj);
-	tpm_bios_log_teardown(chip->bios_dir);
-
-	/* write it this way to be explicit (chip->dev == dev) */
-	put_device(chip->dev);
+	tpm_chip_unregister(chip);
 }
 EXPORT_SYMBOL_GPL(tpm_remove_hardware);

This can move to tpm-chip too, same with tpm_register_hardware

drivers/char/tpm/tpm_i2c_infineon.c static int tpm_tis_i2c_remove(struct i2c_client *client) · line 713

Quoted 9 lines
drivers/char/tpm/tpm_i2c_infineon.c +0 -3
@@ -714,15 +709,10 @@ static int tpm_tis_i2c_remove(struct i2c_client *client)
 	struct tpm_chip *chip = tpm_dev.chip;
 	release_locality(chip, chip->vendor.locality, 1);
 
-	/* close file handles */
-	tpm_dev_vendor_release(chip);
-
 	/* remove hardware */
 	tpm_remove_hardware(chip->dev);

Wrong ordering here, tpm_remove_hardware should always be first - drivers should not tear down internal state before calling it, so release_locality should be second.

Noting that since we use devm the kfree will not happen until remove returns, so the chip pointer is still valid.

drivers/char/tpm/tpm_i2c_infineon.c out_release: · line 641

Quoted 4 lines
drivers/char/tpm/tpm_i2c_infineon.c +0 -2
 	/* reset these pointers, otherwise we oops */
-	chip->dev->release = NULL;
-	chip->release = NULL;
 	tpm_dev.client = NULL;

The comment can go too

Note: tpm_dev should be driver private data, but that is not your problem..

Did you test compile all the drivers? One of my git commits on github has some hackery to make that possible on x86.

Jason

#4 lore raw

Re: [tpmdd-devel] [PATCH v2 2/7] tpm: two-phase chip management functions

Hi

And thanks for the feedback. Change requests look very reasonable.

Quoted 74 lines · Jason Gunthorpe
Quoted 4 lines · Jarkko Sakkinen

Added tpm_chip_alloc() and tpm_chip_register() where tpm_chip_alloc() reserves memory resources and tpm_chip_register() initializes the device driver. This way it is possible to alter struct tpm_chip attributes before passing it to tpm_chip_register().

This looks broadly reasonable to me

Please add a note to the commit that this is known to still have problems with resource reference counting, but they are less severe than what existed before, and this is only an interm step.

Quoted 2 lines

+/**
+ * tpm_chip_alloc() - allocate a new struct tpm_chip instance

This is using devm so it should be called 'tpmm_chip_alloc()' for clarity

I know that was there before, but it sure is racy:

Quoted 1 line
drivers/char/tpm/tpm-chip.c new file +1 -0
+	chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);

[..]

drivers/char/tpm/tpm-chip.c line 104

Quoted 1 line
drivers/char/tpm/tpm-chip.c new file +1 -0
+	set_bit(chip->dev_num, dev_mask);

Someday it should use IDR.

Quoted 9 lines
drivers/char/tpm/tpm-interface.c +0 -5
@@ -896,18 +872,7 @@ void tpm_remove_hardware(struct device *dev)
 		return;
 	}
 
-	spin_lock(&driver_lock);
-	list_del_rcu(&chip->list);
-	spin_unlock(&driver_lock);
-	synchronize_rcu();
-

drivers/char/tpm/tpm-interface.c void tpm_remove_hardware(struct device *dev) · line 877

Quoted 10 lines
drivers/char/tpm/tpm-interface.c +1 -7
-	tpm_dev_del_device(chip);
-	tpm_sysfs_del_device(chip);
-	tpm_remove_ppi(&dev->kobj);
-	tpm_bios_log_teardown(chip->bios_dir);
-
-	/* write it this way to be explicit (chip->dev == dev) */
-	put_device(chip->dev);
+	tpm_chip_unregister(chip);
 }
 EXPORT_SYMBOL_GPL(tpm_remove_hardware);

This can move to tpm-chip too, same with tpm_register_hardware

drivers/char/tpm/tpm_i2c_infineon.c static int tpm_tis_i2c_remove(struct i2c_client *client) · line 713

Quoted 9 lines
drivers/char/tpm/tpm_i2c_infineon.c +0 -3
@@ -714,15 +709,10 @@ static int tpm_tis_i2c_remove(struct i2c_client *client)
 	struct tpm_chip *chip = tpm_dev.chip;
 	release_locality(chip, chip->vendor.locality, 1);
 
-	/* close file handles */
-	tpm_dev_vendor_release(chip);
-
 	/* remove hardware */
 	tpm_remove_hardware(chip->dev);

Wrong ordering here, tpm_remove_hardware should always be first - drivers should not tear down internal state before calling it, so release_locality should be second.

Noting that since we use devm the kfree will not happen until remove returns, so the chip pointer is still valid.

drivers/char/tpm/tpm_i2c_infineon.c out_release: · line 641

Quoted 4 lines
drivers/char/tpm/tpm_i2c_infineon.c +0 -2
 	/* reset these pointers, otherwise we oops */
-	chip->dev->release = NULL;
-	chip->release = NULL;
 	tpm_dev.client = NULL;

The comment can go too

Quoted 5 lines

Note: tpm_dev should be driver private data, but that is not your problem..

Did you test compile all the drivers? One of my git commits on github has some hackery to make that possible on x86.

Yeah, I compiled all the drivers:

$ grep CONFIG_TCG .config
CONFIG_TCG_TPM=m
CONFIG_TCG_TIS=m
CONFIG_TCG_TIS_I2C_ATMEL=m
CONFIG_TCG_TIS_I2C_INFINEON=m
CONFIG_TCG_TIS_I2C_NUVOTON=m
CONFIG_TCG_NSC=m
CONFIG_TCG_ATMEL=m
CONFIG_TCG_INFINEON=m
CONFIG_TCG_ST33_I2C=m
CONFIG_TCG_XEN=m
CONFIG_TCG_CRB=m
Quoted 1 line

Jason

/Jarkko

#5 lore raw

Re: [tpmdd-devel] [PATCH v2 2/7] tpm: two-phase chip management functions

Quoted 5 lines · Jarkko Sakkinen
Quoted 2 lines

Did you test compile all the drivers? One of my git commits on github has some hackery to make that possible on x86.

Yeah, I compiled all the drivers:

Quoted 12 lines
$ grep CONFIG_TCG .config
CONFIG_TCG_TPM=m
CONFIG_TCG_TIS=m
CONFIG_TCG_TIS_I2C_ATMEL=m
CONFIG_TCG_TIS_I2C_INFINEON=m
CONFIG_TCG_TIS_I2C_NUVOTON=m
CONFIG_TCG_NSC=m
CONFIG_TCG_ATMEL=m
CONFIG_TCG_INFINEON=m
CONFIG_TCG_ST33_I2C=m
CONFIG_TCG_XEN=m
CONFIG_TCG_CRB=m

CONFIG_TCG_IBMVTPM is missing, apply this:

https://github.com/jgunthorpe/linux/commit/74afd69de603783858283f9b337e1694988964f8

Jason

#6 lore raw

Re: [tpmdd-devel] [PATCH v2 2/7] tpm: two-phase chip management functions

Quoted 20 lines · Jason Gunthorpe
Quoted 5 lines · Jarkko Sakkinen
Quoted 2 lines

Did you test compile all the drivers? One of my git commits on github has some hackery to make that possible on x86.

Yeah, I compiled all the drivers:

Quoted 12 lines
$ grep CONFIG_TCG .config
CONFIG_TCG_TPM=m
CONFIG_TCG_TIS=m
CONFIG_TCG_TIS_I2C_ATMEL=m
CONFIG_TCG_TIS_I2C_INFINEON=m
CONFIG_TCG_TIS_I2C_NUVOTON=m
CONFIG_TCG_NSC=m
CONFIG_TCG_ATMEL=m
CONFIG_TCG_INFINEON=m
CONFIG_TCG_ST33_I2C=m
CONFIG_TCG_XEN=m
CONFIG_TCG_CRB=m
Quoted 3 lines

CONFIG_TCG_IBMVTPM is missing, apply this:

https://github.com/jgunthorpe/linux/commit/74afd69de603783858283f9b337e1694988964f8

Oops, how did that go unnoticed. Will do.

Quoted 1 line

Jason

/Jarkko

#7 lore raw

Re: [tpmdd-devel] [PATCH v2 2/7] tpm: two-phase chip management functions

In reply to Jason Gunthorpe
Quoted 64 lines · Jason Gunthorpe
Quoted 4 lines · Jarkko Sakkinen

Added tpm_chip_alloc() and tpm_chip_register() where tpm_chip_alloc() reserves memory resources and tpm_chip_register() initializes the device driver. This way it is possible to alter struct tpm_chip attributes before passing it to tpm_chip_register().

This looks broadly reasonable to me

Please add a note to the commit that this is known to still have problems with resource reference counting, but they are less severe than what existed before, and this is only an interm step.

Quoted 2 lines

+/**
+ * tpm_chip_alloc() - allocate a new struct tpm_chip instance

This is using devm so it should be called 'tpmm_chip_alloc()' for clarity

I know that was there before, but it sure is racy:

Quoted 1 line
drivers/char/tpm/tpm-chip.c new file +1 -0
+	chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);

[..]

drivers/char/tpm/tpm-chip.c line 104

Quoted 1 line
drivers/char/tpm/tpm-chip.c new file +1 -0
+	set_bit(chip->dev_num, dev_mask);

Someday it should use IDR.

Quoted 9 lines
drivers/char/tpm/tpm-interface.c +0 -5
@@ -896,18 +872,7 @@ void tpm_remove_hardware(struct device *dev)
 		return;
 	}
 
-	spin_lock(&driver_lock);
-	list_del_rcu(&chip->list);
-	spin_unlock(&driver_lock);
-	synchronize_rcu();
-

drivers/char/tpm/tpm-interface.c void tpm_remove_hardware(struct device *dev) · line 877

Quoted 10 lines
drivers/char/tpm/tpm-interface.c +1 -7
-	tpm_dev_del_device(chip);
-	tpm_sysfs_del_device(chip);
-	tpm_remove_ppi(&dev->kobj);
-	tpm_bios_log_teardown(chip->bios_dir);
-
-	/* write it this way to be explicit (chip->dev == dev) */
-	put_device(chip->dev);
+	tpm_chip_unregister(chip);
 }
 EXPORT_SYMBOL_GPL(tpm_remove_hardware);

This can move to tpm-chip too, same with tpm_register_hardware

drivers/char/tpm/tpm_i2c_infineon.c static int tpm_tis_i2c_remove(struct i2c_client *client) · line 713

Quoted 9 lines
drivers/char/tpm/tpm_i2c_infineon.c +0 -3
@@ -714,15 +709,10 @@ static int tpm_tis_i2c_remove(struct i2c_client *client)
 	struct tpm_chip *chip = tpm_dev.chip;
 	release_locality(chip, chip->vendor.locality, 1);
 
-	/* close file handles */
-	tpm_dev_vendor_release(chip);
-
 	/* remove hardware */
 	tpm_remove_hardware(chip->dev);

Wrong ordering here, tpm_remove_hardware should always be first - drivers should not tear down internal state before calling it, so release_locality should be second.

Quoted 2 lines

Noting that since we use devm the kfree will not happen until remove returns, so the chip pointer is still valid.

Should I fix this ordering? I was thinking to focus putting proper patterns in place only in tpm_tis and tpm_crb because they are the that I'm able to test easily and then they can work as guideline for other drivers.

Quoted 7 lines

drivers/char/tpm/tpm_i2c_infineon.c out_release: · line 641

Quoted 4 lines
drivers/char/tpm/tpm_i2c_infineon.c +0 -2
 	/* reset these pointers, otherwise we oops */
-	chip->dev->release = NULL;
-	chip->release = NULL;
 	tpm_dev.client = NULL;

The comment can go too

Quoted 7 lines

Note: tpm_dev should be driver private data, but that is not your problem..

Did you test compile all the drivers? One of my git commits on github has some hackery to make that possible on x86.

Jason

/Jarkko

#8 lore raw

Re: [tpmdd-devel] [PATCH v2 2/7] tpm: two-phase chip management functions

Quoted 14 lines · Jarkko Sakkinen
Quoted 14 lines

drivers/char/tpm/tpm_i2c_infineon.c static int tpm_tis_i2c_remove(struct i2c_client *client) · line 713

Quoted 9 lines
drivers/char/tpm/tpm_i2c_infineon.c +0 -3
@@ -714,15 +709,10 @@ static int tpm_tis_i2c_remove(struct i2c_client *client)
 	struct tpm_chip *chip = tpm_dev.chip;
 	release_locality(chip, chip->vendor.locality, 1);
 
-	/* close file handles */
-	tpm_dev_vendor_release(chip);
-
 	/* remove hardware */
 	tpm_remove_hardware(chip->dev);

Wrong ordering here, tpm_remove_hardware should always be first - drivers should not tear down internal state before calling it, so release_locality should be second.

Quoted 7 lines
Quoted 2 lines

Noting that since we use devm the kfree will not happen until remove returns, so the chip pointer is still valid.

Should I fix this ordering? I was thinking to focus putting proper patterns in place only in tpm_tis and tpm_crb because they are the that I'm able to test easily and then they can work as guideline for other drivers.

I think since this patch is already touching this function there is no reason not to make it be correct (especially since it was noticed)

The rest can wait till we globally replace tpm_remove_hardware with tpm_unregister - at that time the ordering can be audited and checked.

Then the drivers will be clean and the core can finally be fixed.

Jason

#9 lore raw

Re: [tpmdd-devel] [PATCH v2 2/7] tpm: two-phase chip management functions

Quoted 24 lines · Jason Gunthorpe
Quoted 14 lines · Jarkko Sakkinen
Quoted 14 lines

drivers/char/tpm/tpm_i2c_infineon.c static int tpm_tis_i2c_remove(struct i2c_client *client) · line 713

Quoted 9 lines
drivers/char/tpm/tpm_i2c_infineon.c +0 -3
@@ -714,15 +709,10 @@ static int tpm_tis_i2c_remove(struct i2c_client *client)
 	struct tpm_chip *chip = tpm_dev.chip;
 	release_locality(chip, chip->vendor.locality, 1);
 
-	/* close file handles */
-	tpm_dev_vendor_release(chip);
-
 	/* remove hardware */
 	tpm_remove_hardware(chip->dev);

Wrong ordering here, tpm_remove_hardware should always be first - drivers should not tear down internal state before calling it, so release_locality should be second.

Quoted 7 lines
Quoted 2 lines

Noting that since we use devm the kfree will not happen until remove returns, so the chip pointer is still valid.

Should I fix this ordering? I was thinking to focus putting proper patterns in place only in tpm_tis and tpm_crb because they are the that I'm able to test easily and then they can work as guideline for other drivers.

Quoted 8 lines

I think since this patch is already touching this function there is no reason not to make it be correct (especially since it was noticed)

The rest can wait till we globally replace tpm_remove_hardware with tpm_unregister - at that time the ordering can be audited and checked.

Then the drivers will be clean and the core can finally be fixed.

This makes sense. I'll also document this. And I decided to completely wipe old tpm_register/remove_hardware() completely from v3 because they only cause confusion.

I pushed patch that should implement fix for the ordering into tpm2-v2 branch:

https://github.com/jsakkine/linux-tpm2/commit/63ab650fa6f8dddd95100869e50275801d7d9360

Quoted 1 line

Jason

/Jarkko

#10 lore raw

PATCH v2 3/7 tpm: clean up tpm_tis driver life-cycle

In reply to Jarkko Sakkinen

Updated tpm_tis to properly use tpm-chip API instead of using ad hoc methods. tpm_chip_unregister() is called on remove event when PNP driver is used and on module removal when platform driver is used.

---
 drivers/char/tpm/tpm_tis.c | 64 ++++++++++++++++++++--------------------------
 1 file changed, 28 insertions(+), 36 deletions(-)
drivers/char/tpm/tpm_tis.c +28 -36
index a2780df..df04ce6 100644
@@ -75,9 +75,6 @@ enum tis_defaults {
 #define	TPM_DID_VID(l)			(0x0F00 | ((l) << 12))
 #define	TPM_RID(l)			(0x0F04 | ((l) << 12))
 
-static LIST_HEAD(tis_chips);
-static DEFINE_MUTEX(tis_lock);
-
 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
 static int is_itpm(struct pnp_dev *dev)
 {
@@ -535,14 +532,17 @@ static int tpm_tis_init(struct device *dev, resource_size_t start,
 	int rc, i, irq_s, irq_e, probe;
 	struct tpm_chip *chip;
 
-	if (!(chip = tpm_register_hardware(dev, &tpm_tis)))
+	chip = tpm_chip_alloc(dev, &tpm_tis);
+	if (!chip)
 		return -ENODEV;
 
-	chip->vendor.iobase = ioremap(start, len);
-	if (!chip->vendor.iobase) {
-		rc = -EIO;
-		goto out_err;
-	}
+	chip->vendor.iobase = devm_ioremap(dev, start, len);
+	if (!chip->vendor.iobase)
+		return -EIO;
+
+	rc = tpm_chip_register(chip);
+	if (rc)
+		return -ENODEV;
 
 	/* Default timeouts */
 	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
@@ -720,16 +720,10 @@ static int tpm_tis_init(struct device *dev, resource_size_t start,
 	}
 
 	INIT_LIST_HEAD(&chip->vendor.list);
-	mutex_lock(&tis_lock);
-	list_add(&chip->vendor.list, &tis_chips);
-	mutex_unlock(&tis_lock);
-
 
 	return 0;
 out_err:
-	if (chip->vendor.iobase)
-		iounmap(chip->vendor.iobase);
-	tpm_remove_hardware(chip->dev);
+	tpm_chip_unregister(chip);
 	return rc;
 }
 
@@ -808,13 +802,27 @@ static struct pnp_device_id tpm_pnp_tbl[] = {
 };
 MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
 
+static void tpm_tis_chip_remove(struct tpm_chip *chip)
+{
+	iowrite32(~TPM_GLOBAL_INT_ENABLE &
+		  ioread32(chip->vendor.iobase +
+			   TPM_INT_ENABLE(chip->vendor.
+					  locality)),
+		  chip->vendor.iobase +
+		  TPM_INT_ENABLE(chip->vendor.locality));
+	release_locality(chip, chip->vendor.locality, 1);
+	if (chip->vendor.irq)
+		free_irq(chip->vendor.irq, chip);
+
+	tpm_chip_unregister(chip);
+}
+
 static void tpm_tis_pnp_remove(struct pnp_dev *dev)
 {
 	struct tpm_chip *chip = pnp_get_drvdata(dev);
-	tpm_remove_hardware(chip->dev);
+	tpm_tis_chip_remove(chip);
 }
 
-
 static struct pnp_driver tis_pnp_driver = {
 	.name = "tpm_tis",
 	.id_table = tpm_pnp_tbl,
@@ -873,31 +881,15 @@ err_dev:
 
 static void __exit cleanup_tis(void)
 {
-	struct tpm_vendor_specific *i, *j;
 	struct tpm_chip *chip;
-	mutex_lock(&tis_lock);
-	list_for_each_entry_safe(i, j, &tis_chips, list) {
-		chip = to_tpm_chip(i);
-		tpm_remove_hardware(chip->dev);
-		iowrite32(~TPM_GLOBAL_INT_ENABLE &
-			  ioread32(chip->vendor.iobase +
-				   TPM_INT_ENABLE(chip->vendor.
-						  locality)),
-			  chip->vendor.iobase +
-			  TPM_INT_ENABLE(chip->vendor.locality));
-		release_locality(chip, chip->vendor.locality, 1);
-		if (chip->vendor.irq)
-			free_irq(chip->vendor.irq, chip);
-		iounmap(i->iobase);
-		list_del(&i->list);
-	}
-	mutex_unlock(&tis_lock);
 #ifdef CONFIG_PNP
 	if (!force) {
 		pnp_unregister_driver(&tis_pnp_driver);
 		return;
 	}
 #endif
+	chip = dev_get_drvdata(&pdev->dev);
+	tpm_tis_chip_remove(chip);
 	platform_device_unregister(pdev);
 	platform_driver_unregister(&tis_drv);
 }
Signature · 1 line
2.1.0
#11 lore raw

Re: [tpmdd-devel] [PATCH v2 3/7] tpm: clean up tpm_tis driver life-cycle

drivers/char/tpm/tpm_tis.c static int tpm_tis_init(struct device *dev, resource_size_t start, · line 537

Quoted 3 lines · Jarkko Sakkinen
drivers/char/tpm/tpm_tis.c +2 -0
+	chip = tpm_chip_alloc(dev, &tpm_tis);
+	if (!chip)
 		return -ENODEV;

Needs to use ERR_PTR

drivers/char/tpm/tpm_tis.c static int tpm_tis_init(struct device *dev, resource_size_t start, · line 545

Quoted 3 lines
drivers/char/tpm/tpm_tis.c +3 -0
+	rc = tpm_chip_register(chip);
+	if (rc)
+		return -ENODEV;

Wrong ordering, this needs to be last in the probe function

Return rc not -ENODEV

Quoted 4 lines
drivers/char/tpm/tpm_tis.c +4 -0
+static void tpm_tis_chip_remove(struct tpm_chip *chip)
+{
+	iowrite32(~TPM_GLOBAL_INT_ENABLE &
+		  ioread32(chip->vendor.iobase +

drivers/char/tpm/tpm_tis.c static struct pnp_device_id tpm_pnp_tbl[] = { · line 818

Quoted 10 lines
drivers/char/tpm/tpm_tis.c +10 -0
+			   TPM_INT_ENABLE(chip->vendor.
+					  locality)),
+		  chip->vendor.iobase +
+		  TPM_INT_ENABLE(chip->vendor.locality));
+	release_locality(chip, chip->vendor.locality, 1);
+	if (chip->vendor.irq)
+		free_irq(chip->vendor.irq, chip);
+
+	tpm_chip_unregister(chip);
+}

Wrong ordering, tpm_chip_unregister needs to be first

drivers/char/tpm/tpm_tis.c err_dev: · line 893

Quoted 3 lines
drivers/char/tpm/tpm_tis.c +2 -0
+	chip = dev_get_drvdata(&pdev->dev);
+	tpm_tis_chip_remove(chip);
 	platform_device_unregister(pdev);

I'm under the impression devm does not work outside a device driver context, so adding devm breaks force mode in this driver. Do you see differently?

AFAIK the two options are to fix force mode so that it attaches the dummy platform driver (that is what it is for after all) or remove force mode.

Jason

#12 lore raw

PATCH v2 4/7 tpm: TPM 2.0 commands

In reply to Jarkko Sakkinen

This patch contains the following internal helper functions for tpm.ko:

- tpm2_get_random()
- tpm2_get_tpm_pt()
- tpm2_pcr_extend()
- tpm2_pcr_read()
- tpm2_startup()

and the following exported functions for implementing new device drivers:

- tpm2_do_selftest()
- tpm2_calc_ordinal_durations()
- tpm2_gen_interrupt()

Functions that are defined in include/linux/tpm.h and tpm_transmit() use TPM2 commands for TPM2 chips.

Added a new field "flags" to struct tpm_chip and enum tpm_chip_flags. TPM2 chips can set TPM_CHIP_FLAG_TPM2 after tpm_chip_alloc() to enable TPM2 commands.

The code for tpm2_calc_ordinal_duration() and tpm2_startup() is derived from the code originally made by Will Arthur.

---
 drivers/char/tpm/Makefile        |   2 +-
 drivers/char/tpm/tpm-interface.c |  24 +-
 drivers/char/tpm/tpm.h           |  66 +++++
 drivers/char/tpm/tpm2-cmd.c      | 543 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 631 insertions(+), 4 deletions(-)
 create mode 100644 drivers/char/tpm/tpm2-cmd.c
drivers/char/tpm/Makefile +1 -1
index 837da04..ae56af9 100644
@@ -2,7 +2,7 @@
 # Makefile for the kernel tpm device drivers.
 #
 obj-$(CONFIG_TCG_TPM) += tpm.o
-tpm-y := tpm-interface.o tpm-dev.o tpm-sysfs.o tpm-chip.o
+tpm-y := tpm-interface.o tpm-dev.o tpm-sysfs.o tpm-chip.o tpm2-cmd.o
 tpm-$(CONFIG_ACPI) += tpm_ppi.o
 
 ifdef CONFIG_ACPI
drivers/char/tpm/tpm-interface.c +21 -3
index 1ce3ad3..ec28b5f 100644
@@ -360,7 +360,10 @@ static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
 	if (chip->vendor.irq)
 		goto out_recv;
 
-	stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
+	if (chip->flags & TPM_CHIP_FLAG_TPM2)
+		stop = jiffies + tpm2_calc_ordinal_duration(chip, ordinal);
+	else
+		stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
 	do {
 		u8 status = chip->ops->status(chip);
 		if ((status & chip->ops->req_complete_mask) ==
@@ -482,7 +485,7 @@ static const struct tpm_input_header tpm_startup_header = {
 static int tpm_startup(struct tpm_chip *chip, __be16 startup_type)
 {
 	struct tpm_cmd_t start_cmd;
-	start_cmd.header.in = tpm_startup_header;
+
 	start_cmd.params.startup_in.startup_type = startup_type;
 	return tpm_transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE,
 				"attempting to start the TPM");
@@ -679,7 +682,10 @@ int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
 	chip = tpm_chip_find_get(chip_num);
 	if (chip == NULL)
 		return -ENODEV;
-	rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
+	if (chip->flags & TPM_CHIP_FLAG_TPM2)
+		rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
+	else
+		rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
 	tpm_chip_put(chip);
 	return rc;
 }
@@ -713,6 +719,12 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
 	if (chip == NULL)
 		return -ENODEV;
 
+	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
+		rc = tpm2_pcr_extend(chip, pcr_idx, hash);
+		tpm_chip_put(chip);
+		return rc;
+	}
+
 	cmd.header.in = pcrextend_header;
 	cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
 	memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
@@ -986,6 +998,12 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
 	if (chip == NULL)
 		return -ENODEV;
 
+	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
+		err = tpm2_get_random(chip, out, max);
+		tpm_chip_put(chip);
+		return err;
+	}
+
 	do {
 		tpm_cmd.header.in = tpm_getrandom_header;
 		tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
drivers/char/tpm/tpm.h +66 -0

84 lines not on this page · raw message

drivers/char/tpm/tpm2-cmd.c new file +543 -0

543 lines not on this page · raw message

Signature · 1 line
2.1.0
#13 lore raw

PATCH v2 5/7 tpm: TPM 2.0 sysfs attributes

In reply to Jarkko Sakkinen

Implemented sysfs attributes for TPM2 devices.

Documentation/ABI/stable/sysfs-class/tpm2 contains descriptions of these attributes.

---
 Documentation/ABI/stable/sysfs-class-tpm2 |  69 +++++++
 drivers/char/tpm/Makefile                 |   2 +-
 drivers/char/tpm/tpm-chip.c               |  10 +-
 drivers/char/tpm/tpm.h                    |  24 +++
 drivers/char/tpm/tpm2-sysfs.c             | 314 ++++++++++++++++++++++++++++++
 5 files changed, 416 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/ABI/stable/sysfs-class-tpm2
 create mode 100644 drivers/char/tpm/tpm2-sysfs.c
Documentation/ABI/stable/sysfs-class-tpm2 new file +69 -0
new file mode 100644
index 0000000..e9d4b6a
@@ -0,0 +1,69 @@
+What:		/sys/class/misc/tpmX/device/
+Date:		October 2014
+KernelVersion:	3.19
+Contact:	[email protected]
+Description:	The device/ directory under a specific TPM instance exposes
+		the properties of that TPM chip.
+
+What:		/sys/class/misc/tpmX/device/version
+Date:		October 2014
+KernelVersion:	3.19
+Contact:	[email protected]
+Description:	The "version" property prints the protocol version number
+		in the major.minor format.
+
+What:		/sys/class/misc/tpmX/device/enabled_sh
+Date:		October 2014
+KernelVersion:	3.19
+Contact:	[email protected]
+Description:	The "enabled_sh" property prints a '1' if the Storage Hierarchy
+		is enabled.
+
+What:		/sys/class/misc/tpmX/device/enabled_eh
+Date:		October 2014
+KernelVersion:	3.19
+Contact:	[email protected]
+Description:	The "enabled_eh" property prints a '1' if the Endorsement Hierarchy
+		is enabled.
+
+What:		/sys/class/misc/tpmX/device/owned_sh
+Date:		October 2014
+KernelVersion:	3.19
+Contact:	[email protected]
+Description:	The "owned_sh" property prints a '1' if the ownership of the 
+		Storage Hierarchy has been taken.
+
+What:		/sys/class/misc/tpmX/device/owned_eh
+Date:		October 2014
+KernelVersion:	3.19
+Contact:	[email protected]
+Description:	The "owned_sh" property prints a '1' if the ownership of the
+		Endrosoment Hierarchy has been taken.
+
+What:		/sys/class/misc/tpmX/device/manufacturer
+Date:		October 2014
+KernelVersion:	3.19
+Contact:	[email protected]
+Description:	The "manufacturer" property prints the vendor ID of the TPM
+		manufacturer.
+
+What:		/sys/class/misc/tpmX/device/firmware
+Date:		October 2014
+KernelVersion:	3.19
+Contact:	[email protected]
+Description:	The property prints the vendor-specific value indicating the
+		version of the firmware.
+
+What:		/sys/class/misc/tpmX/device/pcr/sha1/X
+Date:		October 2014
+KernelVersion:	3.19
+Contact:	[email protected]
+Description:	These files print PCR values for the SHA-1 bank.
+
+What:		/sys/class/misc/tpmX/device/cancel
+Date:		October 2014
+KernelVersion:	3.19
+Contact:	[email protected]
+Description:	The "cancel" property allows you to cancel the currently
+		pending TPM command. Writing any value to cancel will call the
+		TPM chip specific cancel operation.
drivers/char/tpm/Makefile +1 -1
index ae56af9..d3cf905 100644
@@ -2,7 +2,7 @@
 # Makefile for the kernel tpm device drivers.
 #
 obj-$(CONFIG_TCG_TPM) += tpm.o
-tpm-y := tpm-interface.o tpm-dev.o tpm-sysfs.o tpm-chip.o tpm2-cmd.o
+tpm-y := tpm-interface.o tpm-dev.o tpm-sysfs.o tpm-chip.o tpm2-cmd.o tpm2-sysfs.o
 tpm-$(CONFIG_ACPI) += tpm_ppi.o
 
 ifdef CONFIG_ACPI
drivers/char/tpm/tpm-chip.c +8 -2
index 6cc4cee..5c50fd7 100644
@@ -130,7 +130,10 @@ int tpm_chip_register(struct tpm_chip *chip)
 	if (rc)
 		return rc;
 
-	rc = tpm_sysfs_add_device(chip);
+	if (chip->flags & TPM_CHIP_FLAG_TPM2)
+		rc = tpm2_sysfs_add_device(chip->dev);
+	else
+		rc = tpm_sysfs_add_device(chip);
 	if (rc)
 		goto del_misc;
 
@@ -171,7 +174,10 @@ void tpm_chip_unregister(struct tpm_chip *chip)
 	synchronize_rcu();
 
 	tpm_dev_del_device(chip);
-	tpm_sysfs_del_device(chip);
+	if (chip->flags & TPM_CHIP_FLAG_TPM2)
+		tpm2_sysfs_del_device(chip->dev);
+	else
+		tpm_sysfs_del_device(chip);
 	tpm_remove_ppi(&chip->dev->kobj);
 	tpm_bios_log_teardown(chip->bios_dir);
 }
drivers/char/tpm/tpm.h +24 -0
index d141639..4678cdf 100644
@@ -107,6 +107,24 @@ enum tpm2_capabilities {
 	TPM2_CAP_TPM_PROPERTIES = 6,
 };
 
+enum tpm2_tpm_properties {
+	TPM2_PT_MANUFACTURER		= 0x00000105,
+	TPM2_PT_FIRMWARE_VERSION_1	= 0x00000111,
+	TPM2_PT_FIRMWARE_VERSION_2	= 0x00000111,
+	TPM2_PT_PERMANENT		= 0x00000200,
+	TPM2_PT_STARTUP_CLEAR		= 0x00000201,
+};
+
+enum tpm2_pt_startup_clear {
+	TPM2_PT_SC_SH_ENABLE	= BIT(1),
+	TPM2_PT_SC_EH_ENABLE	= BIT(2),
+};
+
+enum tpm2_pt_permanent {
+	TPM2_PT_PM_OWNER_AUTH_SET	= BIT(0),
+	TPM2_PT_PM_ENDORSEMENT_AUTH_SET	= BIT(1),
+};
+
 enum tpm2_startup_types {
 	TPM2_SU_CLEAR	= 0x0000,
 	TPM2_SU_STATE	= 0x0001,
@@ -165,6 +183,9 @@ struct tpm_chip {
 
 	struct dentry **bios_dir;
 
+	struct kobject *pcrs_kobj;
+	void *sha1_bank;
+
 	struct list_head list;
 };
 
@@ -419,3 +440,6 @@ extern ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
 			       u32* value, const char *desc);
 extern unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *, u32);
 extern int tpm2_do_selftest(struct tpm_chip *chip);
+
+int tpm2_sysfs_add_device(struct device *dev);
+void tpm2_sysfs_del_device(struct device *dev);
drivers/char/tpm/tpm2-sysfs.c new file +314 -0

314 lines not on this page · raw message

Signature · 1 line
2.1.0
#14 lore raw

PATCH v2 6/7 tpm: TPM 2.0 CRB Interface

In reply to Jarkko Sakkinen

tpm_crb is a driver for TPM 2.0 Command Response Buffer (CRB) Interface as defined in PC Client Platform TPM Profile (PTP) Specification.

Only polling and single locality is supported as these are the limitations of the available hardware, Platform Trust Techonlogy (PTT) in Haswell CPUs.

The driver always applies CRB with ACPI start because PTT reports using only ACPI start as start method but as a result of my testing it requires also CRB start.

---
 drivers/char/tpm/Kconfig   |   9 ++
 drivers/char/tpm/Makefile  |   1 +
 drivers/char/tpm/tpm_crb.c | 329 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 339 insertions(+)
 create mode 100644 drivers/char/tpm/tpm_crb.c
drivers/char/tpm/Kconfig +9 -0
index c54cac3..10c9419 100644
@@ -122,4 +122,13 @@ config TCG_XEN
 	  To compile this driver as a module, choose M here; the module
 	  will be called xen-tpmfront.
 
+config TCG_CRB
+	tristate "TPM 2.0 CRB Interface"
+	depends on X86 && ACPI
+	---help---
+	  If you have a TPM security chip that is compliant with the
+	  TCG CRB 2.0 TPM specification say Yes and it will be accessible
+	  from within Linux.  To compile this driver as a module, choose
+	  M here; the module will be called tpm_crb.
+
 endif # TCG_TPM
drivers/char/tpm/Makefile +1 -0
index d3cf905..15e3b4c 100644
@@ -22,3 +22,4 @@ obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
 obj-$(CONFIG_TCG_XEN) += xen-tpmfront.o
+obj-$(CONFIG_TCG_CRB) += tpm_crb.o
drivers/char/tpm/tpm_crb.c new file +329 -0

329 lines not on this page · raw message

Signature · 1 line
2.1.0
#15 lore raw

Re: [tpmdd-devel] [PATCH v2 6/7] tpm: TPM 2.0 CRB Interface

drivers/char/tpm/tpm_crb.c line 238

Quoted 3 lines · Jarkko Sakkinen
drivers/char/tpm/tpm_crb.c new file +3 -0
+	rc = tpm_chip_register(chip);
+	if (rc)
+		return -ENODEV;

Again, wrong order, this needs to be last in the probe function

Jason

#16 lore raw

PATCH v2 7/7 tpm: TPM 2.0 FIFO Interface

In reply to Jarkko Sakkinen

From: Will Arthur <[email protected]>

Detect TPM 2.0 by using the extended STS (STS3) register. For TPM 2.0, instead of calling tpm_get_timeouts(), assign duration and timeout values defined in the TPM 2.0 PTP specification.

---
 drivers/char/tpm/tpm_tis.c | 77 ++++++++++++++++++++++++++++++++++++----------
 1 file changed, 60 insertions(+), 17 deletions(-)
drivers/char/tpm/tpm_tis.c +60 -17
index df04ce6..5c5a0cc 100644
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2005, 2006 IBM Corporation
+ * Copyright (C) 2014 Intel Corporation
  *
  * Authors:
  * Leendert van Doorn <[email protected]>
@@ -44,6 +45,10 @@ enum tis_status {
 	TPM_STS_DATA_EXPECT = 0x08,
 };
 
+enum tis_status3 {
+	TPM_STS3_TPM2_FAM = 0x04,
+};
+
 enum tis_int_flags {
 	TPM_GLOBAL_INT_ENABLE = 0x80000000,
 	TPM_INTF_BURST_COUNT_STATIC = 0x100,
@@ -70,6 +75,7 @@ enum tis_defaults {
 #define	TPM_INT_STATUS(l)		(0x0010 | ((l) << 12))
 #define	TPM_INTF_CAPS(l)		(0x0014 | ((l) << 12))
 #define	TPM_STS(l)			(0x0018 | ((l) << 12))
+#define	TPM_STS3(l)			(0x001b | ((l) << 12))
 #define	TPM_DATA_FIFO(l)		(0x0024 | ((l) << 12))
 
 #define	TPM_DID_VID(l)			(0x0F00 | ((l) << 12))
@@ -344,6 +350,7 @@ static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
 {
 	int rc;
 	u32 ordinal;
+	unsigned long dur;
 
 	rc = tpm_tis_send_data(chip, buf, len);
 	if (rc < 0)
@@ -355,9 +362,14 @@ static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
 
 	if (chip->vendor.irq) {
 		ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+
+		if (chip->flags & TPM_CHIP_FLAG_TPM2)
+			dur = tpm_calc_ordinal_duration(chip, ordinal);
+		else
+			dur = tpm_calc_ordinal_duration(chip, ordinal);
+
 		if (wait_for_tpm_stat
-		    (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
-		     tpm_calc_ordinal_duration(chip, ordinal),
+		    (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
 		     &chip->vendor.read_queue, false) < 0) {
 			rc = -ETIME;
 			goto out_err;
@@ -531,24 +543,43 @@ static int tpm_tis_init(struct device *dev, resource_size_t start,
 	u32 vendor, intfcaps, intmask;
 	int rc, i, irq_s, irq_e, probe;
 	struct tpm_chip *chip;
+	u8 sts3;
+	u32 dummy;
 
 	chip = tpm_chip_alloc(dev, &tpm_tis);
-	if (!chip)
+	if (IS_ERR(chip))
 		return -ENODEV;
 
 	chip->vendor.iobase = devm_ioremap(dev, start, len);
 	if (!chip->vendor.iobase)
 		return -EIO;
 
+	sts3 = ioread8(chip->vendor.iobase + TPM_STS3(1));
+	if (sts3 & TPM_STS3_TPM2_FAM)
+		chip->flags = TPM_CHIP_FLAG_TPM2;
+
 	rc = tpm_chip_register(chip);
 	if (rc)
 		return -ENODEV;
 
-	/* Default timeouts */
-	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
-	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
-	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
-	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+        /* Default timeouts */
+        if (chip->flags & TPM_CHIP_FLAG_TPM2) {
+                chip->vendor.timeout_a = usecs_to_jiffies(TPM2_TIMEOUT_A);
+                chip->vendor.timeout_b = usecs_to_jiffies(TPM2_TIMEOUT_B);
+                chip->vendor.timeout_c = usecs_to_jiffies(TPM2_TIMEOUT_C);
+                chip->vendor.timeout_d = usecs_to_jiffies(TPM2_TIMEOUT_D);
+                chip->vendor.duration[TPM_SHORT] =
+                        usecs_to_jiffies(TPM2_DURATION_SHORT);
+                chip->vendor.duration[TPM_MEDIUM] =
+                        usecs_to_jiffies(TPM2_DURATION_MEDIUM);
+                chip->vendor.duration[TPM_LONG] =
+                        usecs_to_jiffies(TPM2_DURATION_LONG);
+        } else {
+                chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+                chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+                chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+                chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+        }
 
 	if (wait_startup(chip, 0) != 0) {
 		rc = -ENODEV;
@@ -563,9 +594,9 @@ static int tpm_tis_init(struct device *dev, resource_size_t start,
 	vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
 	chip->vendor.manufacturer_id = vendor;
 
-	dev_info(dev,
-		 "1.2 TPM (device-id 0x%X, rev-id %d)\n",
-		 vendor >> 16, ioread8(chip->vendor.iobase + TPM_RID(0)));
+        dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
+                 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
+                 vendor >> 16, ioread8(chip->vendor.iobase + TPM_RID(0)));
 
 	if (!itpm) {
 		probe = probe_itpm(chip);
@@ -612,7 +643,11 @@ static int tpm_tis_init(struct device *dev, resource_size_t start,
 		goto out_err;
 	}
 
-	if (tpm_do_selftest(chip)) {
+	if (chip->flags & TPM_CHIP_FLAG_TPM2)
+		rc = tpm2_do_selftest(chip);
+	else
+		rc = tpm_do_selftest(chip);
+	if (rc) {
 		dev_err(dev, "TPM self test failed\n");
 		rc = -ENODEV;
 		goto out_err;
@@ -673,7 +708,11 @@ static int tpm_tis_init(struct device *dev, resource_size_t start,
 			chip->vendor.probed_irq = 0;
 
 			/* Generate Interrupts */
-			tpm_gen_interrupt(chip);
+			if (chip->flags & TPM_CHIP_FLAG_TPM2)
+				rc = tpm2_get_tpm_pt(chip, TPM2_CAP_TPM_PROPERTIES, &dummy,
+						     "attempting to generate an interrupt");
+			else
+				tpm_gen_interrupt(chip);
 
 			chip->vendor.irq = chip->vendor.probed_irq;
 
@@ -752,14 +791,18 @@ static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
 static int tpm_tis_resume(struct device *dev)
 {
 	struct tpm_chip *chip = dev_get_drvdata(dev);
-	int ret;
+	int ret = 0;
 
 	if (chip->vendor.irq)
 		tpm_tis_reenable_interrupts(chip);
 
-	ret = tpm_pm_resume(dev);
-	if (!ret)
-		tpm_do_selftest(chip);
+	if (chip->flags & TPM_CHIP_FLAG_TPM2)
+		tpm2_do_selftest(chip);
+	else {
+		ret = tpm_pm_resume(dev);
+		if (!ret)
+			tpm_do_selftest(chip);
+	}
 
 	return ret;
 }
Signature · 1 line
2.1.0