2023-05-24 15:55:47

by Sverdlin, Alexander

[permalink] [raw]
Subject: [PATCH 1/2] tpm: tis_i2c: Limit read bursts to I2C_SMBUS_BLOCK_MAX (32) bytes

From: Alexander Sverdlin <[email protected]>

Underlying I2C bus drivers not always support longer transfers and
imx-lpi2c for instance doesn't. SLB 9673 offers 427-bytes packets.

Visible symptoms are:

tpm tpm0: Error left over data
tpm tpm0: tpm_transmit: tpm_recv: error -5
tpm_tis_i2c: probe of 1-002e failed with error -5

Fixes: bbc23a07b072 ("tpm: Add tpm_tis_i2c backend for tpm_tis_core")
Tested-by: Michael Haener <[email protected]>
Signed-off-by: Alexander Sverdlin <[email protected]>
---
drivers/char/tpm/tpm_tis_i2c.c | 37 ++++++++++++++++++++--------------
1 file changed, 22 insertions(+), 15 deletions(-)

diff --git a/drivers/char/tpm/tpm_tis_i2c.c b/drivers/char/tpm/tpm_tis_i2c.c
index c8c34adc14c0..106fd20d94e4 100644
--- a/drivers/char/tpm/tpm_tis_i2c.c
+++ b/drivers/char/tpm/tpm_tis_i2c.c
@@ -189,21 +189,28 @@ static int tpm_tis_i2c_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
int ret;

for (i = 0; i < TPM_RETRY; i++) {
- /* write register */
- msg.len = sizeof(reg);
- msg.buf = &reg;
- msg.flags = 0;
- ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
- if (ret < 0)
- return ret;
-
- /* read data */
- msg.buf = result;
- msg.len = len;
- msg.flags = I2C_M_RD;
- ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
- if (ret < 0)
- return ret;
+ u16 read = 0;
+
+ while (read < len) {
+ /* write register */
+ msg.len = sizeof(reg);
+ msg.buf = &reg;
+ msg.flags = 0;
+ ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
+ if (ret < 0)
+ return ret;
+
+ /* read data */
+ msg.buf = result + read;
+ msg.len = len - read;
+ msg.flags = I2C_M_RD;
+ if (msg.len > I2C_SMBUS_BLOCK_MAX)
+ msg.len = I2C_SMBUS_BLOCK_MAX;
+ ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
+ if (ret < 0)
+ return ret;
+ read += msg.len;
+ }

ret = tpm_tis_i2c_sanity_check_read(reg, len, result);
if (ret == 0)
--
2.40.1



2023-05-24 16:20:07

by Jarkko Sakkinen

[permalink] [raw]
Subject: Re: [PATCH 1/2] tpm: tis_i2c: Limit read bursts to I2C_SMBUS_BLOCK_MAX (32) bytes

On Wed, 2023-05-24 at 17:40 +0200, A. Sverdlin wrote:
> From: Alexander Sverdlin <[email protected]>
>
> Underlying I2C bus drivers not always support longer transfers and
> imx-lpi2c for instance doesn't. SLB 9673 offers 427-bytes packets.
>
> Visible symptoms are:
>
> tpm tpm0: Error left over data
> tpm tpm0: tpm_transmit: tpm_recv: error -5
> tpm_tis_i2c: probe of 1-002e failed with error -5
>
> Fixes: bbc23a07b072 ("tpm: Add tpm_tis_i2c backend for tpm_tis_core")
> Tested-by: Michael Haener <[email protected]>
> Signed-off-by: Alexander Sverdlin <[email protected]>

Reviewed-by: Jarkko Sakkinen <[email protected]>

BR, Jarkko

2023-05-24 16:28:00

by Sverdlin, Alexander

[permalink] [raw]
Subject: [PATCH 2/2] tpm: tis_i2c: Limit write bursts to I2C_SMBUS_BLOCK_MAX (32) bytes

From: Alexander Sverdlin <[email protected]>

Underlying I2C bus drivers not always support longer transfers and
imx-lpi2c for instance doesn't. The fix is symmetric to previous patch
which fixed the read direction.

Fixes: bbc23a07b072 ("tpm: Add tpm_tis_i2c backend for tpm_tis_core")
Tested-by: Michael Haener <[email protected]>
Signed-off-by: Alexander Sverdlin <[email protected]>
---
drivers/char/tpm/tpm_tis_i2c.c | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/drivers/char/tpm/tpm_tis_i2c.c b/drivers/char/tpm/tpm_tis_i2c.c
index 106fd20d94e4..82fda488e98b 100644
--- a/drivers/char/tpm/tpm_tis_i2c.c
+++ b/drivers/char/tpm/tpm_tis_i2c.c
@@ -230,19 +230,27 @@ static int tpm_tis_i2c_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
struct i2c_msg msg = { .addr = phy->i2c_client->addr };
u8 reg = tpm_tis_i2c_address_to_register(addr);
int ret;
+ u16 wrote = 0;

if (len > TPM_BUFSIZE - 1)
return -EIO;

- /* write register and data in one go */
phy->io_buf[0] = reg;
- memcpy(phy->io_buf + sizeof(reg), value, len);
-
- msg.len = sizeof(reg) + len;
msg.buf = phy->io_buf;
- ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
- if (ret < 0)
- return ret;
+ while (wrote < len) {
+ /* write register and data in one go */
+ msg.len = sizeof(reg) + len - wrote;
+ if (msg.len > I2C_SMBUS_BLOCK_MAX)
+ msg.len = I2C_SMBUS_BLOCK_MAX;
+
+ memcpy(phy->io_buf + sizeof(reg), value + wrote,
+ msg.len - sizeof(reg));
+
+ ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
+ if (ret < 0)
+ return ret;
+ wrote += msg.len - sizeof(reg);
+ }

return 0;
}
--
2.40.1


