* Copyright (c) 2026 Huawei Technologies Co., Ltd.
* openUBMC is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#include "mcu_switch.h"
#include "internal/hex_util.h"
#include <algorithm>
#include <mc/log.h>
using namespace dev;
namespace {
constexpr uint32_t capability_header_data_len = 5;
}
bool chip_mcu_switch::init(mc::mutable_dict& csr_object)
{
if (!chip_base::init(csr_object)) {
return false;
}
try {
if (csr_object.contains("SwitchSupported")) {
m_switch_supported = csr_object["SwitchSupported"].as<bool>();
}
if (m_switch_supported) {
init_smbus_for_switch();
}
return true;
} catch (const std::exception& e) {
elog("MCU switch chip init failed: ${error}", ("error", e.what()));
return false;
}
}
int32_t chip_mcu_switch::open_channel(std::shared_ptr<bus_base> bus)
{
(void)bus;
if (!m_smbus_obj) {
return HAS_OK;
}
if (!check_switch_capability()) {
m_switch_capability_available = false;
ilog("MCU Switch capability check failed for chip ${name}, switch not performed", ("name", m_object_name));
return HAS_OK;
}
m_switch_capability_available = true;
return do_mcu_switch(MCU_SWITCH_OPEN_DATA);
}
int32_t chip_mcu_switch::close_channel(std::shared_ptr<bus_base> bus)
{
(void)bus;
if (!m_switch_capability_available || !m_smbus_obj) {
return HAS_OK;
}
return do_mcu_switch(MCU_SWITCH_CLOSE_DATA);
}
void chip_mcu_switch::init_smbus_for_switch()
{
m_smbus_obj = std::make_shared<dev::std_smbus>(nullptr);
mc::mutable_dict params;
params["BufferLen"] = static_cast<uint32_t>(32);
params["McuAddress"] = m_address;
m_smbus_obj->init(params);
m_smbus_obj->register_ComboWriteRead_method([this](uint32_t write_cmd, const std::vector<uint8_t>& write_data,
uint32_t read_cmd, uint32_t read_len) -> std::vector<uint8_t> {
std::string error;
std::vector<uint8_t> in_buffer;
std::vector<uint8_t> out_buffer;
int32_t ret =
this->combo_block_write_read(write_cmd, write_data, read_cmd, read_len, in_buffer, out_buffer, error);
if (ret != HAS_OK) {
elog("MCU Switch ComboWriteRead failed: ${error}", ("error", error));
return std::vector<uint8_t>();
}
return out_buffer;
});
nlog("MCU Switch smbus initialized for chip ${name}, address ${addr}", ("name", m_object_name)("addr", m_address));
}
int32_t chip_mcu_switch::do_mcu_switch(uint8_t switch_data)
{
if (!m_switch_capability_available || !m_smbus_obj) {
return HAS_OK;
}
const std::string switch_payload(1, static_cast<char>(switch_data));
mc::mutable_dict request;
request["opcode"] = static_cast<uint16_t>(MCU_SWITCH_OPCODE);
request["data"] = switch_payload;
request["expect_data_len"] = static_cast<int32_t>(0);
dlog("${name} SetMcuSwitch ${data}", ("name", m_object_name)("data", to_hex(switch_payload)));
try {
(void)m_smbus_obj->send_request(request);
} catch (const std::exception& e) {
elog("MCU Switch ${action} request exception for chip ${name}: ${error}",
("action", switch_data == MCU_SWITCH_OPEN_DATA ? "open" : "close")("name", m_object_name)("error",
e.what()));
return HAS_ERR;
}
return HAS_OK;
}
bool chip_mcu_switch::check_switch_capability()
{
if (!m_smbus_obj) {
return false;
}
mc::mutable_dict request;
request["opcode"] = static_cast<uint16_t>(MCU_CAPABILITIES_OPCODE);
std::string capability_data;
try {
capability_data = m_smbus_obj->send_request(request);
} catch (const std::exception& e) {
wlog("MCU Capabilities query exception for chip ${name}: ${error}", ("name", m_object_name)("error", e.what()));
return false;
}
if (capability_data.size() < capability_header_data_len) {
ilog("MCU Capabilities response too short for chip ${name}: data_len=${len}",
("name", m_object_name)("len", capability_data.size()));
return false;
}
auto opcode_count = static_cast<uint8_t>(capability_data[3]) | (static_cast<uint8_t>(capability_data[4]) << 8);
auto opcode_bytes = static_cast<size_t>(opcode_count) * 2;
if (capability_data.size() < capability_header_data_len + opcode_bytes) {
ilog("MCU Capabilities response length mismatch for chip ${name}: data_len=${data_len}, "
"expected_min=${expected}",
("name", m_object_name)("data_len", static_cast<uint32_t>(capability_data.size()))(
"expected", static_cast<uint32_t>(capability_header_data_len + opcode_bytes)));
return false;
}
auto supported = false;
for (size_t i = 0; i < opcode_count; ++i) {
auto base = capability_header_data_len + i * 2;
auto opcode =
static_cast<uint8_t>(capability_data[base]) | (static_cast<uint8_t>(capability_data[base + 1]) << 8);
if (opcode == MCU_SWITCH_OPCODE) {
supported = true;
break;
}
}
ilog("MCU Capabilities check for chip ${name}: opcode_count=${count}, switch_supported=${supported}",
("name", m_object_name)("count", opcode_count)("supported", supported));
return supported;
}