blob: d39e92ccb06d9f75ca0eaeae712f6c70beddd19a [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
James Feist58295ad2019-05-30 15:01:41 -070017#include "TachSensor.hpp"
18
19#include "Utils.hpp"
20
James Feist6714a252018-09-10 15:26:18 -070021#include <unistd.h>
22
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/algorithm/string/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
James Feist8086aba2020-08-25 16:00:59 -070025#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070026#include <boost/date_time/posix_time/posix_time.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080027#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -070028#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30
31#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070032#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <istream>
James Feist6714a252018-09-10 15:26:18 -070034#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <memory>
36#include <optional>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070038#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070039#include <utility>
40#include <vector>
James Feist6714a252018-09-10 15:26:18 -070041
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070042static constexpr unsigned int pwmPollMs = 500;
43static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070044
James Feistd8705872019-02-08 13:26:09 -080045TachSensor::TachSensor(const std::string& path, const std::string& objectType,
46 sdbusplus::asio::object_server& objectServer,
47 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist60c0ec72019-03-18 15:08:43 -070048 std::unique_ptr<PresenceSensor>&& presenceSensor,
James Feist7b18b1e2019-05-14 13:42:09 -070049 std::optional<RedundancySensor>* redundancy,
James Feistd8705872019-02-08 13:26:09 -080050 boost::asio::io_service& io, const std::string& fanName,
51 std::vector<thresholds::Threshold>&& _thresholds,
52 const std::string& sensorConfiguration,
Josh Lehanf920e092020-08-07 00:12:54 -070053 const std::pair<size_t, size_t>& limits,
54 const PowerState& powerState) :
James Feist930fcde2019-05-28 12:58:43 -070055 Sensor(boost::replace_all_copy(fanName, " ", "_"), std::move(_thresholds),
James Feiste3338522020-09-15 15:40:30 -070056 sensorConfiguration, objectType, limits.second, limits.first, conn,
Josh Lehanf920e092020-08-07 00:12:54 -070057 powerState),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050058 objServer(objectServer), redundancy(redundancy),
59 presence(std::move(presenceSensor)),
James Feist961bf092020-07-01 16:38:12 -070060 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path)
James Feist6714a252018-09-10 15:26:18 -070061{
James Feist251c7822018-09-12 12:54:15 -070062 sensorInterface = objectServer.add_interface(
63 "/xyz/openbmc_project/sensors/fan_tach/" + name,
64 "xyz.openbmc_project.Sensor.Value");
65
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070066 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070067 {
James Feist251c7822018-09-12 12:54:15 -070068 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070069 "/xyz/openbmc_project/sensors/fan_tach/" + name,
70 "xyz.openbmc_project.Sensor.Threshold.Warning");
71 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070072 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070073 {
James Feist251c7822018-09-12 12:54:15 -070074 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070075 "/xyz/openbmc_project/sensors/fan_tach/" + name,
76 "xyz.openbmc_project.Sensor.Threshold.Critical");
77 }
James Feist078f2322019-03-08 11:09:05 -080078 association = objectServer.add_interface(
79 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070080 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070081
82 if (presence)
83 {
84 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070085 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070086 "xyz.openbmc_project.Inventory.Item");
87 itemIface->register_property("PrettyName",
88 std::string()); // unused property
89 itemIface->register_property("Present", true);
90 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070091 itemAssoc = objectServer.add_interface(
92 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070093 itemAssoc->register_property(
94 "associations",
95 std::vector<Association>{
96 {"sensors", "inventory",
97 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
98 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070099 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700100 setInitialProperties(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700101 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700102}
103
104TachSensor::~TachSensor()
105{
106 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700107 inputDev.close();
108 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -0700109 objServer.remove_interface(thresholdInterfaceWarning);
110 objServer.remove_interface(thresholdInterfaceCritical);
111 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
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700117void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700118{
119 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700120 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800121 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700122 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700123}
124
James Feistd8705872019-02-08 13:26:09 -0800125void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700126{
127 if (err == boost::system::errc::bad_file_descriptor)
128 {
129 return; // we're being destroyed
130 }
James Feist7bc2bab2018-10-26 14:09:45 -0700131 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700132 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700133 if (presence)
134 {
135 if (!presence->getValue())
136 {
James Feist961bf092020-07-01 16:38:12 -0700137 markAvailable(false);
James Feist7bc2bab2018-10-26 14:09:45 -0700138 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700139 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700140 }
James Feist60c0ec72019-03-18 15:08:43 -0700141 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700142 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700143 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700144 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700145 {
James Feist7bc2bab2018-10-26 14:09:45 -0700146 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700147 {
James Feist7bc2bab2018-10-26 14:09:45 -0700148 std::string response;
149 try
James Feist6714a252018-09-10 15:26:18 -0700150 {
James Feist7bc2bab2018-10-26 14:09:45 -0700151 std::getline(responseStream, response);
Zhikui Rend3da1282020-09-11 17:02:01 -0700152 rawValue = std::stod(response);
James Feist7bc2bab2018-10-26 14:09:45 -0700153 responseStream.clear();
Zhikui Rend3da1282020-09-11 17:02:01 -0700154 updateValue(rawValue);
James Feist6714a252018-09-10 15:26:18 -0700155 }
James Feistd8705872019-02-08 13:26:09 -0800156 catch (const std::invalid_argument&)
James Feist7bc2bab2018-10-26 14:09:45 -0700157 {
James Feist961bf092020-07-01 16:38:12 -0700158 incrementError();
159 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700160 }
James Feist6714a252018-09-10 15:26:18 -0700161 }
162 else
163 {
James Feist961bf092020-07-01 16:38:12 -0700164 incrementError();
165 pollTime = sensorFailedPollTimeMs;
James Feist71d31b22019-01-02 16:57:54 -0800166 }
James Feist6714a252018-09-10 15:26:18 -0700167 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700168 responseStream.clear();
169 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700170 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700171 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700172 {
173 return; // we're no longer valid
174 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700175 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700176 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800177 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700178 if (ec == boost::asio::error::operation_aborted)
179 {
180 return; // we're being canceled
181 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700182 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700183 });
184}
185
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700186void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700187{
James Feistdc6c55f2018-10-31 12:53:20 -0700188 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700189
James Feist7b18b1e2019-05-14 13:42:09 -0700190 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700191 {
James Feist7b18b1e2019-05-14 13:42:09 -0700192 (*redundancy)
193 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700194 }
James Feist6714a252018-09-10 15:26:18 -0700195}
196
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800197PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700198 boost::asio::io_service& io,
199 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700200 inverted(inverted),
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800201 gpioLine(gpiod::find_line(gpioName)), gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700202{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800203 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700204 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800205 std::cerr << "Error requesting gpio: " << gpioName << "\n";
206 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700207 return;
208 }
209
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800210 try
James Feist7bc2bab2018-10-26 14:09:45 -0700211 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800212 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
213 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
214 status = gpioLine.get_value();
215
216 int gpioLineFd = gpioLine.event_get_fd();
217 if (gpioLineFd < 0)
218 {
219 std::cerr << "Failed to get " << gpioName << " fd\n";
220 return;
221 }
222
223 gpioFd.assign(gpioLineFd);
224 }
225 catch (std::system_error&)
226 {
227 std::cerr << "Error reading gpio: " << gpioName << "\n";
228 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700229 return;
230 }
James Feist7bc2bab2018-10-26 14:09:45 -0700231
James Feist7bc2bab2018-10-26 14:09:45 -0700232 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700233}
234
235PresenceSensor::~PresenceSensor()
236{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800237 gpioFd.close();
238 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700239}
240
241void PresenceSensor::monitorPresence(void)
242{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800243 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
244 [this](const boost::system::error_code& ec) {
245 if (ec == boost::system::errc::bad_file_descriptor)
246 {
247 return; // we're being destroyed
248 }
249 else if (ec)
250 {
251 std::cerr << "Error on presence sensor " << name
252 << " \n";
253 ;
254 }
255 else
256 {
257 read();
258 }
259 monitorPresence();
260 });
James Feist7bc2bab2018-10-26 14:09:45 -0700261}
262
263void PresenceSensor::read(void)
264{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800265 gpioLine.event_read();
266 status = gpioLine.get_value();
267 // Read is invoked when an edge event is detected by monitorPresence
268 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700269 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800270 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700271 }
272 else
273 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800274 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700275 }
276}
277
278bool PresenceSensor::getValue(void)
279{
280 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700281}
282
James Feist9bb67462019-03-15 15:09:50 -0700283RedundancySensor::RedundancySensor(size_t count,
284 const std::vector<std::string>& children,
285 sdbusplus::asio::object_server& objectServer,
286 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700287 count(count),
288 iface(objectServer.add_interface(
289 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700290 "xyz.openbmc_project.Control.FanRedundancy")),
291 association(objectServer.add_interface(
292 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700293 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700294 objectServer(objectServer)
295{
James Feist9bb67462019-03-15 15:09:50 -0700296 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700297 iface->register_property("Collection", children);
298 iface->register_property("Status", std::string("Full"));
299 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
300 iface->initialize();
301}
302RedundancySensor::~RedundancySensor()
303{
James Feist9bb67462019-03-15 15:09:50 -0700304 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700305 objectServer.remove_interface(iface);
306}
James Feistd8705872019-02-08 13:26:09 -0800307void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700308{
309 statuses[name] = failed;
310 size_t failedCount = 0;
311
James Feist7b18b1e2019-05-14 13:42:09 -0700312 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800313 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700314 {
315 if (status.second)
316 {
317 failedCount++;
318 }
319 if (failedCount > count)
320 {
James Feist7b18b1e2019-05-14 13:42:09 -0700321 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700322 break;
323 }
324 else if (failedCount)
325 {
James Feist7b18b1e2019-05-14 13:42:09 -0700326 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700327 }
328 }
James Feist7b18b1e2019-05-14 13:42:09 -0700329 if (state != newState)
330 {
331 if (state == redundancy::full)
332 {
333 logFanRedundancyLost();
334 }
335 else if (newState == redundancy::full)
336 {
337 logFanRedundancyRestored();
338 }
339 state = newState;
340 iface->set_property("Status", state);
341 }
James Feistdc6c55f2018-10-31 12:53:20 -0700342}