Chrome platform driver development
 help / color / mirror / Atom feed
From: Tzung-Bi Shih <tzungbi@kernel.org>
To: Benson Leung <bleung@chromium.org>
Cc: tzungbi@kernel.org, chrome-platform@lists.linux.dev
Subject: [PATCH 8/9] platform/chrome: cros_ec_proto: Propagate feature probe errors
Date: Tue, 24 Nov 2026 08:21:11 +0000	[thread overview]
Message-ID: <20261124082112.3361229-9-tzungbi@kernel.org> (raw)
In-Reply-To: <20261124082112.3361229-1-tzungbi@kernel.org>

cros_ec_query_all() treats any error when probing MKBP events, host
sleep v1, or the host event wake mask as if the feature were
unsupported.  A transient transport error (such as -EAGAIN) during probe
or a runtime re-query could therefore silently disable MKBP events,
disable host sleep v1, or fall back to the default wake mask.

Map EC_RES_INVALID_PARAM (-EINVAL) from EC_CMD_GET_CMD_VERSIONS to
-EOPNOTSUPP in cros_ec_get_host_command_version_mask() so "unsupported"
is consistently reported as -EOPNOTSUPP across all three feature probes.

In cros_ec_query_all(), only treat -EOPNOTSUPP as unsupported and
propagate all other errors, resetting `ec_dev->proto_version` to
EC_PROTO_VERSION_UNKNOWN so the next transfer re-queries the EC.

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
 drivers/platform/chrome/cros_ec_proto.c      | 61 ++++++++++++--------
 drivers/platform/chrome/cros_ec_proto_test.c | 37 ++++++------
 2 files changed, 55 insertions(+), 43 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c
index d8ab82154c92..823fa8879d92 100644
--- a/drivers/platform/chrome/cros_ec_proto.c
+++ b/drivers/platform/chrome/cros_ec_proto.c
@@ -578,6 +578,17 @@ static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
 
 	mapped = cros_ec_map_error(msg->result);
 	if (mapped) {
+		/*
+		 * EC returns EC_RES_INVALID_COMMAND if EC_CMD_GET_CMD_VERSIONS
+		 * isn't implemented.
+		 *
+		 * EC_CMD_GET_CMD_VERSIONS returns EC_RES_INVALID_PARAM if @cmd
+		 * isn't implemented.
+		 *
+		 * Return -EOPNOTSUPP to indicate "unsupported".
+		 */
+		if (mapped == -EINVAL)
+			mapped = -EOPNOTSUPP;
 		ret = mapped;
 		goto exit;
 	}
@@ -640,34 +651,34 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev)
 		/* pkt_xfer is NULL (v2-only EC); query with v2 directly. */
 		ret = cros_ec_get_proto_info_legacy(ec_dev);
 	}
-	if (ret) {
-		/*
-		 * It's possible for a test to occur too early when
-		 * the EC isn't listening. If this happens, we'll
-		 * test later when the first command is run.
-		 */
-		ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
-		dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
-		return ret;
-	}
+	if (ret)
+		goto exit;
 
 	/* Probe if MKBP event is supported */
 	ret = cros_ec_get_host_command_version_mask(ec_dev, EC_CMD_GET_NEXT_EVENT, &ver_mask);
-	if (ret < 0 || ver_mask == 0) {
+	if (ret == -EOPNOTSUPP)
 		ec_dev->mkbp_event_supported = 0;
-	} else {
+	else if (ret)
+		goto exit;
+	else
 		ec_dev->mkbp_event_supported = fls(ver_mask);
-
+	if (ec_dev->mkbp_event_supported)
 		dev_dbg(ec_dev->dev, "MKBP support version %u\n", ec_dev->mkbp_event_supported - 1);
-	}
+	else
+		dev_dbg(ec_dev->dev, "MKBP unsupported\n");
 
 	/* Probe if host sleep v1 is supported for S0ix failure detection. */
 	ret = cros_ec_get_host_command_version_mask(ec_dev, EC_CMD_HOST_SLEEP_EVENT, &ver_mask);