2023-05-24 18:50:14

by Jerry Snitselaar

[permalink] [raw]
Subject: Re: [PATCH 1/2] tpm: tis_i2c: Limit read bursts to I2C_SMBUS_BLOCK_MAX (32) bytes

On Wed, May 24, 2023 at 05:40:39PM +0200, A. Sverdlin wrote:
> From: Alexander Sverdlin <[email protected]>
>
> Underlying I2C bus drivers not always support longer transfers and
> imx-lpi2c for instance doesn't. SLB 9673 offers 427-bytes packets.
>
> Visible symptoms are:
>
> tpm tpm0: Error left over data
> tpm tpm0: tpm_transmit: tpm_recv: error -5
> tpm_tis_i2c: probe of 1-002e failed with error -5
>
> Fixes: bbc23a07b072 ("tpm: Add tpm_tis_i2c backend for tpm_tis_core")
> Tested-by: Michael Haener <[email protected]>
> Signed-off-by: Alexander Sverdlin <[email protected]>

Reviewed-by: Jerry Snitselaar <[email protected]>

> ---
> drivers/char/tpm/tpm_tis_i2c.c | 37 ++++++++++++++++++++--------------
> 1 file changed, 22 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/char/tpm/tpm_tis_i2c.c b/drivers/char/tpm/tpm_tis_i2c.c
> index c8c34adc14c0..106fd20d94e4 100644
> --- a/drivers/char/tpm/tpm_tis_i2c.c
> +++ b/drivers/char/tpm/tpm_tis_i2c.c
> @@ -189,21 +189,28 @@ static int tpm_tis_i2c_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
> int ret;
>
> for (i = 0; i < TPM_RETRY; i++) {
> - /* write register */
> - msg.len = sizeof(reg);
> - msg.buf = &reg;
> - msg.flags = 0;
> - ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
> - if (ret < 0)
> - return ret;
> -
> - /* read data */
> - msg.buf = result;
> - msg.len = len;
> - msg.flags = I2C_M_RD;
> - ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
> - if (ret < 0)
> - return ret;
> + u16 read = 0;
> +
> + while (read < len) {
> + /* write register */
> + msg.len = sizeof(reg);
> + msg.buf = &reg;
> + msg.flags = 0;
> + ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
> + if (ret < 0)
> + return ret;
> +
> + /* read data */
> + msg.buf = result + read;
> + msg.len = len - read;
> + msg.flags = I2C_M_RD;
> + if (msg.len > I2C_SMBUS_BLOCK_MAX)
> + msg.len = I2C_SMBUS_BLOCK_MAX;
> + ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
> + if (ret < 0)
> + return ret;
> + read += msg.len;
> + }
>
> ret = tpm_tis_i2c_sanity_check_read(reg, len, result);
> if (ret == 0)
> --
> 2.40.1
>


2023-05-24 22:20:42

by Jerry Snitselaar

[permalink] [raw]
Subject: Re: [PATCH 2/2] tpm: tis_i2c: Limit write bursts to I2C_SMBUS_BLOCK_MAX (32) bytes

On Wed, May 24, 2023 at 05:40:40PM +0200, A. Sverdlin wrote:
> From: Alexander Sverdlin <[email protected]>
>
> Underlying I2C bus drivers not always support longer transfers and
> imx-lpi2c for instance doesn't. The fix is symmetric to previous patch
> which fixed the read direction.
>
> Fixes: bbc23a07b072 ("tpm: Add tpm_tis_i2c backend for tpm_tis_core")
> Tested-by: Michael Haener <[email protected]>
> Signed-off-by: Alexander Sverdlin <[email protected]>

Reviewed-by: Jerry Snitselaar <[email protected]>

> ---
> drivers/char/tpm/tpm_tis_i2c.c | 22 +++++++++++++++-------
> 1 file changed, 15 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/char/tpm/tpm_tis_i2c.c b/drivers/char/tpm/tpm_tis_i2c.c
> index 106fd20d94e4..82fda488e98b 100644
> --- a/drivers/char/tpm/tpm_tis_i2c.c
> +++ b/drivers/char/tpm/tpm_tis_i2c.c
> @@ -230,19 +230,27 @@ static int tpm_tis_i2c_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
> struct i2c_msg msg = { .addr = phy->i2c_client->addr };
> u8 reg = tpm_tis_i2c_address_to_register(addr);
> int ret;
> + u16 wrote = 0;
>
> if (len > TPM_BUFSIZE - 1)
> return -EIO;
>
> - /* write register and data in one go */
> phy->io_buf[0] = reg;
> - memcpy(phy->io_buf + sizeof(reg), value, len);
> -
> - msg.len = sizeof(reg) + len;
> msg.buf = phy->io_buf;
> - ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
> - if (ret < 0)
> - return ret;
> + while (wrote < len) {
> + /* write register and data in one go */
> + msg.len = sizeof(reg) + len - wrote;
> + if (msg.len > I2C_SMBUS_BLOCK_MAX)
> + msg.len = I2C_SMBUS_BLOCK_MAX;
> +
> + memcpy(phy->io_buf + sizeof(reg), value + wrote,
> + msg.len - sizeof(reg));
> +
> + ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
> + if (ret < 0)
> + return ret;
> + wrote += msg.len - sizeof(reg);
> + }
>
> return 0;
> }
> --
> 2.40.1
>


