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