1 /*
2 // Copyright (c) 2017 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 "PresenceGpio.hpp"
18 #include "PwmSensor.hpp"
19 #include "TachSensor.hpp"
20 #include "Thresholds.hpp"
21 #include "Utils.hpp"
22 #include "VariantVisitors.hpp"
23
24 #include <boost/algorithm/string/replace.hpp>
25 #include <boost/asio/error.hpp>
26 #include <boost/asio/io_context.hpp>
27 #include <boost/asio/post.hpp>
28 #include <boost/asio/steady_timer.hpp>
29 #include <boost/container/flat_map.hpp>
30 #include <boost/container/flat_set.hpp>
31 #include <sdbusplus/asio/connection.hpp>
32 #include <sdbusplus/asio/object_server.hpp>
33 #include <sdbusplus/bus.hpp>
34 #include <sdbusplus/bus/match.hpp>
35 #include <sdbusplus/message.hpp>
36
37 #include <array>
38 #include <chrono>
39 #include <cstddef>
40 #include <cstdint>
41 #include <filesystem>
42 #include <fstream>
43 #include <functional>
44 #include <ios>
45 #include <iostream>
46 #include <map>
47 #include <memory>
48 #include <optional>
49 #include <regex>
50 #include <string>
51 #include <system_error>
52 #include <utility>
53 #include <variant>
54 #include <vector>
55
56 namespace fs = std::filesystem;
57
58 // The following two structures need to be consistent
59 static auto sensorTypes{std::to_array<const char*>(
60 {"AspeedFan", "I2CFan", "NuvotonFan", "HPEFan"})};
61
62 enum FanTypes
63 {
64 aspeed = 0,
65 i2c,
66 nuvoton,
67 hpe,
68 max,
69 };
70
71 static_assert(std::tuple_size<decltype(sensorTypes)>::value == FanTypes::max,
72 "sensorTypes element number is not equal to FanTypes number");
73
74 constexpr const char* redundancyConfiguration =
75 "xyz.openbmc_project.Configuration.FanRedundancy";
76 static std::regex inputRegex(R"(fan(\d+)_input)");
77
78 // todo: power supply fan redundancy
79 std::optional<RedundancySensor> systemRedundancy;
80
81 static const std::map<std::string, FanTypes> compatibleFanTypes = {
82 {"aspeed,ast2400-pwm-tacho", FanTypes::aspeed},
83 {"aspeed,ast2500-pwm-tacho", FanTypes::aspeed},
84 {"aspeed,ast2600-pwm-tach", FanTypes::aspeed},
85 {"nuvoton,npcm750-pwm-fan", FanTypes::nuvoton},
86 {"nuvoton,npcm845-pwm-fan", FanTypes::nuvoton},
87 {"hpe,gxp-fan-ctrl", FanTypes::hpe}
88 // add compatible string here for new fan type
89 };
90
getFanType(const fs::path & parentPath)91 FanTypes getFanType(const fs::path& parentPath)
92 {
93 fs::path linkPath = parentPath / "of_node";
94 if (!fs::exists(linkPath))
95 {
96 return FanTypes::i2c;
97 }
98
99 std::string canonical = fs::canonical(linkPath);
100 std::string compatiblePath = canonical + "/compatible";
101 std::ifstream compatibleStream(compatiblePath);
102
103 if (!compatibleStream)
104 {
105 std::cerr << "Error opening " << compatiblePath << "\n";
106 return FanTypes::i2c;
107 }
108
109 std::string compatibleString;
110 while (std::getline(compatibleStream, compatibleString))
111 {
112 compatibleString.pop_back(); // trim EOL before comparisons
113
114 std::map<std::string, FanTypes>::const_iterator compatibleIterator =
115 compatibleFanTypes.find(compatibleString);
116
117 if (compatibleIterator != compatibleFanTypes.end())
118 {
119 return compatibleIterator->second;
120 }
121 }
122
123 return FanTypes::i2c;
124 }
enablePwm(const fs::path & filePath)125 void enablePwm(const fs::path& filePath)
126 {
127 std::fstream enableFile(filePath, std::ios::in | std::ios::out);
128 if (!enableFile.good())
129 {
130 std::cerr << "Error read/write " << filePath << "\n";
131 return;
132 }
133
134 std::string regulateMode;
135 std::getline(enableFile, regulateMode);
136 if (regulateMode == "0")
137 {
138 enableFile << 1;
139 }
140 }
findPwmfanPath(unsigned int configPwmfanIndex,fs::path & pwmPath)141 bool findPwmfanPath(unsigned int configPwmfanIndex, fs::path& pwmPath)
142 {
143 /* Search PWM since pwm-fan had separated
144 * PWM from tach directory and 1 channel only*/
145 std::vector<fs::path> pwmfanPaths;
146 std::string pwnfanDevName("pwm-fan");
147
148 pwnfanDevName += std::to_string(configPwmfanIndex);
149
150 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwmfanPaths))
151 {
152 std::cerr << "No PWMs are found!\n";
153 return false;
154 }
155 for (const auto& path : pwmfanPaths)
156 {
157 std::error_code ec;
158 fs::path link = fs::read_symlink(path.parent_path() / "device", ec);
159
160 if (ec)
161 {
162 std::cerr << "read_symlink() failed: " << ec.message() << " ("
163 << ec.value() << ")\n";
164 continue;
165 }
166
167 if (link.filename().string() == pwnfanDevName)
168 {
169 pwmPath = path;
170 return true;
171 }
172 }
173 return false;
174 }
findPwmPath(const fs::path & directory,unsigned int pwm,fs::path & pwmPath)175 bool findPwmPath(const fs::path& directory, unsigned int pwm, fs::path& pwmPath)
176 {
177 std::error_code ec;
178
179 /* Assuming PWM file is appeared in the same directory as fanX_input */
180 auto path = directory / ("pwm" + std::to_string(pwm + 1));
181 bool exists = fs::exists(path, ec);
182
183 if (ec || !exists)
184 {
185 /* PWM file not exist or error happened */
186 if (ec)
187 {
188 std::cerr << "exists() failed: " << ec.message() << " ("
189 << ec.value() << ")\n";
190 }
191 /* try search form pwm-fanX directory */
192 return findPwmfanPath(pwm, pwmPath);
193 }
194
195 pwmPath = path;
196 return true;
197 }
198
199 // The argument to this function should be the fanN_input file that we want to
200 // enable. The function will locate the corresponding fanN_enable file if it
201 // exists. Note that some drivers don't provide this file if the sensors are
202 // always enabled.
enableFanInput(const fs::path & fanInputPath)203 void enableFanInput(const fs::path& fanInputPath)
204 {
205 std::error_code ec;
206 std::string path(fanInputPath.string());
207 boost::replace_last(path, "input", "enable");
208
209 bool exists = fs::exists(path, ec);
210 if (ec || !exists)
211 {
212 return;
213 }
214
215 std::fstream enableFile(path, std::ios::out);
216 if (!enableFile.good())
217 {
218 return;
219 }
220 enableFile << 1;
221 }
222
createRedundancySensor(const boost::container::flat_map<std::string,std::shared_ptr<TachSensor>> & sensors,const std::shared_ptr<sdbusplus::asio::connection> & conn,sdbusplus::asio::object_server & objectServer)223 void createRedundancySensor(
224 const boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
225 sensors,
226 const std::shared_ptr<sdbusplus::asio::connection>& conn,
227 sdbusplus::asio::object_server& objectServer)
228 {
229 conn->async_method_call(
230 [&objectServer, &sensors](boost::system::error_code& ec,
231 const ManagedObjectType& managedObj) {
232 if (ec)
233 {
234 std::cerr << "Error calling entity manager \n";
235 return;
236 }
237 for (const auto& [path, interfaces] : managedObj)
238 {
239 for (const auto& [intf, cfg] : interfaces)
240 {
241 if (intf == redundancyConfiguration)
242 {
243 // currently only support one
244 auto findCount = cfg.find("AllowedFailures");
245 if (findCount == cfg.end())
246 {
247 std::cerr << "Malformed redundancy record \n";
248 return;
249 }
250 std::vector<std::string> sensorList;
251
252 for (const auto& [name, sensor] : sensors)
253 {
254 sensorList.push_back(
255 "/xyz/openbmc_project/sensors/fan_tach/" +
256 sensor->name);
257 }
258 systemRedundancy.reset();
259 systemRedundancy.emplace(RedundancySensor(
260 std::get<uint64_t>(findCount->second), sensorList,
261 objectServer, path));
262
263 return;
264 }
265 }
266 }
267 },
268 "xyz.openbmc_project.EntityManager", "/xyz/openbmc_project/inventory",
269 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
270 }
271
createSensors(boost::asio::io_context & io,sdbusplus::asio::object_server & objectServer,boost::container::flat_map<std::string,std::shared_ptr<TachSensor>> & tachSensors,boost::container::flat_map<std::string,std::unique_ptr<PwmSensor>> & pwmSensors,boost::container::flat_map<std::string,std::weak_ptr<PresenceGpio>> & presenceGpios,std::shared_ptr<sdbusplus::asio::connection> & dbusConnection,const std::shared_ptr<boost::container::flat_set<std::string>> & sensorsChanged,size_t retries=0)272 void createSensors(
273 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
274 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>&
275 tachSensors,
276 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
277 pwmSensors,
278 boost::container::flat_map<std::string, std::weak_ptr<PresenceGpio>>&
279 presenceGpios,
280 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
281 const std::shared_ptr<boost::container::flat_set<std::string>>&
282 sensorsChanged,
283 size_t retries = 0)
284 {
285 auto getter = std::make_shared<
286 GetSensorConfiguration>(dbusConnection, [&io, &objectServer,
287 &tachSensors, &pwmSensors,
288 &presenceGpios,
289 &dbusConnection,
290 sensorsChanged](
291 const ManagedObjectType&
292 sensorConfigurations) {
293 bool firstScan = sensorsChanged == nullptr;
294 std::vector<fs::path> paths;
295 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
296 {
297 std::cerr << "No fan sensors in system\n";
298 return;
299 }
300
301 // iterate through all found fan sensors, and try to match them with
302 // configuration
303 for (const auto& path : paths)
304 {
305 std::smatch match;
306 std::string pathStr = path.string();
307
308 std::regex_search(pathStr, match, inputRegex);
309 std::string indexStr = *(match.begin() + 1);
310
311 fs::path directory = path.parent_path();
312 FanTypes fanType = getFanType(directory);
313 std::string cfgIntf = configInterfaceName(sensorTypes[fanType]);
314
315 // convert to 0 based
316 size_t index = std::stoul(indexStr) - 1;
317
318 const char* baseType = nullptr;
319 const SensorData* sensorData = nullptr;
320 const std::string* interfacePath = nullptr;
321 const SensorBaseConfiguration* baseConfiguration = nullptr;
322 for (const auto& [path, cfgData] : sensorConfigurations)
323 {
324 // find the base of the configuration to see if indexes
325 // match
326 auto sensorBaseFind = cfgData.find(cfgIntf);
327 if (sensorBaseFind == cfgData.end())
328 {
329 continue;
330 }
331
332 baseConfiguration = &(*sensorBaseFind);
333 interfacePath = &path.str;
334 baseType = sensorTypes[fanType];
335
336 auto findIndex = baseConfiguration->second.find("Index");
337 if (findIndex == baseConfiguration->second.end())
338 {
339 std::cerr << baseConfiguration->first << " missing index\n";
340 continue;
341 }
342 unsigned int configIndex = std::visit(
343 VariantToUnsignedIntVisitor(), findIndex->second);
344 if (configIndex != index)
345 {
346 continue;
347 }
348 if (fanType == FanTypes::aspeed ||
349 fanType == FanTypes::nuvoton || fanType == FanTypes::hpe)
350 {
351 // there will be only 1 aspeed or nuvoton or hpe sensor
352 // object in sysfs, we found the fan
353 sensorData = &cfgData;
354 break;
355 }
356 if (fanType == FanTypes::i2c)
357 {
358 std::string deviceName =
359 fs::read_symlink(directory / "device").filename();
360
361 size_t bus = 0;
362 size_t addr = 0;
363 if (!getDeviceBusAddr(deviceName, bus, addr))
364 {
365 continue;
366 }
367
368 auto findBus = baseConfiguration->second.find("Bus");
369 auto findAddress =
370 baseConfiguration->second.find("Address");
371 if (findBus == baseConfiguration->second.end() ||
372 findAddress == baseConfiguration->second.end())
373 {
374 std::cerr << baseConfiguration->first
375 << " missing bus or address\n";
376 continue;
377 }
378 unsigned int configBus = std::visit(
379 VariantToUnsignedIntVisitor(), findBus->second);
380 unsigned int configAddress = std::visit(
381 VariantToUnsignedIntVisitor(), findAddress->second);
382
383 if (configBus == bus && configAddress == addr)
384 {
385 sensorData = &cfgData;
386 break;
387 }
388 }
389 }
390 if (sensorData == nullptr)
391 {
392 std::cerr << "failed to find match for " << path.string()
393 << "\n";
394 continue;
395 }
396
397 auto findSensorName = baseConfiguration->second.find("Name");
398
399 if (findSensorName == baseConfiguration->second.end())
400 {
401 std::cerr << "could not determine configuration name for "
402 << path.string() << "\n";
403 continue;
404 }
405 std::string sensorName =
406 std::get<std::string>(findSensorName->second);
407
408 // on rescans, only update sensors we were signaled by
409 auto findSensor = tachSensors.find(sensorName);
410 if (!firstScan && findSensor != tachSensors.end())
411 {
412 bool found = false;
413 for (auto it = sensorsChanged->begin();
414 it != sensorsChanged->end(); it++)
415 {
416 if (it->ends_with(findSensor->second->name))
417 {
418 sensorsChanged->erase(it);
419 findSensor->second = nullptr;
420 found = true;
421 break;
422 }
423 }
424 if (!found)
425 {
426 continue;
427 }
428 }
429 std::vector<thresholds::Threshold> sensorThresholds;
430 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
431 {
432 std::cerr << "error populating thresholds for " << sensorName
433 << "\n";
434 }
435
436 auto presenceConfig =
437 sensorData->find(cfgIntf + std::string(".Presence"));
438
439 std::shared_ptr<PresenceGpio> presenceGpio(nullptr);
440
441 // presence sensors are optional
442 if (presenceConfig != sensorData->end())
443 {
444 auto findPolarity = presenceConfig->second.find("Polarity");
445 auto findPinName = presenceConfig->second.find("PinName");
446
447 if (findPinName == presenceConfig->second.end() ||
448 findPolarity == presenceConfig->second.end())
449 {
450 std::cerr << "Malformed Presence Configuration\n";
451 }
452 else
453 {
454 bool inverted =
455 std::get<std::string>(findPolarity->second) == "Low";
456 const auto* pinName =
457 std::get_if<std::string>(&findPinName->second);
458
459 if (pinName != nullptr)
460 {
461 auto findPresenceGpio = presenceGpios.find(*pinName);
462 if (findPresenceGpio != presenceGpios.end())
463 {
464 auto p = findPresenceGpio->second.lock();
465 if (p)
466 {
467 presenceGpio = p;
468 }
469 }
470 if (!presenceGpio)
471 {
472 auto findMonitorType =
473 presenceConfig->second.find("MonitorType");
474 bool polling = false;
475 if (findMonitorType != presenceConfig->second.end())
476 {
477 auto mType = std::get<std::string>(
478 findMonitorType->second);
479 if (mType == "Polling")
480 {
481 polling = true;
482 }
483 else if (mType != "Event")
484 {
485 std::cerr
486 << "Unsupported GPIO MonitorType of "
487 << mType << " for " << sensorName
488 << " (supported types: Polling, Event (default))\n";
489 }
490 }
491 try
492 {
493 if (polling)
494 {
495 presenceGpio =
496 std::make_shared<PollingPresenceGpio>(
497 "Fan", sensorName, *pinName,
498 inverted, io);
499 }
500 else
501 {
502 presenceGpio =
503 std::make_shared<EventPresenceGpio>(
504 "Fan", sensorName, *pinName,
505 inverted, io);
506 }
507 presenceGpios[*pinName] = presenceGpio;
508 }
509 catch (const std::system_error& e)
510 {
511 std::cerr
512 << "Failed to create GPIO monitor object for "
513 << *pinName << " / " << sensorName << ": "
514 << e.what() << "\n";
515 }
516 }
517 }
518 else
519 {
520 std::cerr << "Malformed Presence pinName for sensor "
521 << sensorName << " \n";
522 }
523 }
524 }
525 std::optional<RedundancySensor>* redundancy = nullptr;
526 if (fanType == FanTypes::aspeed)
527 {
528 redundancy = &systemRedundancy;
529 }
530
531 PowerState powerState = getPowerState(baseConfiguration->second);
532
533 constexpr double defaultMaxReading = 25000;
534 constexpr double defaultMinReading = 0;
535 std::pair<double, double> limits =
536 std::make_pair(defaultMinReading, defaultMaxReading);
537
538 auto connector =
539 sensorData->find(cfgIntf + std::string(".Connector"));
540
541 std::optional<std::string> led;
542 std::string pwmName;
543 fs::path pwmPath;
544
545 // The Mutable parameter is optional, defaulting to false
546 bool isValueMutable = false;
547 if (connector != sensorData->end())
548 {
549 auto findPwm = connector->second.find("Pwm");
550 if (findPwm != connector->second.end())
551 {
552 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
553 findPwm->second);
554 if (!findPwmPath(directory, pwm, pwmPath))
555 {
556 std::cerr << "Connector for " << sensorName
557 << " no pwm channel found!\n";
558 continue;
559 }
560
561 fs::path pwmEnableFile =
562 "pwm" + std::to_string(pwm + 1) + "_enable";
563 fs::path enablePath = pwmPath.parent_path() / pwmEnableFile;
564 enablePwm(enablePath);
565
566 /* use pwm name override if found in configuration else
567 * use default */
568 auto findOverride = connector->second.find("PwmName");
569 if (findOverride != connector->second.end())
570 {
571 pwmName = std::visit(VariantToStringVisitor(),
572 findOverride->second);
573 }
574 else
575 {
576 pwmName = "Pwm_" + std::to_string(pwm + 1);
577 }
578
579 // Check PWM sensor mutability
580 auto findMutable = connector->second.find("Mutable");
581 if (findMutable != connector->second.end())
582 {
583 const auto* ptrMutable =
584 std::get_if<bool>(&(findMutable->second));
585 if (ptrMutable != nullptr)
586 {
587 isValueMutable = *ptrMutable;
588 }
589 }
590 }
591 else
592 {
593 std::cerr
594 << "Connector for " << sensorName << " missing pwm!\n";
595 }
596
597 auto findLED = connector->second.find("LED");
598 if (findLED != connector->second.end())
599 {
600 const auto* ledName =
601 std::get_if<std::string>(&(findLED->second));
602 if (ledName == nullptr)
603 {
604 std::cerr
605 << "Wrong format for LED of " << sensorName << "\n";
606 }
607 else
608 {
609 led = *ledName;
610 }
611 }
612 }
613
614 findLimits(limits, baseConfiguration);
615
616 enableFanInput(path);
617
618 auto& tachSensor = tachSensors[sensorName];
619 tachSensor = nullptr;
620 tachSensor = std::make_shared<TachSensor>(
621 path.string(), baseType, objectServer, dbusConnection,
622 presenceGpio, redundancy, io, sensorName,
623 std::move(sensorThresholds), *interfacePath, limits, powerState,
624 led);
625 tachSensor->setupRead();
626
627 if (!pwmPath.empty() && fs::exists(pwmPath) &&
628 (pwmSensors.count(pwmPath) == 0U))
629 {
630 pwmSensors[pwmPath] = std::make_unique<PwmSensor>(
631 pwmName, pwmPath, dbusConnection, objectServer,
632 *interfacePath, "Fan", isValueMutable);
633 }
634 }
635
636 createRedundancySensor(tachSensors, dbusConnection, objectServer);
637 });
638 getter->getConfiguration(
639 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()},
640 retries);
641 }
642
main()643 int main()
644 {
645 boost::asio::io_context io;
646 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
647 sdbusplus::asio::object_server objectServer(systemBus, true);
648
649 objectServer.add_manager("/xyz/openbmc_project/sensors");
650 objectServer.add_manager("/xyz/openbmc_project/control");
651 objectServer.add_manager("/xyz/openbmc_project/inventory");
652 systemBus->request_name("xyz.openbmc_project.FanSensor");
653 boost::container::flat_map<std::string, std::shared_ptr<TachSensor>>
654 tachSensors;
655 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
656 pwmSensors;
657 boost::container::flat_map<std::string, std::weak_ptr<PresenceGpio>>
658 presenceGpios;
659 auto sensorsChanged =
660 std::make_shared<boost::container::flat_set<std::string>>();
661
662 boost::asio::post(io, [&]() {
663 createSensors(io, objectServer, tachSensors, pwmSensors, presenceGpios,
664 systemBus, nullptr);
665 });
666
667 boost::asio::steady_timer filterTimer(io);
668 std::function<void(sdbusplus::message_t&)> eventHandler =
669 [&](sdbusplus::message_t& message) {
670 if (message.is_method_error())
671 {
672 std::cerr << "callback method error\n";
673 return;
674 }
675 sensorsChanged->insert(message.get_path());
676 // this implicitly cancels the timer
677 filterTimer.expires_after(std::chrono::seconds(1));
678
679 filterTimer.async_wait([&](const boost::system::error_code& ec) {
680 if (ec == boost::asio::error::operation_aborted)
681 {
682 /* we were canceled*/
683 return;
684 }
685 if (ec)
686 {
687 std::cerr << "timer error\n";
688 return;
689 }
690 createSensors(io, objectServer, tachSensors, pwmSensors,
691 presenceGpios, systemBus, sensorsChanged, 5);
692 });
693 };
694
695 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
696 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
697
698 // redundancy sensor
699 std::function<void(sdbusplus::message_t&)> redundancyHandler =
700 [&tachSensors, &systemBus, &objectServer](sdbusplus::message_t&) {
701 createRedundancySensor(tachSensors, systemBus, objectServer);
702 };
703 auto match = std::make_unique<sdbusplus::bus::match_t>(
704 static_cast<sdbusplus::bus_t&>(*systemBus),
705 "type='signal',member='PropertiesChanged',path_namespace='" +
706 std::string(inventoryPath) + "',arg0namespace='" +
707 redundancyConfiguration + "'",
708 std::move(redundancyHandler));
709 matches.emplace_back(std::move(match));
710
711 setupManufacturingModeMatch(*systemBus);
712 io.run();
713 return 0;
714 }
715