blob: 5019fd568d5306453a22c14fbc41e4928f0f9744 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
2// Copyright (c) 2018 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 "TachSensor.hpp"
18
Ed Tanouseacbfdd2024-04-04 12:00:24 -070019#include "SensorPaths.hpp"
20#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103021#include "Utils.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070022#include "sensor.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103023
Ed Tanouseacbfdd2024-04-04 12:00:24 -070024#include <boost/asio/buffer.hpp>
25#include <boost/asio/error.hpp>
26#include <boost/asio/io_context.hpp>
27#include <boost/asio/posix/stream_descriptor.hpp>
28#include <boost/asio/random_access_file.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080029#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -070030#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
32
Josh Lehan5170fe62022-08-03 13:17:41 -070033#include <charconv>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070034#include <chrono>
35#include <cstddef>
36#include <cstdint>
James Feist6714a252018-09-10 15:26:18 -070037#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070038#include <memory>
39#include <optional>
James Feist6714a252018-09-10 15:26:18 -070040#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070041#include <system_error>
Patrick Venture96e97db2019-10-31 13:44:38 -070042#include <utility>
43#include <vector>
James Feist6714a252018-09-10 15:26:18 -070044
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070045static constexpr unsigned int pwmPollMs = 500;
James Feist6714a252018-09-10 15:26:18 -070046
James Feistd8705872019-02-08 13:26:09 -080047TachSensor::TachSensor(const std::string& path, const std::string& objectType,
48 sdbusplus::asio::object_server& objectServer,
49 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist60c0ec72019-03-18 15:08:43 -070050 std::unique_ptr<PresenceSensor>&& presenceSensor,
James Feist7b18b1e2019-05-14 13:42:09 -070051 std::optional<RedundancySensor>* redundancy,
Ed Tanous1f978632023-02-28 18:16:39 -080052 boost::asio::io_context& io, const std::string& fanName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080053 std::vector<thresholds::Threshold>&& thresholdsIn,
James Feistd8705872019-02-08 13:26:09 -080054 const std::string& sensorConfiguration,
Ed Tanousf69fbf92022-01-14 10:15:33 -080055 const std::pair<double, double>& limits,
James Feist49a8ccd2020-09-16 16:09:52 -070056 const PowerState& powerState,
57 const std::optional<std::string>& ledIn) :
Zhikui Renda98f092021-11-01 09:41:08 -070058 Sensor(escapeName(fanName), std::move(thresholdsIn), sensorConfiguration,
59 objectType, false, false, limits.second, limits.first, conn,
60 powerState),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050061 objServer(objectServer), redundancy(redundancy),
Josh Lehan5170fe62022-08-03 13:17:41 -070062 presence(std::move(presenceSensor)),
63 inputDev(io, path, boost::asio::random_access_file::read_only),
64 waitTimer(io), path(path), led(ledIn)
James Feist6714a252018-09-10 15:26:18 -070065{
James Feist251c7822018-09-12 12:54:15 -070066 sensorInterface = objectServer.add_interface(
67 "/xyz/openbmc_project/sensors/fan_tach/" + name,
68 "xyz.openbmc_project.Sensor.Value");
69
Jayashree Dhanapal56678082022-01-04 17:27:20 +053070 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070071 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053072 std::string interface = thresholds::getInterface(threshold.level);
73 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
74 objectServer.add_interface(
75 "/xyz/openbmc_project/sensors/fan_tach/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070076 }
James Feist078f2322019-03-08 11:09:05 -080077 association = objectServer.add_interface(
78 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070079 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070080
81 if (presence)
82 {
83 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070084 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070085 "xyz.openbmc_project.Inventory.Item");
86 itemIface->register_property("PrettyName",
87 std::string()); // unused property
88 itemIface->register_property("Present", true);
89 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070090 itemAssoc = objectServer.add_interface(
91 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070092 itemAssoc->register_property(
Glukhov Mikhail6e6561d2024-01-10 11:58:48 +030093 "Associations",
James Feistd8bd5622019-06-26 12:09:05 -070094 std::vector<Association>{
95 {"sensors", "inventory",
96 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
97 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070098 }
Andrei Kartashev39287412022-02-04 16:04:47 +030099 setInitialProperties(sensor_paths::unitRPMs);
James Feist6714a252018-09-10 15:26:18 -0700100}
101
102TachSensor::~TachSensor()
103{
104 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700105 inputDev.close();
106 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530107 for (const auto& iface : thresholdInterfaces)
108 {
109 objServer.remove_interface(iface);
110 }
James Feist251c7822018-09-12 12:54:15 -0700111 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800112 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700113 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700114 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700115}
116
Josh Lehan5170fe62022-08-03 13:17:41 -0700117void TachSensor::setupRead()
James Feist6714a252018-09-10 15:26:18 -0700118{
Josh Lehan5170fe62022-08-03 13:17:41 -0700119 std::weak_ptr<TachSensor> weakRef = weak_from_this();
120 inputDev.async_read_some_at(
121 0, boost::asio::buffer(readBuf),
122 [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) {
123 std::shared_ptr<TachSensor> self = weakRef.lock();
124 if (self)
125 {
126 self->handleResponse(ec, bytesRead);
127 }
Patrick Williams597e8422023-10-20 11:19:01 -0500128 });
Josh Lehan5170fe62022-08-03 13:17:41 -0700129}
130
131void TachSensor::restartRead(size_t pollTime)
132{
133 std::weak_ptr<TachSensor> weakRef = weak_from_this();
Ed Tanous83db50c2023-03-01 10:20:24 -0800134 waitTimer.expires_after(std::chrono::milliseconds(pollTime));
Josh Lehan5170fe62022-08-03 13:17:41 -0700135 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
136 if (ec == boost::asio::error::operation_aborted)
137 {
138 return; // we're being canceled
139 }
140 std::shared_ptr<TachSensor> self = weakRef.lock();
141 if (!self)
142 {
143 return;
144 }
145 self->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700146 });
James Feist6714a252018-09-10 15:26:18 -0700147}
148
Josh Lehan5170fe62022-08-03 13:17:41 -0700149void TachSensor::handleResponse(const boost::system::error_code& err,
150 size_t bytesRead)
James Feist6714a252018-09-10 15:26:18 -0700151{
Josh Lehan5170fe62022-08-03 13:17:41 -0700152 if ((err == boost::system::errc::bad_file_descriptor) ||
153 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700154 {
Josh Lehan5170fe62022-08-03 13:17:41 -0700155 std::cerr << "TachSensor " << name << " removed " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700156 return; // we're being destroyed
157 }
James Feist7bc2bab2018-10-26 14:09:45 -0700158 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700159 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700160 if (presence)
161 {
162 if (!presence->getValue())
163 {
James Feist961bf092020-07-01 16:38:12 -0700164 markAvailable(false);
James Feist7bc2bab2018-10-26 14:09:45 -0700165 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700166 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700167 }
James Feist60c0ec72019-03-18 15:08:43 -0700168 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700169 }
Josh Lehan5170fe62022-08-03 13:17:41 -0700170
James Feist7bc2bab2018-10-26 14:09:45 -0700171 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700172 {
James Feist7bc2bab2018-10-26 14:09:45 -0700173 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700174 {
Josh Lehan5170fe62022-08-03 13:17:41 -0700175 const char* bufEnd = readBuf.data() + bytesRead;
176 int nvalue = 0;
Patrick Williams779c96a2023-05-10 07:50:42 -0500177 std::from_chars_result ret = std::from_chars(readBuf.data(), bufEnd,
178 nvalue);
Josh Lehan5170fe62022-08-03 13:17:41 -0700179 if (ret.ec != std::errc())
James Feist7bc2bab2018-10-26 14:09:45 -0700180 {
James Feist961bf092020-07-01 16:38:12 -0700181 incrementError();
182 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700183 }
Josh Lehan5170fe62022-08-03 13:17:41 -0700184 else
185 {
186 updateValue(nvalue);
187 }
James Feist6714a252018-09-10 15:26:18 -0700188 }
189 else
190 {
James Feist961bf092020-07-01 16:38:12 -0700191 incrementError();
192 pollTime = sensorFailedPollTimeMs;
James Feist71d31b22019-01-02 16:57:54 -0800193 }
James Feist6714a252018-09-10 15:26:18 -0700194 }
Ed Tanous99c44092022-01-14 09:59:09 -0800195
Josh Lehan5170fe62022-08-03 13:17:41 -0700196 restartRead(pollTime);
James Feist6714a252018-09-10 15:26:18 -0700197}
198
Ed Tanous201a1012024-04-03 18:07:28 -0700199void TachSensor::checkThresholds()
James Feist6714a252018-09-10 15:26:18 -0700200{
Zhikui Ren00bd56d2021-11-19 12:32:27 -0800201 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700202
Ed Tanous2049bd22022-07-09 07:20:26 -0700203 if ((redundancy != nullptr) && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700204 {
James Feist7b18b1e2019-05-14 13:42:09 -0700205 (*redundancy)
206 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700207 }
James Feist49a8ccd2020-09-16 16:09:52 -0700208
209 bool curLed = !status;
210 if (led && ledState != curLed)
211 {
212 ledState = curLed;
213 setLed(dbusConnection, *led, curLed);
214 }
James Feist6714a252018-09-10 15:26:18 -0700215}
216
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800217PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
Ed Tanous1f978632023-02-28 18:16:39 -0800218 boost::asio::io_context& io,
James Feist7b18b1e2019-05-14 13:42:09 -0700219 const std::string& name) :
Ed Tanous8a17c302021-09-02 15:07:11 -0700220 gpioLine(gpiod::find_line(gpioName)),
221 gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700222{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800223 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700224 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800225 std::cerr << "Error requesting gpio: " << gpioName << "\n";
226 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700227 return;
228 }
229
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800230 try
James Feist7bc2bab2018-10-26 14:09:45 -0700231 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800232 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
233 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
Ed Tanous2049bd22022-07-09 07:20:26 -0700234 status = (gpioLine.get_value() != 0);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800235
236 int gpioLineFd = gpioLine.event_get_fd();
237 if (gpioLineFd < 0)
238 {
239 std::cerr << "Failed to get " << gpioName << " fd\n";
240 return;
241 }
242
243 gpioFd.assign(gpioLineFd);
244 }
Patrick Williams26601e82021-10-06 12:43:25 -0500245 catch (const std::system_error&)
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800246 {
247 std::cerr << "Error reading gpio: " << gpioName << "\n";
248 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700249 return;
250 }
James Feist7bc2bab2018-10-26 14:09:45 -0700251
James Feist7bc2bab2018-10-26 14:09:45 -0700252 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700253}
254
255PresenceSensor::~PresenceSensor()
256{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800257 gpioFd.close();
258 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700259}
260
Ed Tanous201a1012024-04-03 18:07:28 -0700261void PresenceSensor::monitorPresence()
James Feist7bc2bab2018-10-26 14:09:45 -0700262{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800263 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
264 [this](const boost::system::error_code& ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700265 if (ec == boost::system::errc::bad_file_descriptor)
266 {
267 return; // we're being destroyed
268 }
269 if (ec)
270 {
271 std::cerr << "Error on presence sensor " << name << " \n";
272 ;
273 }
274 else
275 {
276 read();
277 }
278 monitorPresence();
279 });
James Feist7bc2bab2018-10-26 14:09:45 -0700280}
281
Ed Tanous201a1012024-04-03 18:07:28 -0700282void PresenceSensor::read()
James Feist7bc2bab2018-10-26 14:09:45 -0700283{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800284 gpioLine.event_read();
Ed Tanous2049bd22022-07-09 07:20:26 -0700285 status = (gpioLine.get_value() != 0);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800286 // Read is invoked when an edge event is detected by monitorPresence
287 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700288 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800289 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700290 }
291 else
292 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800293 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700294 }
295}
296
Ed Tanous201a1012024-04-03 18:07:28 -0700297bool PresenceSensor::getValue() const
James Feist7bc2bab2018-10-26 14:09:45 -0700298{
299 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700300}
301
James Feist9bb67462019-03-15 15:09:50 -0700302RedundancySensor::RedundancySensor(size_t count,
303 const std::vector<std::string>& children,
304 sdbusplus::asio::object_server& objectServer,
305 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700306 count(count),
307 iface(objectServer.add_interface(
308 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700309 "xyz.openbmc_project.Control.FanRedundancy")),
310 association(objectServer.add_interface(
311 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700312 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700313 objectServer(objectServer)
314{
James Feist9bb67462019-03-15 15:09:50 -0700315 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700316 iface->register_property("Collection", children);
317 iface->register_property("Status", std::string("Full"));
318 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
319 iface->initialize();
320}
321RedundancySensor::~RedundancySensor()
322{
James Feist9bb67462019-03-15 15:09:50 -0700323 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700324 objectServer.remove_interface(iface);
325}
James Feistd8705872019-02-08 13:26:09 -0800326void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700327{
328 statuses[name] = failed;
329 size_t failedCount = 0;
330
James Feist7b18b1e2019-05-14 13:42:09 -0700331 std::string newState = redundancy::full;
Zev Weiss0521a062022-08-12 18:21:02 -0700332 for (const auto& [name, status] : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700333 {
Zev Weiss0521a062022-08-12 18:21:02 -0700334 if (status)
James Feistdc6c55f2018-10-31 12:53:20 -0700335 {
336 failedCount++;
337 }
338 if (failedCount > count)
339 {
James Feist7b18b1e2019-05-14 13:42:09 -0700340 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700341 break;
342 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700343 if (failedCount != 0U)
James Feistdc6c55f2018-10-31 12:53:20 -0700344 {
James Feist7b18b1e2019-05-14 13:42:09 -0700345 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700346 }
347 }
James Feist7b18b1e2019-05-14 13:42:09 -0700348 if (state != newState)
349 {
350 if (state == redundancy::full)
351 {
352 logFanRedundancyLost();
353 }
354 else if (newState == redundancy::full)
355 {
356 logFanRedundancyRestored();
357 }
358 state = newState;
359 iface->set_property("Status", state);
360 }
James Feistdc6c55f2018-10-31 12:53:20 -0700361}