-	ec_dev->host_sleep_v1 = (ret == 0 && (ver_mask & EC_VER_MASK(1)));
+	if (ret == -EOPNOTSUPP)
+		ec_dev->host_sleep_v1 = false;
+	else if (ret)
+		goto exit;
+	else
+		ec_dev->host_sleep_v1 = ver_mask & EC_VER_MASK(1);
 
 	/* Get host event wake mask. */
 	ret = cros_ec_get_host_event_wake_mask(ec_dev, &ec_dev->host_event_wake_mask);
-	if (ret < 0) {
+	if (ret == -EOPNOTSUPP) {
 		/*
 		 * If the EC doesn't support EC_CMD_HOST_EVENT_GET_WAKE_MASK,
 		 * use a reasonable default. Note that we ignore various
@@ -685,16 +696,20 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev)
 			  EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY) |
 			  EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU) |
 			  EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS));
-		/*
-		 * Old ECs may not support this command. Complain about all
-		 * other errors.
-		 */
-		if (ret != -EOPNOTSUPP)
-			dev_err(ec_dev->dev,
-				"failed to retrieve wake mask: %d\n", ret);
+	} else if (ret) {
+		dev_err(ec_dev->dev, "failed to retrieve wake mask: %d\n", ret);
+		goto exit;
 	}
 
 	return 0;
+exit:
+	/*
+	 * Reset `proto_version` so the next transfer retries
+	 * cros_ec_query_all() once the EC is ready.
+	 */
+	ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
+	dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
+	return ret;
 }
 EXPORT_SYMBOL(cros_ec_query_all);
 
diff --git a/drivers/platform/chrome/cros_ec_proto_test.c b/drivers/platform/chrome/cros_ec_proto_test.c
index 43c0b265db24..92eb09f32a08 100644
--- a/drivers/platform/chrome/cros_ec_proto_test.c
+++ b/drivers/platform/chrome/cros_ec_proto_test.c
@@ -188,6 +188,9 @@ static void cros_ec_proto_test_query_all_pretest(struct kunit *test)
 	 */
 	ec_dev->din = NULL;
 	ec_dev->dout = NULL;
+
+	/* Fallback to -EOPNOTSUPP for all un-specified mocks. */
+	cros_kunit_ec_xfer_mock_default_result = EC_RES_INVALID_COMMAND;
 }
 
 static void cros_ec_proto_test_query_all_normal(struct kunit *test)
@@ -1069,7 +1072,7 @@ static void cros_ec_proto_test_query_all_no_mkbp_return0(struct kunit *test)
 
 	cros_ec_proto_test_query_all_pretest(test);
 	ret = cros_ec_query_all(ec_dev);
-	KUNIT_EXPECT_EQ(test, ret, 0);
+	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
 
 	/* For cros_ec_get_proto_info() without passthru. */
 	{
@@ -1113,7 +1116,8 @@ static void cros_ec_proto_test_query_all_no_mkbp_return0(struct kunit *test)
 		data = (struct ec_params_get_cmd_versions *)mock->i_data;
 		KUNIT_EXPECT_EQ(test, data->cmd, EC_CMD_GET_NEXT_EVENT);
 
-		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 0);
+		KUNIT_EXPECT_EQ(test, ec_dev->mkbp_event_supported, 0xbf);
+		KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
 	}
 }
 
@@ -1151,7 +1155,7 @@ static void cros_ec_proto_test_query_all_no_host_sleep(struct kunit *test)
 
 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
 	{
-		mock = cros_kunit_ec_xfer_mock_add(test, 0);
+		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
 	}
 
@@ -1275,7 +1279,7 @@ static void cros_ec_proto_test_query_all_no_host_sleep_return0(struct kunit *tes
 
 	cros_ec_proto_test_query_all_pretest(test);
 	ret = cros_ec_query_all(ec_dev);
-	KUNIT_EXPECT_EQ(test, ret, 0);
+	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
 
 	/* For cros_ec_get_proto_info() without passthru. */
 	{
@@ -1326,7 +1330,8 @@ static void cros_ec_proto_test_query_all_no_host_sleep_return0(struct kunit *tes
 				sizeof(struct ec_response_get_cmd_versions));
 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
 
-		KUNIT_EXPECT_FALSE(test, ec_dev->host_sleep_v1);
+		KUNIT_EXPECT_TRUE(test, ec_dev->host_sleep_v1);
+		KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
 	}
 }
 
