tpm: Simple implementation of tpm_tis_i2c
Subsystems (2)
cover tpm: Simple implementation of tpm_tis_i2c
Following the discussion in https://lkml.org/lkml/2019/7/5/303 I went ahead and created what I had in mind, trying to keep it as simple as possible. It is meant more as a starting point than a final implementation, as I'm not sure yet how to best integrate it with the existing tpm_tis_core (hence RFC):
It would probably be a cleaner interface, if locality and register address were two separate parameters. I did not implement it here, since that would have meant touching all the tpm_tis_core-based drivers. Also, I noticed that the kernel only ever uses locality 0 anyway. So maybe it would be even better to remove the locality parameter completely and just hardcode locality to 0 where necessary?
I also did not introduce any abstraction for register addresses, but simply remap the well-known addresses from the old TIS versions (which are used within tpm_tis_core) to the corresponding new addresses for I2C before accessing them. Again, it might be cleaner to have maybe an enum to address registers within tpm_tis_core and then a lookup table for the actual addresses in the low-level drivers?
IRQ support is deactivated for the moment, though it probably would work (or at least fall back to polling without catastrophic failures). But I'm not sure what exactly happens when the IRQ code path in tpm_tis_core accesses TPM_INT_VECTOR, which does not exist for I2C. Those accesses should just get ignored by the TPM, but actually testing it would have been more effort.
Speaking of tests, I only verified that basic communication with the TPM (i.e. reading/writing registers) works. For more tests, more test devices would be really helpful, which is why asked for that (https://lkml.org/lkml/2019/7/17/110).
Alexander Steffen (2):
tpm: Make implementation of read16/read32/write32 optional
tpm: Add tpm_tis_i2c backend for tpm_tis_core
drivers/char/tpm/Kconfig | 11 ++ drivers/char/tpm/Makefile | 1 + drivers/char/tpm/tpm_tis_core.h | 41 +++++- drivers/char/tpm/tpm_tis_i2c.c | 233 ++++++++++++++++++++++++++++++++ drivers/char/tpm/tpm_tis_spi.c | 41 ------ 5 files changed, 283 insertions(+), 44 deletions(-) create mode 100644 drivers/char/tpm/tpm_tis_i2c.c
Signature · 1 line
2.17.1
RFC PATCH 1/2 tpm: Make implementation of read16/read32/write32 optional
Only tpm_tis has a faster way to access multiple bytes at once, every other driver will just fall back to read_bytes/write_bytes. Therefore, move this common code out of tpm_tis_spi into tpm_tis_core, so that it is automatically used when low-level drivers do not implement the specialized methods.
--- drivers/char/tpm/tpm_tis_core.h | 41 ++++++++++++++++++++++++++++++--- drivers/char/tpm/tpm_tis_spi.c | 41 --------------------------------- 2 files changed, 38 insertions(+), 44 deletions(-)
drivers/char/tpm/tpm_tis_core.h +38 -3
@@ -122,13 +122,37 @@ static inline int tpm_tis_read8(struct tpm_tis_data *data, u32 addr, u8 *result)static inline int tpm_tis_read16 (struct tpm_tis_data *data,u32 addr,u16 *result) { -return data->phy_ops->read16 (data, addr, result); +if (data->phy_ops->read16) { +return data->phy_ops->read16 (data, addr, result); + }else { +__le16 result_le; +int rc; + + rc = data->phy_ops->read_bytes (data, addr,sizeof (u16), + (u8 *)&result_le); +if (!rc) + *result =le16_to_cpu (result_le); + +return rc; + } }static inline int tpm_tis_read32 (struct tpm_tis_data *data,u32 addr,u32 *result) { -return data->phy_ops->read32 (data, addr, result); +if (data->phy_ops->read32) { +return data->phy_ops->read32 (data, addr, result); + }else { +__le32 result_le; +int rc; + + rc = data->phy_ops->read_bytes (data, addr,sizeof (u32), + (u8 *)&result_le); +if (!rc) + *result =le32_to_cpu (result_le); + +return rc; + } } static inline int tpm_tis_write_bytes(struct tpm_tis_data *data, u32 addr,
@@ -145,7 +169,18 @@ static inline int tpm_tis_write8(struct tpm_tis_data *data, u32 addr, u8 value)static inline int tpm_tis_write32 (struct tpm_tis_data *data,u32 addr,u32 value) { -return data->phy_ops->write32 (data, addr, value); +if (data->phy_ops->write32) { +return data->phy_ops->write32 (data, addr, value); + }else { +__le32 value_le; +int rc; + + value_le =cpu_to_le32 (value); + rc = data->phy_ops->write_bytes (data, addr,sizeof (u32), + (u8 *)&value_le); + +return rc; + } }static inline bool is_bsw (void )
drivers/char/tpm/tpm_tis_spi.c +0 -41
50 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [RFC PATCH 1/2] tpm: Make implementation of read16/read32/write32 optional
Quoted 24 lines · Alexander Steffen
Only
tpm_tishas a faster way to access multiple bytes at once, every other driver will just fall back to read_bytes/write_bytes. Therefore, move this common code out oftpm_tis_spiintotpm_tis_core, so that it is automatically used when low-level drivers do not implement the specialized methods.Signed-off-by Alexander Steffen <[email protected]>--- drivers/char/tpm/tpm_tis_core.h | 41 ++++++++++++++++++++++++++++++--- drivers/char/tpm/tpm_tis_spi.c | 41 --------------------------------- 2 files changed, 38 insertions(+), 44 deletions(-)drivers/char/tpm/tpm_tis_core.h +3
-1@@ -122,13 +122,37 @@ static inline int tpm_tis_read8(struct tpm_tis_data *data, u32 addr, u8 *result)static inline int tpm_tis_read16 (struct tpm_tis_data *data,u32 addr,u16 *result) { -return data->phy_ops->read16 (data, addr, result); +if (data->phy_ops->read16) { +return data->phy_ops->read16 (data, addr, result); + }else {
drivers/char/tpm/tpm_tis_core.h static inline int tpm_tis_read8(struct tpm_tis_data *data, u32 addr, u8 *result) · line 137
Quoted 10 lines
drivers/char/tpm/tpm_tis_core.h +10
-0+__le16 result_le; +int rc; + + rc = data->phy_ops->read_bytes (data, addr,sizeof (u16), + (u8 *)&result_le); +if (!rc) + *result =le16_to_cpu (result_le); + +return rc; + }
Looks great.
I prefer to have variable declarations in the beginning of function for most of the time. Other than that looks legit.
/Jarkko
PATCH v2 1/1 tpm: Make implementation of read16/read32/write32 optional
In reply to Alexander SteffenFrom: Alexander Steffen <[email protected]>
Only tpm_tis and tpm_tis_synquacer have a dedicated way to access multiple bytes at once, every other driver will just fall back to read_bytes/write_bytes. Therefore, move this common code out of tpm_tis_spi_main and tpm_tis_spi_cr50 into tpm_tis_core, so that it is automatically used when low-level drivers do not implement the specialized methods.
--- drivers/char/tpm/tpm_tis_core.h | 37 +++++++++++++++++++++++--- drivers/char/tpm/tpm_tis_spi.h | 4 --- drivers/char/tpm/tpm_tis_spi_cr50.c | 3 --- drivers/char/tpm/tpm_tis_spi_main.c | 41 ----------------------------- 4 files changed, 34 insertions(+), 51 deletions(-)
drivers/char/tpm/tpm_tis_core.h +34 -3
@@ -128,13 +128,35 @@ static inline int tpm_tis_read8(struct tpm_tis_data *data, u32 addr, u8 *result)static inline int tpm_tis_read16 (struct tpm_tis_data *data,u32 addr,u16 *result) { -return data->phy_ops->read16 (data, addr, result); +__le16 result_le; +int rc; + +if (data->phy_ops->read16) +return data->phy_ops->read16 (data, addr, result); + + rc = data->phy_ops->read_bytes (data, addr,sizeof (u16), + (u8 *)&result_le); +if (!rc) + *result =le16_to_cpu (result_le); + +return rc; }static inline int tpm_tis_read32 (struct tpm_tis_data *data,u32 addr,u32 *result) { -return data->phy_ops->read32 (data, addr, result); +__le32 result_le; +int rc; + +if (data->phy_ops->read32) +return data->phy_ops->read32 (data, addr, result); + + rc = data->phy_ops->read_bytes (data, addr,sizeof (u32), + (u8 *)&result_le); +if (!rc) + *result =le32_to_cpu (result_le); + +return rc; } static inline int tpm_tis_write_bytes(struct tpm_tis_data *data, u32 addr,
@@ -151,7 +173,16 @@ static inline int tpm_tis_write8(struct tpm_tis_data *data, u32 addr, u8 value)static inline int tpm_tis_write32 (struct tpm_tis_data *data,u32 addr,u32 value) { -return data->phy_ops->write32 (data, addr, value); +__le32 value_le; +int rc; + +if (data->phy_ops->write32) +return data->phy_ops->write32 (data, addr, value); + + value_le =cpu_to_le32 (value); + rc = data->phy_ops->write_bytes (data, addr,sizeof (u32), + (u8 *)&value_le); +return rc; }static inline bool is_bsw (void )
drivers/char/tpm/tpm_tis_spi.h +0 -4
@@ -31,10 +31,6 @@ extern int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,extern int tpm_tis_spi_transfer (struct tpm_tis_data *data,u32 addr,u16 len,u8 *in,const u8 *out); -extern int tpm_tis_spi_read16 (struct tpm_tis_data *data,u32 addr,u16 *result); -extern int tpm_tis_spi_read32 (struct tpm_tis_data *data,u32 addr,u32 *result); -extern int tpm_tis_spi_write32 (struct tpm_tis_data *data,u32 addr,u32 value); -#ifdef CONFIG_TCG_TIS_SPI_CR50 extern int cr50_spi_probe (struct spi_device *spi);#else
drivers/char/tpm/tpm_tis_spi_cr50.c +0 -3
@@ -236,9 +236,6 @@ static int tpm_tis_spi_cr50_write_bytes(struct tpm_tis_data *data, u32 addr,static const struct tpm_tis_phy_ops tpm_spi_cr50_phy_ops = { .read_bytes = tpm_tis_spi_cr50_read_bytes, .write_bytes = tpm_tis_spi_cr50_write_bytes, - .read16 = tpm_tis_spi_read16, - .read32 = tpm_tis_spi_read32, - .write32 = tpm_tis_spi_write32, };static void cr50_print_fw_version (struct tpm_tis_data *data)
drivers/char/tpm/tpm_tis_spi_main.c +0 -41
53 lines not on this page · raw message
Signature · 1 line
2.31.1.windows.1
Jarkko Sakkinen
maintainerRe: [PATCH v2] tpm: Make implementation of read16/read32/write32 optional
Quoted 9 lines · Johannes Holland
From: Alexander Steffen <[email protected]>
Only
tpm_tisandtpm_tis_synquacerhave a dedicated way to access multiple bytes at once, every other driver will just fall back to read_bytes/write_bytes. Therefore, move this common code out oftpm_tis_spi_mainandtpm_tis_spi_cr50intotpm_tis_core, so that it is automatically used when low-level drivers do not implement the specialized methods.
Quoted 3 lines
Signed-off-by Alexander Steffen <[email protected]>Signed-off-by Johannes Holland <[email protected]>---
Please, add a change log to the next version, which also includes description of v2 changes.
Redudancy is insignificant, and how things are now does not require special cases from the upper layer.
Instead you should do this:
enum tpm_tis_io_mode {
TPM_TIS_PHYS_8,
TPM_TIS_PHYS_16,
TPM_TIS_PHYS_32,
};struct tpm_tis_phy_ops {
int (*read_bytes)(struct tpm_tis_data *data, u32 addr, u16 len, u8 *result,
int mode);
int (*write_bytes)(struct tpm_tis_data *data, u32 addr, u16 len, const u8 *value,
int mode);
};And e.g.
static inline int tpm_tis_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
{
rc = data->phy_ops->read_bytes(data, addr, sizeof(u16), &result_le, TPM_TIS_PHYS_16);
}This takes away over half of the callback API, and does not require special cases on the top layer.
BR, Jarkko
RFC PATCH 2/2 tpm: Add tpm_tis_i2c backend for tpm_tis_core
In reply to Alexander SteffenImplements the minimal functionality necessary to talk to an I2C TPM according to the TCG TPM I2C Interface Specification.
Limitations:
* No IRQ support
* No support for updating GUARD_TIME (uses always the default of 250µs)
* No support for Data Checksum register (optional feature only for I2C)
--- drivers/char/tpm/Kconfig | 11 ++ drivers/char/tpm/Makefile | 1 + drivers/char/tpm/tpm_tis_i2c.c | 233 +++++++++++++++++++++++++++++++++ 3 files changed, 245 insertions(+) create mode 100644 drivers/char/tpm/tpm_tis_i2c.c
drivers/char/tpm/Kconfig +11 -0
@@ -67,6 +67,17 @@ config TCG_TIS_SPI within Linux. To compile this driver as a module, choose M here; the module will be called tpm_tis_spi. +config TCG_TIS_I2C + tristate "TPM I2C Interface Specification" + depends on I2C + select TCG_TIS_CORE + ---help--- + If you have a TPM security chip which is connected to a regular + I2C master (i.e. most embedded platforms) that is compliant with the + TCG TPM I2C Interface 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_tis_i2c. + config TCG_TIS_I2C_ATMEL tristate "TPM Interface Specification 1.2 Interface (I2C - Atmel)" depends on I2C
drivers/char/tpm/Makefile +1 -0
@@ -22,6 +22,7 @@ tpm-$(CONFIG_OF) += eventlog/of.o obj-$(CONFIG_TCG_TIS_CORE) += tpm_tis_core.o obj-$(CONFIG_TCG_TIS) += tpm_tis.o obj-$(CONFIG_TCG_TIS_SPI) += tpm_tis_spi.o +obj-$(CONFIG_TCG_TIS_I2C) += tpm_tis_i2c.o obj-$(CONFIG_TCG_TIS_I2C_ATMEL) += tpm_i2c_atmel.o obj-$(CONFIG_TCG_TIS_I2C_INFINEON) += tpm_i2c_infineon.o obj-$(CONFIG_TCG_TIS_I2C_NUVOTON) += tpm_i2c_nuvoton.o
drivers/char/tpm/tpm_tis_i2c.c new file +233 -0
233 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [RFC PATCH 2/2] tpm: Add tpm_tis_i2c backend for tpm_tis_core
Quoted 55 lines · Alexander Steffen
drivers/char/tpm/tpm_tis_i2c.c new file +55
-0+static int tpm_tis_i2c_write_bytes (struct tpm_tis_data *data,u32 addr, +u16 len,const u8 *value) +{ +struct tpm_tis_i2c_phy *phy =to_tpm_tis_i2c_phy (data); +int ret; + +u8 locality[] = { +0 ,// TPM_LOC_SEL + addr >>12 ,// locality + }; + +if (phy->iobuf) { +if (len >TPM_BUFSIZE -1 ) +return -EIO ; + + phy->iobuf[0 ] =address_to_register (addr); +memcpy (phy->iobuf +1 , value, len); + + { +struct i2c_msg msgs[] = { + { + .addr = phy->i2c_client->addr, + .len =sizeof (locality), + .buf = locality, + }, + { + .addr = phy->i2c_client->addr, + .len = len +1 , + .buf = phy->iobuf, + }, + }; + + ret =i2c_transfer (phy->i2c_client->adapter, msgs, +ARRAY_SIZE (msgs)); + } + }else { +u8 reg =address_to_register (addr); + +struct i2c_msg msgs[] = { + { + .addr = phy->i2c_client->addr, + .len =sizeof (locality), + .buf = locality, + }, + { + .addr = phy->i2c_client->addr, + .len =sizeof (reg), + .buf = ®, + }, + { + .addr = phy->i2c_client->addr, + .len = len, + .buf = (u8 *)value, + .flags =I2C_M_NOSTART , + },
drivers/char/tpm/tpm_tis_i2c.c line 165
Quoted 10 lines
drivers/char/tpm/tpm_tis_i2c.c new file +10
-0+ }; + + ret =i2c_transfer (phy->i2c_client->adapter, msgs, +ARRAY_SIZE (msgs)); + } + +if (ret <0 ) +return ret; + +usleep_range (250 ,300 );// wait default GUARD_TIME of 250µs
This does not look good. Would prefer to have named constants.
drivers/char/tpm/tpm_tis_i2c.c line 99
Quoted 3 lines
drivers/char/tpm/tpm_tis_i2c.c new file +3
-0+ +return 0 ; +}
You could probably simplify this by using branching for constructing the message arrays and then use the same code path for transfer.
/Jarkko