2023-06-06 22:03:15

by Jarkko Sakkinen

[permalink] [raw]
Subject: Re: [PATCH 2/2] tpm: tis_i2c: Limit write bursts to I2C_SMBUS_BLOCK_MAX (32) bytes

On Wed May 24, 2023 at 6:40 PM EEST, A. Sverdlin wrote:
> From: Alexander Sverdlin <[email protected]>
>
> Underlying I2C bus drivers not always support longer transfers and
> imx-lpi2c for instance doesn't. The fix is symmetric to previous patch
> which fixed the read direction.
>

Cc: [email protected] # v5.20+

> Fixes: bbc23a07b072 ("tpm: Add tpm_tis_i2c backend for tpm_tis_core")
> Tested-by: Michael Haener <[email protected]>
> Signed-off-by: Alexander Sverdlin <[email protected]>
> ---
> drivers/char/tpm/tpm_tis_i2c.c | 22 +++++++++++++++-------
> 1 file changed, 15 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/char/tpm/tpm_tis_i2c.c b/drivers/char/tpm/tpm_tis_i2c.c
> index 106fd20d94e4..82fda488e98b 100644
> --- a/drivers/char/tpm/tpm_tis_i2c.c
> +++ b/drivers/char/tpm/tpm_tis_i2c.c
> @@ -230,19 +230,27 @@ static int tpm_tis_i2c_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
> struct i2c_msg msg = { .addr = phy->i2c_client->addr };
> u8 reg = tpm_tis_i2c_address_to_register(addr);
> int ret;
> + u16 wrote = 0;
>
> if (len > TPM_BUFSIZE - 1)
> return -EIO;
>
> - /* write register and data in one go */
> phy->io_buf[0] = reg;
> - memcpy(phy->io_buf + sizeof(reg), value, len);
> -
> - msg.len = sizeof(reg) + len;
> msg.buf = phy->io_buf;
> - ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
> - if (ret < 0)
> - return ret;
> + while (wrote < len) {
> + /* write register and data in one go */
> + msg.len = sizeof(reg) + len - wrote;
> + if (msg.len > I2C_SMBUS_BLOCK_MAX)
> + msg.len = I2C_SMBUS_BLOCK_MAX;
> +
> + memcpy(phy->io_buf + sizeof(reg), value + wrote,
> + msg.len - sizeof(reg));
> +
> + ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
> + if (ret < 0)
> + return ret;
> + wrote += msg.len - sizeof(reg);
> + }
>
> return 0;
> }
> --
> 2.40.1

Reviewed-by: Jarkko Sakkinen <[email protected]>

BR, Jarkko

2023-06-06 22:15:05

by Jarkko Sakkinen

[permalink] [raw]
Subject: Re: [PATCH 1/2] tpm: tis_i2c: Limit read bursts to I2C_SMBUS_BLOCK_MAX (32) bytes

On Wed May 24, 2023 at 6:40 PM EEST, A. Sverdlin wrote:
> From: Alexander Sverdlin <[email protected]>
>
> Underlying I2C bus drivers not always support longer transfers and
> imx-lpi2c for instance doesn't. SLB 9673 offers 427-bytes packets.
>
> Visible symptoms are:
>
> tpm tpm0: Error left over data
> tpm tpm0: tpm_transmit: tpm_recv: error -5
> tpm_tis_i2c: probe of 1-002e failed with error -5
>

Cc: [email protected] # v5.20+

> Fixes: bbc23a07b072 ("tpm: Add tpm_tis_i2c backend for tpm_tis_core")
> Tested-by: Michael Haener <[email protected]>
> Signed-off-by: Alexander Sverdlin <[email protected]>
> ---
> drivers/char/tpm/tpm_tis_i2c.c | 37 ++++++++++++++++++++--------------
> 1 file changed, 22 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/char/tpm/tpm_tis_i2c.c b/drivers/char/tpm/tpm_tis_i2c.c
> index c8c34adc14c0..106fd20d94e4 100644
> --- a/drivers/char/tpm/tpm_tis_i2c.c
> +++ b/drivers/char/tpm/tpm_tis_i2c.c
> @@ -189,21 +189,28 @@ static int tpm_tis_i2c_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
> int ret;
>
> for (i = 0; i < TPM_RETRY; i++) {
> - /* write register */
> - msg.len = sizeof(reg);
> - msg.buf = &reg;
> - msg.flags = 0;
> - ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
> - if (ret < 0)
> - return ret;
> -
> - /* read data */
> - msg.buf = result;
> - msg.len = len;
> - msg.flags = I2C_M_RD;
> - ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
> - if (ret < 0)
> - return ret;
> + u16 read = 0;
> +
> + while (read < len) {
> + /* write register */
> + msg.len = sizeof(reg);
> + msg.buf = &reg;
> + msg.flags = 0;
> + ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
> + if (ret < 0)
> + return ret;
> +
> + /* read data */
> + msg.buf = result + read;
> + msg.len = len - read;
> + msg.flags = I2C_M_RD;
> + if (msg.len > I2C_SMBUS_BLOCK_MAX)
> + msg.len = I2C_SMBUS_BLOCK_MAX;
> + ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
> + if (ret < 0)
> + return ret;
> + read += msg.len;
> + }
>
> ret = tpm_tis_i2c_sanity_check_read(reg, len, result);
> if (ret == 0)
> --
> 2.40.1

Reviewed-by: Jarkko Sakkinen <[email protected]>

BR, Jarkko