diff options
author | Daisuke Nojiri <dnojiri@chromium.org> | 2018-01-17 17:22:42 -0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-01-18 20:18:11 -0800 |
commit | 4941dd5542339f603b5d73ec61846a282e6556a8 (patch) | |
tree | 53510a09b42b0aeb7c6182eff723b4b80600df7c | |
parent | f02c8c4c87cdf9c30b44be9c7ba49e506115a188 (diff) | |
download | chrome-ec-4941dd5542339f603b5d73ec61846a282e6556a8.tar.gz |
CBI: Retry read and cache the result
This patch makes read_board_info retry to read CBI on error. The
successive calls will be immediately returned with the cached result.
This will avoid attempting reads doomed to fail.
BUG=b:70294260
BRANCH=none
TEST=ectool cbi get 0
Change-Id: Iacd9cc38bab814af9188c4557c2ee751c421e3a3
Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/872259
Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r-- | common/cbi.c | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/common/cbi.c b/common/cbi.c index 41e8606487..bc21d901fa 100644 --- a/common/cbi.c +++ b/common/cbi.c @@ -17,12 +17,9 @@ #define CPRINTS(format, args...) cprints(CC_SYSTEM, "CBI " format, ## args) #define EEPROM_PAGE_WRITE_SIZE 16 +#define EC_ERROR_CBI_CACHE_INVALID EC_ERROR_INTERNAL_FIRST static struct board_info bi; -/* TODO: Init it to -1. On error (I2C or bad contents), retry a read and set it - * to enum ec_error_list if it still fails. The successive calls can be - * immediately returned with the cached error code. This will avoid attempting - * reads doomed to fail. */ -static int initialized; +static int cached_read_result = EC_ERROR_CBI_CACHE_INVALID; static uint8_t cbi_crc8(const struct board_info *bi) { @@ -32,14 +29,11 @@ static uint8_t cbi_crc8(const struct board_info *bi) /* * Get board information from EEPROM */ -static int read_board_info(void) +static int do_read_board_info(void) { uint8_t buf[256]; uint8_t offset; - if (initialized) - return EC_SUCCESS; - CPRINTS("Reading board info"); /* Read header */ @@ -92,11 +86,24 @@ static int read_board_info(void) sizeof(bi) - sizeof(bi.head)); /* If we're handling previous version, clear all new fields */ - initialized = 1; - return EC_SUCCESS; } +static int read_board_info(void) +{ + if (cached_read_result != EC_ERROR_CBI_CACHE_INVALID) + /* We already tried and know the result. Return the cached + * error code immediately to avoid wasteful reads. */ + return cached_read_result; + + cached_read_result = do_read_board_info(); + if (cached_read_result) + /* On error (I2C or bad contents), retry a read */ + cached_read_result = do_read_board_info(); + + return cached_read_result; +} + static int eeprom_is_write_protected(void) { return !gpio_get_level(GPIO_WP_L); @@ -196,7 +203,7 @@ static int hc_cbi_set(struct host_cmd_handler_args *args) if (p->flag & CBI_SET_INIT) { memset(&bi, 0, sizeof(bi)); memcpy(&bi.head.magic, cbi_magic, sizeof(cbi_magic)); - initialized = 1; + cached_read_result = EC_SUCCESS; } else { if (read_board_info()) return EC_RES_ERROR; |