From bd6ae0980fde3d20a49385caa6d64adb6aee3276 Mon Sep 17 00:00:00 2001 From: Junior Martinez <67972863+jmartinez-silabs@users.noreply.github.com> Date: Tue, 10 Sep 2024 15:01:55 -0400 Subject: [PATCH] [Silabs] Increase the max supported KVS entries (#35480) * Increase the maximum KVS entries our implementation can handle * add gn argument to configure the nvm3 max object size --- src/platform/silabs/KeyValueStoreManagerImpl.cpp | 10 +++++----- src/platform/silabs/SilabsConfig.h | 16 +++++++++++----- third_party/silabs/SiWx917_sdk.gni | 2 +- third_party/silabs/efr32_sdk.gni | 3 ++- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/platform/silabs/KeyValueStoreManagerImpl.cpp b/src/platform/silabs/KeyValueStoreManagerImpl.cpp index 012c951aaaa729..34d88388badf22 100644 --- a/src/platform/silabs/KeyValueStoreManagerImpl.cpp +++ b/src/platform/silabs/KeyValueStoreManagerImpl.cpp @@ -88,9 +88,9 @@ uint16_t KeyValueStoreManagerImpl::hashKvsKeyString(const char * key) const CHIP_ERROR KeyValueStoreManagerImpl::MapKvsKeyToNvm3(const char * key, uint16_t hash, uint32_t & nvm3Key, bool isSlotNeeded) const { CHIP_ERROR err; - char * strPrefix = nullptr; - uint8_t firstEmptyKeySlot = kMaxEntries; - for (uint8_t keyIndex = 0; keyIndex < kMaxEntries; keyIndex++) + char * strPrefix = nullptr; + uint16_t firstEmptyKeySlot = kMaxEntries; + for (uint16_t keyIndex = 0; keyIndex < kMaxEntries; keyIndex++) { if (mKvsKeyMap[keyIndex] == hash) { @@ -165,7 +165,7 @@ void KeyValueStoreManagerImpl::ScheduleKeyMapSave(void) Commit the key map in nvm once it as stabilized. */ SystemLayer().StartTimer( - std::chrono::duration_cast(System::Clock::Seconds32(SILABS_KVS_SAVE_DELAY_SECONDS)), + std::chrono::duration_cast(System::Clock::Seconds32(SL_KVS_SAVE_DELAY_SECONDS)), KeyValueStoreManagerImpl::OnScheduledKeyMapSave, NULL); } @@ -247,7 +247,7 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key) void KeyValueStoreManagerImpl::ErasePartition(void) { // Iterate over all the Matter Kvs nvm3 records and delete each one... - for (uint32_t nvm3Key = SilabsConfig::kMinConfigKey_MatterKvs; nvm3Key <= SilabsConfig::kMaxConfigKey_MatterKvs; nvm3Key++) + for (uint32_t nvm3Key = SilabsConfig::kMinConfigKey_MatterKvs; nvm3Key <= SilabsConfig::kConfigKey_KvsLastKeySlot; nvm3Key++) { SilabsConfig::ClearConfigValue(nvm3Key); } diff --git a/src/platform/silabs/SilabsConfig.h b/src/platform/silabs/SilabsConfig.h index ccb14beeb8f177..cef9ca323f74d2 100644 --- a/src/platform/silabs/SilabsConfig.h +++ b/src/platform/silabs/SilabsConfig.h @@ -35,9 +35,11 @@ #endif // Delay before Key/Value is actually saved in NVM -#define SILABS_KVS_SAVE_DELAY_SECONDS 5 +#ifndef SL_KVS_SAVE_DELAY_SECONDS +#define SL_KVS_SAVE_DELAY_SECONDS 2 +#endif -static_assert((KVS_MAX_ENTRIES <= 255), "Implementation supports up to 255 Kvs entries"); +static_assert((KVS_MAX_ENTRIES <= 511), "Implementation supports up to 511 Kvs entries"); static_assert((KVS_MAX_ENTRIES >= 30), "Mininimal Kvs entries requirement is not met"); namespace chip { @@ -89,7 +91,8 @@ class SilabsConfig // Persistent counter values set at runtime. Retained during factory reset. static constexpr uint8_t kMatterCounter_KeyBase = 0x74; // Persistent config values set at runtime. Cleared during factory reset. - static constexpr uint8_t kMatterKvs_KeyBase = 0x75; + static constexpr uint8_t kMatterKvs_KeyBase = 0x75; + static constexpr uint8_t kMatterKvs_ExtendedRange = 0x76; // Key definitions for well-known configuration values. // Factory config keys @@ -167,7 +170,8 @@ class SilabsConfig // Matter KVS storage Keys static constexpr Key kConfigKey_KvsStringKeyMap = SilabsConfigKey(kMatterKvs_KeyBase, 0x00); static constexpr Key kConfigKey_KvsFirstKeySlot = SilabsConfigKey(kMatterKvs_KeyBase, 0x01); - static constexpr Key kConfigKey_KvsLastKeySlot = SilabsConfigKey(kMatterKvs_KeyBase, KVS_MAX_ENTRIES); + static constexpr Key kConfigKey_KvsLastKeySlot = + SilabsConfigKey(kMatterKvs_KeyBase + (KVS_MAX_ENTRIES >> 8), KVS_MAX_ENTRIES & UINT8_MAX); // Set key id limits for each group. static constexpr Key kMinConfigKey_MatterFactory = SilabsConfigKey(kMatterFactory_KeyBase, 0x00); @@ -180,7 +184,9 @@ class SilabsConfig static constexpr Key kMaxConfigKey_MatterCounter = SilabsConfigKey(kMatterCounter_KeyBase, 0x1F); static constexpr Key kMinConfigKey_MatterKvs = kConfigKey_KvsStringKeyMap; - static constexpr Key kMaxConfigKey_MatterKvs = kConfigKey_KvsLastKeySlot; + static constexpr Key kMaxConfigKey_MatterKvs = SilabsConfigKey(kMatterKvs_ExtendedRange, 0xFF); + static_assert(kConfigKey_KvsLastKeySlot <= kMaxConfigKey_MatterKvs, + "Configured KVS_MAX_ENTRIES overflows the reserved KVS Key range"); static CHIP_ERROR Init(void); static void DeInit(void); diff --git a/third_party/silabs/SiWx917_sdk.gni b/third_party/silabs/SiWx917_sdk.gni index 30e1595493c60c..e91afe7483b3fa 100644 --- a/third_party/silabs/SiWx917_sdk.gni +++ b/third_party/silabs/SiWx917_sdk.gni @@ -187,7 +187,7 @@ template("siwx917_sdk") { # 1->SOC and 0->NCP "RSI_WLAN_API_ENABLE", "NVM3_DEFAULT_NVM_SIZE=40960", - "NVM3_DEFAULT_MAX_OBJECT_SIZE=4092", + "NVM3_DEFAULT_MAX_OBJECT_SIZE=${sl_nvm3_max_object_size}", "KVS_MAX_ENTRIES=${kvs_max_entries}", "${silabs_mcu}=1", "${silabs_board}=1", diff --git a/third_party/silabs/efr32_sdk.gni b/third_party/silabs/efr32_sdk.gni index 1cd69d31e7084b..3f30a0ca52f867 100644 --- a/third_party/silabs/efr32_sdk.gni +++ b/third_party/silabs/efr32_sdk.gni @@ -38,6 +38,7 @@ declare_args() { # enable by default for thread/non-wifi-ncp builds enable_openthread_cli = !(use_rs9116 || use_wf200 || use_SiWx917) kvs_max_entries = 255 + sl_nvm3_max_object_size = 4092 # Use Silabs factory data provider example. # Users can implement their own. @@ -421,7 +422,7 @@ template("efr32_sdk") { "HARD_FAULT_LOG_ENABLE", "CORTEXM3_EFM32_MICRO", "NVM3_DEFAULT_NVM_SIZE=40960", - "NVM3_DEFAULT_MAX_OBJECT_SIZE=4092", + "NVM3_DEFAULT_MAX_OBJECT_SIZE=${sl_nvm3_max_object_size}", "KVS_MAX_ENTRIES=${kvs_max_entries}", "CORTEXM3=1", "MICRO=EMBER_MICRO_CORTEXM3_EFR32",