@@ -1364,13 +1369,13 @@ static void cros_ec_proto_test_query_all_default_wake_mask_return_error(struct k
 
 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
 	{
-		mock = cros_kunit_ec_xfer_mock_add(test, 0);
+		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
 	}
 
 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
 	{
-		mock = cros_kunit_ec_xfer_mock_add(test, 0);
+		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
 	}
 
@@ -1491,13 +1496,13 @@ static void cros_ec_proto_test_query_all_default_wake_mask_return0(struct kunit
 
 	/* For cros_ec_get_host_command_version_mask() for MKBP. */
 	{
-		mock = cros_kunit_ec_xfer_mock_add(test, 0);
+		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
 	}
 
 	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
 	{
-		mock = cros_kunit_ec_xfer_mock_add(test, 0);
+		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
 		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
 	}
 
@@ -1509,7 +1514,7 @@ static void cros_ec_proto_test_query_all_default_wake_mask_return0(struct kunit
 
 	cros_ec_proto_test_query_all_pretest(test);
 	ret = cros_ec_query_all(ec_dev);
-	KUNIT_EXPECT_EQ(test, ret, 0);
+	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
 
 	/* For cros_ec_get_proto_info() without passthru. */
 	{
@@ -1563,8 +1568,6 @@ static void cros_ec_proto_test_query_all_default_wake_mask_return0(struct kunit
 
 	/* For get_host_event_wake_mask(). */
 	{
-		u32 mask;
-
 		mock = cros_kunit_ec_xfer_mock_next();
 		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
 
@@ -1573,14 +1576,8 @@ static void cros_ec_proto_test_query_all_default_wake_mask_return0(struct kunit
 		KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_host_event_mask));
 		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
 
-		mask = ec_dev->host_event_wake_mask;
-		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED), 0);
-		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED), 0);
-		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW), 0);
-		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL), 0);
-		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY), 0);
-		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU), 0);
-		KUNIT_EXPECT_EQ(test, mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS), 0);
+		KUNIT_EXPECT_EQ(test, ec_dev->host_event_wake_mask, U32_MAX);
+		KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
 	}
 }
 
-- 
2.56.1.831.g720f3f5937-goog


  parent reply	other threads:[~2026-11-24  8:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  8:21 [PATCH 0/9] platform/chrome: cros_ec_proto: Harden query_all() and retries Tzung-Bi Shih
2026-11-24  8:21 ` [PATCH 1/9] platform/chrome: cros_ec_proto: Reset msg->result before sending command Tzung-Bi Shih
2026-11-24  8:21 ` [PATCH 2/9] platform/chrome: cros_ec_proto: Retry sending command on -EAGAIN Tzung-Bi Shih
2026-11-24  8:21 ` [PATCH 3/9] platform/chrome: KUnit: Add tests for retrying commands " Tzung-Bi Shih
2026-11-24  8:21 ` [PATCH 4/9] platform/chrome: cros_ec_proto: Reallocate din/dout buffers atomically Tzung-Bi Shih
2026-11-24  8:21 ` [PATCH 5/9] platform/chrome: cros_ec_proto: Guard protocol v2 fallback Tzung-Bi Shih
2026-11-24  8:21 ` [PATCH 6/9] platform/chrome: KUnit: Update test cases for protocol v2 Tzung-Bi Shih
2026-11-24  8:21 ` [PATCH 7/9] platform/chrome: KUnit: Add test cases for protocol fallback Tzung-Bi Shih
2026-11-24  8:21 ` Tzung-Bi Shih [this message]
2026-11-24  8:21 ` [PATCH 9/9] platform/chrome: KUnit: Add feature probe unsupported tests Tzung-Bi Shih
2026-11-27  5:03 ` [PATCH 0/9] platform/chrome: cros_ec_proto: Harden query_all() and retries Tzung-Bi Shih

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124082112.3361229-9-tzungbi@kernel.org \
    --to=tzungbi@kernel.org \
    --cc=bleung@chromium.org \
    --cc=chrome-platform@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox