2022-07-18 05:11:26

by Tzung-Bi Shih

[permalink] [raw]
Subject: [PATCH v2 00/10] platform/chrome: Kunit tests and refactor for cros_ec_cmd_xfer()

The 1st patch fixes an issue that cros_kunit_ec_xfer_mock() could return
garbage bytes for `msg->result` if the mock list is empty.

The 2nd ~ 6th patches add Kunit tests and refactors for cros_ec_cmd_xfer().

The last 4 patches change the behavior a bit by altering return codes.


Asynchronous mechanism in EC protocol:
EC returns EC_RES_IN_PROGRESS if the host command needs more time to
complete. It saves the result into `saved_result` once the command
completed[1].

By design, AP should send another command EC_CMD_RESEND_RESPONSE for
getting the result from the previous pending command[2]. The mechanism
was only designed for commands that don't need responses[3].

However, the kernel code doesn't have such logic when dealing with
EC_RES_IN_PROGRESS.

The series doesn't fix it but leave it as is. I doubt there is no existing
use case.

[1]: https://crrev.com/9b80051e01872d8cae86fff999b5d31a0bea985b/common/host_command.c#113
[2]: https://crrev.com/9b80051e01872d8cae86fff999b5d31a0bea985b/common/host_command.c#748
[3]: https://crrev.com/9b80051e01872d8cae86fff999b5d31a0bea985b/common/host_command.c#126


Return value overridden in cros_ec_send_command() if asynchronous:
By original intention, cros_ec_send_command() returns number of available
bytes of input payload.

When it falls into asynchronous path (i.e. EC_RES_IN_PROGRESS), both return
value and `msg->result` will be overridden by the subsequent
EC_CMD_GET_COMMS_STATUS.

The series doesn't fix it but leave it as is.

Tzung-Bi Shih (10):
platform/chrome: cros_kunit_util: add default value for `msg->result`
platform/chrome: cros_ec_proto: add "cros_ec_" prefix to
send_command()
platform/chrome: cros_ec_proto: add Kunit tests for cros_ec_cmd_xfer()
platform/chrome: cros_ec_proto: add Kunit tests for
cros_ec_send_command()
platform/chrome: cros_ec_proto: separate cros_ec_xfer_command()
platform/chrome: cros_ec_proto: separate cros_ec_wait_until_complete()
platform/chrome: cros_ec_proto: change Kunit expectation when timed
out
platform/chrome: cros_ec_proto: return -EAGAIN when retries timed out
platform/chrome: cros_ec_proto: add Kunit test for empty payload
platform/chrome: cros_ec_proto: return -EPROTO if empty payload

drivers/platform/chrome/cros_ec_proto.c | 102 +++--
drivers/platform/chrome/cros_ec_proto_test.c | 443 +++++++++++++++++++
drivers/platform/chrome/cros_kunit_util.c | 27 +-
drivers/platform/chrome/cros_kunit_util.h | 5 +
4 files changed, 530 insertions(+), 47 deletions(-)

Changes from v1 (https://patchwork.kernel.org/project/chrome-platform/cover/[email protected]/):
- Drop "platform/chrome: cros_ec_proto: change Kunit expectation for EC errors"
and "platform/chrome: cros_ec_proto: return standard error codes for EC
errors" because they could break existing ABI.
- Add "platform/chrome: cros_kunit_util: add default value for `msg->result`".

--
2.37.0.170.g444d1eabd0-goog


2022-07-18 05:11:30

by Tzung-Bi Shih

[permalink] [raw]
Subject: [PATCH v2 01/10] platform/chrome: cros_kunit_util: add default value for `msg->result`

Add default value for `msg->result` so that it won't be garbage bytes
when the mock list is empty.

Signed-off-by: Tzung-Bi Shih <[email protected]>
---
No v1. New in the series.

drivers/platform/chrome/cros_kunit_util.c | 7 ++++++-
drivers/platform/chrome/cros_kunit_util.h | 1 +
2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/platform/chrome/cros_kunit_util.c b/drivers/platform/chrome/cros_kunit_util.c
index e031777dea87..6810d558d462 100644
--- a/drivers/platform/chrome/cros_kunit_util.c
+++ b/drivers/platform/chrome/cros_kunit_util.c
@@ -13,6 +13,8 @@
#include "cros_ec.h"
#include "cros_kunit_util.h"

+int cros_kunit_ec_xfer_mock_default_result;
+EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_default_result);
int cros_kunit_ec_xfer_mock_default_ret;
EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_default_ret);

@@ -24,8 +26,10 @@ int cros_kunit_ec_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_comman
struct ec_xfer_mock *mock;

mock = list_first_entry_or_null(&cros_kunit_ec_xfer_mock_in, struct ec_xfer_mock, list);
- if (!mock)
+ if (!mock) {
+ msg->result = cros_kunit_ec_xfer_mock_default_result;
return cros_kunit_ec_xfer_mock_default_ret;
+ }

list_del(&mock->list);

@@ -89,6 +93,7 @@ EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_next);

void cros_kunit_mock_reset(void)
{
+ cros_kunit_ec_xfer_mock_default_result = 0;
cros_kunit_ec_xfer_mock_default_ret = 0;
INIT_LIST_HEAD(&cros_kunit_ec_xfer_mock_in);
INIT_LIST_HEAD(&cros_kunit_ec_xfer_mock_out);
diff --git a/drivers/platform/chrome/cros_kunit_util.h b/drivers/platform/chrome/cros_kunit_util.h
index 79c4525f873c..79c4f259f2bb 100644
--- a/drivers/platform/chrome/cros_kunit_util.h
+++ b/drivers/platform/chrome/cros_kunit_util.h
@@ -23,6 +23,7 @@ struct ec_xfer_mock {
u32 o_data_len;
};

+extern int cros_kunit_ec_xfer_mock_default_result;
extern int cros_kunit_ec_xfer_mock_default_ret;

int cros_kunit_ec_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg);
--
2.37.0.170.g444d1eabd0-goog

2022-07-18 05:11:35

by Tzung-Bi Shih

[permalink] [raw]
Subject: [PATCH v2 03/10] platform/chrome: cros_ec_proto: add Kunit tests for cros_ec_cmd_xfer()

cros_ec_cmd_xfer() transfers the given command and data if any. It
performs some sanity checks and calls cros_ec_send_command().

Add Kunit tests for cros_ec_cmd_xfer().

Reviewed-by: Guenter Roeck <[email protected]>
Signed-off-by: Tzung-Bi Shih <[email protected]>
---
Changes from v1:
- Add R-b tag.

drivers/platform/chrome/cros_ec_proto_test.c | 149 +++++++++++++++++++
1 file changed, 149 insertions(+)

diff --git a/drivers/platform/chrome/cros_ec_proto_test.c b/drivers/platform/chrome/cros_ec_proto_test.c
index 1e2a1522c288..33721607a5b9 100644
--- a/drivers/platform/chrome/cros_ec_proto_test.c
+++ b/drivers/platform/chrome/cros_ec_proto_test.c
@@ -1535,6 +1535,151 @@ static void cros_ec_proto_test_query_all_default_wake_mask_return0(struct kunit
}
}

