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