Skip to contentSkip to conversation

platform/chrome: cros_ec_proto: Harden query_all() and retries

PATCH New Tzung-Bi Shih 1 participant 10 messages Nov 24

Subsystems (2)
lore
Share & tools
lore

Tzung-Bi Shih

author maintainer
#0 lore raw

cover platform/chrome: cros_ec_proto: Harden query_all() and retries

This series hardens ChromeOS EC command transmission and cros_ec_query_all() against transient transport errors (such as -EAGAIN) and buffer reallocation hazards:

- Patches 1-2: Reset msg->result before each transfer and retry transient -EAGAIN errors in cros_ec_send_command() and EC_CMD_RESEND_RESPONSE polling.

- Patch 4: Reallocate din/dout safely with devm_krealloc() directly when protocol info arrives, preventing din_size/dout_size OOB mismatches, -ENOMEM dangling pointers, and runtime reallocation churn.

- Patch 5: Guard protocol v3-to-v2 fallback so transient errors, v3-only transports, or runtime re-queries on registered v3 devices do not downgrade the EC to v2.

- Patch 8: Distinguish -EOPNOTSUPP from real transport/protocol errors when probing MKBP, host sleep v1, and wake mask, propagating real errors and resetting proto_version so the next transfer re-queries.

New KUnit test cases and non-breaking test cleanups are kept in separate patches (patches 3, 6, 7, and 9). In patch 8, updates to existing KUnit tests are folded into the code change itself so that the test suite remains passing at every commit for bisectability.

Tzung-Bi Shih (9):
platform/chrome: cros_ec_proto: Reset msg->result before sending
command
platform/chrome: cros_ec_proto: Retry sending command on -EAGAIN platform/chrome: KUnit: Add tests for retrying commands on -EAGAIN platform/chrome: cros_ec_proto: Reallocate din/dout buffers atomically platform/chrome: cros_ec_proto: Guard protocol v2 fallback platform/chrome: KUnit: Update test cases for protocol v2 platform/chrome: KUnit: Add test cases for protocol fallback platform/chrome: cros_ec_proto: Propagate feature probe errors platform/chrome: KUnit: Add feature probe unsupported tests

 drivers/platform/chrome/cros_ec_proto.c      | 167 +++++---
 drivers/platform/chrome/cros_ec_proto_test.c | 429 +++++++++++++++----
 2 files changed, 469 insertions(+), 127 deletions(-)
Signature · 1 line
2.56.1.831.g720f3f5937-goog
author maintainer
#1 lore raw

PATCH 1/9 platform/chrome: cros_ec_proto: Reset msg->result before sending command

msg->result could be uninitialized or stale. Reset it before sending a command. Otherwise, if the transfer fails before updating msg->result, cros_ec_send_command() may spuriously enter cros_ec_wait_until_complete() when msg->result happens to be EC_RES_IN_PROGRESS.

Fixes d86c21fd3111 ("mfd: cros_ec: wait for completion of commands that return IN_PROGRESS")
---
 drivers/platform/chrome/cros_ec_proto.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/platform/chrome/cros_ec_proto.c +2 -0
index b81b37c7169a..0306bedd0f0f 100644
@@ -116,6 +116,8 @@ static int cros_ec_xfer_command(struct cros_ec_device *ec_dev, struct cros_ec_co
 	int ret;
 	int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
 
+	msg->result = EC_RES_SUCCESS;
+
 	if (ec_dev->proto_version > 2)
 		xfer_fxn = ec_dev->pkt_xfer;
 	else
Signature · 1 line
2.56.1.831.g720f3f5937-goog
author maintainer
#2 lore raw

PATCH 2/9 platform/chrome: cros_ec_proto: Retry sending command on -EAGAIN

In reply to Tzung-Bi Shih

Underlying transport mediums can be transiently unavailable and return -EAGAIN. Retry sending the command in cros_ec_send_command() up to EC_COMMAND_SEND_RETRIES times with a short delay between attempts.

Check msg->result == EC_RES_IN_PROGRESS before retrying so commands that are already executing on the EC go directly to cros_ec_wait_until_complete().

Similarly, continue polling in cros_ec_wait_until_complete() when EC_CMD_RESEND_RESPONSE fails with -EAGAIN.

---
 drivers/platform/chrome/cros_ec_proto.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/platform/chrome/cros_ec_proto.c +15 -2
index 0306bedd0f0f..135134a1e317 100644
@@ -16,6 +16,7 @@
 #include "cros_ec_trace.h"
 
 #define EC_COMMAND_RETRIES	50
+#define EC_COMMAND_SEND_RETRIES	5
 #define RWSIG_CONTINUE_RETRIES	8
 #define RWSIG_CONTINUE_MAX_ERRORS_IN_ROW	3
 
@@ -194,6 +195,9 @@ static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev,
 			orig_msg->command = orig_cmd;
 			orig_msg->outsize = orig_outsize;
 			orig_msg->version = orig_version;
+
+			if (ret == -EAGAIN)
+				continue;
 			return ret;
 		}
 	}
