blob: 1ec979f2bca8cfe62b09406ef7b0093daa9c409b [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
17#include <unistd.h>
18
Ed Tanous8a57ec02020-10-09 12:46:52 -070019#include <TachSensor.hpp>
20#include <Utils.hpp>
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
James Feist8086aba2020-08-25 16:00:59 -070023#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070024#include <boost/date_time/posix_time/posix_time.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080025#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -070026#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28
29#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070030#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <istream>
James Feist6714a252018-09-10 15:26:18 -070032#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <memory>
34#include <optional>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070036#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <utility>
38#include <vector>
James Feist6714a252018-09-10 15:26:18 -070039
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070040static constexpr unsigned int pwmPollMs = 500;
41static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070042
James Feistd8705872019-02-08 13:26:09 -080043TachSensor::TachSensor(const std::string& path, const std::string& objectType,
44 sdbusplus::asio::object_server& objectServer,
45 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist60c0ec72019-03-18 15:08:43 -070046 std::unique_ptr<PresenceSensor>&& presenceSensor,
James Feist7b18b1e2019-05-14 13:42:09 -070047 std::optional<RedundancySensor>* redundancy,
James Feistd8705872019-02-08 13:26:09 -080048 boost::asio::io_service& io, const std::string& fanName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080049 std::vector<thresholds::Threshold>&& thresholdsIn,
James Feistd8705872019-02-08 13:26:09 -080050 const std::string& sensorConfiguration,
Josh Lehanf920e092020-08-07 00:12:54 -070051 const std::pair<size_t, size_t>& limits,
James Feist49a8ccd2020-09-16 16:09:52 -070052 const PowerState& powerState,
53 const std::optional<std::string>& ledIn) :
Jeff Lin7b7a9de2021-02-22 11:16:27 +080054 Sensor(boost::replace_all_copy(fanName, " ", "_"), std::move(thresholdsIn),
James Feiste3338522020-09-15 15:40:30 -070055 sensorConfiguration, objectType, limits.second, limits.first, conn,
Josh Lehanf920e092020-08-07 00:12:54 -070056 powerState),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050057 objServer(objectServer), redundancy(redundancy),
58 presence(std::move(presenceSensor)),
James Feist49a8ccd2020-09-16 16:09:52 -070059 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
60 led(ledIn)
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 Feist49a8ccd2020-09-16 16:09:52 -0700195
196 bool curLed = !status;
197 if (led && ledState != curLed)
198 {
199 ledState = curLed;
200 setLed(dbusConnection, *led, curLed);
201 }
James Feist6714a252018-09-10 15:26:18 -0700202}
203
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800204PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700205 boost::asio::io_service& io,
206 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700207 inverted(inverted),
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800208 gpioLine(gpiod::find_line(gpioName)), gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700209{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800210 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700211 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800212 std::cerr << "Error requesting gpio: " << gpioName << "\n";
213 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700214 return;
215 }
216
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800217 try
James Feist7bc2bab2018-10-26 14:09:45 -0700218 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800219 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
220 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
221 status = gpioLine.get_value();
222
223 int gpioLineFd = gpioLine.event_get_fd();
224 if (gpioLineFd < 0)
225 {
226 std::cerr << "Failed to get " << gpioName << " fd\n";
227 return;
228 }
229
230 gpioFd.assign(gpioLineFd);
231 }
232 catch (std::system_error&)
233 {
234 std::cerr << "Error reading gpio: " << gpioName << "\n";
235 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700236 return;
237 }
James Feist7bc2bab2018-10-26 14:09:45 -0700238
James Feist7bc2bab2018-10-26 14:09:45 -0700239 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700240}
241
242PresenceSensor::~PresenceSensor()
243{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800244 gpioFd.close();
245 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700246}
247
248void PresenceSensor::monitorPresence(void)
249{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800250 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
251 [this](const boost::system::error_code& ec) {
252 if (ec == boost::system::errc::bad_file_descriptor)
253 {
254 return; // we're being destroyed
255 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700256 if (ec)
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800257 {
258 std::cerr << "Error on presence sensor " << name
259 << " \n";
260 ;
261 }
262 else
263 {
264 read();
265 }
266 monitorPresence();
267 });
James Feist7bc2bab2018-10-26 14:09:45 -0700268}
269
270void PresenceSensor::read(void)
271{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800272 gpioLine.event_read();
273 status = gpioLine.get_value();
274 // Read is invoked when an edge event is detected by monitorPresence
275 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700276 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800277 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700278 }
279 else
280 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800281 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700282 }
283}
284
285bool PresenceSensor::getValue(void)
286{
287 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700288}
289
James Feist9bb67462019-03-15 15:09:50 -0700290RedundancySensor::RedundancySensor(size_t count,
291 const std::vector<std::string>& children,
292 sdbusplus::asio::object_server& objectServer,
293 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700294 count(count),
295 iface(objectServer.add_interface(
296 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700297 "xyz.openbmc_project.Control.FanRedundancy")),
298 association(objectServer.add_interface(
299 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700300 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700301 objectServer(objectServer)
302{
James Feist9bb67462019-03-15 15:09:50 -0700303 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700304 iface->register_property("Collection", children);
305 iface->register_property("Status", std::string("Full"));
306 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
307 iface->initialize();
308}
309RedundancySensor::~RedundancySensor()
310{
James Feist9bb67462019-03-15 15:09:50 -0700311 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700312 objectServer.remove_interface(iface);
313}
James Feistd8705872019-02-08 13:26:09 -0800314void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700315{
316 statuses[name] = failed;
317 size_t failedCount = 0;
318
James Feist7b18b1e2019-05-14 13:42:09 -0700319 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800320 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700321 {
322 if (status.second)
323 {
324 failedCount++;
325 }
326 if (failedCount > count)
327 {
James Feist7b18b1e2019-05-14 13:42:09 -0700328 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700329 break;
330 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700331 if (failedCount)
James Feistdc6c55f2018-10-31 12:53:20 -0700332 {
James Feist7b18b1e2019-05-14 13:42:09 -0700333 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700334 }
335 }
James Feist7b18b1e2019-05-14 13:42:09 -0700336 if (state != newState)
337 {
338 if (state == redundancy::full)
339 {
340 logFanRedundancyLost();
341 }
342 else if (newState == redundancy::full)
343 {
344 logFanRedundancyRestored();
345 }
346 state = newState;
347 iface->set_property("Status", state);
348 }
James Feistdc6c55f2018-10-31 12:53:20 -0700349}