+static void cros_ec_proto_test_cmd_xfer_normal(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;
+ struct {
+ struct cros_ec_command msg;
+ u8 data[0x100];
+ } __packed buf;
+
+ ec_dev->max_request = 0xff;
+ ec_dev->max_response = 0xee;
+ ec_dev->max_passthru = 0xdd;
+
+ buf.msg.version = 0;
+ buf.msg.command = EC_CMD_HELLO;
+ buf.msg.insize = 4;
+ buf.msg.outsize = 2;
+ buf.data[0] = 0x55;
+ buf.data[1] = 0xaa;
+
+ {
+ u8 *data;
+
+ mock = cros_kunit_ec_xfer_mock_add(test, 4);
+ KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+
+ data = (u8 *)mock->o_data;
+ data[0] = 0xaa;
+ data[1] = 0x55;
+ data[2] = 0xcc;
+ data[3] = 0x33;
+ }
+
+ ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
+ KUNIT_EXPECT_EQ(test, ret, 4);
+
+ {
+ u8 *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_HELLO);
+ KUNIT_EXPECT_EQ(test, mock->msg.insize, 4);
+ KUNIT_EXPECT_EQ(test, mock->msg.outsize, 2);
+
+ data = (u8 *)mock->i_data;
+ KUNIT_EXPECT_EQ(test, data[0], 0x55);
+ KUNIT_EXPECT_EQ(test, data[1], 0xaa);
+
+ KUNIT_EXPECT_EQ(test, buf.data[0], 0xaa);
+ KUNIT_EXPECT_EQ(test, buf.data[1], 0x55);
+ KUNIT_EXPECT_EQ(test, buf.data[2], 0xcc);
+ KUNIT_EXPECT_EQ(test, buf.data[3], 0x33);
+ }
+}
+
+static void cros_ec_proto_test_cmd_xfer_excess_msg_insize(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;
+ struct {
+ struct cros_ec_command msg;
+ u8 data[0x100];
+ } __packed buf;
+
+ ec_dev->max_request = 0xff;
+ ec_dev->max_response = 0xee;
+ ec_dev->max_passthru = 0xdd;
+
+ buf.msg.version = 0;
+ buf.msg.command = EC_CMD_HELLO;
+ buf.msg.insize = 0xee + 1;
+ buf.msg.outsize = 2;
+
+ {
+ mock = cros_kunit_ec_xfer_mock_add(test, 0xcc);
+ KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+ }
+
+ ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
+ KUNIT_EXPECT_EQ(test, ret, 0xcc);
+
+ {
+ 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_HELLO);
+ KUNIT_EXPECT_EQ(test, mock->msg.insize, 0xee);
+ KUNIT_EXPECT_EQ(test, mock->msg.outsize, 2);
+ }
+}
+
+static void cros_ec_proto_test_cmd_xfer_excess_msg_outsize_without_passthru(struct kunit *test)
+{
+ struct cros_ec_proto_test_priv *priv = test->priv;
+ struct cros_ec_device *ec_dev = &priv->ec_dev;
+ int ret;
+ struct {
+ struct cros_ec_command msg;
+ u8 data[0x100];
+ } __packed buf;
+
+ ec_dev->max_request = 0xff;
+ ec_dev->max_response = 0xee;
+ ec_dev->max_passthru = 0xdd;
+
+ buf.msg.version = 0;
+ buf.msg.command = EC_CMD_HELLO;
+ buf.msg.insize = 4;
+ buf.msg.outsize = 0xff + 1;
+
+ ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
+ KUNIT_EXPECT_EQ(test, ret, -EMSGSIZE);
+}
+
+static void cros_ec_proto_test_cmd_xfer_excess_msg_outsize_with_passthru(struct kunit *test)
+{
+ struct cros_ec_proto_test_priv *priv = test->priv;
+ struct cros_ec_device *ec_dev = &priv->ec_dev;
+ int ret;
+ struct {
+ struct cros_ec_command msg;
+ u8 data[0x100];
+ } __packed buf;
+
+ ec_dev->max_request = 0xff;
+ ec_dev->max_response = 0xee;
+ ec_dev->max_passthru = 0xdd;
+
+ buf.msg.version = 0;
+ buf.msg.command = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) + EC_CMD_HELLO;
+ buf.msg.insize = 4;
+ buf.msg.outsize = 0xdd + 1;
+
+ ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
+ KUNIT_EXPECT_EQ(test, ret, -EMSGSIZE);
+}
+
static void cros_ec_proto_test_release(struct device *dev)
{
}
@@ -1601,6 +1746,10 @@ static struct kunit_case cros_ec_proto_test_cases[] = {
KUNIT_CASE(cros_ec_proto_test_query_all_no_host_sleep_return0),
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),
+ KUNIT_CASE(cros_ec_proto_test_cmd_xfer_excess_msg_insize),
+ KUNIT_CASE(cros_ec_proto_test_cmd_xfer_excess_msg_outsize_without_passthru),
+ KUNIT_CASE(cros_ec_proto_test_cmd_xfer_excess_msg_outsize_with_passthru),
{}
};

--
2.37.0.170.g444d1eabd0-goog

2022-07-18 05:14:46

by Tzung-Bi Shih

[permalink] [raw]
Subject: [PATCH v2 10/10] platform/chrome: cros_ec_proto: return -EPROTO if empty payload

cros_ec_wait_until_complete() sends EC_CMD_GET_COMMS_STATUS which expects
to receive sizeof(struct ec_response_get_comms_status) from
cros_ec_xfer_command().

Return -EPROTO if cros_ec_xfer_command() returns 0.

Reviewed-by: Guenter Roeck <[email protected]>
Signed-off-by: Tzung-Bi Shih <[email protected]>
---
Changes from v1:
- Add R-b tag.

drivers/platform/chrome/cros_ec_proto.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c
index 9dec475edc84..05d2e8765a66 100644
--- a/drivers/platform/chrome/cros_ec_proto.c
+++ b/drivers/platform/chrome/cros_ec_proto.c
@@ -163,6 +163,11 @@ static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev, uint32_t *
if (msg->result != EC_RES_SUCCESS)
return ret;

+ if (ret == 0) {
+ ret = -EPROTO;
+ break;
+ }
+
if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
return ret;
}
--
2.37.0.170.g444d1eabd0-goog

2022-07-18 05:15:10

by Tzung-Bi Shih

[permalink] [raw]
Subject: [PATCH v2 07/10] platform/chrome: cros_ec_proto: change Kunit expectation when timed out

While EC_COMMS_STATUS_PROCESSING flag is still on after it tries
EC_COMMAND_RETRIES times for sending EC_CMD_GET_COMMS_STATUS,
cros_ec_wait_until_complete() doesn't return an error code.

Change the expectation to an error code.

Reviewed-by: Guenter Roeck <[email protected]>
Signed-off-by: Tzung-Bi Shih <[email protected]>
---
Changes from v1:
- Add R-b tag.

drivers/platform/chrome/cros_ec_proto_test.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_proto_test.c b/drivers/platform/chrome/cros_ec_proto_test.c
index 64100fd81c6a..fbb872040711 100644
--- a/drivers/platform/chrome/cros_ec_proto_test.c
+++ b/drivers/platform/chrome/cros_ec_proto_test.c
@@ -1870,9 +1870,7 @@ static void cros_ec_proto_test_cmd_xfer_in_progress_retries_status_processing(st
}

ret = cros_ec_cmd_xfer(ec_dev, &msg);
- KUNIT_EXPECT_EQ(test, ret, sizeof(struct ec_response_get_comms_status));
-
- KUNIT_EXPECT_EQ(test, msg.result, EC_RES_SUCCESS);
+ KUNIT_EXPECT_EQ(test, ret, -EAGAIN);

/* For EC_CMD_GET_COMMS_STATUS EC_COMMAND_RETRIES times. */
KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 51);
--
2.37.0.170.g444d1eabd0-goog

2022-07-18 05:20:28

by Tzung-Bi Shih

[permalink] [raw]
Subject: [PATCH v2 09/10] platform/chrome: cros_ec_proto: add Kunit test for empty payload

