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