Skip to contentSkip to conversation

tpm: Minor improvements

RFC PATCH In review Stefan Wahren 2 participants 9 messages Sep 6, 2024 – Sep 9, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover tpm: Minor improvements

These minor improvements are the results of wiring up a Infineon SLB9670 chip on a custom board.

Stefan Wahren (2):
tpm: tpm_tis_spi: Ensure SPI mode 0
tpm: tpm_tis: Provide error messages in tpm_tis_core_init

 drivers/char/tpm/tpm_tis_core.c     | 15 ++++++++++++---
 drivers/char/tpm/tpm_tis_spi_main.c | 11 +++++++++++
 2 files changed, 23 insertions(+), 3 deletions(-)

--
2.34.1

#1 lore raw

RFC PATCH 1/2 tpm: tpm_tis_spi: Ensure SPI mode 0

According to TCG PC Client Platform TPM Profile (PTP) Specification only SPI mode 0 is supported. In order to ensure the SPI controller supports the necessary settings, call spi_setup() and bail out as soon as possible in error case.

This change should affect all supported TPM SPI devices, because tpm_tis_spi_probe is either called direct or indirectly.

---
 drivers/char/tpm/tpm_tis_spi_main.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/char/tpm/tpm_tis_spi_main.c +11 -0
index 61b42c83ced8..e62d297b040e 100644
@@ -248,6 +248,17 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
 int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
 		     int irq, const struct tpm_tis_phy_ops *phy_ops)
 {
+	int ret;
+
+	spi->mode &= ~SPI_MODE_X_MASK;
+	spi->mode |= SPI_MODE_0;
+
+	ret = spi_setup(spi);
+	if (ret < 0) {
+		dev_err(&spi->dev, "SPI setup failed: %d\n", ret);
+		return ret;
+	}
+
 	phy->iobuf = devm_kmalloc(&spi->dev, SPI_HDRSIZE + MAX_SPI_FRAMESIZE, GFP_KERNEL);
 	if (!phy->iobuf)
 		return -ENOMEM;

--
2.34.1

maintainer
#2 lore raw

Re: [PATCH RFC 1/2] tpm: tpm_tis_spi: Ensure SPI mode 0

Quoted 36 lines · Stefan Wahren

According to TCG PC Client Platform TPM Profile (PTP) Specification only SPI mode 0 is supported. In order to ensure the SPI controller supports the necessary settings, call spi_setup() and bail out as soon as possible in error case.

This change should affect all supported TPM SPI devices, because tpm_tis_spi_probe is either called direct or indirectly.

---
 drivers/char/tpm/tpm_tis_spi_main.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/char/tpm/tpm_tis_spi_main.c +11 -1
index 61b42c83ced8..e62d297b040e 100644
@@ -248,6 +248,17 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
 int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
 		     int irq, const struct tpm_tis_phy_ops *phy_ops)
 {
+	int ret;
+
+	spi->mode &= ~SPI_MODE_X_MASK;
+	spi->mode |= SPI_MODE_0;
+
+	ret = spi_setup(spi);
+	if (ret < 0) {
+		dev_err(&spi->dev, "SPI setup failed: %d\n", ret);
+		return ret;
+	}
+
 	phy->iobuf = devm_kmalloc(&spi->dev, SPI_HDRSIZE + MAX_SPI_FRAMESIZE, GFP_KERNEL);
 	if (!phy->iobuf)
 		return -ENOMEM;
--
Quoted 1 line

2.34.1

Why?

BR, Jarkko

#3 lore raw

Re: [PATCH RFC 1/2] tpm: tpm_tis_spi: Ensure SPI mode 0

Am 06.09.24 um 11:47 schrieb Jarkko Sakkinen:

Quoted 32 lines · Stefan Wahren
Quoted 31 lines

According to TCG PC Client Platform TPM Profile (PTP) Specification only SPI mode 0 is supported. In order to ensure the SPI controller supports the necessary settings, call spi_setup() and bail out as soon as possible in error case.

This change should affect all supported TPM SPI devices, because tpm_tis_spi_probe is either called direct or indirectly.

---
  drivers/char/tpm/tpm_tis_spi_main.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
drivers/char/tpm/tpm_tis_spi_main.c +10 -0
index 61b42c83ced8..e62d297b040e 100644
@@ -248,6 +248,17 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
  int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
  		     int irq, const struct tpm_tis_phy_ops *phy_ops)
  {
+	int ret;
+
+	spi->mode &= ~SPI_MODE_X_MASK;
+	spi->mode |= SPI_MODE_0;
+
+	ret = spi_setup(spi);
+	if (ret < 0) {
+		dev_err(&spi->dev, "SPI setup failed: %d\n", ret);
+		return ret;
+	}
Quoted 8 lines
Quoted 6 lines
drivers/char/tpm/tpm_tis_spi_main.c +1 -1
+
  	phy->iobuf = devm_kmalloc(&spi->dev, SPI_HDRSIZE + MAX_SPI_FRAMESIZE, GFP_KERNEL);
  	if (!phy->iobuf)
  		return -ENOMEM;
--

2.34.1

Why?

SPI protocol driver usually call spi_setup to verify that the SPI controller accept the settings (SPI mode, bit, clock rate ...). Bailing out early is more helpful for debugging issues.

Quoted 2 lines

BR, Jarkko

maintainer
#4 lore raw

Re: [PATCH RFC 1/2] tpm: tpm_tis_spi: Ensure SPI mode 0

Quoted 41 lines · Stefan Wahren

Am 06.09.24 um 11:47 schrieb Jarkko Sakkinen:

Quoted 32 lines · Stefan Wahren
Quoted 31 lines

According to TCG PC Client Platform TPM Profile (PTP) Specification only SPI mode 0 is supported. In order to ensure the SPI controller supports the necessary settings, call spi_setup() and bail out as soon as possible in error case.

This change should affect all supported TPM SPI devices, because tpm_tis_spi_probe is either called direct or indirectly.

---
  drivers/char/tpm/tpm_tis_spi_main.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
drivers/char/tpm/tpm_tis_spi_main.c +10 -0
index 61b42c83ced8..e62d297b040e 100644
@@ -248,6 +248,17 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
  int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
  		     int irq, const struct tpm_tis_phy_ops *phy_ops)
  {
+	int ret;
+
+	spi->mode &= ~SPI_MODE_X_MASK;
+	spi->mode |= SPI_MODE_0;
+
+	ret = spi_setup(spi);
+	if (ret < 0) {
+		dev_err(&spi->dev, "SPI setup failed: %d\n", ret);
+		return ret;
+	}
Quoted 8 lines
Quoted 6 lines
drivers/char/tpm/tpm_tis_spi_main.c +1 -1
+
  	phy->iobuf = devm_kmalloc(&spi->dev, SPI_HDRSIZE + MAX_SPI_FRAMESIZE, GFP_KERNEL);
  	if (!phy->iobuf)
  		return -ENOMEM;
--

2.34.1

Why?

Quoted 3 lines

SPI protocol driver usually call spi_setup to verify that the SPI controller accept the settings (SPI mode, bit, clock rate ...). Bailing out early is more helpful for debugging issues.

What problem has this patch solved for you? I think it makes the code only less robust and more error prone.

BR, Jarkko

#5 lore raw

Re: [PATCH RFC 1/2] tpm: tpm_tis_spi: Ensure SPI mode 0

Hi Jarkko,

Am 09.09.24 um 19:05 schrieb Jarkko Sakkinen:

Quoted 41 lines · Stefan Wahren
Quoted 40 lines

Am 06.09.24 um 11:47 schrieb Jarkko Sakkinen:

Quoted 39 lines · Stefan Wahren
Quoted 36 lines

According to TCG PC Client Platform TPM Profile (PTP) Specification only SPI mode 0 is supported. In order to ensure the SPI controller supports the necessary settings, call spi_setup() and bail out as soon as possible in error case.

This change should affect all supported TPM SPI devices, because tpm_tis_spi_probe is either called direct or indirectly.

---
   drivers/char/tpm/tpm_tis_spi_main.c | 11 +++++++++++
   1 file changed, 11 insertions(+)
drivers/char/tpm/tpm_tis_spi_main.c +11 -1
index 61b42c83ced8..e62d297b040e 100644
@@ -248,6 +248,17 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
   int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
   		     int irq, const struct tpm_tis_phy_ops *phy_ops)
   {
+	int ret;
+
+	spi->mode &= ~SPI_MODE_X_MASK;
+	spi->mode |= SPI_MODE_0;
+
+	ret = spi_setup(spi);
+	if (ret < 0) {
+		dev_err(&spi->dev, "SPI setup failed: %d\n", ret);
+		return ret;
+	}
+
   	phy->iobuf = devm_kmalloc(&spi->dev, SPI_HDRSIZE + MAX_SPI_FRAMESIZE, GFP_KERNEL);
   	if (!phy->iobuf)
   		return -ENOMEM;
--
Quoted 1 line

2.34.1

Why?

Quoted 5 lines
Quoted 3 lines

SPI protocol driver usually call spi_setup to verify that the SPI controller accept the settings (SPI mode, bit, clock rate ...). Bailing out early is more helpful for debugging issues.

What problem has this patch solved for you? I think it makes the code only less robust and more error prone.

this is not a fix for an issue i was experiencing.

It is just an improvement to catch possible invalid settings which has been passed via DT device for example or the SPI controller doesn't support SPI mode 0.

Quoted 2 lines

BR, Jarkko

#6 lore raw

RFC PATCH 2/2 tpm: tpm_tis: Provide error messages in tpm_tis_core_init

In reply to Stefan Wahren

In case of SPI wiring issues (e.g. MISO pin permanent high) the core init won't provide any error message on failure. Add some helpful error messages to tpm_tis_core_init(), which helps to better understand why the driver won't probe.

---
 drivers/char/tpm/tpm_tis_core.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/char/tpm/tpm_tis_core.c +12 -3
index fdef214b9f6b..830e331fcebe 100644
@@ -1133,8 +1133,10 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 	dev_set_drvdata(&chip->dev, priv);
 
 	rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
-	if (rc < 0)
+	if (rc < 0) {
+		dev_err(dev, "Failed to read TPM_DID_VID register: %d\n", rc);
 		return rc;
+	}
 
 	priv->manufacturer_id = vendor;
 
@@ -1162,19 +1164,25 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 		chip->ops->clk_enable(chip, true);
 
 	if (wait_startup(chip, 0) != 0) {
+		dev_err(dev, "TPM device not accessible (0x%0x)\n",
+			vendor);
 		rc = -ENODEV;
 		goto out_err;
 	}
 
 	/* Take control of the TPM's interrupt hardware and shut it off */
 	rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
-	if (rc < 0)
+	if (rc < 0) {
+		dev_err(dev, "Failed to read TPM_INT_ENABLE register: %d\n", rc);
 		goto out_err;
+	}
 
 	/* Figure out the capabilities */
 	rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
-	if (rc < 0)
+	if (rc < 0) {
+		dev_err(dev, "Failed to read TPM_INTF_CAPS register: %d\n", rc);
 		goto out_err;
+	}
 
 	dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
 		intfcaps);