cros_ec_wait_until_complete() sends EC_CMD_GET_COMMS_STATUS which expects
to receive sizeof(struct ec_response_get_comms_status) from
cros_ec_xfer_command().

Add Kunit test and expect to receive an error code when
cros_ec_xfer_command() returns 0.

Signed-off-by: Tzung-Bi Shih <[email protected]>
---
No changes from v1.

drivers/platform/chrome/cros_ec_proto_test.c | 31 ++++++++++++++++++++
1 file changed, 31 insertions(+)

diff --git a/drivers/platform/chrome/cros_ec_proto_test.c b/drivers/platform/chrome/cros_ec_proto_test.c
index fbb872040711..d76e09b8a36a 100644
--- a/drivers/platform/chrome/cros_ec_proto_test.c
+++ b/drivers/platform/chrome/cros_ec_proto_test.c
@@ -1934,6 +1934,36 @@ static void cros_ec_proto_test_cmd_xfer_in_progress_return_error(struct kunit *t
KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 2);
}

+static void cros_ec_proto_test_cmd_xfer_in_progress_return0(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;
+ struct cros_ec_command msg;
+
+ memset(&msg, 0, sizeof(msg));
+
+ ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+
+ /* For the first host command to return EC_RES_IN_PROGRESS. */
+ {
+ mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
+ KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+ }
+
+ /* For EC_CMD_GET_COMMS_STATUS. */
+ {
+ mock = cros_kunit_ec_xfer_mock_add(test, 0);
+ KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+ }
+
+ ret = cros_ec_cmd_xfer(ec_dev, &msg);
+ KUNIT_EXPECT_EQ(test, ret, -EPROTO);
+
+ KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 2);
+}
+
static void cros_ec_proto_test_release(struct device *dev)
{
}
@@ -2013,6 +2043,7 @@ static struct kunit_case cros_ec_proto_test_cases[] = {
KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_retries_status_processing),
KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_xfer_error),
KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_return_error),
+ KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_return0),
{}
};

--
2.37.0.170.g444d1eabd0-goog

2022-07-18 05:20:30

by Tzung-Bi Shih

[permalink] [raw]
Subject: [PATCH v2 08/10] platform/chrome: cros_ec_proto: return -EAGAIN when retries timed out

While EC_COMMS_STATUS_PROCESSING flag is still on after it tries
EC_COMMAND_RETRIES times for sending EC_CMD_GET_COMMS_STATUS,
cros_ec_wait_until_complete() doesn't return an error code.

Return -EAGAIN in the case instead.

Signed-off-by: Tzung-Bi Shih <[email protected]>
---
No changes from v1.

drivers/platform/chrome/cros_ec_proto.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c
index a6ad7f7956e6..9dec475edc84 100644
--- a/drivers/platform/chrome/cros_ec_proto.c
+++ b/drivers/platform/chrome/cros_ec_proto.c
@@ -167,6 +167,9 @@ static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev, uint32_t *
return ret;
}

+ if (i >= EC_COMMAND_RETRIES)
+ ret = -EAGAIN;
+
return ret;
}

--
2.37.0.170.g444d1eabd0-goog

2022-07-18 05:25:26

by Tzung-Bi Shih

[permalink] [raw]
Subject: [PATCH v2 02/10] platform/chrome: cros_ec_proto: add "cros_ec_" prefix to send_command()

To be neat, add "cros_ec_" prefix to static function send_command().

Reviewed-by: Guenter Roeck <[email protected]>
Signed-off-by: Tzung-Bi Shih <[email protected]>
---
Changes from v1:
- Add R-b tag.

drivers/platform/chrome/cros_ec_proto.c | 15 +++++++--------
1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c
index 6923ea4401e5..b02fd1414e52 100644
--- a/drivers/platform/chrome/cros_ec_proto.c
+++ b/drivers/platform/chrome/cros_ec_proto.c
@@ -107,8 +107,7 @@ static int prepare_tx_legacy(struct cros_ec_device *ec_dev,
return EC_MSG_TX_PROTO_BYTES + msg->outsize;
}