@@ -203,10 +207,19 @@ static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev,
 
 static int cros_ec_send_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
 {
-	int ret = cros_ec_xfer_command(ec_dev, msg);
+	int retry_count = EC_COMMAND_SEND_RETRIES;
+	int ret;
+
+retry:
+	ret = cros_ec_xfer_command(ec_dev, msg);
 
 	if (msg->result == EC_RES_IN_PROGRESS)
-		ret = cros_ec_wait_until_complete(ec_dev, msg);
+		return cros_ec_wait_until_complete(ec_dev, msg);
+
+	if (ret == -EAGAIN && retry_count--) {
+		usleep_range(10000, 11000);
+		goto retry;
+	}
 
 	return ret;
 }
Signature · 1 line
2.56.1.831.g720f3f5937-goog
author maintainer
#3 lore raw

PATCH 3/9 platform/chrome: KUnit: Add tests for retrying commands on -EAGAIN

In reply to Tzung-Bi Shih

Add KUnit test cases to verify cros_ec_send_command() retries when the underlying transport returns -EAGAIN.

---
 drivers/platform/chrome/cros_ec_proto_test.c | 42 ++++++++++++++++++++
 1 file changed, 42 insertions(+)
drivers/platform/chrome/cros_ec_proto_test.c +42 -0
index cbaaf98d6877..ef94bafe93d8 100644
@@ -1744,6 +1744,46 @@ static void cros_ec_proto_test_cmd_xfer_protocol_v2_no_op(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, ret, -EIO);
 }
 
+static void cros_ec_proto_test_cmd_xfer_eagain_once(struct kunit *test)
+{
+	struct cros_ec_proto_test_priv *priv = test->priv;
+	struct cros_ec_device *ec_dev = &priv->ec_dev;
+	struct ec_xfer_mock *mock;
+	struct cros_ec_command msg;
+	int ret;
+
+	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+	memset(&msg, 0, sizeof(msg));
+
+	{
+		mock = cros_kunit_ec_xfer_mock_addx(test, -EAGAIN, EC_RES_SUCCESS, 0);
+		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+	}
+
+	ret = cros_ec_cmd_xfer(ec_dev, &msg);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+
+	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 2);
+}
+
+static void cros_ec_proto_test_cmd_xfer_eagain_exhausted(struct kunit *test)
+{
+	struct cros_ec_proto_test_priv *priv = test->priv;
+	struct cros_ec_device *ec_dev = &priv->ec_dev;
+	struct cros_ec_command msg;
+	int ret;
+
+	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+	memset(&msg, 0, sizeof(msg));
+
+	cros_kunit_ec_xfer_mock_default_ret = -EAGAIN;
+
+	ret = cros_ec_cmd_xfer(ec_dev, &msg);
+	KUNIT_EXPECT_EQ(test, ret, -EAGAIN);
+
+	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 6);
+}
+
 static void cros_ec_proto_test_cmd_xfer_in_progress_payload_0byte(struct kunit *test)
 {
 	struct cros_ec_proto_test_priv *priv = test->priv;
@@ -2853,6 +2893,8 @@ static struct kunit_case cros_ec_proto_test_cases[] = {
 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_protocol_v3_no_op),
 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_protocol_v2_normal),
 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_protocol_v2_no_op),
+	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_eagain_once),
+	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_eagain_exhausted),
 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_payload_0byte),
 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_payload_4bytes),
 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_payload_gt4bytes),
Signature · 1 line
2.56.1.831.g720f3f5937-goog
author maintainer
#4 lore raw

PATCH 4/9 platform/chrome: cros_ec_proto: Reallocate din/dout buffers atomically

In reply to Tzung-Bi Shih

In cros_ec_query_all(), cros_ec_get_proto_info() for EC_INDEX updated din_size and dout_size before really reallocating din and dout, while the subsequent PD_INDEX query sent another EC command in between. Transport drivers that bounds-check against din_size/dout_size could therefore access din/dout out of bounds.

In addition, cros_ec_query_all() freed din and dout before allocating the new buffers, leaving dangling pointers on -ENOMEM and needlessly reallocating on runtime re-queries even when buffer sizes did not grow.

Use devm_krealloc() to update din/din_size and dout/dout_size directly in cros_ec_get_proto_info() and cros_ec_get_proto_info_legacy(), before updating the other protocol fields.

Fixes 2c7589af3c4d ("mfd: cros_ec: add proto v3 skeleton")
---
 drivers/platform/chrome/cros_ec_proto.c | 58 ++++++++++++++-----------
 1 file changed, 32 insertions(+), 26 deletions(-)
