Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2017 Google Inc. |
| 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 | |
Patrick Venture | 36ab6f6 | 2020-08-03 10:50:26 -0700 | [diff] [blame] | 17 | #include "manualcmds.hpp" |
| 18 | |
William A. Kennington III | 331143c | 2019-02-07 15:52:44 -0800 | [diff] [blame] | 19 | #include <ipmid/api.h> |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 20 | |
William A. Kennington III | 331143c | 2019-02-07 15:52:44 -0800 | [diff] [blame] | 21 | #include <ipmid/iana.hpp> |
| 22 | #include <ipmid/oemopenbmc.hpp> |
| 23 | #include <ipmid/oemrouter.hpp> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 24 | #include <sdbusplus/bus.hpp> |
| 25 | #include <sdbusplus/message.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 26 | |
| 27 | #include <map> |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 28 | #include <string> |
| 29 | #include <tuple> |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 30 | #include <variant> |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 31 | |
Patrick Venture | 36ab6f6 | 2020-08-03 10:50:26 -0700 | [diff] [blame] | 32 | namespace pid_control |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 33 | { |
Patrick Venture | 36ab6f6 | 2020-08-03 10:50:26 -0700 | [diff] [blame] | 34 | namespace ipmi |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 35 | { |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 36 | |
| 37 | static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone"; |
| 38 | static constexpr auto busName = "xyz.openbmc_project.State.FanCtrl"; |
Patrick Venture | dc3b790 | 2018-03-24 10:41:19 -0700 | [diff] [blame] | 39 | static constexpr auto intf = "xyz.openbmc_project.Control.Mode"; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 40 | static constexpr auto manualProperty = "Manual"; |
| 41 | static constexpr auto failsafeProperty = "FailSafe"; |
| 42 | static constexpr auto propertiesintf = "org.freedesktop.DBus.Properties"; |
| 43 | |
| 44 | using Property = std::string; |
William A. Kennington III | 323f1d9 | 2020-06-03 11:18:24 -0700 | [diff] [blame] | 45 | using Value = std::variant<bool>; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 46 | using PropertyMap = std::map<Property, Value>; |
| 47 | |
| 48 | /* The following was copied directly from my manual thread handler. */ |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 49 | static std::string getControlPath(int8_t zone) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 50 | { |
| 51 | return std::string(objectPath) + std::to_string(zone); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * busctl call xyz.openbmc_project.State.FanCtrl \ |
| 56 | * /xyz/openbmc_project/settings/fanctrl/zone1 \ |
| 57 | * org.freedesktop.DBus.Properties \ |
| 58 | * GetAll \ |
| 59 | * s \ |
Patrick Venture | dc3b790 | 2018-03-24 10:41:19 -0700 | [diff] [blame] | 60 | * xyz.openbmc_project.Control.Mode |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 61 | * a{sv} 2 "Manual" b false "FailSafe" b false |
| 62 | */ |
| 63 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 64 | static ipmi_ret_t getFanCtrlProperty(uint8_t zoneId, bool* value, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 65 | const std::string& property) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 66 | { |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 67 | std::string path = getControlPath(zoneId); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 68 | |
James Feist | 9fa90c1 | 2019-01-11 15:35:22 -0800 | [diff] [blame] | 69 | auto propertyReadBus = sdbusplus::bus::new_system(); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 70 | auto pimMsg = propertyReadBus.new_method_call(busName, path.c_str(), |
| 71 | propertiesintf, "GetAll"); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 72 | pimMsg.append(intf); |
| 73 | |
Patrick Venture | acecf6b | 2018-09-06 17:56:41 -0700 | [diff] [blame] | 74 | try |
| 75 | { |
Patrick Venture | acecf6b | 2018-09-06 17:56:41 -0700 | [diff] [blame] | 76 | PropertyMap propMap; |
Patrick Venture | 8f17914 | 2018-09-10 09:24:12 -0700 | [diff] [blame] | 77 | |
| 78 | /* a method could error but the call not error. */ |
| 79 | auto valueResponseMsg = propertyReadBus.call(pimMsg); |
Patrick Venture | 8f17914 | 2018-09-10 09:24:12 -0700 | [diff] [blame] | 80 | |
Patrick Venture | acecf6b | 2018-09-06 17:56:41 -0700 | [diff] [blame] | 81 | valueResponseMsg.read(propMap); |
| 82 | |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 83 | *value = std::get<bool>(propMap[property]); |
Patrick Venture | acecf6b | 2018-09-06 17:56:41 -0700 | [diff] [blame] | 84 | } |
| 85 | catch (const sdbusplus::exception::SdBusError& ex) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 86 | { |
| 87 | return IPMI_CC_INVALID; |
| 88 | } |
| 89 | |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 90 | return IPMI_CC_OK; |
| 91 | } |
| 92 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 93 | static ipmi_ret_t getFailsafeModeState(const uint8_t* reqBuf, uint8_t* replyBuf, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 94 | size_t* dataLen) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 95 | { |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 96 | bool current; |
| 97 | |
| 98 | if (*dataLen < sizeof(struct FanCtrlRequest)) |
| 99 | { |
| 100 | return IPMI_CC_INVALID; |
| 101 | } |
| 102 | |
| 103 | const auto request = |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 104 | reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 105 | |
Patrick Venture | 37b247a | 2020-08-03 11:15:21 -0700 | [diff] [blame] | 106 | ipmi_ret_t rc = |
| 107 | getFanCtrlProperty(request->zone, ¤t, failsafeProperty); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 108 | if (rc) |
| 109 | { |
| 110 | return rc; |
| 111 | } |
| 112 | |
| 113 | *replyBuf = (uint8_t)current; |
| 114 | *dataLen = sizeof(uint8_t); |
Patrick Venture | 37b247a | 2020-08-03 11:15:21 -0700 | [diff] [blame] | 115 | return IPMI_CC_OK; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* |
| 119 | * <method name="GetAll"> |
| 120 | * <arg name="interface" direction="in" type="s"/> |
| 121 | * <arg name="properties" direction="out" type="a{sv}"/> |
| 122 | * </method> |
| 123 | */ |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 124 | static ipmi_ret_t getManualModeState(const uint8_t* reqBuf, uint8_t* replyBuf, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 125 | size_t* dataLen) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 126 | { |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 127 | bool current; |
| 128 | |
| 129 | if (*dataLen < sizeof(struct FanCtrlRequest)) |
| 130 | { |
| 131 | return IPMI_CC_INVALID; |
| 132 | } |
| 133 | |
| 134 | const auto request = |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 135 | reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 136 | |
Patrick Venture | 37b247a | 2020-08-03 11:15:21 -0700 | [diff] [blame] | 137 | ipmi_ret_t rc = getFanCtrlProperty(request->zone, ¤t, manualProperty); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 138 | if (rc) |
| 139 | { |
| 140 | return rc; |
| 141 | } |
| 142 | |
| 143 | *replyBuf = (uint8_t)current; |
| 144 | *dataLen = sizeof(uint8_t); |
Patrick Venture | 37b247a | 2020-08-03 11:15:21 -0700 | [diff] [blame] | 145 | return IPMI_CC_OK; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * <method name="Set"> |
| 150 | * <arg name="interface" direction="in" type="s"/> |
| 151 | * <arg name="property" direction="in" type="s"/> |
| 152 | * <arg name="value" direction="in" type="v"/> |
| 153 | * </method> |
| 154 | */ |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 155 | static ipmi_ret_t setManualModeState(const uint8_t* reqBuf, uint8_t* replyBuf, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 156 | size_t* dataLen) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 157 | { |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 158 | if (*dataLen < sizeof(struct FanCtrlRequestSet)) |
| 159 | { |
| 160 | return IPMI_CC_INVALID; |
| 161 | } |
| 162 | |
William A. Kennington III | 323f1d9 | 2020-06-03 11:18:24 -0700 | [diff] [blame] | 163 | using Value = std::variant<bool>; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 164 | |
| 165 | const auto request = |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 166 | reinterpret_cast<const struct FanCtrlRequestSet*>(&reqBuf[0]); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 167 | |
| 168 | /* 0 is false, 1 is true */ |
| 169 | bool setValue = static_cast<bool>(request->value); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 170 | Value v{setValue}; |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 171 | |
James Feist | 9fa90c1 | 2019-01-11 15:35:22 -0800 | [diff] [blame] | 172 | auto PropertyWriteBus = sdbusplus::bus::new_system(); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 173 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 174 | std::string path = getControlPath(request->zone); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 175 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 176 | auto pimMsg = PropertyWriteBus.new_method_call(busName, path.c_str(), |
| 177 | propertiesintf, "Set"); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 178 | pimMsg.append(intf); |
| 179 | pimMsg.append(manualProperty); |
| 180 | pimMsg.append(v); |
Patrick Venture | acecf6b | 2018-09-06 17:56:41 -0700 | [diff] [blame] | 181 | |
Patrick Venture | 37b247a | 2020-08-03 11:15:21 -0700 | [diff] [blame] | 182 | ipmi_ret_t rc = IPMI_CC_OK; |
| 183 | |
Patrick Venture | acecf6b | 2018-09-06 17:56:41 -0700 | [diff] [blame] | 184 | try |
| 185 | { |
| 186 | PropertyWriteBus.call_noreply(pimMsg); |
| 187 | } |
| 188 | catch (const sdbusplus::exception::SdBusError& ex) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 189 | { |
| 190 | rc = IPMI_CC_INVALID; |
| 191 | } |
| 192 | /* TODO(venture): Should sanity check the result. */ |
| 193 | |
| 194 | return rc; |
| 195 | } |
| 196 | |
| 197 | /* Three command packages: get, set true, set false */ |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 198 | static ipmi_ret_t manualModeControl(ipmi_cmd_t cmd, const uint8_t* reqBuf, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 199 | uint8_t* replyCmdBuf, size_t* dataLen) |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 200 | { |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 201 | // FanCtrlRequest is the smaller of the requests, so it's at a minimum. |
| 202 | if (*dataLen < sizeof(struct FanCtrlRequest)) |
| 203 | { |
| 204 | return IPMI_CC_INVALID; |
| 205 | } |
| 206 | |
| 207 | const auto request = |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 208 | reinterpret_cast<const struct FanCtrlRequest*>(&reqBuf[0]); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 209 | |
Patrick Venture | 37b247a | 2020-08-03 11:15:21 -0700 | [diff] [blame] | 210 | ipmi_ret_t rc = IPMI_CC_OK; |
| 211 | |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 212 | switch (request->command) |
| 213 | { |
Patrick Venture | 1277543 | 2020-08-04 09:57:36 -0700 | [diff] [blame] | 214 | case getControlState: |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 215 | return getManualModeState(reqBuf, replyCmdBuf, dataLen); |
Patrick Venture | 1277543 | 2020-08-04 09:57:36 -0700 | [diff] [blame] | 216 | case setControlState: |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 217 | return setManualModeState(reqBuf, replyCmdBuf, dataLen); |
Patrick Venture | 1277543 | 2020-08-04 09:57:36 -0700 | [diff] [blame] | 218 | case getFailsafeState: |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 219 | return getFailsafeModeState(reqBuf, replyCmdBuf, dataLen); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 220 | default: |
| 221 | rc = IPMI_CC_INVALID; |
| 222 | } |
| 223 | |
| 224 | return rc; |
| 225 | } |
| 226 | |
Patrick Venture | 36ab6f6 | 2020-08-03 10:50:26 -0700 | [diff] [blame] | 227 | } // namespace ipmi |
| 228 | } // namespace pid_control |
| 229 | |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 230 | void setupGlobalOemFanControl() __attribute__((constructor)); |
| 231 | |
| 232 | void setupGlobalOemFanControl() |
| 233 | { |
Patrick Venture | ba00343 | 2018-07-27 06:59:05 -0700 | [diff] [blame] | 234 | oem::Router* router = oem::mutableRouter(); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 235 | |
| 236 | fprintf(stderr, |
| 237 | "Registering OEM:[%#08X], Cmd:[%#04X] for Manual Zone Control\n", |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 238 | oem::obmcOemNumber, oem::Cmd::fanManualCmd); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 239 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 240 | router->registerHandler(oem::obmcOemNumber, oem::Cmd::fanManualCmd, |
Patrick Venture | 36ab6f6 | 2020-08-03 10:50:26 -0700 | [diff] [blame] | 241 | pid_control::ipmi::manualModeControl); |
Patrick Venture | 391b8b0 | 2018-03-08 08:31:13 -0800 | [diff] [blame] | 242 | } |