-static int send_command(struct cros_ec_device *ec_dev,
- struct cros_ec_command *msg)
+static int cros_ec_send_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
{
int ret;
int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
@@ -255,7 +254,7 @@ static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev, uint3
msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
msg->insize = sizeof(*r);

- ret = send_command(ec_dev, msg);
+ ret = cros_ec_send_command(ec_dev, msg);
if (ret < 0)
goto exit;

@@ -295,7 +294,7 @@ static int cros_ec_get_proto_info(struct cros_ec_device *ec_dev, int devidx)
msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
msg->insize = sizeof(*info);

- ret = send_command(ec_dev, msg);
+ ret = cros_ec_send_command(ec_dev, msg);
/*
* Send command once again when timeout occurred.
* Fingerprint MCU (FPMCU) is restarted during system boot which
@@ -304,7 +303,7 @@ static int cros_ec_get_proto_info(struct cros_ec_device *ec_dev, int devidx)
* attempt because we waited at least EC_MSG_DEADLINE_MS.
*/
if (ret == -ETIMEDOUT)
- ret = send_command(ec_dev, msg);
+ ret = cros_ec_send_command(ec_dev, msg);

if (ret < 0) {
dev_dbg(ec_dev->dev,
@@ -376,7 +375,7 @@ static int cros_ec_get_proto_info_legacy(struct cros_ec_device *ec_dev)
params = (struct ec_params_hello *)msg->data;
params->in_data = 0xa0b0c0d0;

- ret = send_command(ec_dev, msg);
+ ret = cros_ec_send_command(ec_dev, msg);
if (ret < 0) {
dev_dbg(ec_dev->dev, "EC failed to respond to v2 hello: %d\n", ret);
goto exit;
@@ -453,7 +452,7 @@ static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
pver = (struct ec_params_get_cmd_versions *)msg->data;
pver->cmd = cmd;

- ret = send_command(ec_dev, msg);
+ ret = cros_ec_send_command(ec_dev, msg);
if (ret < 0)
goto exit;

@@ -634,7 +633,7 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
}
}

- ret = send_command(ec_dev, msg);
+ ret = cros_ec_send_command(ec_dev, msg);
mutex_unlock(&ec_dev->lock);

return ret;
--
2.37.0.170.g444d1eabd0-goog

2022-07-18 05:28:46

by Tzung-Bi Shih

[permalink] [raw]
Subject: [PATCH v2 05/10] platform/chrome: cros_ec_proto: separate cros_ec_xfer_command()

cros_ec_send_command() has extra logic to handle EC_RES_IN_PROGRESS.
Separate the command transfer part into cros_ec_xfer_command() so
that other functions can re-use it.

Reviewed-by: Guenter Roeck <[email protected]>
Signed-off-by: Tzung-Bi Shih <[email protected]>
---
Changes from v1:
- Add R-b tag.

drivers/platform/chrome/cros_ec_proto.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c
index b02fd1414e52..0cec013be3d3 100644
--- a/drivers/platform/chrome/cros_ec_proto.c
+++ b/drivers/platform/chrome/cros_ec_proto.c
@@ -107,7 +107,7 @@ static int prepare_tx_legacy(struct cros_ec_device *ec_dev,
return EC_MSG_TX_PROTO_BYTES + msg->outsize;
}

-static int cros_ec_send_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
+static int cros_ec_xfer_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
{
int ret;
int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
@@ -123,14 +123,21 @@ static int cros_ec_send_command(struct cros_ec_device *ec_dev, struct cros_ec_co
* the EC is trying to use protocol v2, on an underlying
* communication mechanism that does not support v2.
*/
- dev_err_once(ec_dev->dev,
- "missing EC transfer API, cannot send command\n");
+ dev_err_once(ec_dev->dev, "missing EC transfer API, cannot send command\n");
return -EIO;
}

trace_cros_ec_request_start(msg);
ret = (*xfer_fxn)(ec_dev, msg);
trace_cros_ec_request_done(msg, ret);
+
+ return ret;
+}
+
+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);
+
if (msg->result == EC_RES_IN_PROGRESS) {
int i;
struct cros_ec_command *status_msg;
--
2.37.0.170.g444d1eabd0-goog

2022-07-18 05:29:52

by Tzung-Bi Shih

[permalink] [raw]
Subject: [PATCH v2 04/10] platform/chrome: cros_ec_proto: add Kunit tests for cros_ec_send_command()

cros_ec_cmd_xfer() is the only exported function that calls static
function cros_ec_send_command().

Add Kunit tests for cros_ec_send_command() through calling
cros_ec_cmd_xfer().

Reviewed-by: Guenter Roeck <[email protected]>
Signed-off-by: Tzung-Bi Shih <[email protected]>
---
Changes from v1:
- Add R-b tag.

drivers/platform/chrome/cros_ec_proto_test.c | 265 +++++++++++++++++++
drivers/platform/chrome/cros_kunit_util.c | 20 ++
drivers/platform/chrome/cros_kunit_util.h | 4 +
3 files changed, 289 insertions(+)

diff --git a/drivers/platform/chrome/cros_ec_proto_test.c b/drivers/platform/chrome/cros_ec_proto_test.c
index 33721607a5b9..64100fd81c6a 100644
--- a/drivers/platform/chrome/cros_ec_proto_test.c
+++ b/drivers/platform/chrome/cros_ec_proto_test.c
@@ -1680,6 +1680,262 @@ static void cros_ec_proto_test_cmd_xfer_excess_msg_outsize_with_passthru(struct
KUNIT_EXPECT_EQ(test, ret, -EMSGSIZE);
}

+static void cros_ec_proto_test_cmd_xfer_protocol_v3_normal(struct kunit *test)
+{
+ struct cros_ec_proto_test_priv *priv = test->priv;
+ struct cros_ec_device *ec_dev = &priv->ec_dev;
+ int ret;
+ struct cros_ec_command msg;
+
+ memset(&msg, 0, sizeof(msg));
+
+ ec_dev->proto_version = 3;
+ ec_dev->cmd_xfer = cros_kunit_ec_cmd_xfer_mock;
+ ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+
+ ret = cros_ec_cmd_xfer(ec_dev, &msg);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+
+ KUNIT_EXPECT_EQ(test, cros_kunit_ec_cmd_xfer_mock_called, 0);
+ KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 1);
+}
+
+static void cros_ec_proto_test_cmd_xfer_protocol_v3_no_op(struct kunit *test)
+{
+ struct cros_ec_proto_test_priv *priv = test->priv;
+ struct cros_ec_device *ec_dev = &priv->ec_dev;
+ int ret;
+ struct cros_ec_command msg;
+
+ memset(&msg, 0, sizeof(msg));
+
+ ec_dev->proto_version = 3;
+ ec_dev->cmd_xfer = cros_kunit_ec_cmd_xfer_mock;
+ ec_dev->pkt_xfer = NULL;
+
+ ret = cros_ec_cmd_xfer(ec_dev, &msg);
+ KUNIT_EXPECT_EQ(test, ret, -EIO);
+}
+
+static void cros_ec_proto_test_cmd_xfer_protocol_v2_normal(struct kunit *test)
+{
+ struct cros_ec_proto_test_priv *priv = test->priv;
+ struct cros_ec_device *ec_dev = &priv->ec_dev;
+ int ret;
+ struct cros_ec_command msg;
+
+ memset(&msg, 0, sizeof(msg));
+
+ ec_dev->proto_version = 2;
+ ec_dev->cmd_xfer = cros_kunit_ec_cmd_xfer_mock;
+ ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+
+ ret = cros_ec_cmd_xfer(ec_dev, &msg);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+
+ KUNIT_EXPECT_EQ(test, cros_kunit_ec_cmd_xfer_mock_called, 1);
+ KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 0);
+}
+
+static void cros_ec_proto_test_cmd_xfer_protocol_v2_no_op(struct kunit *test)
+{
+ struct cros_ec_proto_test_priv *priv = test->priv;
+ struct cros_ec_device *ec_dev = &priv->ec_dev;
+ int ret;
+ struct cros_ec_command msg;
+
+ memset(&msg, 0, sizeof(msg));
+
+ ec_dev->proto_version = 2;
+ ec_dev->cmd_xfer = NULL;
+ ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+
+ ret = cros_ec_cmd_xfer(ec_dev, &msg);
+ KUNIT_EXPECT_EQ(test, ret, -EIO);
+}
+
+static void cros_ec_proto_test_cmd_xfer_in_progress_normal(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;
+ struct cros_ec_command msg;
+
+ memset(&msg, 0, sizeof(msg));
+
+ ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+
+ /* For the first host command to return EC_RES_IN_PROGRESS. */
+ {
+ mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
+ KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+ }
+
+ /* For EC_CMD_GET_COMMS_STATUS. */
+ {
+ struct ec_response_get_comms_status *data;
+
+ mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
+ KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+
+ data = (struct ec_response_get_comms_status *)mock->o_data;
+ data->flags = 0;
+ }
+
+ ret = cros_ec_cmd_xfer(ec_dev, &msg);
+ KUNIT_EXPECT_EQ(test, ret, sizeof(struct ec_response_get_comms_status));
+
+ KUNIT_EXPECT_EQ(test, msg.result, EC_RES_SUCCESS);
+
+ /* For the first host command to return EC_RES_IN_PROGRESS. */
+ {
+ mock = cros_kunit_ec_xfer_mock_next();
+ KUNIT_EXPECT_PTR_NE(test, mock, NULL);
+ }
+
+ /* For EC_CMD_GET_COMMS_STATUS. */
+ {
+ 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_COMMS_STATUS);
+ KUNIT_EXPECT_EQ(test, mock->msg.insize,
+ sizeof(struct ec_response_get_comms_status));
+ KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0);
+ }
+
+ KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 2);
+}
+
+static void cros_ec_proto_test_cmd_xfer_in_progress_retries_eagain(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;
+ struct cros_ec_command msg;
+
+ memset(&msg, 0, sizeof(msg));
+
+ ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+
+ /* For the first host command to return EC_RES_IN_PROGRESS. */
+ {
+ mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
+ KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+ }
+
+ /* For EC_CMD_GET_COMMS_STATUS EC_COMMAND_RETRIES times. */
+ cros_kunit_ec_xfer_mock_default_ret = -EAGAIN;
+
+ ret = cros_ec_cmd_xfer(ec_dev, &msg);
+ KUNIT_EXPECT_EQ(test, ret, -EAGAIN);
+
+ /* For EC_CMD_GET_COMMS_STATUS EC_COMMAND_RETRIES times. */
+ KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 51);
+}
+
+static void cros_ec_proto_test_cmd_xfer_in_progress_retries_status_processing(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;
+ struct cros_ec_command msg;
+
+ memset(&msg, 0, sizeof(msg));
+
+ ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+
+ /* For the first host command to return EC_RES_IN_PROGRESS. */
+ {
+ mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
+ KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+ }
+
+ /* For EC_CMD_GET_COMMS_STATUS EC_COMMAND_RETRIES times. */
+ {
+ struct ec_response_get_comms_status *data;
+ int i;
+
+ for (i = 0; i < 50; ++i) {
+ mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data));
+ KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+
+ data = (struct ec_response_get_comms_status *)mock->o_data;
+ data->flags |= EC_COMMS_STATUS_PROCESSING;
+ }
+ }
+
+ ret = cros_ec_cmd_xfer(ec_dev, &msg);
+ KUNIT_EXPECT_EQ(test, ret, sizeof(struct ec_response_get_comms_status));
+
+ KUNIT_EXPECT_EQ(test, msg.result, EC_RES_SUCCESS);
+
+ /* For EC_CMD_GET_COMMS_STATUS EC_COMMAND_RETRIES times. */
+ KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 51);
+}
+
+static void cros_ec_proto_test_cmd_xfer_in_progress_xfer_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;
+ struct cros_ec_command msg;
+
+ memset(&msg, 0, sizeof(msg));
+
+ /* For the first host command to return EC_RES_IN_PROGRESS. */
+ {
+ mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
+ KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+ }
+
+ /* For EC_CMD_GET_COMMS_STATUS. */
+ {
+ mock = cros_kunit_ec_xfer_mock_addx(test, -EIO, EC_RES_SUCCESS, 0);
+ KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+ }
+
+ ret = cros_ec_cmd_xfer(ec_dev, &msg);
+ KUNIT_EXPECT_EQ(test, ret, -EIO);
+}
+
+static void cros_ec_proto_test_cmd_xfer_in_progress_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;
+ struct cros_ec_command msg;
+
+ memset(&msg, 0, sizeof(msg));
+
+ ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
+
+ /* For the first host command to return EC_RES_IN_PROGRESS. */
+ {
+ mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
+ KUNIT_ASSERT_PTR_NE(test, mock, NULL);
+ }
+
+ /* For EC_CMD_GET_COMMS_STATUS. */
+ {
+ mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 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, msg.result, EC_RES_INVALID_COMMAND);
+
+ KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 2);
+}
+
static void cros_ec_proto_test_release(struct device *dev)
{
}
@@ -1750,6 +2006,15 @@ static struct kunit_case cros_ec_proto_test_cases[] = {
KUNIT_CASE(cros_ec_proto_test_cmd_xfer_excess_msg_insize),
KUNIT_CASE(cros_ec_proto_test_cmd_xfer_excess_msg_outsize_without_passthru),
KUNIT_CASE(cros_ec_proto_test_cmd_xfer_excess_msg_outsize_with_passthru),
+ KUNIT_CASE(cros_ec_proto_test_cmd_xfer_protocol_v3_normal),
+ 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_in_progress_normal),
+ KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_retries_eagain),
+ KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_retries_status_processing),
+ KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_xfer_error),
+ KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_return_error),
{}
};

