blob: c8a3c40639fcd7e35382110f769fa8afc0b3d2fd [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
Chris Cain83929002024-03-06 14:20:09 -060019#include "PresenceGpio.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"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070023#include "sensor.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103024
Ed Tanouseacbfdd2024-04-04 12:00:24 -070025#include <boost/asio/buffer.hpp>
26#include <boost/asio/error.hpp>
27#include <boost/asio/io_context.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070028#include <boost/asio/random_access_file.hpp>
George Liue34e1232025-02-20 11:27:48 +080029#include <phosphor-logging/lg2.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>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <memory>
38#include <optional>
James Feist6714a252018-09-10 15:26:18 -070039#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070040#include <system_error>
Patrick Venture96e97db2019-10-31 13:44:38 -070041#include <utility>
42#include <vector>
James Feist6714a252018-09-10 15:26:18 -070043
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070044static constexpr unsigned int pwmPollMs = 500;
James Feist6714a252018-09-10 15:26:18 -070045
Patrick Williams2aaf7172024-08-16 15:20:40 -040046TachSensor::TachSensor(
47 const std::string& path, const std::string& objectType,
48 sdbusplus::asio::object_server& objectServer,
49 std::shared_ptr<sdbusplus::asio::connection>& conn,
Chris Cain83929002024-03-06 14:20:09 -060050 std::shared_ptr<PresenceGpio>& presenceGpio,
Patrick Williams2aaf7172024-08-16 15:20:40 -040051 std::optional<RedundancySensor>* redundancy, boost::asio::io_context& io,
52 const std::string& fanName,
53 std::vector<thresholds::Threshold>&& thresholdsIn,
54 const std::string& sensorConfiguration,
55 const std::pair<double, double>& limits, const PowerState& powerState,
56 const std::optional<std::string>& ledIn) :
Zhikui Renda98f092021-11-01 09:41:08 -070057 Sensor(escapeName(fanName), std::move(thresholdsIn), sensorConfiguration,
58 objectType, false, false, limits.second, limits.first, conn,
59 powerState),
Chris Cain83929002024-03-06 14:20:09 -060060 objServer(objectServer), redundancy(redundancy), presence(presenceGpio),
Josh Lehan5170fe62022-08-03 13:17:41 -070061 inputDev(io, path, boost::asio::random_access_file::read_only),
62 waitTimer(io), path(path), led(ledIn)
James Feist6714a252018-09-10 15:26:18 -070063{
James Feist251c7822018-09-12 12:54:15 -070064 sensorInterface = objectServer.add_interface(
65 "/xyz/openbmc_project/sensors/fan_tach/" + name,
66 "xyz.openbmc_project.Sensor.Value");
67
Jayashree Dhanapal56678082022-01-04 17:27:20 +053068 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070069 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053070 std::string interface = thresholds::getInterface(threshold.level);
71 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
72 objectServer.add_interface(
73 "/xyz/openbmc_project/sensors/fan_tach/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070074 }
James Feist078f2322019-03-08 11:09:05 -080075 association = objectServer.add_interface(
76 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070077 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070078
79 if (presence)
80 {
Chris Cainc45e18f2024-07-24 15:58:00 -050081 presence->monitorPresence();
James Feist60c0ec72019-03-18 15:08:43 -070082 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070083 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070084 "xyz.openbmc_project.Inventory.Item");
85 itemIface->register_property("PrettyName",
86 std::string()); // unused property
87 itemIface->register_property("Present", true);
88 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070089 itemAssoc = objectServer.add_interface(
90 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070091 itemAssoc->register_property(
Glukhov Mikhail6e6561d2024-01-10 11:58:48 +030092 "Associations",
James Feistd8bd5622019-06-26 12:09:05 -070093 std::vector<Association>{
94 {"sensors", "inventory",
95 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
96 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070097 }
Andrei Kartashev39287412022-02-04 16:04:47 +030098 setInitialProperties(sensor_paths::unitRPMs);
James Feist6714a252018-09-10 15:26:18 -070099}
100
101TachSensor::~TachSensor()
102{
103 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700104 inputDev.close();
105 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530106 for (const auto& iface : thresholdInterfaces)
107 {
108 objServer.remove_interface(iface);
109 }
James Feist251c7822018-09-12 12:54:15 -0700110 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800111 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700112 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700113 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700114}
115
Josh Lehan5170fe62022-08-03 13:17:41 -0700116void TachSensor::setupRead()
James Feist6714a252018-09-10 15:26:18 -0700117{
Josh Lehan5170fe62022-08-03 13:17:41 -0700118 std::weak_ptr<TachSensor> weakRef = weak_from_this();
119 inputDev.async_read_some_at(
120 0, boost::asio::buffer(readBuf),
121 [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400122 std::shared_ptr<TachSensor> self = weakRef.lock();
123 if (self)
124 {
125 self->handleResponse(ec, bytesRead);
126 }
127 });
Josh Lehan5170fe62022-08-03 13:17:41 -0700128}
129
130void TachSensor::restartRead(size_t pollTime)
131{
132 std::weak_ptr<TachSensor> weakRef = weak_from_this();
Ed Tanous83db50c2023-03-01 10:20:24 -0800133 waitTimer.expires_after(std::chrono::milliseconds(pollTime));
Josh Lehan5170fe62022-08-03 13:17:41 -0700134 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
135 if (ec == boost::asio::error::operation_aborted)
136 {
137 return; // we're being canceled
138 }
139 std::shared_ptr<TachSensor> self = weakRef.lock();
140 if (!self)
141 {
142 return;
143 }
144 self->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700145 });
James Feist6714a252018-09-10 15:26:18 -0700146}
147
Josh Lehan5170fe62022-08-03 13:17:41 -0700148void TachSensor::handleResponse(const boost::system::error_code& err,
149 size_t bytesRead)
James Feist6714a252018-09-10 15:26:18 -0700150{
Josh Lehan5170fe62022-08-03 13:17:41 -0700151 if ((err == boost::system::errc::bad_file_descriptor) ||
152 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700153 {
George Liue34e1232025-02-20 11:27:48 +0800154 lg2::error("TachSensor '{NAME}' removed '{PATH}'", "NAME", name, "PATH",
155 path);
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 {
Chris Cain83929002024-03-06 14:20:09 -0600162 if (!presence->isPresent())
James Feist7bc2bab2018-10-26 14:09:45 -0700163 {
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 Williams2aaf7172024-08-16 15:20:40 -0400177 std::from_chars_result ret =
178 std::from_chars(readBuf.data(), bufEnd, 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
James Feist9bb67462019-03-15 15:09:50 -0700217RedundancySensor::RedundancySensor(size_t count,
218 const std::vector<std::string>& children,
219 sdbusplus::asio::object_server& objectServer,
220 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700221 count(count),
222 iface(objectServer.add_interface(
223 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700224 "xyz.openbmc_project.Control.FanRedundancy")),
225 association(objectServer.add_interface(
226 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700227 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700228 objectServer(objectServer)
229{
James Feist9bb67462019-03-15 15:09:50 -0700230 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700231 iface->register_property("Collection", children);
232 iface->register_property("Status", std::string("Full"));
233 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
234 iface->initialize();
235}
236RedundancySensor::~RedundancySensor()
237{
James Feist9bb67462019-03-15 15:09:50 -0700238 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700239 objectServer.remove_interface(iface);
240}
James Feistd8705872019-02-08 13:26:09 -0800241void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700242{
243 statuses[name] = failed;
244 size_t failedCount = 0;
245
James Feist7b18b1e2019-05-14 13:42:09 -0700246 std::string newState = redundancy::full;
Zev Weiss0521a062022-08-12 18:21:02 -0700247 for (const auto& [name, status] : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700248 {
Zev Weiss0521a062022-08-12 18:21:02 -0700249 if (status)
James Feistdc6c55f2018-10-31 12:53:20 -0700250 {
251 failedCount++;
252 }
253 if (failedCount > count)
254 {
James Feist7b18b1e2019-05-14 13:42:09 -0700255 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700256 break;
257 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700258 if (failedCount != 0U)
James Feistdc6c55f2018-10-31 12:53:20 -0700259 {
James Feist7b18b1e2019-05-14 13:42:09 -0700260 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700261 }
262 }
James Feist7b18b1e2019-05-14 13:42:09 -0700263 if (state != newState)
264 {
265 if (state == redundancy::full)
266 {
267 logFanRedundancyLost();
268 }
269 else if (newState == redundancy::full)
270 {
271 logFanRedundancyRestored();
272 }
273 state = newState;
274 iface->set_property("Status", state);
275 }
James Feistdc6c55f2018-10-31 12:53:20 -0700276}