drivers/platform/chrome/cros_ec_proto.c +32 -26
index 135134a1e317..d48d3c73ca0b 100644
@@ -385,7 +385,8 @@ static int cros_ec_get_proto_info(struct cros_ec_device *ec_dev, int devidx)
 {
 	struct cros_ec_command *msg;
 	struct ec_response_get_protocol_info *info;
-	int ret, mapped;
+	int ret, mapped, din_size, dout_size;
+	u8 *din, *dout;
 
 	ec_dev->proto_version = 3;
 	if (devidx > 0)
@@ -422,14 +423,27 @@ static int cros_ec_get_proto_info(struct cros_ec_device *ec_dev, int devidx)
 
 	switch (devidx) {
 	case CROS_EC_DEV_EC_INDEX:
+		ret = -ENOMEM;
+		din_size = info->max_response_packet_size + EC_MAX_RESPONSE_OVERHEAD;
+		din = devm_krealloc(ec_dev->dev, ec_dev->din, din_size, GFP_KERNEL);
+		if (!din)
+			goto exit;
+		ec_dev->din_size = din_size;
+		ec_dev->din = din;
+
+		dout_size = info->max_request_packet_size + EC_MAX_REQUEST_OVERHEAD;
+		dout = devm_krealloc(ec_dev->dev, ec_dev->dout, dout_size, GFP_KERNEL);
+		if (!dout)
+			goto exit;
+		ec_dev->dout_size = dout_size;
+		ec_dev->dout = dout;
+
 		ec_dev->max_request = info->max_request_packet_size -
 						sizeof(struct ec_host_request);
 		ec_dev->max_response = info->max_response_packet_size -
 						sizeof(struct ec_host_response);
 		ec_dev->proto_version = min(EC_HOST_REQUEST_VERSION,
 					    fls(info->protocol_versions) - 1);
-		ec_dev->din_size = info->max_response_packet_size + EC_MAX_RESPONSE_OVERHEAD;
-		ec_dev->dout_size = info->max_request_packet_size + EC_MAX_REQUEST_OVERHEAD;
 
 		dev_dbg(ec_dev->dev, "using proto v%u\n", ec_dev->proto_version);
 		break;
@@ -456,6 +470,7 @@ static int cros_ec_get_proto_info_legacy(struct cros_ec_device *ec_dev)
 	struct ec_params_hello *params;
 	struct ec_response_hello *response;
 	int ret, mapped;
+	u8 *din, *dout;
 
 	ec_dev->proto_version = 2;
 
@@ -497,12 +512,23 @@ static int cros_ec_get_proto_info_legacy(struct cros_ec_device *ec_dev)
 		goto exit;
 	}
 
+	ret = -ENOMEM;
+	din = devm_krealloc(ec_dev->dev, ec_dev->din, EC_PROTO2_MSG_BYTES, GFP_KERNEL);
+	if (!din)
+		goto exit;
+	ec_dev->din_size = EC_PROTO2_MSG_BYTES;
+	ec_dev->din = din;
+
+	dout = devm_krealloc(ec_dev->dev, ec_dev->dout, EC_PROTO2_MSG_BYTES, GFP_KERNEL);
+	if (!dout)
+		goto exit;
+	ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
+	ec_dev->dout = dout;
+
 	ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
 	ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
 	ec_dev->max_passthru = 0;
 	ec_dev->pkt_xfer = NULL;
-	ec_dev->din_size = EC_PROTO2_MSG_BYTES;
-	ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
 
 	dev_dbg(ec_dev->dev, "falling back to proto v2\n");
 	ret = 0;
@@ -578,7 +604,6 @@ static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
  */
 int cros_ec_query_all(struct cros_ec_device *ec_dev)
 {
-	struct device *dev = ec_dev->dev;
 	u32 ver_mask;
 	int ret;
 
@@ -601,22 +626,6 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev)
 		}
 	}
 
-	devm_kfree(dev, ec_dev->din);
-	devm_kfree(dev, ec_dev->dout);
-
-	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
-	if (!ec_dev->din) {
-		ret = -ENOMEM;
-		goto exit;
-	}
-
-	ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
-	if (!ec_dev->dout) {
-		devm_kfree(dev, ec_dev->din);
-		ret = -ENOMEM;
-		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) {
@@ -660,10 +669,7 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev)
 				"failed to retrieve wake mask: %d\n", ret);
 	}
 
-	ret = 0;
-
-exit:
-	return ret;
+	return 0;
 }
 EXPORT_SYMBOL(cros_ec_query_all);
 
Signature · 1 line
2.56.1.831.g720f3f5937-goog
author maintainer
#5 lore raw