diff --git a/drivers/platform/chrome/cros_kunit_util.c b/drivers/platform/chrome/cros_kunit_util.c
index 6810d558d462..e6a0a1b4ed4b 100644
--- a/drivers/platform/chrome/cros_kunit_util.c
+++ b/drivers/platform/chrome/cros_kunit_util.c
@@ -17,6 +17,10 @@ int cros_kunit_ec_xfer_mock_default_result;
EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_default_result);
int cros_kunit_ec_xfer_mock_default_ret;
EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_default_ret);
+int cros_kunit_ec_cmd_xfer_mock_called;
+EXPORT_SYMBOL_GPL(cros_kunit_ec_cmd_xfer_mock_called);
+int cros_kunit_ec_pkt_xfer_mock_called;
+EXPORT_SYMBOL_GPL(cros_kunit_ec_pkt_xfer_mock_called);

static struct list_head cros_kunit_ec_xfer_mock_in;
static struct list_head cros_kunit_ec_xfer_mock_out;
@@ -50,6 +54,20 @@ int cros_kunit_ec_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_comman
}
EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock);

+int cros_kunit_ec_cmd_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
+{
+ ++cros_kunit_ec_cmd_xfer_mock_called;
+ return cros_kunit_ec_xfer_mock(ec_dev, msg);
+}
+EXPORT_SYMBOL_GPL(cros_kunit_ec_cmd_xfer_mock);
+
+int cros_kunit_ec_pkt_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
+{
+ ++cros_kunit_ec_pkt_xfer_mock_called;
+ return cros_kunit_ec_xfer_mock(ec_dev, msg);
+}
+EXPORT_SYMBOL_GPL(cros_kunit_ec_pkt_xfer_mock);
+
struct ec_xfer_mock *cros_kunit_ec_xfer_mock_add(struct kunit *test, size_t size)
{
return cros_kunit_ec_xfer_mock_addx(test, size, EC_RES_SUCCESS, size);
@@ -95,6 +113,8 @@ void cros_kunit_mock_reset(void)
{
cros_kunit_ec_xfer_mock_default_result = 0;
cros_kunit_ec_xfer_mock_default_ret = 0;
+ cros_kunit_ec_cmd_xfer_mock_called = 0;
+ cros_kunit_ec_pkt_xfer_mock_called = 0;
INIT_LIST_HEAD(&cros_kunit_ec_xfer_mock_in);
INIT_LIST_HEAD(&cros_kunit_ec_xfer_mock_out);
}
diff --git a/drivers/platform/chrome/cros_kunit_util.h b/drivers/platform/chrome/cros_kunit_util.h
index 79c4f259f2bb..85e2974fd048 100644
--- a/drivers/platform/chrome/cros_kunit_util.h
+++ b/drivers/platform/chrome/cros_kunit_util.h
@@ -25,8 +25,12 @@ struct ec_xfer_mock {

extern int cros_kunit_ec_xfer_mock_default_result;
extern int cros_kunit_ec_xfer_mock_default_ret;
+extern int cros_kunit_ec_cmd_xfer_mock_called;
+extern int cros_kunit_ec_pkt_xfer_mock_called;

int cros_kunit_ec_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg);
+int cros_kunit_ec_cmd_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg);
+int cros_kunit_ec_pkt_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg);
struct ec_xfer_mock *cros_kunit_ec_xfer_mock_add(struct kunit *test, size_t size);
struct ec_xfer_mock *cros_kunit_ec_xfer_mock_addx(struct kunit *test,
int ret, int result, size_t size);
--
2.37.0.170.g444d1eabd0-goog

2022-07-18 05:38:06

by Tzung-Bi Shih

[permalink] [raw]
Subject: [PATCH v2 06/10] platform/chrome: cros_ec_proto: separate cros_ec_wait_until_complete()

EC returns EC_RES_IN_PROGRESS if the host command needs more time to
complete. Whenever receives the return code, cros_ec_send_command()
sends EC_CMD_GET_COMMS_STATUS to query the command status.

Separate cros_ec_wait_until_complete() from cros_ec_send_command().
It sends EC_CMD_GET_COMMS_STATUS and waits until the previous command
was completed, or encountered error, or timed out.

Signed-off-by: Tzung-Bi Shih <[email protected]>
---
Changes from v1:
- Allocate buffer in cros_ec_wait_until_complete() statically.
- Use `return ret` instead of `break` to make the intent explicit.

drivers/platform/chrome/cros_ec_proto.c | 74 ++++++++++++-------------
1 file changed, 35 insertions(+), 39 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c
index 0cec013be3d3..a6ad7f7956e6 100644
--- a/drivers/platform/chrome/cros_ec_proto.c
+++ b/drivers/platform/chrome/cros_ec_proto.c
@@ -134,56 +134,52 @@ static int cros_ec_xfer_command(struct cros_ec_device *ec_dev, struct cros_ec_co
return ret;
}

