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