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