blob: a1f5db2f368e66251a3651e415a47cacead268b4 [file] [log] [blame]
James Feist6ef20402019-01-07 16:45:08 -08001/*
2// Copyright (c) 2019 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "IpmbSensor.hpp"
18
19#include "IpmbSDRSensor.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070020#include "SensorPaths.hpp"
21#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103022#include "Utils.hpp"
23#include "VariantVisitors.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070024#include "sensor.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103025
Ed Tanouseacbfdd2024-04-04 12:00:24 -070026#include <boost/asio/error.hpp>
27#include <boost/asio/io_context.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070028#include <boost/asio/steady_timer.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <boost/container/flat_map.hpp>
George Liu73f6cdc2025-02-20 15:15:19 +080030#include <phosphor-logging/lg2.hpp>
James Feist38fb5982020-05-28 10:09:54 -070031#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070033#include <sdbusplus/message.hpp>
34#include <sdbusplus/message/native_types.hpp>
James Feist38fb5982020-05-28 10:09:54 -070035
Ed Tanouseacbfdd2024-04-04 12:00:24 -070036#include <algorithm>
37#include <array>
James Feist6ef20402019-01-07 16:45:08 -080038#include <chrono>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070039#include <cstddef>
40#include <cstdint>
George Liu73f6cdc2025-02-20 15:15:19 +080041#include <iomanip>
James Feist6ef20402019-01-07 16:45:08 -080042#include <iostream>
43#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070044#include <memory>
George Liu73f6cdc2025-02-20 15:15:19 +080045#include <sstream>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070046#include <stdexcept>
Patrick Venture96e97db2019-10-31 13:44:38 -070047#include <string>
48#include <tuple>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070049#include <utility>
Patrick Venture96e97db2019-10-31 13:44:38 -070050#include <variant>
James Feist6ef20402019-01-07 16:45:08 -080051#include <vector>
52
53constexpr const bool debug = false;
54
James Feist6ef20402019-01-07 16:45:08 -080055static constexpr double ipmbMaxReading = 0xFF;
56static constexpr double ipmbMinReading = 0;
57
58static constexpr uint8_t meAddress = 1;
59static constexpr uint8_t lun = 0;
Anoop S832a2c62020-11-20 19:21:22 +000060static constexpr uint8_t hostSMbusIndexDefault = 0x03;
Jayashree Dhanapal6ee62942021-12-14 15:22:23 +053061static constexpr uint8_t ipmbBusIndexDefault = 0;
Jayashree-D9f6d4fd2021-04-13 18:27:22 +053062static constexpr float pollRateDefault = 1; // in seconds
James Feist6ef20402019-01-07 16:45:08 -080063
Vijay Khemka682a5cb2019-07-18 17:34:03 -070064static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
65
Patrick Williams2aaf7172024-08-16 15:20:40 -040066IpmbSensor::IpmbSensor(
67 std::shared_ptr<sdbusplus::asio::connection>& conn,
68 boost::asio::io_context& io, const std::string& sensorName,
69 const std::string& sensorConfiguration,
70 sdbusplus::asio::object_server& objectServer,
71 std::vector<thresholds::Threshold>&& thresholdData, uint8_t deviceAddress,
72 uint8_t hostSMbusIndex, const float pollRate, std::string& sensorTypeName) :
Zhikui Renda98f092021-11-01 09:41:08 -070073 Sensor(escapeName(sensorName), std::move(thresholdData),
Zev Weiss054aad82022-08-18 01:37:34 -070074 sensorConfiguration, "IpmbSensor", false, false, ipmbMaxReading,
75 ipmbMinReading, conn, PowerState::on),
Anoop S832a2c62020-11-20 19:21:22 +000076 deviceAddress(deviceAddress), hostSMbusIndex(hostSMbusIndex),
Jayashree-D9f6d4fd2021-04-13 18:27:22 +053077 sensorPollMs(static_cast<int>(pollRate * 1000)), objectServer(objectServer),
78 waitTimer(io)
James Feist6ef20402019-01-07 16:45:08 -080079{
Vijay Khemka682a5cb2019-07-18 17:34:03 -070080 std::string dbusPath = sensorPathPrefix + sensorTypeName + "/" + name;
81
James Feist6ef20402019-01-07 16:45:08 -080082 sensorInterface = objectServer.add_interface(
Vijay Khemka682a5cb2019-07-18 17:34:03 -070083 dbusPath, "xyz.openbmc_project.Sensor.Value");
James Feist6ef20402019-01-07 16:45:08 -080084
Jayashree Dhanapal56678082022-01-04 17:27:20 +053085 for (const auto& threshold : thresholds)
James Feist6ef20402019-01-07 16:45:08 -080086 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053087 std::string interface = thresholds::getInterface(threshold.level);
88 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
89 objectServer.add_interface(dbusPath, interface);
James Feist6ef20402019-01-07 16:45:08 -080090 }
James Feist2adc95c2019-09-30 14:55:28 -070091 association = objectServer.add_interface(dbusPath, association::interface);
James Feist6ef20402019-01-07 16:45:08 -080092}
93
94IpmbSensor::~IpmbSensor()
95{
96 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +053097 for (const auto& iface : thresholdInterfaces)
98 {
99 objectServer.remove_interface(iface);
100 }
James Feist6ef20402019-01-07 16:45:08 -0800101 objectServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800102 objectServer.remove_interface(association);
James Feist6ef20402019-01-07 16:45:08 -0800103}
104
Ed Tanous201a1012024-04-03 18:07:28 -0700105std::string IpmbSensor::getSubTypeUnits() const
Zev Weiss6b6891c2021-04-22 02:46:21 -0500106{
107 switch (subType)
108 {
109 case IpmbSubType::temp:
110 return sensor_paths::unitDegreesC;
111 case IpmbSubType::curr:
112 return sensor_paths::unitAmperes;
113 case IpmbSubType::power:
114 return sensor_paths::unitWatts;
115 case IpmbSubType::volt:
116 return sensor_paths::unitVolts;
117 case IpmbSubType::util:
118 return sensor_paths::unitPercent;
119 default:
120 throw std::runtime_error("Invalid sensor type");
121 }
122}
123
Ed Tanous201a1012024-04-03 18:07:28 -0700124void IpmbSensor::init()
James Feist6ef20402019-01-07 16:45:08 -0800125{
James Feist6ef20402019-01-07 16:45:08 -0800126 loadDefaults();
Andrei Kartashev39287412022-02-04 16:04:47 +0300127 setInitialProperties(getSubTypeUnits());
Patrick Williamsebd2a382025-05-14 08:48:58 -0400128 runInitCmd();
James Feistf7e2c5d2019-02-13 17:27:51 -0800129 read();
130}
131
Vikash Chandola1f847972022-09-28 09:47:32 +0000132static void initCmdCb(const std::weak_ptr<IpmbSensor>& weakRef,
133 const boost::system::error_code& ec,
134 const IpmbMethodType& response)
135{
136 std::shared_ptr<IpmbSensor> self = weakRef.lock();
137 if (!self)
138 {
139 return;
140 }
141 const int& status = std::get<0>(response);
142 if (ec || (status != 0))
143 {
George Liu73f6cdc2025-02-20 15:15:19 +0800144 lg2::error("Error setting init command for device: '{NAME}'", "NAME",
145 self->name);
Vikash Chandola1f847972022-09-28 09:47:32 +0000146 }
147}
148
James Feistf7e2c5d2019-02-13 17:27:51 -0800149void IpmbSensor::runInitCmd()
150{
Patrick Williamsebd2a382025-05-14 08:48:58 -0400151 if (!initCommand.has_value())
James Feistf7e2c5d2019-02-13 17:27:51 -0800152 {
Vikash Chandola1f847972022-09-28 09:47:32 +0000153 return;
James Feist6ef20402019-01-07 16:45:08 -0800154 }
Vikash Chandola1f847972022-09-28 09:47:32 +0000155 dbusConnection->async_method_call(
156 [weakRef{weak_from_this()}](const boost::system::error_code& ec,
157 const IpmbMethodType& response) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400158 initCmdCb(weakRef, ec, response);
159 },
Vikash Chandola1f847972022-09-28 09:47:32 +0000160 "xyz.openbmc_project.Ipmi.Channel.Ipmb",
161 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
Patrick Williamsebd2a382025-05-14 08:48:58 -0400162 "sendRequest", commandAddress, netfn, lun, initCommand.value_or(0),
163 initData);
James Feist6ef20402019-01-07 16:45:08 -0800164}
165
166void IpmbSensor::loadDefaults()
167{
168 if (type == IpmbType::meSensor)
169 {
170 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200171 netfn = ipmi::sensor::netFn;
172 command = ipmi::sensor::getSensorReading;
James Feist6ef20402019-01-07 16:45:08 -0800173 commandData = {deviceAddress};
James Feistd7ae29a2020-06-25 15:42:46 -0700174 readingFormat = ReadingFormat::byte0;
James Feist6ef20402019-01-07 16:45:08 -0800175 }
176 else if (type == IpmbType::PXE1410CVR)
177 {
178 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200179 netfn = ipmi::me_bridge::netFn;
180 command = ipmi::me_bridge::sendRawPmbus;
181 initCommand = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700182 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000183 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
184 deviceAddress, 0x00, 0x00, 0x00, 0x00,
185 0x01, 0x02, 0x8d};
James Feistd7ae29a2020-06-25 15:42:46 -0700186 // goto page 0
Anoop S832a2c62020-11-20 19:21:22 +0000187 initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex,
188 deviceAddress, 0x00, 0x00, 0x00, 0x00,
189 0x02, 0x00, 0x00, 0x00};
Jayashree-D37322572021-03-19 17:40:56 +0530190 readingFormat = ReadingFormat::linearElevenBit;
James Feist6ef20402019-01-07 16:45:08 -0800191 }
192 else if (type == IpmbType::IR38363VR)
193 {
194 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200195 netfn = ipmi::me_bridge::netFn;
196 command = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700197 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000198 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
199 deviceAddress, 00, 0x00, 0x00, 0x00,
200 0x01, 0x02, 0x8D};
James Feistd7ae29a2020-06-25 15:42:46 -0700201 readingFormat = ReadingFormat::elevenBitShift;
James Feist6ef20402019-01-07 16:45:08 -0800202 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700203 else if (type == IpmbType::ADM1278HSC)
204 {
205 commandAddress = meAddress;
Ed Tanousa771f6a2022-01-14 09:36:51 -0800206 uint8_t snsNum = 0;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700207 switch (subType)
208 {
209 case IpmbSubType::temp:
210 case IpmbSubType::curr:
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700211 if (subType == IpmbSubType::temp)
Ed Tanous8a57ec02020-10-09 12:46:52 -0700212 {
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700213 snsNum = 0x8d;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700214 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700215 else
Ed Tanous8a57ec02020-10-09 12:46:52 -0700216 {
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700217 snsNum = 0x8c;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700218 }
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200219 netfn = ipmi::me_bridge::netFn;
220 command = ipmi::me_bridge::sendRawPmbus;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700221 commandData = {0x57, 0x01, 0x00, 0x86, deviceAddress,
222 0x00, 0x00, 0x01, 0x02, snsNum};
James Feistd7ae29a2020-06-25 15:42:46 -0700223 readingFormat = ReadingFormat::elevenBit;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700224 break;
225 case IpmbSubType::power:
226 case IpmbSubType::volt:
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200227 netfn = ipmi::sensor::netFn;
228 command = ipmi::sensor::getSensorReading;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700229 commandData = {deviceAddress};
James Feistd7ae29a2020-06-25 15:42:46 -0700230 readingFormat = ReadingFormat::byte0;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700231 break;
232 default:
233 throw std::runtime_error("Invalid sensor type");
234 }
235 }
James Feist6ef20402019-01-07 16:45:08 -0800236 else if (type == IpmbType::mpsVR)
237 {
238 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200239 netfn = ipmi::me_bridge::netFn;
240 command = ipmi::me_bridge::sendRawPmbus;
241 initCommand = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700242 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000243 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
244 deviceAddress, 0x00, 0x00, 0x00, 0x00,
245 0x01, 0x02, 0x8d};
James Feistd7ae29a2020-06-25 15:42:46 -0700246 // goto page 0
Anoop S832a2c62020-11-20 19:21:22 +0000247 initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex,
248 deviceAddress, 0x00, 0x00, 0x00, 0x00,
249 0x02, 0x00, 0x00, 0x00};
James Feistd7ae29a2020-06-25 15:42:46 -0700250 readingFormat = ReadingFormat::byte3;
James Feist6ef20402019-01-07 16:45:08 -0800251 }
Rebecca Cran394f0c52023-12-17 20:08:55 -0700252 else if (type == IpmbType::SMPro)
253 {
254 // This is an Ampere SMPro reachable via a BMC. For example,
255 // this architecture is used on ADLINK Ampere Altra systems.
256 // See the Ampere Family SoC BMC Interface Specification at
257 // https://amperecomputing.com/customer-connect/products/altra-family-software---firmware
258 // for details of the sensors.
259 commandAddress = 0;
260 netfn = 0x30;
261 command = 0x31;
262 commandData = {0x9e, deviceAddress};
263 switch (subType)
264 {
265 case IpmbSubType::temp:
266 readingFormat = ReadingFormat::nineBit;
267 break;
268 case IpmbSubType::power:
269 readingFormat = ReadingFormat::tenBit;
270 break;
271 case IpmbSubType::curr:
272 case IpmbSubType::volt:
273 readingFormat = ReadingFormat::fifteenBit;
274 break;
275 default:
276 throw std::runtime_error("Invalid sensor type");
277 }
278 }
James Feist6ef20402019-01-07 16:45:08 -0800279 else
280 {
281 throw std::runtime_error("Invalid sensor type");
282 }
Adrian Ambrożewicz45e92772020-06-04 13:59:55 +0200283
284 if (subType == IpmbSubType::util)
285 {
286 // Utilization need to be scaled to percent
287 maxValue = 100;
288 minValue = 0;
289 }
James Feist6ef20402019-01-07 16:45:08 -0800290}
291
Ed Tanous201a1012024-04-03 18:07:28 -0700292void IpmbSensor::checkThresholds()
James Feist6ef20402019-01-07 16:45:08 -0800293{
James Feist6ef20402019-01-07 16:45:08 -0800294 thresholds::checkThresholds(this);
295}
296
Ed Tanous828c5a62024-02-09 16:59:22 -0800297bool IpmbSensor::processReading(ReadingFormat readingFormat, uint8_t command,
298 const std::vector<uint8_t>& data, double& resp,
299 size_t errCount)
James Feistd7ae29a2020-06-25 15:42:46 -0700300{
James Feistd7ae29a2020-06-25 15:42:46 -0700301 switch (readingFormat)
302 {
303 case (ReadingFormat::byte0):
James Feiste4a970d2020-08-19 11:21:58 -0700304 {
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200305 if (command == ipmi::sensor::getSensorReading &&
306 !ipmi::sensor::isValid(data))
James Feistcf4238e2020-07-28 16:40:03 -0700307 {
308 return false;
309 }
James Feist961bf092020-07-01 16:38:12 -0700310 resp = data[0];
311 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700312 }
James Feistd7ae29a2020-06-25 15:42:46 -0700313 case (ReadingFormat::byte3):
James Feiste4a970d2020-08-19 11:21:58 -0700314 {
James Feistd7ae29a2020-06-25 15:42:46 -0700315 if (data.size() < 4)
316 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700317 if (errCount == 0U)
James Feist961bf092020-07-01 16:38:12 -0700318 {
George Liu73f6cdc2025-02-20 15:15:19 +0800319 lg2::error("Invalid data length returned");
James Feist961bf092020-07-01 16:38:12 -0700320 }
321 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700322 }
James Feist961bf092020-07-01 16:38:12 -0700323 resp = data[3];
324 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700325 }
Rebecca Cran394f0c52023-12-17 20:08:55 -0700326 case (ReadingFormat::nineBit):
327 case (ReadingFormat::tenBit):
328 case (ReadingFormat::fifteenBit):
329 {
330 if (data.size() != 2)
331 {
332 if (errCount == 0U)
333 {
George Liu73f6cdc2025-02-20 15:15:19 +0800334 lg2::error("Invalid data length returned");
Rebecca Cran394f0c52023-12-17 20:08:55 -0700335 }
336 return false;
337 }
338
339 // From the Altra Family SoC BMC Interface Specification:
340 // 0xFFFF – This sensor data is either missing or is not supported
341 // by the device.
342 if ((data[0] == 0xff) && (data[1] == 0xff))
343 {
344 return false;
345 }
346
347 if (readingFormat == ReadingFormat::nineBit)
348 {
349 int16_t value = data[0];
350 if ((data[1] & 0x1) != 0)
351 {
352 // Sign extend to 16 bits
353 value |= 0xFF00;
354 }
355 resp = value;
356 }
357 else if (readingFormat == ReadingFormat::tenBit)
358 {
359 uint16_t value = ((data[1] & 0x3) << 8) + data[0];
360 resp = value;
361 }
362 else if (readingFormat == ReadingFormat::fifteenBit)
363 {
364 uint16_t value = ((data[1] & 0x7F) << 8) + data[0];
365 // Convert mV to V
366 resp = value / 1000.0;
367 }
368
369 return true;
370 }
James Feistd7ae29a2020-06-25 15:42:46 -0700371 case (ReadingFormat::elevenBit):
James Feiste4a970d2020-08-19 11:21:58 -0700372 {
James Feistd7ae29a2020-06-25 15:42:46 -0700373 if (data.size() < 5)
374 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700375 if (errCount == 0U)
James Feist961bf092020-07-01 16:38:12 -0700376 {
George Liu73f6cdc2025-02-20 15:15:19 +0800377 lg2::error("Invalid data length returned");
James Feist961bf092020-07-01 16:38:12 -0700378 }
379 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700380 }
381
James Feiste4a970d2020-08-19 11:21:58 -0700382 int16_t value = ((data[4] << 8) | data[3]);
James Feiste4a970d2020-08-19 11:21:58 -0700383 resp = value;
James Feist961bf092020-07-01 16:38:12 -0700384 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700385 }
James Feistd7ae29a2020-06-25 15:42:46 -0700386 case (ReadingFormat::elevenBitShift):
James Feiste4a970d2020-08-19 11:21:58 -0700387 {
James Feistd7ae29a2020-06-25 15:42:46 -0700388 if (data.size() < 5)
389 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700390 if (errCount == 0U)
James Feist961bf092020-07-01 16:38:12 -0700391 {
George Liu73f6cdc2025-02-20 15:15:19 +0800392 lg2::error("Invalid data length returned");
James Feist961bf092020-07-01 16:38:12 -0700393 }
394 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700395 }
396
James Feist961bf092020-07-01 16:38:12 -0700397 resp = ((data[4] << 8) | data[3]) >> 3;
398 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700399 }
Jayashree-D37322572021-03-19 17:40:56 +0530400 case (ReadingFormat::linearElevenBit):
401 {
402 if (data.size() < 5)
403 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700404 if (errCount == 0U)
Jayashree-D37322572021-03-19 17:40:56 +0530405 {
George Liu73f6cdc2025-02-20 15:15:19 +0800406 lg2::error("Invalid data length returned");
Jayashree-D37322572021-03-19 17:40:56 +0530407 }
408 return false;
409 }
410
411 int16_t value = ((data[4] << 8) | data[3]);
412 constexpr const size_t shift = 16 - 11; // 11bit into 16bit
413 value <<= shift;
414 value >>= shift;
415 resp = value;
416 return true;
417 }
James Feistd7ae29a2020-06-25 15:42:46 -0700418 default:
419 throw std::runtime_error("Invalid reading type");
420 }
421}
422
Vikash Chandola1f847972022-09-28 09:47:32 +0000423void IpmbSensor::ipmbRequestCompletionCb(const boost::system::error_code& ec,
424 const IpmbMethodType& response)
425{
426 const int& status = std::get<0>(response);
427 if (ec || (status != 0))
428 {
429 incrementError();
430 read();
431 return;
432 }
433 const std::vector<uint8_t>& data = std::get<5>(response);
434 if constexpr (debug)
435 {
George Liu73f6cdc2025-02-20 15:15:19 +0800436 std::ostringstream tempStream;
437 for (int d : data)
Vikash Chandola1f847972022-09-28 09:47:32 +0000438 {
George Liu73f6cdc2025-02-20 15:15:19 +0800439 tempStream << std::setfill('0') << std::setw(2) << std::hex << d
440 << " ";
Vikash Chandola1f847972022-09-28 09:47:32 +0000441 }
George Liu73f6cdc2025-02-20 15:15:19 +0800442 lg2::info("'{NAME}': '{DATA}'", "NAME", name, "DATA", tempStream.str());
Vikash Chandola1f847972022-09-28 09:47:32 +0000443 }
444 if (data.empty())
445 {
446 incrementError();
447 read();
448 return;
449 }
450
451 double value = 0;
452
Ed Tanous828c5a62024-02-09 16:59:22 -0800453 if (!processReading(readingFormat, command, data, value, errCount))
Vikash Chandola1f847972022-09-28 09:47:32 +0000454 {
455 incrementError();
456 read();
457 return;
458 }
459
460 // rawValue only used in debug logging
461 // up to 5th byte in data are used to derive value
462 size_t end = std::min(sizeof(uint64_t), data.size());
463 uint64_t rawData = 0;
464 for (size_t i = 0; i < end; i++)
465 {
466 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
467 reinterpret_cast<uint8_t*>(&rawData)[i] = data[i];
468 }
469 rawValue = static_cast<double>(rawData);
470
471 /* Adjust value as per scale and offset */
472 value = (value * scaleVal) + offsetVal;
473 updateValue(value);
474 read();
475}
476
Ed Tanous201a1012024-04-03 18:07:28 -0700477void IpmbSensor::read()
James Feist6ef20402019-01-07 16:45:08 -0800478{
Ed Tanous83db50c2023-03-01 10:20:24 -0800479 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
Vikash Chandola1f847972022-09-28 09:47:32 +0000480 waitTimer.async_wait(
481 [weakRef{weak_from_this()}](const boost::system::error_code& ec) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400482 if (ec == boost::asio::error::operation_aborted)
483 {
484 return; // we're being canceled
485 }
486 std::shared_ptr<IpmbSensor> self = weakRef.lock();
487 if (!self)
488 {
489 return;
490 }
491 self->sendIpmbRequest();
492 });
James Feist6ef20402019-01-07 16:45:08 -0800493}
Jayashree Dhanapal84189752022-03-07 12:51:54 +0530494
Vikash Chandola1f847972022-09-28 09:47:32 +0000495void IpmbSensor::sendIpmbRequest()
496{
497 if (!readingStateGood())
498 {
499 updateValue(std::numeric_limits<double>::quiet_NaN());
500 read();
501 return;
502 }
503 dbusConnection->async_method_call(
504 [weakRef{weak_from_this()}](boost::system::error_code ec,
505 const IpmbMethodType& response) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400506 std::shared_ptr<IpmbSensor> self = weakRef.lock();
507 if (!self)
508 {
509 return;
510 }
511 self->ipmbRequestCompletionCb(ec, response);
512 },
Vikash Chandola1f847972022-09-28 09:47:32 +0000513 "xyz.openbmc_project.Ipmi.Channel.Ipmb",
514 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
515 "sendRequest", commandAddress, netfn, lun, command, commandData);
516}
517
Jayashree Dhanapal84189752022-03-07 12:51:54 +0530518bool IpmbSensor::sensorClassType(const std::string& sensorClass)
519{
520 if (sensorClass == "PxeBridgeTemp")
521 {
522 type = IpmbType::PXE1410CVR;
523 }
524 else if (sensorClass == "IRBridgeTemp")
525 {
526 type = IpmbType::IR38363VR;
527 }
528 else if (sensorClass == "HSCBridge")
529 {
530 type = IpmbType::ADM1278HSC;
531 }
532 else if (sensorClass == "MpsBridgeTemp")
533 {
534 type = IpmbType::mpsVR;
535 }
536 else if (sensorClass == "METemp" || sensorClass == "MESensor")
537 {
538 type = IpmbType::meSensor;
539 }
Rebecca Cran394f0c52023-12-17 20:08:55 -0700540 else if (sensorClass == "SMPro")
541 {
542 type = IpmbType::SMPro;
543 }
Jayashree Dhanapal84189752022-03-07 12:51:54 +0530544 else
545 {
George Liu73f6cdc2025-02-20 15:15:19 +0800546 lg2::error("Invalid class '{SENSOR}'", "SENSOR", sensorClass);
Jayashree Dhanapal84189752022-03-07 12:51:54 +0530547 return false;
548 }
549 return true;
550}
551
552void IpmbSensor::sensorSubType(const std::string& sensorTypeName)
553{
554 if (sensorTypeName == "voltage")
555 {
556 subType = IpmbSubType::volt;
557 }
558 else if (sensorTypeName == "power")
559 {
560 subType = IpmbSubType::power;
561 }
562 else if (sensorTypeName == "current")
563 {
564 subType = IpmbSubType::curr;
565 }
566 else if (sensorTypeName == "utilization")
567 {
568 subType = IpmbSubType::util;
569 }
570 else
571 {
572 subType = IpmbSubType::temp;
573 }
574}
575
576void IpmbSensor::parseConfigValues(const SensorBaseConfigMap& entry)
577{
578 auto findScaleVal = entry.find("ScaleValue");
579 if (findScaleVal != entry.end())
580 {
581 scaleVal = std::visit(VariantToDoubleVisitor(), findScaleVal->second);
582 }
583
584 auto findOffsetVal = entry.find("OffsetValue");
585 if (findOffsetVal != entry.end())
586 {
587 offsetVal = std::visit(VariantToDoubleVisitor(), findOffsetVal->second);
588 }
589
Zev Weissa4d27682022-07-19 15:30:36 -0700590 readState = getPowerState(entry);
Jayashree Dhanapal84189752022-03-07 12:51:54 +0530591}
592
James Feist6ef20402019-01-07 16:45:08 -0800593void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800594 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Vikash Chandola1f847972022-09-28 09:47:32 +0000595 boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>&
James Feist6ef20402019-01-07 16:45:08 -0800596 sensors,
597 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
598{
599 if (!dbusConnection)
600 {
George Liu73f6cdc2025-02-20 15:15:19 +0800601 lg2::error("Connection not created");
James Feist6ef20402019-01-07 16:45:08 -0800602 return;
603 }
604 dbusConnection->async_method_call(
605 [&](boost::system::error_code ec, const ManagedObjectType& resp) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400606 if (ec)
James Feist6ef20402019-01-07 16:45:08 -0800607 {
George Liu73f6cdc2025-02-20 15:15:19 +0800608 lg2::error("Error contacting entity manager");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400609 return;
James Feist6ef20402019-01-07 16:45:08 -0800610 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400611 for (const auto& [path, interfaces] : resp)
612 {
613 for (const auto& [intf, cfg] : interfaces)
614 {
615 if (intf != configInterfaceName(sensorType))
616 {
617 continue;
618 }
619 std::string name = loadVariant<std::string>(cfg, "Name");
620
621 std::vector<thresholds::Threshold> sensorThresholds;
622 if (!parseThresholdsFromConfig(interfaces,
623 sensorThresholds))
624 {
George Liu73f6cdc2025-02-20 15:15:19 +0800625 lg2::error("error populating thresholds '{NAME}'",
626 "NAME", name);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400627 }
628 uint8_t deviceAddress =
629 loadVariant<uint8_t>(cfg, "Address");
630
631 std::string sensorClass =
632 loadVariant<std::string>(cfg, "Class");
633
634 uint8_t hostSMbusIndex = hostSMbusIndexDefault;
635 auto findSmType = cfg.find("HostSMbusIndex");
636 if (findSmType != cfg.end())
637 {
638 hostSMbusIndex = std::visit(
639 VariantToUnsignedIntVisitor(), findSmType->second);
640 }
641
642 float pollRate = getPollRate(cfg, pollRateDefault);
643
644 uint8_t ipmbBusIndex = ipmbBusIndexDefault;
645 auto findBusType = cfg.find("Bus");
646 if (findBusType != cfg.end())
647 {
648 ipmbBusIndex = std::visit(VariantToUnsignedIntVisitor(),
649 findBusType->second);
George Liu73f6cdc2025-02-20 15:15:19 +0800650 lg2::error("Ipmb Bus Index for '{NAME}' is '{INDEX}'",
651 "NAME", name, "INDEX", ipmbBusIndex);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400652 }
653
654 /* Default sensor type is "temperature" */
655 std::string sensorTypeName = "temperature";
656 auto findType = cfg.find("SensorType");
657 if (findType != cfg.end())
658 {
659 sensorTypeName = std::visit(VariantToStringVisitor(),
660 findType->second);
661 }
662
663 auto& sensor = sensors[name];
664 sensor = nullptr;
665 sensor = std::make_shared<IpmbSensor>(
666 dbusConnection, io, name, path, objectServer,
667 std::move(sensorThresholds), deviceAddress,
668 hostSMbusIndex, pollRate, sensorTypeName);
669
670 sensor->parseConfigValues(cfg);
671 if (!(sensor->sensorClassType(sensorClass)))
672 {
673 continue;
674 }
675 sensor->sensorSubType(sensorTypeName);
676 sensor->init();
677 }
678 }
679 },
JeffLin2c5a1f22022-10-05 15:19:09 +0800680 entityManagerName, "/xyz/openbmc_project/inventory",
681 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist6ef20402019-01-07 16:45:08 -0800682}
683
Kumar Thangavel5d032bc2022-11-30 20:24:47 +0530684void interfaceRemoved(
685 sdbusplus::message_t& message,
686 boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>&
687 sensors)
688{
689 if (message.is_method_error())
690 {
George Liu73f6cdc2025-02-20 15:15:19 +0800691 lg2::error("interfacesRemoved callback method error");
Kumar Thangavel5d032bc2022-11-30 20:24:47 +0530692 return;
693 }
694
695 sdbusplus::message::object_path removedPath;
696 std::vector<std::string> interfaces;
697
698 message.read(removedPath, interfaces);
699
700 // If the xyz.openbmc_project.Confguration.X interface was removed
701 // for one or more sensors, delete those sensor objects.
702 auto sensorIt = sensors.begin();
703 while (sensorIt != sensors.end())
704 {
705 if ((sensorIt->second->configurationPath == removedPath) &&
706 (std::find(interfaces.begin(), interfaces.end(),
707 configInterfaceName(sdrInterface)) != interfaces.end()))
708 {
709 sensorIt = sensors.erase(sensorIt);
710 }
711 else
712 {
713 sensorIt++;
714 }
715 }
716}