Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 3 | #include "env.hpp" |
| 4 | #include "fan_pwm.hpp" |
| 5 | #include "fan_speed.hpp" |
| 6 | #include "hwmonio.hpp" |
| 7 | |
Matt Spinler | b4e6557 | 2017-10-17 10:09:18 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include <phosphor-logging/log.hpp> |
| 10 | #include <xyz/openbmc_project/Sensor/Device/error.hpp> |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 11 | |
Patrick Williams | e8771fd | 2023-05-10 07:51:06 -0500 | [diff] [blame] | 12 | #include <filesystem> |
Patrick Williams | 6412993 | 2024-02-13 21:10:17 -0600 | [diff] [blame] | 13 | #include <format> |
Patrick Williams | e8771fd | 2023-05-10 07:51:06 -0500 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Matt Spinler | a7e2c1e | 2018-02-23 12:18:19 -0600 | [diff] [blame] | 16 | enum class targetType |
| 17 | { |
| 18 | DEFAULT, |
| 19 | RPM, |
| 20 | PWM |
| 21 | }; |
| 22 | |
| 23 | static constexpr auto RPM_TARGET = "RPM"; |
| 24 | static constexpr auto PWM_TARGET = "PWM"; |
| 25 | |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 26 | /** @class Targets |
| 27 | * @brief Target type traits. |
| 28 | * |
| 29 | * @tparam T - The target type. |
| 30 | */ |
| 31 | template <typename T> |
| 32 | struct Targets |
| 33 | { |
| 34 | static void fail() |
| 35 | { |
| 36 | static_assert(sizeof(Targets) == -1, "Unsupported Target type"); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | /**@brief Targets specialization for fan speed. */ |
| 41 | template <> |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 42 | struct Targets<hwmon::FanSpeed> |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 43 | { |
| 44 | static constexpr InterfaceType type = InterfaceType::FAN_SPEED; |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 45 | }; |
| 46 | |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 47 | template <> |
| 48 | struct Targets<hwmon::FanPwm> |
| 49 | { |
| 50 | static constexpr InterfaceType type = InterfaceType::FAN_PWM; |
| 51 | }; |
| 52 | |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 53 | /** @brief addTarget |
| 54 | * |
| 55 | * Creates the target type interface |
| 56 | * |
| 57 | * @tparam T - The target type |
| 58 | * |
| 59 | * @param[in] sensor - A sensor type and name |
Matt Spinler | b4e6557 | 2017-10-17 10:09:18 -0500 | [diff] [blame] | 60 | * @param[in] ioAccess - hwmon sysfs access object |
| 61 | * @param[in] devPath - The /sys/devices sysfs path |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 62 | * @param[in] info - The sdbusplus server connection and interfaces |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 63 | * |
| 64 | * @return A shared pointer to the target interface object |
| 65 | * Will be empty if no interface was created |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 66 | */ |
| 67 | template <typename T> |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 68 | std::shared_ptr<T> addTarget(const SensorSet::key_type& sensor, |
Patrick Venture | 16805a6 | 2019-06-21 14:19:33 -0700 | [diff] [blame] | 69 | const hwmonio::HwmonIOInterface* ioAccess, |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 70 | const std::string& devPath, ObjectInfo& info) |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 71 | { |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 72 | std::shared_ptr<T> target; |
Patrick Venture | 9e997b4 | 2019-03-08 13:42:10 -0800 | [diff] [blame] | 73 | namespace fs = std::filesystem; |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 74 | |
Patrick Venture | 6206723 | 2019-06-19 17:39:33 -0700 | [diff] [blame] | 75 | auto& obj = std::get<InterfaceMap>(info); |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 76 | auto& objPath = std::get<std::string>(info); |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 77 | auto type = Targets<T>::type; |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 78 | |
| 79 | // Check if target sysfs file exists |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 80 | std::string sysfsFullPath; |
Lei YU | a4bba96 | 2018-01-31 13:51:05 +0800 | [diff] [blame] | 81 | std::string targetName = sensor.first; |
| 82 | std::string targetId = sensor.second; |
| 83 | std::string entry = hwmon::entry::target; |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 84 | |
| 85 | using namespace std::literals; |
| 86 | const std::string pwm = "pwm"s; |
| 87 | const std::string empty = ""s; |
| 88 | |
| 89 | if (InterfaceType::FAN_PWM == type) |
| 90 | { |
Lei YU | a4bba96 | 2018-01-31 13:51:05 +0800 | [diff] [blame] | 91 | targetName = pwm; |
| 92 | // If PWM_TARGET is set, use the specified pwm id |
Patrick Venture | 7a5285d | 2018-04-17 19:15:05 -0700 | [diff] [blame] | 93 | auto id = env::getEnv("PWM_TARGET", sensor); |
Lei YU | a4bba96 | 2018-01-31 13:51:05 +0800 | [diff] [blame] | 94 | if (!id.empty()) |
| 95 | { |
| 96 | targetId = id; |
| 97 | } |
| 98 | entry = empty; |
Patrick Venture | 9331ab7 | 2018-01-29 09:48:47 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Patrick Williams | e8771fd | 2023-05-10 07:51:06 -0500 | [diff] [blame] | 101 | sysfsFullPath = sysfs::make_sysfs_path(ioAccess->path(), targetName, |
| 102 | targetId, entry); |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 103 | if (fs::exists(sysfsFullPath)) |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 104 | { |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 105 | auto useTarget = true; |
Patrick Venture | a24c880 | 2018-04-17 19:38:06 -0700 | [diff] [blame] | 106 | auto tmEnv = env::getEnv("TARGET_MODE"); |
| 107 | if (!tmEnv.empty()) |
Matt Spinler | b4e6557 | 2017-10-17 10:09:18 -0500 | [diff] [blame] | 108 | { |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 109 | std::string mode{tmEnv}; |
| 110 | std::transform(mode.begin(), mode.end(), mode.begin(), toupper); |
Matt Spinler | b4e6557 | 2017-10-17 10:09:18 -0500 | [diff] [blame] | 111 | |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 112 | if (mode == RPM_TARGET) |
| 113 | { |
| 114 | if (type != InterfaceType::FAN_SPEED) |
| 115 | { |
| 116 | useTarget = false; |
| 117 | } |
| 118 | } |
| 119 | else if (mode == PWM_TARGET) |
| 120 | { |
| 121 | if (type != InterfaceType::FAN_PWM) |
| 122 | { |
| 123 | useTarget = false; |
| 124 | } |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | using namespace phosphor::logging; |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 129 | log<level::ERR>( |
| 130 | "Invalid TARGET_MODE env var found", |
| 131 | phosphor::logging::entry("TARGET_MODE=%s", tmEnv.c_str()), |
| 132 | phosphor::logging::entry("DEVPATH=%s", devPath.c_str())); |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 133 | } |
Matt Spinler | b4e6557 | 2017-10-17 10:09:18 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 136 | if (useTarget) |
| 137 | { |
| 138 | uint32_t targetSpeed = 0; |
| 139 | |
| 140 | try |
| 141 | { |
Patrick Venture | 16805a6 | 2019-06-21 14:19:33 -0700 | [diff] [blame] | 142 | targetSpeed = ioAccess->read(targetName, targetId, entry, |
| 143 | hwmonio::retries, hwmonio::delay); |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 144 | } |
| 145 | catch (const std::system_error& e) |
| 146 | { |
| 147 | using namespace phosphor::logging; |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 148 | using namespace sdbusplus::xyz::openbmc_project::Sensor:: |
| 149 | Device::Error; |
| 150 | using metadata = |
| 151 | xyz::openbmc_project::Sensor::Device::ReadFailure; |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 152 | |
| 153 | report<ReadFailure>( |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 154 | metadata::CALLOUT_ERRNO(e.code().value()), |
| 155 | metadata::CALLOUT_DEVICE_PATH(devPath.c_str())); |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 156 | |
Patrick Williams | 6412993 | 2024-02-13 21:10:17 -0600 | [diff] [blame] | 157 | log<level::INFO>(std::format("Failing sysfs file: {} errno: {}", |
Matt Spinler | 6a391de | 2020-07-08 13:03:10 -0500 | [diff] [blame] | 158 | sysfsFullPath, e.code().value()) |
| 159 | .c_str()); |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 160 | } |
| 161 | |
Patrick Venture | 685efa1 | 2018-10-12 18:00:13 -0700 | [diff] [blame] | 162 | static constexpr bool deferSignals = true; |
Patrick Williams | ad6043f | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 163 | auto& bus = *std::get<sdbusplus::bus_t*>(info); |
Patrick Venture | 685efa1 | 2018-10-12 18:00:13 -0700 | [diff] [blame] | 164 | |
Patrick Venture | 16805a6 | 2019-06-21 14:19:33 -0700 | [diff] [blame] | 165 | // ioAccess->path() is a path like: /sys/class/hwmon/hwmon1 |
| 166 | // NOTE: When unit-testing, the target won't have an inject-ible |
| 167 | // ioAccess: fan_pwm/fan_speed. |
Patrick Venture | 5055237 | 2018-06-07 10:53:56 -0700 | [diff] [blame] | 168 | target = std::make_shared<T>( |
Patrick Venture | 16805a6 | 2019-06-21 14:19:33 -0700 | [diff] [blame] | 169 | std::move(std::make_unique<hwmonio::HwmonIO>(ioAccess->path())), |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 170 | devPath, targetId, bus, objPath.c_str(), deferSignals, |
| 171 | targetSpeed); |
Matthew Barth | 28f8e66 | 2018-03-26 16:57:36 -0500 | [diff] [blame] | 172 | obj[type] = target; |
| 173 | } |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 174 | } |
Matt Spinler | 0a8de64 | 2017-05-11 10:59:39 -0500 | [diff] [blame] | 175 | |
| 176 | return target; |
Matthew Barth | bf7b7b1 | 2017-03-07 15:46:59 -0600 | [diff] [blame] | 177 | } |