PATCH 5/9 platform/chrome: cros_ec_proto: Guard protocol v2 fallback

In reply to Tzung-Bi Shih

cros_ec_query_all() falls back to cros_ec_get_proto_info_legacy() whenever cros_ec_get_proto_info() returned any error. Because cros_ec_get_proto_info_legacy() clears ec_dev->pkt_xfer, a transient failure during initial probe or a runtime re-query can permanently downgrade a v3 EC to v2, or mask the real error with -EIO on v3-only transports without cmd_xfer.

Add cros_ec_proto_can_fallback_to_v2() to avoid falling back when:
- The device is already registered with pkt_xfer (known v3+ EC).
- ec_dev->cmd_xfer is not supported (doesn't support v2).
- The v3 query failed with a transient error (-ENOMEM or -EAGAIN).

Also skip the v3 probe and call cros_ec_get_proto_info_legacy() directly when ec_dev->pkt_xfer is already NULL (known v2-only EC).

---
 drivers/platform/chrome/cros_ec_proto.c | 55 ++++++++++++++++++-------
 1 file changed, 40 insertions(+), 15 deletions(-)
drivers/platform/chrome/cros_ec_proto.c +40 -15
index d48d3c73ca0b..d8ab82154c92 100644
@@ -595,6 +595,24 @@ static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
 	return ret;
 }
 