@@ -1209,6 +1217,7 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 
 	rc = tpm_tis_request_locality(chip, 0);
 	if (rc < 0) {
+		dev_err(dev, "Failed to request locality (0x%0x)\n", vendor);
 		rc = -ENODEV;
 		goto out_err;
 	}

--
2.34.1

maintainer
#7 lore raw

Re: [PATCH RFC 2/2] tpm: tpm_tis: Provide error messages in tpm_tis_core_init

Quoted 63 lines · Stefan Wahren

In case of SPI wiring issues (e.g. MISO pin permanent high) the core init won't provide any error message on failure. Add some helpful error messages to tpm_tis_core_init(), which helps to better understand why the driver won't probe.

---
 drivers/char/tpm/tpm_tis_core.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/char/tpm/tpm_tis_core.c +12 -4
index fdef214b9f6b..830e331fcebe 100644
@@ -1133,8 +1133,10 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 	dev_set_drvdata(&chip->dev, priv);
 
 	rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
-	if (rc < 0)
+	if (rc < 0) {
+		dev_err(dev, "Failed to read TPM_DID_VID register: %d\n", rc);
 		return rc;
+	}
 
 	priv->manufacturer_id = vendor;
 
@@ -1162,19 +1164,25 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 		chip->ops->clk_enable(chip, true);
 
 	if (wait_startup(chip, 0) != 0) {
+		dev_err(dev, "TPM device not accessible (0x%0x)\n",
+			vendor);
 		rc = -ENODEV;
 		goto out_err;
 	}
 
 	/* Take control of the TPM's interrupt hardware and shut it off */
 	rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
-	if (rc < 0)
+	if (rc < 0) {
+		dev_err(dev, "Failed to read TPM_INT_ENABLE register: %d\n", rc);
 		goto out_err;
+	}
 
 	/* Figure out the capabilities */
 	rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
-	if (rc < 0)
+	if (rc < 0) {
+		dev_err(dev, "Failed to read TPM_INTF_CAPS register: %d\n", rc);
 		goto out_err;
+	}
 
 	dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
 		intfcaps);
@@ -1209,6 +1217,7 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 
 	rc = tpm_tis_request_locality(chip, 0);
 	if (rc < 0) {
+		dev_err(dev, "Failed to request locality (0x%0x)\n", vendor);
 		rc = -ENODEV;
 		goto out_err;
 	}
--
Quoted 1 line

2.34.1

No thanks. It not only adds helpful messages but has potential to add unwanted merge conflicts.

BR, Jarkko

#8 lore raw

Re: [PATCH RFC 2/2] tpm: tpm_tis: Provide error messages in tpm_tis_core_init

Am 06.09.24 um 11:50 schrieb Jarkko Sakkinen:

Quoted 57 lines · Stefan Wahren
Quoted 56 lines

In case of SPI wiring issues (e.g. MISO pin permanent high) the core init won't provide any error message on failure. Add some helpful error messages to tpm_tis_core_init(), which helps to better understand why the driver won't probe.

