Skip to contentSkip to conversation

platform/chrome: cros_ec: jump to RW before probing

PATCH Superseded Dawid Niedzwiecki 2 participants 4 messages Dec 4, 2024 – Dec 6, 2024

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 platform/chrome: cros_ec: jump to RW before probing

There are EC devices, like FPMCU, that use RWSIG as a method of authenticating RW section. After the authentication succeeds, EC device waits some time before jumping to RW. EC can be probed before the jump, which means there is a time window after jump to RW in which EC won't respond, because it is not initialized. It can cause a communication errors after probing.

To avoid such problems, send the RWSIG continue command first, which skips waiting for the jump to RW. Send the command more times, to make sure EC is ready in RW before the start of the actual probing process. If a EC device doesn't support the RWSIG, it will respond with invalid command error code and probing will continue as usual.

---
V1 -> V2:
- Use dev_info instead of dev_warn when cros_ec_rwsig_continue returns an error.
- Return immediately from cros_ec_rwsig_continue, if the RWSIG_ACTION command fails itself, not SPI transaction.

 drivers/platform/chrome/cros_ec.c           |  5 ++
 drivers/platform/chrome/cros_ec_i2c.c       |  3 +-
 drivers/platform/chrome/cros_ec_ishtp.c     |  2 +-
 drivers/platform/chrome/cros_ec_lpc.c       |  2 +-
 drivers/platform/chrome/cros_ec_proto.c     | 62 +++++++++++++++++++++
 drivers/platform/chrome/cros_ec_rpmsg.c     |  2 +-
 drivers/platform/chrome/cros_ec_spi.c       |  2 +-
 drivers/platform/chrome/cros_ec_uart.c      |  2 +-
 include/linux/platform_data/cros_ec_proto.h |  2 +
 9 files changed, 76 insertions(+), 6 deletions(-)
drivers/platform/chrome/cros_ec.c +5 -0
index e821b3d39590..110771a8645e 100644
@@ -204,6 +204,11 @@ int cros_ec_register(struct cros_ec_device *ec_dev)
 	mutex_init(&ec_dev->lock);
 	lockdep_set_class(&ec_dev->lock, &ec_dev->lockdep_key);
 