-static int cros_ec_send_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
+static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev, uint32_t *result)
{
- int ret = cros_ec_xfer_command(ec_dev, msg);
+ struct {
+ struct cros_ec_command msg;
+ struct ec_response_get_comms_status status;
+ } __packed buf;
+ struct cros_ec_command *msg = &buf.msg;
+ struct ec_response_get_comms_status *status = &buf.status;
+ int ret = 0, i;

- if (msg->result == EC_RES_IN_PROGRESS) {
- int i;
- struct cros_ec_command *status_msg;
- struct ec_response_get_comms_status *status;
+ msg->version = 0;
+ msg->command = EC_CMD_GET_COMMS_STATUS;
+ msg->insize = sizeof(*status);
+ msg->outsize = 0;

- status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
- GFP_KERNEL);
- if (!status_msg)
- return -ENOMEM;
+ /* Query the EC's status until it's no longer busy or we encounter an error. */
+ for (i = 0; i < EC_COMMAND_RETRIES; ++i) {
+ usleep_range(10000, 11000);

- status_msg->version = 0;
- status_msg->command = EC_CMD_GET_COMMS_STATUS;
- status_msg->insize = sizeof(*status);
- status_msg->outsize = 0;
+ ret = cros_ec_xfer_command(ec_dev, msg);
+ if (ret == -EAGAIN)
+ continue;
+ if (ret < 0)
+ return ret;

- /*
- * Query the EC's status until it's no longer busy or
- * we encounter an error.
- */
- for (i = 0; i < EC_COMMAND_RETRIES; i++) {
- usleep_range(10000, 11000);
-
- trace_cros_ec_request_start(status_msg);
- ret = (*xfer_fxn)(ec_dev, status_msg);
- trace_cros_ec_request_done(status_msg, ret);
- if (ret == -EAGAIN)
- continue;
- if (ret < 0)
- break;
-
- msg->result = status_msg->result;
- if (status_msg->result != EC_RES_SUCCESS)
- break;
-
- status = (struct ec_response_get_comms_status *)
- status_msg->data;
- if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
- break;
- }
+ *result = msg->result;
+ if (msg->result != EC_RES_SUCCESS)
+ return ret;

- kfree(status_msg);
+ if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
+ return ret;
}

return ret;
}

