blob: 87ad632577386850019687d525ca71aade1f6923 [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
17#include "IpmbSensor.hpp"
18
19#include "Utils.hpp"
20#include "VariantVisitors.hpp"
21
22#include <math.h>
23
24#include <boost/algorithm/string.hpp>
25#include <boost/algorithm/string/predicate.hpp>
26#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -070028#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30#include <sdbusplus/bus/match.hpp>
31
James Feist6ef20402019-01-07 16:45:08 -080032#include <chrono>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <functional>
James Feist6ef20402019-01-07 16:45:08 -080034#include <iostream>
35#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <memory>
James Feist6ef20402019-01-07 16:45:08 -080037#include <numeric>
Patrick Venture96e97db2019-10-31 13:44:38 -070038#include <string>
39#include <tuple>
40#include <variant>
James Feist6ef20402019-01-07 16:45:08 -080041#include <vector>
42
43constexpr const bool debug = false;
44
45constexpr const char* configInterface =
46 "xyz.openbmc_project.Configuration.IpmbSensor";
47static constexpr double ipmbMaxReading = 0xFF;
48static constexpr double ipmbMinReading = 0;
49
50static constexpr uint8_t meAddress = 1;
51static constexpr uint8_t lun = 0;
52
Vijay Khemka682a5cb2019-07-18 17:34:03 -070053static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
54
James Feist6ef20402019-01-07 16:45:08 -080055using IpmbMethodType =
56 std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>;
57
James Feistf7e2c5d2019-02-13 17:27:51 -080058boost::container::flat_map<std::string, std::unique_ptr<IpmbSensor>> sensors;
59
James Feist0d4f2bd2019-03-05 13:15:40 -080060std::unique_ptr<boost::asio::deadline_timer> initCmdTimer;
61
James Feist6ef20402019-01-07 16:45:08 -080062IpmbSensor::IpmbSensor(std::shared_ptr<sdbusplus::asio::connection>& conn,
63 boost::asio::io_service& io,
64 const std::string& sensorName,
65 const std::string& sensorConfiguration,
66 sdbusplus::asio::object_server& objectServer,
67 std::vector<thresholds::Threshold>&& thresholdData,
Vijay Khemka682a5cb2019-07-18 17:34:03 -070068 uint8_t deviceAddress, std::string& sensorTypeName) :
James Feist6ef20402019-01-07 16:45:08 -080069 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feist930fcde2019-05-28 12:58:43 -070070 std::move(thresholdData), sensorConfiguration,
71 "xyz.openbmc_project.Configuration.ExitAirTemp", ipmbMaxReading,
72 ipmbMinReading),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050073 deviceAddress(deviceAddress), readState(PowerState::on),
74 objectServer(objectServer), dbusConnection(conn), waitTimer(io)
James Feist6ef20402019-01-07 16:45:08 -080075{
Vijay Khemka682a5cb2019-07-18 17:34:03 -070076 std::string dbusPath = sensorPathPrefix + sensorTypeName + "/" + name;
77
James Feist6ef20402019-01-07 16:45:08 -080078 sensorInterface = objectServer.add_interface(
Vijay Khemka682a5cb2019-07-18 17:34:03 -070079 dbusPath, "xyz.openbmc_project.Sensor.Value");
James Feist6ef20402019-01-07 16:45:08 -080080
81 if (thresholds::hasWarningInterface(thresholds))
82 {
83 thresholdInterfaceWarning = objectServer.add_interface(
Vijay Khemka682a5cb2019-07-18 17:34:03 -070084 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
James Feist6ef20402019-01-07 16:45:08 -080085 }
86 if (thresholds::hasCriticalInterface(thresholds))
87 {
88 thresholdInterfaceCritical = objectServer.add_interface(
Vijay Khemka682a5cb2019-07-18 17:34:03 -070089 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
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 setupPowerMatch(conn);
93}
94
95IpmbSensor::~IpmbSensor()
96{
97 waitTimer.cancel();
98 objectServer.remove_interface(thresholdInterfaceWarning);
99 objectServer.remove_interface(thresholdInterfaceCritical);
100 objectServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800101 objectServer.remove_interface(association);
James Feist6ef20402019-01-07 16:45:08 -0800102}
103
104void IpmbSensor::init(void)
105{
106 setInitialProperties(dbusConnection);
107 loadDefaults();
108 if (initCommand)
109 {
James Feistf7e2c5d2019-02-13 17:27:51 -0800110 runInitCmd();
111 }
112 read();
113}
114
115void IpmbSensor::runInitCmd()
116{
117 if (initCommand)
118 {
James Feist6ef20402019-01-07 16:45:08 -0800119 dbusConnection->async_method_call(
120 [this](boost::system::error_code ec,
121 const IpmbMethodType& response) {
122 const int& status = std::get<0>(response);
123
124 if (ec || status)
125 {
126 std::cerr
127 << "Error setting init command for device: " << name
128 << "\n";
129 }
James Feist6ef20402019-01-07 16:45:08 -0800130 },
131 "xyz.openbmc_project.Ipmi.Channel.Ipmb",
132 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
133 "sendRequest", commandAddress, netfn, lun, *initCommand, initData);
134 }
James Feist6ef20402019-01-07 16:45:08 -0800135}
136
137void IpmbSensor::loadDefaults()
138{
139 if (type == IpmbType::meSensor)
140 {
141 commandAddress = meAddress;
142 netfn = 0x4; // sensor
143 command = 0x2d; // get sensor reading
144 commandData = {deviceAddress};
145 }
146 else if (type == IpmbType::PXE1410CVR)
147 {
148 commandAddress = meAddress;
149 netfn = 0x2e; // me bridge
150 command = 0xd9; // send raw pmbus
151 initCommand = 0xd9; // send raw pmbus
152 commandData = {0x57, 0x01, 0x00, 0x16, 0x03, deviceAddress, 00,
153 0x00, 0x00, 0x00, 0x01, 0x02, 0x29};
154 initData = {0x57, 0x01, 0x00, 0x14, 0x03, deviceAddress, 0x00,
155 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x60};
156 }
157 else if (type == IpmbType::IR38363VR)
158 {
159 commandAddress = meAddress;
160 netfn = 0x2e; // me bridge
161 command = 0xd9; // send raw pmbus
162 commandData = {0x57, 0x01, 0x00, 0x16, 0x03, deviceAddress, 00,
163 0x00, 0x00, 0x00, 0x01, 0x02, 0x8D};
164 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700165 else if (type == IpmbType::ADM1278HSC)
166 {
167 commandAddress = meAddress;
168 switch (subType)
169 {
170 case IpmbSubType::temp:
171 case IpmbSubType::curr:
172 uint8_t snsNum;
173 if (subType == IpmbSubType::temp)
174 snsNum = 0x8d;
175 else
176 snsNum = 0x8c;
177 netfn = 0x2e; // me bridge
178 command = 0xd9; // send raw pmbus
179 commandData = {0x57, 0x01, 0x00, 0x86, deviceAddress,
180 0x00, 0x00, 0x01, 0x02, snsNum};
181 break;
182 case IpmbSubType::power:
183 case IpmbSubType::volt:
184 netfn = 0x4; // sensor
185 command = 0x2d; // get sensor reading
186 commandData = {deviceAddress};
187 break;
188 default:
189 throw std::runtime_error("Invalid sensor type");
190 }
191 }
James Feist6ef20402019-01-07 16:45:08 -0800192 else if (type == IpmbType::mpsVR)
193 {
194 commandAddress = meAddress;
195 netfn = 0x2e; // me bridge
196 command = 0xd9; // send raw pmbus
197 initCommand = 0xd9; // send raw pmbus
198 commandData = {0x57, 0x01, 0x00, 0x16, 0x3, deviceAddress, 0x00,
199 0x00, 0x00, 0x00, 0x01, 0x02, 0x8d};
200 initData = {0x57, 0x01, 0x00, 0x14, 0x03, deviceAddress, 0x00,
201 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00};
202 }
203 else
204 {
205 throw std::runtime_error("Invalid sensor type");
206 }
207}
208
209void IpmbSensor::checkThresholds(void)
210{
211 if (readState == PowerState::on && !isPowerOn())
212 {
213 return;
214 }
James Feistfc94b212019-02-06 16:14:51 -0800215 else if (readState == PowerState::biosPost && !hasBiosPost())
216 {
217 return;
218 }
James Feist6ef20402019-01-07 16:45:08 -0800219 thresholds::checkThresholds(this);
220}
221
James Feist551087a2019-12-09 11:17:12 -0800222void IpmbSensor::processError(void)
223{
224 static constexpr size_t errorFilter = 2;
225
226 if (!errorCount)
227 {
228 std::cerr << "Invalid data from device: " << name << "\n";
229 }
230 else if (errorCount > errorFilter)
231 {
232 updateValue(0);
233 }
234 if (errorCount < std::numeric_limits<uint8_t>::max())
235 {
236 errorCount++;
237 }
238}
239
James Feist6ef20402019-01-07 16:45:08 -0800240void IpmbSensor::read(void)
241{
242 static constexpr size_t pollTime = 1; // in seconds
243
244 waitTimer.expires_from_now(boost::posix_time::seconds(pollTime));
245 waitTimer.async_wait([this](const boost::system::error_code& ec) {
246 if (ec == boost::asio::error::operation_aborted)
247 {
248 return; // we're being canceled
249 }
James Feist52497fd2019-06-07 13:01:33 -0700250 if (!isPowerOn() && readState != PowerState::always)
James Feist6ef20402019-01-07 16:45:08 -0800251 {
252 updateValue(0);
253 read();
254 return;
255 }
256 dbusConnection->async_method_call(
257 [this](boost::system::error_code ec,
258 const IpmbMethodType& response) {
259 const int& status = std::get<0>(response);
260 if (ec || status)
261 {
James Feist551087a2019-12-09 11:17:12 -0800262 processError();
James Feist6ef20402019-01-07 16:45:08 -0800263 updateValue(0);
264 read();
265 return;
266 }
James Feist52497fd2019-06-07 13:01:33 -0700267 if (!isPowerOn() && readState != PowerState::always)
James Feist6ef20402019-01-07 16:45:08 -0800268 {
269 updateValue(0);
270 read();
271 return;
272 }
273 const std::vector<uint8_t>& data = std::get<5>(response);
274 if constexpr (debug)
275 {
276 std::cout << name << ": ";
277 for (size_t d : data)
278 {
279 std::cout << d << " ";
280 }
281 std::cout << "\n";
282 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700283 double value = 0;
James Feist6ef20402019-01-07 16:45:08 -0800284 if (type == IpmbType::meSensor)
285 {
286 if (data.empty())
287 {
James Feist551087a2019-12-09 11:17:12 -0800288 processError();
James Feist6ef20402019-01-07 16:45:08 -0800289 read();
290 return;
291 }
292 value = data[0];
293 }
294 else if (type == IpmbType::PXE1410CVR ||
295 type == IpmbType::IR38363VR)
296 {
Patrick Venture61bc6dc2019-10-10 09:17:04 -0700297 if (data.size() < 5)
James Feist6ef20402019-01-07 16:45:08 -0800298 {
James Feist551087a2019-12-09 11:17:12 -0800299 processError();
James Feist6ef20402019-01-07 16:45:08 -0800300 read();
301 return;
302 }
303 // format based on the 11 bit linear data format
304 value = ((data[4] << 8) | data[3]) >> 3;
305 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700306 else if (type == IpmbType::ADM1278HSC)
307 {
308 if (data.empty())
309 {
James Feist551087a2019-12-09 11:17:12 -0800310 processError();
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700311 read();
312 return;
313 }
314 switch (subType)
315 {
316 case IpmbSubType::temp:
317 case IpmbSubType::curr:
318 // format based on the 11 bit linear data format
319 value = ((data[4] << 8) | data[3]);
320 break;
321 case IpmbSubType::power:
322 case IpmbSubType::volt:
323 value = data[0];
324 break;
325 }
326 }
James Feist6ef20402019-01-07 16:45:08 -0800327 else if (type == IpmbType::mpsVR)
328 {
329 if (data.size() < 4)
330 {
James Feist551087a2019-12-09 11:17:12 -0800331 processError();
James Feist6ef20402019-01-07 16:45:08 -0800332 read();
333 return;
334 }
335 value = data[3];
336 }
337 else
338 {
339 throw std::runtime_error("Invalid sensor type");
340 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700341
342 /* Adjust value as per scale and offset */
343 value = (value * scaleVal) + offsetVal;
James Feist6ef20402019-01-07 16:45:08 -0800344 updateValue(value);
345 read();
James Feist551087a2019-12-09 11:17:12 -0800346 errorCount = 0; // success
James Feist6ef20402019-01-07 16:45:08 -0800347 },
348 "xyz.openbmc_project.Ipmi.Channel.Ipmb",
349 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
350 "sendRequest", commandAddress, netfn, lun, command, commandData);
351 });
352}
353void createSensors(
354 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
355 boost::container::flat_map<std::string, std::unique_ptr<IpmbSensor>>&
356 sensors,
357 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
358{
359 if (!dbusConnection)
360 {
361 std::cerr << "Connection not created\n";
362 return;
363 }
364 dbusConnection->async_method_call(
365 [&](boost::system::error_code ec, const ManagedObjectType& resp) {
366 if (ec)
367 {
368 std::cerr << "Error contacting entity manager\n";
369 return;
370 }
371 for (const auto& pathPair : resp)
372 {
373 for (const auto& entry : pathPair.second)
374 {
375 if (entry.first != configInterface)
376 {
377 continue;
378 }
379 std::string name =
380 loadVariant<std::string>(entry.second, "Name");
381
382 std::vector<thresholds::Threshold> sensorThresholds;
383 if (!parseThresholdsFromConfig(pathPair.second,
384 sensorThresholds))
385 {
386 std::cerr << "error populating thresholds for " << name
387 << "\n";
388 }
389 uint8_t deviceAddress =
390 loadVariant<uint8_t>(entry.second, "Address");
391
392 std::string sensorClass =
393 loadVariant<std::string>(entry.second, "Class");
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700394
395 /* Default sensor type is "temperature" */
396 std::string sensorTypeName = "temperature";
397 auto findType = entry.second.find("SensorType");
398 if (findType != entry.second.end())
399 {
400 sensorTypeName = std::visit(VariantToStringVisitor(),
401 findType->second);
402 }
403
James Feist6ef20402019-01-07 16:45:08 -0800404 auto& sensor = sensors[name];
405 sensor = std::make_unique<IpmbSensor>(
406 dbusConnection, io, name, pathPair.first, objectServer,
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700407 std::move(sensorThresholds), deviceAddress,
408 sensorTypeName);
409
410 /* Initialize scale and offset value */
411 sensor->scaleVal = 1;
412 sensor->offsetVal = 0;
413
414 auto findScaleVal = entry.second.find("ScaleValue");
415 if (findScaleVal != entry.second.end())
416 {
417 sensor->scaleVal = std::visit(VariantToDoubleVisitor(),
418 findScaleVal->second);
419 }
420
421 auto findOffsetVal = entry.second.find("OffsetValue");
422 if (findOffsetVal != entry.second.end())
423 {
424 sensor->offsetVal = std::visit(VariantToDoubleVisitor(),
425 findOffsetVal->second);
426 }
James Feist6ef20402019-01-07 16:45:08 -0800427
James Feistfc94b212019-02-06 16:14:51 -0800428 auto findPowerState = entry.second.find("PowerState");
429
430 if (findPowerState != entry.second.end())
431 {
432 std::string powerState = std::visit(
433 VariantToStringVisitor(), findPowerState->second);
434
435 setReadState(powerState, sensor->readState);
436 }
437
James Feist6ef20402019-01-07 16:45:08 -0800438 if (sensorClass == "PxeBridgeTemp")
439 {
440 sensor->type = IpmbType::PXE1410CVR;
441 }
442 else if (sensorClass == "IRBridgeTemp")
443 {
444 sensor->type = IpmbType::IR38363VR;
445 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700446 else if (sensorClass == "HSCBridge")
447 {
448 sensor->type = IpmbType::ADM1278HSC;
449 }
James Feist6ef20402019-01-07 16:45:08 -0800450 else if (sensorClass == "MpsBridgeTemp")
451 {
452 sensor->type = IpmbType::mpsVR;
453 }
454 else if (sensorClass == "METemp")
455 {
456 sensor->type = IpmbType::meSensor;
457 }
458 else
459 {
460 std::cerr << "Invalid class " << sensorClass << "\n";
461 continue;
462 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700463
464 if (sensorTypeName == "voltage")
465 {
466 sensor->subType = IpmbSubType::volt;
467 }
468 else if (sensorTypeName == "power")
469 {
470 sensor->subType = IpmbSubType::power;
471 }
472 else if (sensorTypeName == "current")
473 {
474 sensor->subType = IpmbSubType::curr;
475 }
476 else
477 {
478 sensor->subType = IpmbSubType::temp;
479 }
James Feist6ef20402019-01-07 16:45:08 -0800480 sensor->init();
481 }
482 }
483 },
484 entityManagerName, "/", "org.freedesktop.DBus.ObjectManager",
485 "GetManagedObjects");
486}
487
James Feistf7e2c5d2019-02-13 17:27:51 -0800488void reinitSensors(sdbusplus::message::message& message)
489{
James Feist0d4f2bd2019-03-05 13:15:40 -0800490 constexpr const size_t reinitWaitSeconds = 2;
James Feistf7e2c5d2019-02-13 17:27:51 -0800491 std::string objectName;
James Feist52497fd2019-06-07 13:01:33 -0700492 boost::container::flat_map<std::string, std::variant<std::string>> values;
James Feistf7e2c5d2019-02-13 17:27:51 -0800493 message.read(objectName, values);
James Feist0d4f2bd2019-03-05 13:15:40 -0800494
James Feist52497fd2019-06-07 13:01:33 -0700495 auto findStatus = values.find(power::property);
496 if (findStatus != values.end())
James Feistf7e2c5d2019-02-13 17:27:51 -0800497 {
James Feist52497fd2019-06-07 13:01:33 -0700498 bool powerStatus = boost::ends_with(
499 std::get<std::string>(findStatus->second), "Running");
James Feistf7e2c5d2019-02-13 17:27:51 -0800500 if (powerStatus)
501 {
James Feist0d4f2bd2019-03-05 13:15:40 -0800502 if (!initCmdTimer)
James Feistf7e2c5d2019-02-13 17:27:51 -0800503 {
James Feist0d4f2bd2019-03-05 13:15:40 -0800504 // this should be impossible
505 return;
James Feistf7e2c5d2019-02-13 17:27:51 -0800506 }
James Feist0d4f2bd2019-03-05 13:15:40 -0800507 // we seem to send this command too fast sometimes, wait before
508 // sending
509 initCmdTimer->expires_from_now(
510 boost::posix_time::seconds(reinitWaitSeconds));
511
512 initCmdTimer->async_wait([](const boost::system::error_code ec) {
513 if (ec == boost::asio::error::operation_aborted)
514 {
515 return; // we're being canceled
516 }
517
518 for (const auto& sensor : sensors)
519 {
520 if (sensor.second)
521 {
522 sensor.second->runInitCmd();
523 }
524 }
525 });
James Feistf7e2c5d2019-02-13 17:27:51 -0800526 }
527 }
528}
529
James Feistb6c0b912019-07-09 12:21:44 -0700530int main()
James Feist6ef20402019-01-07 16:45:08 -0800531{
532
533 boost::asio::io_service io;
534 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
535 systemBus->request_name("xyz.openbmc_project.IpmbSensor");
536 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6ef20402019-01-07 16:45:08 -0800537
James Feist0d4f2bd2019-03-05 13:15:40 -0800538 initCmdTimer = std::make_unique<boost::asio::deadline_timer>(io);
539
James Feist6ef20402019-01-07 16:45:08 -0800540 io.post([&]() { createSensors(io, objectServer, sensors, systemBus); });
541
542 boost::asio::deadline_timer configTimer(io);
543
544 std::function<void(sdbusplus::message::message&)> eventHandler =
James Feistb6c0b912019-07-09 12:21:44 -0700545 [&](sdbusplus::message::message&) {
James Feist6ef20402019-01-07 16:45:08 -0800546 configTimer.expires_from_now(boost::posix_time::seconds(1));
547 // create a timer because normally multiple properties change
548 configTimer.async_wait([&](const boost::system::error_code& ec) {
549 if (ec == boost::asio::error::operation_aborted)
550 {
551 return; // we're being canceled
552 }
553 createSensors(io, objectServer, sensors, systemBus);
554 if (sensors.empty())
555 {
556 std::cout << "Configuration not detected\n";
557 }
558 });
559 };
560
James Feistf7e2c5d2019-02-13 17:27:51 -0800561 sdbusplus::bus::match::match configMatch(
James Feist6ef20402019-01-07 16:45:08 -0800562 static_cast<sdbusplus::bus::bus&>(*systemBus),
563 "type='signal',member='PropertiesChanged',path_namespace='" +
564 std::string(inventoryPath) + "',arg0namespace='" + configInterface +
565 "'",
566 eventHandler);
567
James Feistf7e2c5d2019-02-13 17:27:51 -0800568 sdbusplus::bus::match::match powerChangeMatch(
569 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist52497fd2019-06-07 13:01:33 -0700570 "type='signal',interface='" + std::string(properties::interface) +
571 "',path='" + std::string(power::path) + "',arg0='" +
572 std::string(power::interface) + "'",
James Feistf7e2c5d2019-02-13 17:27:51 -0800573 reinitSensors);
574
James Feist6ef20402019-01-07 16:45:08 -0800575 io.run();
576}