+static bool cros_ec_proto_can_fallback_to_v2(struct cros_ec_device *ec_dev,
+					     int ret)
+{
+	/* The EC does support v3. */
+	if (ec_dev->registered && ec_dev->pkt_xfer)
+		return false;
+
+	/* The transport doesn't support v2. */
+	if (!ec_dev->cmd_xfer)
+		return false;
+
+	/* The system or EC is encountering transient failures. */
+	if (ret == -ENOMEM || ret == -EAGAIN)
+		return false;
+
+	return true;
+}
+
 /**
  * cros_ec_query_all() -  Query the protocol version supported by the
  *         ChromeOS EC.
@@ -607,23 +625,30 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev)
 	u32 ver_mask;
 	int ret;
 
-	/* First try sending with proto v3. */
-	if (!cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_EC_INDEX)) {
-		/* Check for PD. */
-		cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_PD_INDEX);
+	if (ec_dev->pkt_xfer) {
+		/* First try sending with proto v3. */
+		ret = cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_EC_INDEX);
+
+		if (!ret) {
+			/* Check for optional PD chip; ignore errors. */
+			cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_PD_INDEX);
+		} else if (cros_ec_proto_can_fallback_to_v2(ec_dev, ret)) {
+			/* Fall back to querying with a v2 hello message. */
+			ret = cros_ec_get_proto_info_legacy(ec_dev);
+		}
 	} else {
-		/* Try querying with a v2 hello message. */
+		/* 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) {
+		/*
+		 * 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;
 	}
 
 	/* Probe if MKBP event is supported */
Signature · 1 line
2.56.1.831.g720f3f5937-goog
author maintainer
#6 lore raw

PATCH 6/9 platform/chrome: KUnit: Update test cases for protocol v2

In reply to Tzung-Bi Shih

cros_ec_query_all() now uses v2 directly when ec_dev->pkt_xfer is NULL. Update the test cases for protocol v2.

---
 drivers/platform/chrome/cros_ec_proto_test.c | 72 ++------------------
 1 file changed, 4 insertions(+), 68 deletions(-)
drivers/platform/chrome/cros_ec_proto_test.c +4 -68
index ef94bafe93d8..c31d0ef87fb2 100644
@@ -611,11 +611,7 @@ static void cros_ec_proto_test_query_all_legacy_xfer_error(struct kunit *test)
 	struct ec_xfer_mock *mock;
 	int ret;
 
-	/* For cros_ec_get_proto_info() without passthru. */
-	{
-		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
-		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
-	}
+	ec_dev->pkt_xfer = NULL;
 
 	/* For cros_ec_get_proto_info_legacy(). */
 	{
@@ -628,18 +624,6 @@ static void cros_ec_proto_test_query_all_legacy_xfer_error(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, ret, -EIO);
 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
 
-	/* For cros_ec_get_proto_info() without passthru. */
-	{
-		mock = cros_kunit_ec_xfer_mock_next();
-		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
-
-		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
-		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
-		KUNIT_EXPECT_EQ(test, mock->msg.insize,
-				sizeof(struct ec_response_get_protocol_info));
-		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
-	}
-
 	/* For cros_ec_get_proto_info_legacy(). */
 	{
 		mock = cros_kunit_ec_xfer_mock_next();
@@ -659,11 +643,7 @@ static void cros_ec_proto_test_query_all_legacy_return_error(struct kunit *test)
 	struct ec_xfer_mock *mock;
 	int ret;
 
-	/* For cros_ec_get_proto_info() without passthru. */
-	{
-		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
-		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
-	}
+	ec_dev->pkt_xfer = NULL;
 
 	/* For cros_ec_get_proto_info_legacy(). */
 	{
@@ -676,18 +656,6 @@ static void cros_ec_proto_test_query_all_legacy_return_error(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, ret, -EOPNOTSUPP);
 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
 
-	/* For cros_ec_get_proto_info() without passthru. */
-	{
-		mock = cros_kunit_ec_xfer_mock_next();
-		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
-
-		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
-		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
-		KUNIT_EXPECT_EQ(test, mock->msg.insize,
-				sizeof(struct ec_response_get_protocol_info));
-		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
-	}
-
 	/* For cros_ec_get_proto_info_legacy(). */
 	{
 		mock = cros_kunit_ec_xfer_mock_next();
@@ -707,11 +675,7 @@ static void cros_ec_proto_test_query_all_legacy_data_error(struct kunit *test)
 	struct ec_xfer_mock *mock;
 	int ret;
 
-	/* For cros_ec_get_proto_info() without passthru. */
-	{
-		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
-		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
-	}
+	ec_dev->pkt_xfer = NULL;
 
 	/* For cros_ec_get_proto_info_legacy(). */
 	{
@@ -729,18 +693,6 @@ static void cros_ec_proto_test_query_all_legacy_data_error(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, ret, -EBADMSG);
 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
 
-	/* For cros_ec_get_proto_info() without passthru. */
-	{
-		mock = cros_kunit_ec_xfer_mock_next();
-		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
-
-		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
-		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
-		KUNIT_EXPECT_EQ(test, mock->msg.insize,
-				sizeof(struct ec_response_get_protocol_info));
-		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
-	}
-
 	/* For cros_ec_get_proto_info_legacy(). */
 	{
 		mock = cros_kunit_ec_xfer_mock_next();
@@ -760,11 +712,7 @@ static void cros_ec_proto_test_query_all_legacy_return0(struct kunit *test)
 	struct ec_xfer_mock *mock;
 	int ret;
 
-	/* For cros_ec_get_proto_info() without passthru. */
-	{
-		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
-		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
-	}
+	ec_dev->pkt_xfer = NULL;
 
 	/* For cros_ec_get_proto_info_legacy(). */
 	{
@@ -777,18 +725,6 @@ static void cros_ec_proto_test_query_all_legacy_return0(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
 	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
 
-	/* For cros_ec_get_proto_info() without passthru. */
-	{
-		mock = cros_kunit_ec_xfer_mock_next();
-		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
-
-		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
-		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
-		KUNIT_EXPECT_EQ(test, mock->msg.insize,
-				sizeof(struct ec_response_get_protocol_info));
-		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
-	}
-
 	/* For cros_ec_get_proto_info_legacy(). */
 	{
 		mock = cros_kunit_ec_xfer_mock_next();
Signature · 1 line
2.56.1.831.g720f3f5937-goog
author maintainer
#7 lore raw

PATCH 7/9 platform/chrome: KUnit: Add test cases for protocol fallback

In reply to Tzung-Bi Shih

Add new test cases verifying cros_ec_query_all() does not fall back to protocol v2 when:
- The device is already registered with pkt_xfer (known v3+ EC).
- ec_dev->cmd_xfer is not supported (doesn't support v2).
- The v3 query failed with a transient error (-ENOMEM or -EAGAIN).

Also rename the existing protocol v3-to-v2 fallback tests to cros_ec_proto_test_query_all_fallback_v3_* for clarity.

---
 drivers/platform/chrome/cros_ec_proto_test.c | 123 ++++++++++++++++++-
 1 file changed, 119 insertions(+), 4 deletions(-)
drivers/platform/chrome/cros_ec_proto_test.c +119 -4
index c31d0ef87fb2..43c0b265db24 100644
@@ -474,7 +474,7 @@ static void cros_ec_proto_test_query_all_no_pd_return0(struct kunit *test)
 	}
 }
 
-static void cros_ec_proto_test_query_all_legacy_normal_v3_return_error(struct kunit *test)
+static void cros_ec_proto_test_query_all_fallback_v3_return_error(struct kunit *test)
 {
 	struct cros_ec_proto_test_priv *priv = test->priv;
 	struct cros_ec_device *ec_dev = &priv->ec_dev;
@@ -539,7 +539,7 @@ static void cros_ec_proto_test_query_all_legacy_normal_v3_return_error(struct ku
 	}
 }
 
-static void cros_ec_proto_test_query_all_legacy_normal_v3_return0(struct kunit *test)
+static void cros_ec_proto_test_query_all_fallback_v3_return0(struct kunit *test)
 {
 	struct cros_ec_proto_test_priv *priv = test->priv;
 	struct cros_ec_device *ec_dev = &priv->ec_dev;
@@ -604,6 +604,117 @@ static void cros_ec_proto_test_query_all_legacy_normal_v3_return0(struct kunit *
 	}
 }
 
+static void cros_ec_proto_test_query_all_no_fallback_registered(struct kunit *test)
+{
+	struct cros_ec_proto_test_priv *priv = test->priv;
+	struct cros_ec_device *ec_dev = &priv->ec_dev;
+	struct ec_xfer_mock *mock;
+	int ret;
+
+	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+	ec_dev->cmd_xfer = cros_kunit_ec_cmd_xfer_mock;
+	ec_dev->registered = true;
+
+	/* For cros_ec_get_proto_info() without passthru. */
+	{
+		mock = cros_kunit_ec_xfer_mock_add(test, 0);
+		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+	}
+
+	cros_ec_proto_test_query_all_pretest(test);
+	ret = cros_ec_query_all(ec_dev);
+	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
+	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
+	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 1);
+	KUNIT_EXPECT_EQ(test, cros_kunit_ec_cmd_xfer_mock_called, 0);
+
+	/* For cros_ec_get_proto_info() without passthru. */
+	{
+		mock = cros_kunit_ec_xfer_mock_next();
+		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
+
+		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
+		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
+		KUNIT_EXPECT_EQ(test, mock->msg.insize,
+				sizeof(struct ec_response_get_protocol_info));
+		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
+	}
+}
+
+static void cros_ec_proto_test_query_all_no_fallback_no_cmd_xfer(struct kunit *test)
+{
+	struct cros_ec_proto_test_priv *priv = test->priv;
+	struct cros_ec_device *ec_dev = &priv->ec_dev;
+	struct ec_xfer_mock *mock;
+	int ret;
+
+	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+	ec_dev->cmd_xfer = NULL;
+
+	/* For cros_ec_get_proto_info() without passthru. */
+	{
+		mock = cros_kunit_ec_xfer_mock_add(test, 0);
+		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+	}
+
+	cros_ec_proto_test_query_all_pretest(test);
+	ret = cros_ec_query_all(ec_dev);
+	KUNIT_EXPECT_EQ(test, ret, -EPROTO);
+	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
+	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 1);
+	KUNIT_EXPECT_EQ(test, cros_kunit_ec_cmd_xfer_mock_called, 0);
+
+	/* For cros_ec_get_proto_info() without passthru. */
+	{
+		mock = cros_kunit_ec_xfer_mock_next();
+		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
+
+		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
+		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
+		KUNIT_EXPECT_EQ(test, mock->msg.insize,
+				sizeof(struct ec_response_get_protocol_info));
+		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
+	}
+}
+
+static void cros_ec_proto_test_query_all_no_fallback_eagain(struct kunit *test)
+{
+	struct cros_ec_proto_test_priv *priv = test->priv;
+	struct cros_ec_device *ec_dev = &priv->ec_dev;
+	int ret;
+
+	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+	ec_dev->cmd_xfer = cros_kunit_ec_cmd_xfer_mock;
+
+	cros_kunit_ec_xfer_mock_default_ret = -EAGAIN;
+
+	cros_ec_proto_test_query_all_pretest(test);
+	ret = cros_ec_query_all(ec_dev);
+	KUNIT_EXPECT_EQ(test, ret, -EAGAIN);
+	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
+	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 6);
+	KUNIT_EXPECT_EQ(test, cros_kunit_ec_cmd_xfer_mock_called, 0);
+}
+
+static void cros_ec_proto_test_query_all_no_fallback_enomem(struct kunit *test)
+{
+	struct cros_ec_proto_test_priv *priv = test->priv;
+	struct cros_ec_device *ec_dev = &priv->ec_dev;
+	int ret;
+
+	ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+	ec_dev->cmd_xfer = cros_kunit_ec_cmd_xfer_mock;
+
+	cros_kunit_ec_xfer_mock_default_ret = -ENOMEM;
+
+	cros_ec_proto_test_query_all_pretest(test);
+	ret = cros_ec_query_all(ec_dev);
+	KUNIT_EXPECT_EQ(test, ret, -ENOMEM);
+	KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN);
+	KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 1);
+	KUNIT_EXPECT_EQ(test, cros_kunit_ec_cmd_xfer_mock_called, 0);
+}
+
 static void cros_ec_proto_test_query_all_legacy_xfer_error(struct kunit *test)
 {
 	struct cros_ec_proto_test_priv *priv = test->priv;
@@ -2808,8 +2919,12 @@ static struct kunit_case cros_ec_proto_test_cases[] = {
 	KUNIT_CASE(cros_ec_proto_test_query_all_normal),
 	KUNIT_CASE(cros_ec_proto_test_query_all_no_pd_return_error),
 	KUNIT_CASE(cros_ec_proto_test_query_all_no_pd_return0),
-	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_normal_v3_return_error),
-	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_normal_v3_return0),
+	KUNIT_CASE(cros_ec_proto_test_query_all_fallback_v3_return_error),
+	KUNIT_CASE(cros_ec_proto_test_query_all_fallback_v3_return0),
+	KUNIT_CASE(cros_ec_proto_test_query_all_no_fallback_registered),
+	KUNIT_CASE(cros_ec_proto_test_query_all_no_fallback_no_cmd_xfer),
+	KUNIT_CASE(cros_ec_proto_test_query_all_no_fallback_eagain),
+	KUNIT_CASE(cros_ec_proto_test_query_all_no_fallback_enomem),
 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_xfer_error),
 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_return_error),
 	KUNIT_CASE(cros_ec_proto_test_query_all_legacy_data_error),
Signature · 1 line
2.56.1.831.g720f3f5937-goog
author maintainer
#8 lore raw

PATCH 8/9 platform/chrome: cros_ec_proto: Propagate feature probe errors

In reply to Tzung-Bi Shih

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.

---
 drivers/platform/chrome/cros_ec_proto.c      | 61 ++++++++++++--------
 drivers/platform/chrome/cros_ec_proto_test.c | 37 ++++++------
 2 files changed, 55 insertions(+), 43 deletions(-)
drivers/platform/chrome/cros_ec_proto.c +38 -23
index d8ab82154c92..823fa8879d92 100644
@@ -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);
 
drivers/platform/chrome/cros_ec_proto_test.c +17 -20
index 43c0b265db24..92eb09f32a08 100644
@@ -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);
 	}
 }
 