+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);
+
+ if (msg->result == EC_RES_IN_PROGRESS)
+ ret = cros_ec_wait_until_complete(ec_dev, &msg->result);
+
+ return ret;
+}
+
/**
* cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
* @ec_dev: Device to register.
--
2.37.0.170.g444d1eabd0-goog

2022-07-18 13:44:49

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCH v2 08/10] platform/chrome: cros_ec_proto: return -EAGAIN when retries timed out

On Sun, Jul 17, 2022 at 10:10 PM Tzung-Bi Shih <[email protected]> wrote:
>
> While EC_COMMS_STATUS_PROCESSING flag is still on after it tries
> EC_COMMAND_RETRIES times for sending EC_CMD_GET_COMMS_STATUS,
> cros_ec_wait_until_complete() doesn't return an error code.
>
> Return -EAGAIN in the case instead.
>
> Signed-off-by: Tzung-Bi Shih <[email protected]>

Reviewed-by: Guenter Roeck <[email protected]>

> ---
> No changes from v1.
>
> drivers/platform/chrome/cros_ec_proto.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c
> index a6ad7f7956e6..9dec475edc84 100644
> --- a/drivers/platform/chrome/cros_ec_proto.c
> +++ b/drivers/platform/chrome/cros_ec_proto.c
> @@ -167,6 +167,9 @@ static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev, uint32_t *
> return ret;
> }
>
> + if (i >= EC_COMMAND_RETRIES)
> + ret = -EAGAIN;
> +
> return ret;
> }
>
> --
> 2.37.0.170.g444d1eabd0-goog
>

2022-07-18 14:15:11

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCH v2 01/10] platform/chrome: cros_kunit_util: add default value for `msg->result`

On Sun, Jul 17, 2022 at 10:10 PM Tzung-Bi Shih <[email protected]> wrote:
>
> Add default value for `msg->result` so that it won't be garbage bytes
> when the mock list is empty.
>
> Signed-off-by: Tzung-Bi Shih <[email protected]>
> ---
> No v1. New in the series.
>
> drivers/platform/chrome/cros_kunit_util.c | 7 ++++++-
> drivers/platform/chrome/cros_kunit_util.h | 1 +
> 2 files changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/platform/chrome/cros_kunit_util.c b/drivers/platform/chrome/cros_kunit_util.c
> index e031777dea87..6810d558d462 100644
> --- a/drivers/platform/chrome/cros_kunit_util.c
> +++ b/drivers/platform/chrome/cros_kunit_util.c
> @@ -13,6 +13,8 @@
> #include "cros_ec.h"
> #include "cros_kunit_util.h"
>
> +int cros_kunit_ec_xfer_mock_default_result;
> +EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_default_result);

Is this needed as a global variable and, if so, does it really have to
be exported ?

> int cros_kunit_ec_xfer_mock_default_ret;
> EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_default_ret);

Same here, really, only I didn't notice before.

Thanks,
Guenter

>
> @@ -24,8 +26,10 @@ int cros_kunit_ec_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_comman
> struct ec_xfer_mock *mock;
>
> mock = list_first_entry_or_null(&cros_kunit_ec_xfer_mock_in, struct ec_xfer_mock, list);
> - if (!mock)
> + if (!mock) {
> + msg->result = cros_kunit_ec_xfer_mock_default_result;
> return cros_kunit_ec_xfer_mock_default_ret;
> + }
>
> list_del(&mock->list);
>
> @@ -89,6 +93,7 @@ EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_next);
>
> void cros_kunit_mock_reset(void)
> {
> + cros_kunit_ec_xfer_mock_default_result = 0;
> cros_kunit_ec_xfer_mock_default_ret = 0;
> INIT_LIST_HEAD(&cros_kunit_ec_xfer_mock_in);
> INIT_LIST_HEAD(&cros_kunit_ec_xfer_mock_out);
> diff --git a/drivers/platform/chrome/cros_kunit_util.h b/drivers/platform/chrome/cros_kunit_util.h
> index 79c4525f873c..79c4f259f2bb 100644
> --- a/drivers/platform/chrome/cros_kunit_util.h
> +++ b/drivers/platform/chrome/cros_kunit_util.h
> @@ -23,6 +23,7 @@ struct ec_xfer_mock {
> u32 o_data_len;
> };
>
> +extern int cros_kunit_ec_xfer_mock_default_result;
> extern int cros_kunit_ec_xfer_mock_default_ret;
>
> int cros_kunit_ec_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg);
> --
> 2.37.0.170.g444d1eabd0-goog
>

2022-07-18 14:17:17

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCH v2 06/10] platform/chrome: cros_ec_proto: separate cros_ec_wait_until_complete()

On Sun, Jul 17, 2022 at 10:10 PM Tzung-Bi Shih <[email protected]> wrote:
>
> EC returns EC_RES_IN_PROGRESS if the host command needs more time to
> complete. Whenever receives the return code, cros_ec_send_command()
> sends EC_CMD_GET_COMMS_STATUS to query the command status.
>
> Separate cros_ec_wait_until_complete() from cros_ec_send_command().
> It sends EC_CMD_GET_COMMS_STATUS and waits until the previous command
> was completed, or encountered error, or timed out.
>
> Signed-off-by: Tzung-Bi Shih <[email protected]>

Reviewed-by: Guenter Roeck <[email protected]>

> ---
> Changes from v1:
> - Allocate buffer in cros_ec_wait_until_complete() statically.
> - Use `return ret` instead of `break` to make the intent explicit.
>
> drivers/platform/chrome/cros_ec_proto.c | 74 ++++++++++++-------------
> 1 file changed, 35 insertions(+), 39 deletions(-)
>
> diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c
> index 0cec013be3d3..a6ad7f7956e6 100644
> --- a/drivers/platform/chrome/cros_ec_proto.c
> +++ b/drivers/platform/chrome/cros_ec_proto.c
> @@ -134,56 +134,52 @@ static int cros_ec_xfer_command(struct cros_ec_device *ec_dev, struct cros_ec_co
> return ret;
> }
>
> -static int cros_ec_send_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
> +static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev, uint32_t *result)
> {
> - int ret = cros_ec_xfer_command(ec_dev, msg);
> + struct {
> + struct cros_ec_command msg;
> + struct ec_response_get_comms_status status;
> + } __packed buf;
> + struct cros_ec_command *msg = &buf.msg;
> + struct ec_response_get_comms_status *status = &buf.status;
> + int ret = 0, i;
>
> - if (msg->result == EC_RES_IN_PROGRESS) {
> - int i;
> - struct cros_ec_command *status_msg;
> - struct ec_response_get_comms_status *status;
> + msg->version = 0;
> + msg->command = EC_CMD_GET_COMMS_STATUS;
> + msg->insize = sizeof(*status);
> + msg->outsize = 0;
>
> - status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
> - GFP_KERNEL);
> - if (!status_msg)
> - return -ENOMEM;
> + /* Query the EC's status until it's no longer busy or we encounter an error. */
> + for (i = 0; i < EC_COMMAND_RETRIES; ++i) {
> + usleep_range(10000, 11000);
>
> - status_msg->version = 0;
> - status_msg->command = EC_CMD_GET_COMMS_STATUS;
> - status_msg->insize = sizeof(*status);
> - status_msg->outsize = 0;
> + ret = cros_ec_xfer_command(ec_dev, msg);
> + if (ret == -EAGAIN)
> + continue;
> + if (ret < 0)
> + return ret;
>
> - /*
> - * Query the EC's status until it's no longer busy or
> - * we encounter an error.
> - */
> - for (i = 0; i < EC_COMMAND_RETRIES; i++) {
> - usleep_range(10000, 11000);
> -
> - trace_cros_ec_request_start(status_msg);
> - ret = (*xfer_fxn)(ec_dev, status_msg);
> - trace_cros_ec_request_done(status_msg, ret);
> - if (ret == -EAGAIN)
> - continue;
> - if (ret < 0)
> - break;
> -
> - msg->result = status_msg->result;
> - if (status_msg->result != EC_RES_SUCCESS)
> - break;
> -
> - status = (struct ec_response_get_comms_status *)
> - status_msg->data;
> - if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
> - break;
> - }
> + *result = msg->result;
> + if (msg->result != EC_RES_SUCCESS)
> + return ret;
>
> - kfree(status_msg);
> + if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
> + return ret;
> }
>
> return ret;
> }
>
> +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);
> +
> + if (msg->result == EC_RES_IN_PROGRESS)
> + ret = cros_ec_wait_until_complete(ec_dev, &msg->result);
> +
> + return ret;
> +}
> +
> /**
> * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
> * @ec_dev: Device to register.
> --
> 2.37.0.170.g444d1eabd0-goog
>

2022-07-18 14:34:04

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCH v2 09/10] platform/chrome: cros_ec_proto: add Kunit test for empty payload

On Sun, Jul 17, 2022 at 10:10 PM Tzung-Bi Shih <[email protected]> wrote:
>
> cros_ec_wait_until_complete() sends EC_CMD_GET_COMMS_STATUS which expects
> to receive sizeof(struct ec_response_get_comms_status) from
> cros_ec_xfer_command().
>
> Add Kunit test and expect to receive an error code when
> cros_ec_xfer_command() returns 0.
>
> Signed-off-by: Tzung-Bi Shih <[email protected]>

Reviewed-by: Guenter Roeck <[email protected]>

> ---
> No changes from v1.
>
> drivers/platform/chrome/cros_ec_proto_test.c | 31 ++++++++++++++++++++
> 1 file changed, 31 insertions(+)
>
> diff --git a/drivers/platform/chrome/cros_ec_proto_test.c b/drivers/platform/chrome/cros_ec_proto_test.c
> index fbb872040711..d76e09b8a36a 100644
> --- a/drivers/platform/chrome/cros_ec_proto_test.c
> +++ b/drivers/platform/chrome/cros_ec_proto_test.c
> @@ -1934,6 +1934,36 @@ static void cros_ec_proto_test_cmd_xfer_in_progress_return_error(struct kunit *t
> KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 2);
> }
>
> +static void cros_ec_proto_test_cmd_xfer_in_progress_return0(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;
> + struct cros_ec_command msg;
> +
> + memset(&msg, 0, sizeof(msg));
> +
> + ec_dev->pkt_xfer = cros_kunit_ec_pkt_xfer_mock;
> +
> + /* For the first host command to return EC_RES_IN_PROGRESS. */
> + {
> + mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_IN_PROGRESS, 0);
> + KUNIT_ASSERT_PTR_NE(test, mock, NULL);
> + }
> +
> + /* For EC_CMD_GET_COMMS_STATUS. */
> + {
> + mock = cros_kunit_ec_xfer_mock_add(test, 0);
> + KUNIT_ASSERT_PTR_NE(test, mock, NULL);
> + }
> +
> + ret = cros_ec_cmd_xfer(ec_dev, &msg);
> + KUNIT_EXPECT_EQ(test, ret, -EPROTO);
> +
> + KUNIT_EXPECT_EQ(test, cros_kunit_ec_pkt_xfer_mock_called, 2);
> +}
> +
> static void cros_ec_proto_test_release(struct device *dev)
> {
> }
> @@ -2013,6 +2043,7 @@ static struct kunit_case cros_ec_proto_test_cases[] = {
> KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_retries_status_processing),
> KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_xfer_error),
> KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_return_error),
> + KUNIT_CASE(cros_ec_proto_test_cmd_xfer_in_progress_return0),
> {}
> };
>
> --
> 2.37.0.170.g444d1eabd0-goog
>

2022-07-19 04:36:48

by Tzung-Bi Shih

[permalink] [raw]
Subject: Re: [PATCH v2 01/10] platform/chrome: cros_kunit_util: add default value for `msg->result`

On Mon, Jul 18, 2022 at 06:35:42AM -0700, Guenter Roeck wrote:
> On Sun, Jul 17, 2022 at 10:10 PM Tzung-Bi Shih <[email protected]> wrote:
> > +int cros_kunit_ec_xfer_mock_default_result;
> > +EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_default_result);
>
> Is this needed as a global variable and, if so, does it really have to
> be exported ?
>
> > int cros_kunit_ec_xfer_mock_default_ret;
> > EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_default_ret);
>
> Same here, really, only I didn't notice before.

Global variables: I'm afraid yes. They should be accessible to test cases
(e.g. drivers/platform/chrome/cros_ec_proto_test.c).

Exported: I'm not sure if I fixed it in a proper way. They were added for
fixing https://lkml.org/lkml/2022/6/2/452. When CONFIG_CROS_KUNIT=m and
CONFIG_CROS_EC_PROTO_KUNIT_TEST=m, cros_ec_proto_test.c still needs to access
them (in cros_kunit_util.c).

2022-07-19 21:38:47

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCH v2 01/10] platform/chrome: cros_kunit_util: add default value for `msg->result`