+	/* Send RWSIG continue to jump to RW for devices using RWSIG. */
+	err = cros_ec_rwsig_continue(ec_dev);
+	if (err)
+		dev_info(dev, "Failed to continue RWSIG: %d\n", err);
+
 	err = cros_ec_query_all(ec_dev);
 	if (err) {
 		dev_err(dev, "Cannot identify the EC: error %d\n", err);
drivers/platform/chrome/cros_ec_i2c.c +2 -1
index 62662ba5bf6e..38af97cdaab2 100644
@@ -305,7 +305,8 @@ static int cros_ec_i2c_probe(struct i2c_client *client)
 	ec_dev->phys_name = client->adapter->name;
 	ec_dev->din_size = sizeof(struct ec_host_response_i2c) +
 			   sizeof(struct ec_response_get_protocol_info);
-	ec_dev->dout_size = sizeof(struct ec_host_request_i2c);
+	ec_dev->dout_size = sizeof(struct ec_host_request_i2c) +
+			    sizeof(struct ec_params_rwsig_action);
 
 	err = cros_ec_register(ec_dev);
 	if (err) {
drivers/platform/chrome/cros_ec_ishtp.c +1 -1
index 5ac37bd024c8..7e7190b30cbb 100644
@@ -557,7 +557,7 @@ static int cros_ec_dev_init(struct ishtp_cl_data *client_data)
 	ec_dev->phys_name = dev_name(dev);
 	ec_dev->din_size = sizeof(struct cros_ish_in_msg) +
 			   sizeof(struct ec_response_get_protocol_info);
-	ec_dev->dout_size = sizeof(struct cros_ish_out_msg);
+	ec_dev->dout_size = sizeof(struct cros_ish_out_msg) + sizeof(struct ec_params_rwsig_action);
 
 	return cros_ec_register(ec_dev);
 }
drivers/platform/chrome/cros_ec_lpc.c +1 -1
index 924bf4d3cc77..1e1d68359958 100644
@@ -573,7 +573,7 @@ static int cros_ec_lpc_probe(struct platform_device *pdev)
 	ec_dev->cmd_readmem = cros_ec_lpc_readmem;
 	ec_dev->din_size = sizeof(struct ec_host_response) +
 			   sizeof(struct ec_response_get_protocol_info);
-	ec_dev->dout_size = sizeof(struct ec_host_request);
+	ec_dev->dout_size = sizeof(struct ec_host_request) + sizeof(struct ec_params_rwsig_action);
 	ec_dev->priv = ec_lpc;
 
 	/*
drivers/platform/chrome/cros_ec_proto.c +62 -0

74 lines not on this page · raw message

drivers/platform/chrome/cros_ec_rpmsg.c +1 -1
index 39d3b50a7c09..bc2666491db1 100644
@@ -231,7 +231,7 @@ static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)
 	ec_dev->phys_name = dev_name(&rpdev->dev);
 	ec_dev->din_size = sizeof(struct ec_host_response) +
 			   sizeof(struct ec_response_get_protocol_info);
-	ec_dev->dout_size = sizeof(struct ec_host_request);
+	ec_dev->dout_size = sizeof(struct ec_host_request) + sizeof(struct ec_params_rwsig_action);
 	dev_set_drvdata(dev, ec_dev);
 
 	ec_rpmsg->rpdev = rpdev;
drivers/platform/chrome/cros_ec_spi.c +1 -1
index 86a3d32a7763..946373238502 100644
@@ -766,7 +766,7 @@ static int cros_ec_spi_probe(struct spi_device *spi)
 	ec_dev->din_size = EC_MSG_PREAMBLE_COUNT +
 			   sizeof(struct ec_host_response) +
 			   sizeof(struct ec_response_get_protocol_info);
-	ec_dev->dout_size = sizeof(struct ec_host_request);
+	ec_dev->dout_size = sizeof(struct ec_host_request) + sizeof(struct ec_params_rwsig_action);
 
 	ec_spi->last_transfer_ns = ktime_get_ns();
 
drivers/platform/chrome/cros_ec_uart.c +1 -1
index 62bc24f6dcc7..19c179d49c90 100644
@@ -283,7 +283,7 @@ static int cros_ec_uart_probe(struct serdev_device *serdev)
 	ec_dev->pkt_xfer = cros_ec_uart_pkt_xfer;
 	ec_dev->din_size = sizeof(struct ec_host_response) +
 			   sizeof(struct ec_response_get_protocol_info);
-	ec_dev->dout_size = sizeof(struct ec_host_request);
+	ec_dev->dout_size = sizeof(struct ec_host_request) + sizeof(struct ec_params_rwsig_action);
 
 	serdev_device_set_client_ops(serdev, &cros_ec_uart_client_ops);
 
include/linux/platform_data/cros_ec_proto.h +2 -0
index b34ed0cc1f8d..701389c16fa7 100644
@@ -246,6 +246,8 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
 int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
 			    struct cros_ec_command *msg);
 
+int cros_ec_rwsig_continue(struct cros_ec_device *ec_dev);
+
 int cros_ec_query_all(struct cros_ec_device *ec_dev);
 
 int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
Signature · 1 line
2.47.0.338.g60cca15819-goog
#1 lore raw

PATCH v2 2/2 platform/chrome: cros_ec_proto: remove unnecessary retries

Remove the retry of the get protocol info command. It is not needed anymore, because RWSIG continue command is send before start of the probing. That assures the EC device is ready in RW and there is no need to try again because FPMCU is not fully booted.

---
 drivers/platform/chrome/cros_ec_proto.c | 9 ---------
 1 file changed, 9 deletions(-)
drivers/platform/chrome/cros_ec_proto.c +0 -9
index 9b451fac275d..2b90eed3a439 100644
@@ -368,15 +368,6 @@ static int cros_ec_get_proto_info(struct cros_ec_device *ec_dev, int devidx)
 	msg->insize = sizeof(*info);
 
 	ret = cros_ec_send_command(ec_dev, msg);
-	/*
-	 * Send command once again when timeout occurred.
-	 * Fingerprint MCU (FPMCU) is restarted during system boot which
-	 * introduces small window in which FPMCU won't respond for any
-	 * messages sent by kernel. There is no need to wait before next
-	 * attempt because we waited at least EC_MSG_DEADLINE_MS.
-	 */
-	if (ret == -ETIMEDOUT)
-		ret = cros_ec_send_command(ec_dev, msg);
 
 	if (ret < 0) {
 		dev_dbg(ec_dev->dev,
Signature · 1 line
2.47.0.338.g60cca15819-goog
maintainer
#2 lore raw
In reply to Dawid Niedzwiecki
Quoted 1 line · Dawid Niedzwiecki
drivers/platform/chrome/cros_ec_proto.c +0 -0

[...]

Quoted 2 lines
drivers/platform/chrome/cros_ec_proto.c +2 -0
+int cros_ec_rwsig_continue(struct cros_ec_device *ec_dev)
+{

[...]

Quoted 18 lines
drivers/platform/chrome/cros_ec_proto.c +18 -0
+	for (int i = 0; i < RWSIG_CONTINUE_RETRIES; i++) {
+		ret = cros_ec_send_command(ec_dev, msg);
+
+		if (ret < 0) {
+			error_count++;
+		} else if (msg->result == EC_RES_INVALID_COMMAND) {
+			/*
+			 * If EC_RES_INVALID_COMMAND is retured, it means RWSIG
+			 * is not supported or EC is already in RW, so there is
+			 * nothing left to do.
+			 */
+			break;
+		} else if (msg->result != EC_RES_SUCCESS) {
+			/* Unexpected command error. */
+			ret = cros_ec_map_error(msg->result);
+			break;
+		} else {
+			/*

drivers/platform/chrome/cros_ec_proto.c static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev, uint3 · line 341

Quoted 10 lines
drivers/platform/chrome/cros_ec_proto.c +10 -0
+			 * The EC_CMD_RWSIG_ACTION succeed. Send the command
+			 * more times, to make sure EC is in RW. A following
+			 * command can timeout, because EC may need some time to
+			 * initialize after jump to RW.
+			 */
+			error_count = 0;
+		}
+
+		if (error_count >= RWSIG_CONTINUE_MAX_ERRORS_IN_ROW)
+			break;

Only if ret < 0 makes the condition true. How about move the statement to where error_count increases to make the intent more obvious? I.e.:

    if (++error_count >= ...)
#3 lore raw
Quoted 38 lines · Tzung-Bi Shih
Quoted 1 line · Dawid Niedzwiecki
drivers/platform/chrome/cros_ec_proto.c +0 -0

[...]

Quoted 2 lines
drivers/platform/chrome/cros_ec_proto.c +2 -0
+int cros_ec_rwsig_continue(struct cros_ec_device *ec_dev)
+{

[...]

Quoted 18 lines
drivers/platform/chrome/cros_ec_proto.c +18 -0
+     for (int i = 0; i < RWSIG_CONTINUE_RETRIES; i++) {
+             ret = cros_ec_send_command(ec_dev, msg);
+
+             if (ret < 0) {
+                     error_count++;
+             } else if (msg->result == EC_RES_INVALID_COMMAND) {
+                     /*
+                      * If EC_RES_INVALID_COMMAND is retured, it means RWSIG
+                      * is not supported or EC is already in RW, so there is
+                      * nothing left to do.
+                      */
+                     break;
+             } else if (msg->result != EC_RES_SUCCESS) {
+                     /* Unexpected command error. */
+                     ret = cros_ec_map_error(msg->result);
+                     break;
+             } else {
+                     /*

drivers/platform/chrome/cros_ec_proto.c static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev, uint3 · line 341

Quoted 10 lines
drivers/platform/chrome/cros_ec_proto.c +10 -0
+                      * The EC_CMD_RWSIG_ACTION succeed. Send the command
+                      * more times, to make sure EC is in RW. A following
+                      * command can timeout, because EC may need some time to
+                      * initialize after jump to RW.
+                      */
+                     error_count = 0;
+             }
+
+             if (error_count >= RWSIG_CONTINUE_MAX_ERRORS_IN_ROW)
+                     break;

Only if ret < 0 makes the condition true. How about move the statement to where error_count increases to make the intent more obvious? I.e.:

Quoted 1 line
    if (++error_count >= ...)

Good point. I'll change that.