Signature · 1 line
2.56.1.831.g720f3f5937-goog
author maintainer
#9 lore raw

PATCH 9/9 platform/chrome: KUnit: Add feature probe unsupported tests

In reply to Tzung-Bi Shih

Add KUnit test cases covering unsupported responses in cros_ec_query_all().

---
 drivers/platform/chrome/cros_ec_proto_test.c | 191 +++++++++++++++++++
 1 file changed, 191 insertions(+)
drivers/platform/chrome/cros_ec_proto_test.c +191 -0
index 92eb09f32a08..0829b0dc9fda 100644
@@ -1121,6 +1121,94 @@ static void cros_ec_proto_test_query_all_no_mkbp_return0(struct kunit *test)
 	}
 }
 
+static void cros_ec_proto_test_query_all_no_mkbp_invalid_param(struct kunit *test)
+{
+	struct cros_ec_proto_test_priv *priv = test->priv;
+	struct cros_ec_device *ec_dev = &priv->ec_dev;
+	struct ec_xfer_mock *mock;
+	int ret;
+
+	/* Set some garbage bytes. */
+	ec_dev->mkbp_event_supported = 0xbf;
+
+	/* For cros_ec_get_proto_info() without passthru. */
+	{
+		struct ec_response_get_protocol_info *data;
+
+		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
+		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+
+		/*
+		 * Although it doesn't check the value, provides valid sizes so that
+		 * cros_ec_query_all() allocates din and dout correctly.
+		 */
+		data = (struct ec_response_get_protocol_info *)mock->o_data;
+		data->max_request_packet_size = 0xbe;
+		data->max_response_packet_size = 0xef;
+	}
+
+	/* For cros_ec_get_proto_info() with passthru. */
+	{
+		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 MKBP. */
+	{
+		mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_PARAM, 0);
+		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+	}
+
+	cros_ec_proto_test_query_all_pretest(test);
+	ret = cros_ec_query_all(ec_dev);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+
+	/* For cros_ec_get_proto_info() without passthru. */
+	{
+		mock = cros_kunit_ec_xfer_mock_next();
+		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
+
+		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
+		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
+		KUNIT_EXPECT_EQ(test, mock->msg.insize,
+				sizeof(struct ec_response_get_protocol_info));
+		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
+	}
+
+	/* For cros_ec_get_proto_info() with passthru. */
+	{
+		mock = cros_kunit_ec_xfer_mock_next();
+		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
+
+		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
+		KUNIT_EXPECT_EQ(test, mock->msg.command,
+				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
+				EC_CMD_GET_PROTOCOL_INFO);
+		KUNIT_EXPECT_EQ(test, mock->msg.insize,
+				sizeof(struct ec_response_get_protocol_info));
+		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
+	}
+
+	/* For cros_ec_get_host_command_version_mask() for MKBP. */
+	{
+		struct ec_params_get_cmd_versions *data;
+
+		mock = cros_kunit_ec_xfer_mock_next();
+		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
+
+		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
+		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
+		KUNIT_EXPECT_EQ(test, mock->msg.insize,
+				sizeof(struct ec_response_get_cmd_versions));
+		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data));
+
+		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);
+	}
+}
+
 static void cros_ec_proto_test_query_all_no_host_sleep(struct kunit *test)
 {
 	struct cros_ec_proto_test_priv *priv = test->priv;
@@ -1335,6 +1423,107 @@ static void cros_ec_proto_test_query_all_no_host_sleep_return0(struct kunit *tes
 	}
 }
 
+static void cros_ec_proto_test_query_all_no_host_sleep_return_error(struct kunit *test)
+{
+	struct cros_ec_proto_test_priv *priv = test->priv;
+	struct cros_ec_device *ec_dev = &priv->ec_dev;
+	struct ec_xfer_mock *mock;
+	int ret;
+
+	/* Set some garbage bytes. */
+	ec_dev->host_sleep_v1 = true;
+
+	/* For cros_ec_get_proto_info() without passthru. */
+	{
+		struct ec_response_get_protocol_info *data;
+
+		mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
+		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+
+		/*
+		 * Although it doesn't check the value, provides valid sizes so that
+		 * cros_ec_query_all() allocates din and dout correctly.
+		 */
+		data = (struct ec_response_get_protocol_info *)mock->o_data;
+		data->max_request_packet_size = 0xbe;
+		data->max_response_packet_size = 0xef;
+	}
+
+	/* For cros_ec_get_proto_info() with passthru. */
+	{
+		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 MKBP. */
+	{
+		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_addx(test, 0, EC_RES_INVALID_COMMAND, 0);
+		KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+	}
+
+	cros_ec_proto_test_query_all_pretest(test);
+	ret = cros_ec_query_all(ec_dev);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+
+	/* For cros_ec_get_proto_info() without passthru. */
+	{
+		mock = cros_kunit_ec_xfer_mock_next();
+		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
+
+		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
+		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO);
+		KUNIT_EXPECT_EQ(test, mock->msg.insize,
+				sizeof(struct ec_response_get_protocol_info));
+		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
+	}
+
+	/* For cros_ec_get_proto_info() with passthru. */
+	{
+		mock = cros_kunit_ec_xfer_mock_next();
+		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
+
+		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
+		KUNIT_EXPECT_EQ(test, mock->msg.command,
+				EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) |
+				EC_CMD_GET_PROTOCOL_INFO);
+		KUNIT_EXPECT_EQ(test, mock->msg.insize,
+				sizeof(struct ec_response_get_protocol_info));
+		KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
+	}
+
+	/* For cros_ec_get_host_command_version_mask() for MKBP. */
+	{
+		mock = cros_kunit_ec_xfer_mock_next();
+		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
+
+		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
+		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
+		KUNIT_EXPECT_EQ(test, mock->msg.insize,
+				sizeof(struct ec_response_get_cmd_versions));
+		KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_get_cmd_versions));
+	}
+
+	/* For cros_ec_get_host_command_version_mask() for host sleep v1. */
+	{
+		mock = cros_kunit_ec_xfer_mock_next();
+		KUNIT_EXPECT_PTR_NE(test, mock, NULL);
+
+		KUNIT_EXPECT_EQ(test, mock->msg.version, 0);
+		KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_CMD_VERSIONS);
+		KUNIT_EXPECT_EQ(test, mock->msg.insize,
+				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);
+	}
+}
+
 static void cros_ec_proto_test_query_all_default_wake_mask_return_error(struct kunit *test)
 {
 	struct cros_ec_proto_test_priv *priv = test->priv;
@@ -2929,8 +3118,10 @@ static struct kunit_case cros_ec_proto_test_cases[] = {
 	KUNIT_CASE(cros_ec_proto_test_query_all_no_mkbp),
 	KUNIT_CASE(cros_ec_proto_test_query_all_no_mkbp_return_error),
 	KUNIT_CASE(cros_ec_proto_test_query_all_no_mkbp_return0),
+	KUNIT_CASE(cros_ec_proto_test_query_all_no_mkbp_invalid_param),
 	KUNIT_CASE(cros_ec_proto_test_query_all_no_host_sleep),
 	KUNIT_CASE(cros_ec_proto_test_query_all_no_host_sleep_return0),
+	KUNIT_CASE(cros_ec_proto_test_query_all_no_host_sleep_return_error),
 	KUNIT_CASE(cros_ec_proto_test_query_all_default_wake_mask_return_error),
 	KUNIT_CASE(cros_ec_proto_test_query_all_default_wake_mask_return0),
 	KUNIT_CASE(cros_ec_proto_test_cmd_xfer_normal),
Signature · 1 line
2.56.1.831.g720f3f5937-goog