On Mon, Jul 18, 2022 at 9:00 PM Tzung-Bi Shih <[email protected]> wrote:
>
> On Mon, Jul 18, 2022 at 06:35:42AM -0700, Guenter Roeck wrote:
> > On Sun, Jul 17, 2022 at 10:10 PM Tzung-Bi Shih <[email protected]> wrote:
> > > +int cros_kunit_ec_xfer_mock_default_result;
> > > +EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_default_result);
> >
> > Is this needed as a global variable and, if so, does it really have to
> > be exported ?
> >
> > > int cros_kunit_ec_xfer_mock_default_ret;
> > > EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_default_ret);
> >
> > Same here, really, only I didn't notice before.
>
> Global variables: I'm afraid yes. They should be accessible to test cases
> (e.g. drivers/platform/chrome/cros_ec_proto_test.c).
>

Hmm, I don't see where that is used. Either case, even if the
variables are supposed to be used from cros_ec_proto_test.o, I don't
see why cros_ec_proto_test.o and cros_kunit_util.o need to be separate
modules. Can you combine them into a single module ? That would avoid
the exports.

Thanks,
Guenter

> Exported: I'm not sure if I fixed it in a proper way. They were added for
> fixing https://lkml.org/lkml/2022/6/2/452. When CONFIG_CROS_KUNIT=m and
> CONFIG_CROS_EC_PROTO_KUNIT_TEST=m, cros_ec_proto_test.c still needs to access
> them (in cros_kunit_util.c).

2022-07-20 01:52:35

by Tzung-Bi Shih

[permalink] [raw]
Subject: Re: [PATCH v2 01/10] platform/chrome: cros_kunit_util: add default value for `msg->result`

On Tue, Jul 19, 2022 at 02:27:49PM -0700, Guenter Roeck wrote:
> On Mon, Jul 18, 2022 at 9:00 PM Tzung-Bi Shih <[email protected]> wrote:
> >
> > On Mon, Jul 18, 2022 at 06:35:42AM -0700, Guenter Roeck wrote:
> > > On Sun, Jul 17, 2022 at 10:10 PM Tzung-Bi Shih <[email protected]> wrote:
> > > > +int cros_kunit_ec_xfer_mock_default_result;
> > > > +EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_default_result);
> > >
> > > Is this needed as a global variable and, if so, does it really have to
> > > be exported ?
> > >
> > > > int cros_kunit_ec_xfer_mock_default_ret;
> > > > EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_default_ret);
> > >
> > > Same here, really, only I didn't notice before.
> >
> > Global variables: I'm afraid yes. They should be accessible to test cases
> > (e.g. drivers/platform/chrome/cros_ec_proto_test.c).
> >
>
> Hmm, I don't see where that is used. Either case, even if the
> variables are supposed to be used from cros_ec_proto_test.o, I don't
> see why cros_ec_proto_test.o and cros_kunit_util.o need to be separate
> modules. Can you combine them into a single module ? That would avoid
> the exports.

Ack. I realized these shouldn't be in current series. Will separate them
into an indepedent series.

Subject: Re: [PATCH v2 00/10] platform/chrome: Kunit tests and refactor for cros_ec_cmd_xfer()

Hello:

This series was applied to chrome-platform/linux.git (for-kernelci)
by Tzung-Bi Shih <[email protected]>:

On Mon, 18 Jul 2022 05:09:04 +0000 you wrote:
> The 1st patch fixes an issue that cros_kunit_ec_xfer_mock() could return
> garbage bytes for `msg->result` if the mock list is empty.
>
> The 2nd ~ 6th patches add Kunit tests and refactors for cros_ec_cmd_xfer().
>
> The last 4 patches change the behavior a bit by altering return codes.
>
> [...]

Here is the summary with links:
- [v2,01/10] platform/chrome: cros_kunit_util: add default value for `msg->result`
(no matching commit)
- [v2,02/10] platform/chrome: cros_ec_proto: add "cros_ec_" prefix to send_command()
https://git.kernel.org/chrome-platform/c/d311664b9057
- [v2,03/10] platform/chrome: cros_ec_proto: add Kunit tests for cros_ec_cmd_xfer()
https://git.kernel.org/chrome-platform/c/82f4def2d822
- [v2,04/10] platform/chrome: cros_ec_proto: add Kunit tests for cros_ec_send_command()
(no matching commit)
- [v2,05/10] platform/chrome: cros_ec_proto: separate cros_ec_xfer_command()
https://git.kernel.org/chrome-platform/c/810be30d27bd
- [v2,06/10] platform/chrome: cros_ec_proto: separate cros_ec_wait_until_complete()
https://git.kernel.org/chrome-platform/c/0aad9aff6a64
- [v2,07/10] platform/chrome: cros_ec_proto: change Kunit expectation when timed out
https://git.kernel.org/chrome-platform/c/00eb36d52872
- [v2,08/10] platform/chrome: cros_ec_proto: return -EAGAIN when retries timed out
https://git.kernel.org/chrome-platform/c/7f95d2b68b9a
- [v2,09/10] platform/chrome: cros_ec_proto: add Kunit test for empty payload
https://git.kernel.org/chrome-platform/c/82c9b7ed8c5c
- [v2,10/10] platform/chrome: cros_ec_proto: return -EPROTO if empty payload
https://git.kernel.org/chrome-platform/c/3e1c715ea179

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html


Subject: Re: [PATCH v2 00/10] platform/chrome: Kunit tests and refactor for cros_ec_cmd_xfer()

Hello:

This series was applied to chrome-platform/linux.git (for-next)
by Tzung-Bi Shih <[email protected]>:

On Mon, 18 Jul 2022 05:09:04 +0000 you wrote:
> The 1st patch fixes an issue that cros_kunit_ec_xfer_mock() could return
> garbage bytes for `msg->result` if the mock list is empty.
>
> The 2nd ~ 6th patches add Kunit tests and refactors for cros_ec_cmd_xfer().
>
> The last 4 patches change the behavior a bit by altering return codes.
>
> [...]

Here is the summary with links:
- [v2,01/10] platform/chrome: cros_kunit_util: add default value for `msg->result`
(no matching commit)
- [v2,02/10] platform/chrome: cros_ec_proto: add "cros_ec_" prefix to send_command()
https://git.kernel.org/chrome-platform/c/d311664b9057
- [v2,03/10] platform/chrome: cros_ec_proto: add Kunit tests for cros_ec_cmd_xfer()
https://git.kernel.org/chrome-platform/c/82f4def2d822
- [v2,04/10] platform/chrome: cros_ec_proto: add Kunit tests for cros_ec_send_command()
(no matching commit)
- [v2,05/10] platform/chrome: cros_ec_proto: separate cros_ec_xfer_command()
https://git.kernel.org/chrome-platform/c/810be30d27bd
- [v2,06/10] platform/chrome: cros_ec_proto: separate cros_ec_wait_until_complete()
https://git.kernel.org/chrome-platform/c/0aad9aff6a64
- [v2,07/10] platform/chrome: cros_ec_proto: change Kunit expectation when timed out
https://git.kernel.org/chrome-platform/c/00eb36d52872
- [v2,08/10] platform/chrome: cros_ec_proto: return -EAGAIN when retries timed out
https://git.kernel.org/chrome-platform/c/7f95d2b68b9a
- [v2,09/10] platform/chrome: cros_ec_proto: add Kunit test for empty payload
https://git.kernel.org/chrome-platform/c/82c9b7ed8c5c
- [v2,10/10] platform/chrome: cros_ec_proto: return -EPROTO if empty payload
https://git.kernel.org/chrome-platform/c/3e1c715ea179

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html