---
  drivers/char/tpm/tpm_tis_core.c | 15 ++++++++++++---
  1 file changed, 12 insertions(+), 3 deletions(-)
drivers/char/tpm/tpm_tis_core.c +11 -3
index fdef214b9f6b..830e331fcebe 100644
@@ -1133,8 +1133,10 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
  	dev_set_drvdata(&chip->dev, priv);
 
  	rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
-	if (rc < 0)
+	if (rc < 0) {
+		dev_err(dev, "Failed to read TPM_DID_VID register: %d\n", rc);
  		return rc;
+	}
 
  	priv->manufacturer_id = vendor;
 
@@ -1162,19 +1164,25 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
  		chip->ops->clk_enable(chip, true);
 
  	if (wait_startup(chip, 0) != 0) {
+		dev_err(dev, "TPM device not accessible (0x%0x)\n",
+			vendor);
  		rc = -ENODEV;
  		goto out_err;
  	}
 
  	/* Take control of the TPM's interrupt hardware and shut it off */
  	rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
-	if (rc < 0)
+	if (rc < 0) {
+		dev_err(dev, "Failed to read TPM_INT_ENABLE register: %d\n", rc);
  		goto out_err;
+	}
 
  	/* Figure out the capabilities */
  	rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
-	if (rc < 0)
+	if (rc < 0) {
+		dev_err(dev, "Failed to read TPM_INTF_CAPS register: %d\n", rc);
  		goto out_err;
+	}
 
  	dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
  		intfcaps);
@@ -1209,6 +1217,7 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
 
Quoted 10 lines
Quoted 8 lines
drivers/char/tpm/tpm_tis_core.c +1 -1
  	rc = tpm_tis_request_locality(chip, 0);
  	if (rc < 0) {
+		dev_err(dev, "Failed to request locality (0x%0x)\n", vendor);
  		rc = -ENODEV;
  		goto out_err;
  	}
--

2.34.1

No thanks. It not only adds helpful messages but has potential to add unwanted merge conflicts.

So this is a general NAK?

So with every setup where the driver exits without an error message, the developer should add its own debug?

Quoted 2 lines

BR, Jarkko