blob: 790848815e0d5de63f6867c9f961db2b795f7c55 [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>
James Feist38fb5982020-05-28 10:09:54 -070029#include <sdbusplus/asio/connection.hpp>
30#include <sdbusplus/asio/object_server.hpp>
31
Josh Lehan5170fe62022-08-03 13:17:41 -070032#include <charconv>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070033#include <chrono>
34#include <cstddef>
35#include <cstdint>
James Feist6714a252018-09-10 15:26:18 -070036#include <iostream>
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 {
81 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070082 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070083 "xyz.openbmc_project.Inventory.Item");
84 itemIface->register_property("PrettyName",
85 std::string()); // unused property
86 itemIface->register_property("Present", true);
87 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070088 itemAssoc = objectServer.add_interface(
89 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070090 itemAssoc->register_property(
Glukhov Mikhail6e6561d2024-01-10 11:58:48 +030091 "Associations",
James Feistd8bd5622019-06-26 12:09:05 -070092 std::vector<Association>{
93 {"sensors", "inventory",
94 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
95 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070096 }
Andrei Kartashev39287412022-02-04 16:04:47 +030097 setInitialProperties(sensor_paths::unitRPMs);
James Feist6714a252018-09-10 15:26:18 -070098}
99
100TachSensor::~TachSensor()
101{
102 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700103 inputDev.close();
104 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530105 for (const auto& iface : thresholdInterfaces)
106 {
107 objServer.remove_interface(iface);
108 }
James Feist251c7822018-09-12 12:54:15 -0700109 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800110 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700111 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700112 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700113}
114
Josh Lehan5170fe62022-08-03 13:17:41 -0700115void TachSensor::setupRead()
James Feist6714a252018-09-10 15:26:18 -0700116{
Josh Lehan5170fe62022-08-03 13:17:41 -0700117 std::weak_ptr<TachSensor> weakRef = weak_from_this();
118 inputDev.async_read_some_at(
119 0, boost::asio::buffer(readBuf),
120 [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400121 std::shared_ptr<TachSensor> self = weakRef.lock();
122 if (self)
123 {
124 self->handleResponse(ec, bytesRead);
125 }
126 });
Josh Lehan5170fe62022-08-03 13:17:41 -0700127}
128
129void TachSensor::restartRead(size_t pollTime)
130{
131 std::weak_ptr<TachSensor> weakRef = weak_from_this();
Ed Tanous83db50c2023-03-01 10:20:24 -0800132 waitTimer.expires_after(std::chrono::milliseconds(pollTime));
Josh Lehan5170fe62022-08-03 13:17:41 -0700133 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
134 if (ec == boost::asio::error::operation_aborted)
135 {
136 return; // we're being canceled
137 }
138 std::shared_ptr<TachSensor> self = weakRef.lock();
139 if (!self)
140 {
141 return;
142 }
143 self->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700144 });
James Feist6714a252018-09-10 15:26:18 -0700145}
146
Josh Lehan5170fe62022-08-03 13:17:41 -0700147void TachSensor::handleResponse(const boost::system::error_code& err,
148 size_t bytesRead)
James Feist6714a252018-09-10 15:26:18 -0700149{
Josh Lehan5170fe62022-08-03 13:17:41 -0700150 if ((err == boost::system::errc::bad_file_descriptor) ||
151 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700152 {
Josh Lehan5170fe62022-08-03 13:17:41 -0700153 std::cerr << "TachSensor " << name << " removed " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700154 return; // we're being destroyed
155 }
James Feist7bc2bab2018-10-26 14:09:45 -0700156 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700157 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700158 if (presence)
159 {
Chris Cain83929002024-03-06 14:20:09 -0600160 if (!presence->isPresent())
James Feist7bc2bab2018-10-26 14:09:45 -0700161 {
James Feist961bf092020-07-01 16:38:12 -0700162 markAvailable(false);
James Feist7bc2bab2018-10-26 14:09:45 -0700163 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700164 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700165 }
James Feist60c0ec72019-03-18 15:08:43 -0700166 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700167 }
Josh Lehan5170fe62022-08-03 13:17:41 -0700168
James Feist7bc2bab2018-10-26 14:09:45 -0700169 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700170 {
James Feist7bc2bab2018-10-26 14:09:45 -0700171 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700172 {
Josh Lehan5170fe62022-08-03 13:17:41 -0700173 const char* bufEnd = readBuf.data() + bytesRead;
174 int nvalue = 0;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400175 std::from_chars_result ret =
176 std::from_chars(readBuf.data(), bufEnd, nvalue);
Josh Lehan5170fe62022-08-03 13:17:41 -0700177 if (ret.ec != std::errc())
James Feist7bc2bab2018-10-26 14:09:45 -0700178 {
James Feist961bf092020-07-01 16:38:12 -0700179 incrementError();
180 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700181 }
Josh Lehan5170fe62022-08-03 13:17:41 -0700182 else
183 {
184 updateValue(nvalue);
185 }
James Feist6714a252018-09-10 15:26:18 -0700186 }
187 else
188 {
James Feist961bf092020-07-01 16:38:12 -0700189 incrementError();
190 pollTime = sensorFailedPollTimeMs;
James Feist71d31b22019-01-02 16:57:54 -0800191 }
James Feist6714a252018-09-10 15:26:18 -0700192 }
Ed Tanous99c44092022-01-14 09:59:09 -0800193
Josh Lehan5170fe62022-08-03 13:17:41 -0700194 restartRead(pollTime);
James Feist6714a252018-09-10 15:26:18 -0700195}
196
Ed Tanous201a1012024-04-03 18:07:28 -0700197void TachSensor::checkThresholds()
James Feist6714a252018-09-10 15:26:18 -0700198{
Zhikui Ren00bd56d2021-11-19 12:32:27 -0800199 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700200
Ed Tanous2049bd22022-07-09 07:20:26 -0700201 if ((redundancy != nullptr) && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700202 {
James Feist7b18b1e2019-05-14 13:42:09 -0700203 (*redundancy)
204 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700205 }
James Feist49a8ccd2020-09-16 16:09:52 -0700206
207 bool curLed = !status;
208 if (led && ledState != curLed)
209 {
210 ledState = curLed;
211 setLed(dbusConnection, *led, curLed);
212 }
James Feist6714a252018-09-10 15:26:18 -0700213}
214
James Feist9bb67462019-03-15 15:09:50 -0700215RedundancySensor::RedundancySensor(size_t count,
216 const std::vector<std::string>& children,
217 sdbusplus::asio::object_server& objectServer,
218 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700219 count(count),
220 iface(objectServer.add_interface(
221 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700222 "xyz.openbmc_project.Control.FanRedundancy")),
223 association(objectServer.add_interface(
224 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700225 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700226 objectServer(objectServer)
227{
James Feist9bb67462019-03-15 15:09:50 -0700228 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700229 iface->register_property("Collection", children);
230 iface->register_property("Status", std::string("Full"));
231 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
232 iface->initialize();
233}
234RedundancySensor::~RedundancySensor()
235{
James Feist9bb67462019-03-15 15:09:50 -0700236 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700237 objectServer.remove_interface(iface);
238}
James Feistd8705872019-02-08 13:26:09 -0800239void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700240{
241 statuses[name] = failed;
242 size_t failedCount = 0;
243
James Feist7b18b1e2019-05-14 13:42:09 -0700244 std::string newState = redundancy::full;
Zev Weiss0521a062022-08-12 18:21:02 -0700245 for (const auto& [name, status] : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700246 {
Zev Weiss0521a062022-08-12 18:21:02 -0700247 if (status)
James Feistdc6c55f2018-10-31 12:53:20 -0700248 {
249 failedCount++;
250 }
251 if (failedCount > count)
252 {
James Feist7b18b1e2019-05-14 13:42:09 -0700253 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700254 break;
255 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700256 if (failedCount != 0U)
James Feistdc6c55f2018-10-31 12:53:20 -0700257 {
James Feist7b18b1e2019-05-14 13:42:09 -0700258 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700259 }
260 }
James Feist7b18b1e2019-05-14 13:42:09 -0700261 if (state != newState)
262 {
263 if (state == redundancy::full)
264 {
265 logFanRedundancyLost();
266 }
267 else if (newState == redundancy::full)
268 {
269 logFanRedundancyRestored();
270 }
271 state = newState;
272 iface->set_property("Status", state);
273 }
James Feistdc6c55f2018-10-31 12:53:20 -0700274}