All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] tpm: Apply an adapterlimit for retransmission.
@ 2017-02-21 14:44 Enric Balletbo i Serra
  2017-02-21 14:45 ` [PATCH 2/2] tpm: Do not assume an i2c adapter preserves the msg len Enric Balletbo i Serra
  2017-02-21 16:29 ` [tpmdd-devel] [PATCH 1/2] tpm: Apply an adapterlimit for retransmission Andrew Lunn
  0 siblings, 2 replies; 16+ messages in thread
From: Enric Balletbo i Serra @ 2017-02-21 14:44 UTC (permalink / raw)
  To: Peter Huewe, Marcel Selhorst
  Cc: apronin, tpmdd-devel, linux-kernel, Bryan Freed

From: Bryan Freed <bfreed@chromium.org>

When the I2C Infineon part is attached to an I2C adapter that imposes
a size limitation, large requests will fail -EINVAL.
Retry them with size backoff without re-issuing the 0x05 command
as this appears to occasionally put the TPM in a bad state.

Signed-off-by: Bryan Freed <bfreed@chromium.org>
Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>
---
 drivers/char/tpm/tpm_i2c_infineon.c | 44 ++++++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 18 deletions(-)

diff --git a/drivers/char/tpm/tpm_i2c_infineon.c b/drivers/char/tpm/tpm_i2c_infineon.c
index 62ee44e..f04c6b7 100644
--- a/drivers/char/tpm/tpm_i2c_infineon.c
+++ b/drivers/char/tpm/tpm_i2c_infineon.c
@@ -111,35 +111,24 @@ static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
 
 	int rc = 0;
 	int count;
+	int adapterlimit = len;
 
 	/* Lock the adapter for the duration of the whole sequence. */
 	if (!tpm_dev.client->adapter->algo->master_xfer)
 		return -EOPNOTSUPP;
 	i2c_lock_adapter(tpm_dev.client->adapter);
 
-	if (tpm_dev.chip_type == SLB9645) {
-		/* use a combined read for newer chips
-		 * unfortunately the smbus functions are not suitable due to
-		 * the 32 byte limit of the smbus.
-		 * retries should usually not be needed, but are kept just to
-		 * be on the safe side.
-		 */
-		for (count = 0; count < MAX_COUNT; count++) {
-			rc = __i2c_transfer(tpm_dev.client->adapter, msgs, 2);
-			if (rc > 0)
-				break;	/* break here to skip sleep */
-			usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
-		}
-	} else {
+	/* Expect to send one command message and one data message, but
+	 * support looping over each or both if necessary.
+	 */
+	while (len > 0) {
 		/* slb9635 protocol should work in all cases */
 		for (count = 0; count < MAX_COUNT; count++) {
 			rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1);
 			if (rc > 0)
-				break;	/* break here to skip sleep */
-
+				break;
 			usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
 		}
-
 		if (rc <= 0)
 			goto out;
 
@@ -149,10 +138,29 @@ static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
 		 */
 		for (count = 0; count < MAX_COUNT; count++) {
 			usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
+			msg2.len = min(adapterlimit, len);
 			rc = __i2c_transfer(tpm_dev.client->adapter, &msg2, 1);
-			if (rc > 0)
+			if (rc > 0) {
+				/* Since len is unsigned, make doubly sure we
+				 * do not underflow it.
+				 */
+				if (msg2.len > len)
+					len = 0;
+				else
+					len -= msg2.len;
+				msg2.buf += msg2.len;
 				break;
+			}
+			/* If the I2C adapter rejected the request,
+			 * try a smaller chunk.
+			 */
+			if (rc == -EINVAL) {
+				adapterlimit = (adapterlimit + 1) / 2;
+				adapterlimit = max(adapterlimit, 32);
+			}
 		}
+		if (rc <= 0)
+			goto out;
 	}
 
 out:
-- 
2.9.3

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2017-03-02 14:19 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-21 14:44 [PATCH 1/2] tpm: Apply an adapterlimit for retransmission Enric Balletbo i Serra
2017-02-21 14:45 ` [PATCH 2/2] tpm: Do not assume an i2c adapter preserves the msg len Enric Balletbo i Serra
2017-02-21 16:29 ` [tpmdd-devel] [PATCH 1/2] tpm: Apply an adapterlimit for retransmission Andrew Lunn
2017-02-22 11:16   ` Enric Balletbo i Serra
2017-02-22 14:01     ` Andrew Lunn
2017-02-27 18:48       ` Enric Balletbo Serra
2017-02-27 19:12         ` Wolfram Sang
2017-02-27 19:12           ` Wolfram Sang
2017-02-27 21:30           ` [tpmdd-devel] " Enric Balletbo Serra
2017-02-27 21:30             ` Enric Balletbo Serra
2017-03-02 13:15           ` [tpmdd-devel] " Peter Huewe
2017-03-02 13:15             ` Peter Huewe
2017-03-02 14:05             ` [tpmdd-devel] " Wolfram Sang
2017-03-02 14:10             ` Wolfram Sang
2017-02-22 12:39   ` Peter Huewe
2017-02-22 12:39     ` Peter Huewe

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.