Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1 | #include "sensorhandler.h" |
| 2 | |
| 3 | #include "fruread.hpp" |
| 4 | #include "ipmid.hpp" |
| 5 | #include "types.hpp" |
| 6 | #include "utils.hpp" |
| 7 | |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 8 | #include <mapper.h> |
Emily Shaffer | 1fabf22 | 2017-04-05 08:53:21 -0700 | [diff] [blame] | 9 | #include <math.h> |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <string.h> |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 12 | #include <systemd/sd-bus.h> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 13 | |
| 14 | #include <bitset> |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 15 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 16 | #include <phosphor-logging/log.hpp> |
| 17 | #include <set> |
| 18 | #include <xyz/openbmc_project/Common/error.hpp> |
| 19 | #include <xyz/openbmc_project/Sensor/Value/server.hpp> |
| 20 | |
| 21 | #include "host-ipmid/ipmid-api.h" |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 22 | |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 23 | static constexpr uint8_t fruInventoryDevice = 0x10; |
| 24 | static constexpr uint8_t IPMIFruInventory = 0x02; |
| 25 | static constexpr uint8_t BMCSlaveAddress = 0x20; |
| 26 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 27 | extern int updateSensorRecordFromSSRAESC(const void*); |
| 28 | extern sd_bus* bus; |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 29 | extern const ipmi::sensor::IdInfoMap sensors; |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 30 | extern const FruMap frus; |
| 31 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 32 | using namespace phosphor::logging; |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 33 | using InternalFailure = |
| 34 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 35 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 36 | void register_netfn_sen_functions() __attribute__((constructor)); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 37 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 38 | struct sensorTypemap_t |
| 39 | { |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 40 | uint8_t number; |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 41 | uint8_t typecode; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 42 | char dbusname[32]; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 43 | }; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 44 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 45 | sensorTypemap_t g_SensorTypeMap[] = { |
| 46 | |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 47 | {0x01, 0x6F, "Temp"}, |
| 48 | {0x0C, 0x6F, "DIMM"}, |
| 49 | {0x0C, 0x6F, "MEMORY_BUFFER"}, |
| 50 | {0x07, 0x6F, "PROC"}, |
| 51 | {0x07, 0x6F, "CORE"}, |
| 52 | {0x07, 0x6F, "CPU"}, |
| 53 | {0x0F, 0x6F, "BootProgress"}, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 54 | {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor |
| 55 | // type code os 0x09 |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 56 | {0xC3, 0x6F, "BootCount"}, |
| 57 | {0x1F, 0x6F, "OperatingSystemStatus"}, |
Chris Austen | 800ba71 | 2015-12-03 15:31:00 -0600 | [diff] [blame] | 58 | {0x12, 0x6F, "SYSTEM_EVENT"}, |
| 59 | {0xC7, 0x03, "SYSTEM"}, |
| 60 | {0xC7, 0x03, "MAIN_PLANAR"}, |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 61 | {0xC2, 0x6F, "PowerCap"}, |
Tom Joseph | 558184e | 2017-09-01 13:45:05 +0530 | [diff] [blame] | 62 | {0x0b, 0xCA, "PowerSupplyRedundancy"}, |
Jayanth Othayoth | 0661beb | 2017-03-22 06:00:58 -0500 | [diff] [blame] | 63 | {0xDA, 0x03, "TurboAllowed"}, |
Tom Joseph | 558184e | 2017-09-01 13:45:05 +0530 | [diff] [blame] | 64 | {0xD8, 0xC8, "PowerSupplyDerating"}, |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 65 | {0xFF, 0x00, ""}, |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 66 | }; |
| 67 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 68 | struct sensor_data_t |
| 69 | { |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 70 | uint8_t sennum; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 71 | } __attribute__((packed)); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 72 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 73 | struct sensorreadingresp_t |
| 74 | { |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 75 | uint8_t value; |
| 76 | uint8_t operation; |
| 77 | uint8_t indication[2]; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 78 | } __attribute__((packed)); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 79 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 80 | int get_bus_for_path(const char* path, char** busname) |
| 81 | { |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 82 | return mapper_get_service(bus, path, busname); |
| 83 | } |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 84 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 85 | int legacy_dbus_openbmc_path(const char* type, const uint8_t num, |
| 86 | dbus_interface_t* interface) |
| 87 | { |
| 88 | char* busname = NULL; |
| 89 | const char* iface = "org.openbmc.managers.System"; |
| 90 | const char* objname = "/org/openbmc/managers/System"; |
| 91 | char *str1 = NULL, *str2, *str3; |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 92 | sd_bus_error error = SD_BUS_ERROR_NULL; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 93 | sd_bus_message* reply = NULL; |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 94 | |
| 95 | int r; |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 96 | r = get_bus_for_path(objname, &busname); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 97 | if (r < 0) |
| 98 | { |
| 99 | fprintf(stderr, "Failed to get %s busname: %s\n", objname, |
| 100 | strerror(-r)); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 101 | goto final; |
| 102 | } |
| 103 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 104 | r = sd_bus_call_method(bus, busname, objname, iface, "getObjectFromByteId", |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 105 | &error, &reply, "sy", type, num); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 106 | if (r < 0) |
| 107 | { |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 108 | fprintf(stderr, "Failed to create a method call: %s", strerror(-r)); |
| 109 | goto final; |
| 110 | } |
| 111 | |
| 112 | r = sd_bus_message_read(reply, "(ss)", &str2, &str3); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 113 | if (r < 0) |
| 114 | { |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 115 | fprintf(stderr, "Failed to get a response: %s", strerror(-r)); |
| 116 | goto final; |
| 117 | } |
| 118 | |
Lei YU | 91875f7 | 2018-04-03 15:14:49 +0800 | [diff] [blame] | 119 | if (strlen(str2) == 0) |
| 120 | { |
| 121 | // Path being empty occurs when the sensor id is not in SystemManager |
| 122 | r = -EINVAL; |
| 123 | goto final; |
| 124 | } |
| 125 | |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 126 | r = get_bus_for_path(str2, &str1); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 127 | if (r < 0) |
| 128 | { |
| 129 | fprintf(stderr, "Failed to get %s busname: %s\n", str2, strerror(-r)); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 130 | goto final; |
| 131 | } |
| 132 | |
| 133 | strncpy(interface->bus, str1, MAX_DBUS_PATH); |
| 134 | strncpy(interface->path, str2, MAX_DBUS_PATH); |
| 135 | strncpy(interface->interface, str3, MAX_DBUS_PATH); |
| 136 | |
| 137 | interface->sensornumber = num; |
Emily Shaffer | 7117441 | 2017-04-05 15:10:40 -0700 | [diff] [blame] | 138 | // Make sure we know that the type hasn't been set, as newer codebase will |
| 139 | // set it automatically from the YAML at this step. |
| 140 | interface->sensortype = 0; |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 141 | |
| 142 | final: |
| 143 | |
| 144 | sd_bus_error_free(&error); |
| 145 | reply = sd_bus_message_unref(reply); |
| 146 | free(busname); |
| 147 | free(str1); |
| 148 | |
| 149 | return r; |
| 150 | } |
| 151 | |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 152 | // Use a lookup table to find the interface name of a specific sensor |
| 153 | // This will be used until an alternative is found. this is the first |
| 154 | // step for mapping IPMI |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 155 | int find_openbmc_path(uint8_t num, dbus_interface_t* interface) |
| 156 | { |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 157 | int rc; |
| 158 | |
| 159 | // When the sensor map does not contain the sensor requested, |
| 160 | // fall back to the legacy DBus lookup (deprecated) |
| 161 | const auto& sensor_it = sensors.find(num); |
| 162 | if (sensor_it == sensors.end()) |
| 163 | { |
| 164 | return legacy_dbus_openbmc_path("SENSOR", num, interface); |
| 165 | } |
| 166 | |
| 167 | const auto& info = sensor_it->second; |
| 168 | |
Patrick Williams | 8451edf | 2017-06-13 09:01:06 -0500 | [diff] [blame] | 169 | char* busname = nullptr; |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 170 | rc = get_bus_for_path(info.sensorPath.c_str(), &busname); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 171 | if (rc < 0) |
| 172 | { |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 173 | fprintf(stderr, "Failed to get %s busname: %s\n", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 174 | info.sensorPath.c_str(), busname); |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 175 | goto final; |
| 176 | } |
| 177 | |
| 178 | interface->sensortype = info.sensorType; |
| 179 | strcpy(interface->bus, busname); |
| 180 | strcpy(interface->path, info.sensorPath.c_str()); |
| 181 | // Take the interface name from the beginning of the DbusInterfaceMap. This |
| 182 | // works for the Value interface but may not suffice for more complex |
| 183 | // sensors. |
| 184 | // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103 |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 185 | strcpy(interface->interface, |
| 186 | info.propertyInterfaces.begin()->first.c_str()); |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 187 | interface->sensornumber = num; |
| 188 | |
| 189 | final: |
| 190 | free(busname); |
| 191 | return rc; |
| 192 | } |
| 193 | |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 194 | ///////////////////////////////////////////////////////////////////// |
| 195 | // |
| 196 | // Routines used by ipmi commands wanting to interact on the dbus |
| 197 | // |
| 198 | ///////////////////////////////////////////////////////////////////// |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 199 | int set_sensor_dbus_state_s(uint8_t number, const char* method, |
| 200 | const char* value) |
| 201 | { |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 202 | |
| 203 | dbus_interface_t a; |
| 204 | int r; |
| 205 | sd_bus_error error = SD_BUS_ERROR_NULL; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 206 | sd_bus_message* m = NULL; |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 207 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 208 | fprintf(ipmidbus, |
| 209 | "Attempting to set a dbus Variant Sensor 0x%02x via %s with a " |
| 210 | "value of %s\n", |
| 211 | number, method, value); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 212 | |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 213 | r = find_openbmc_path(number, &a); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 214 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 215 | if (r < 0) |
| 216 | { |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 217 | fprintf(stderr, "Failed to find Sensor 0x%02x\n", number); |
| 218 | return 0; |
| 219 | } |
| 220 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 221 | r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface, |
| 222 | method); |
| 223 | if (r < 0) |
| 224 | { |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 225 | fprintf(stderr, "Failed to create a method call: %s", strerror(-r)); |
| 226 | goto final; |
| 227 | } |
| 228 | |
| 229 | r = sd_bus_message_append(m, "v", "s", value); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 230 | if (r < 0) |
| 231 | { |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 232 | fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r)); |
| 233 | goto final; |
| 234 | } |
| 235 | |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 236 | r = sd_bus_call(bus, m, 0, &error, NULL); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 237 | if (r < 0) |
| 238 | { |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 239 | fprintf(stderr, "Failed to call the method: %s", strerror(-r)); |
| 240 | } |
| 241 | |
| 242 | final: |
| 243 | sd_bus_error_free(&error); |
| 244 | m = sd_bus_message_unref(m); |
| 245 | |
| 246 | return 0; |
| 247 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 248 | int set_sensor_dbus_state_y(uint8_t number, const char* method, |
| 249 | const uint8_t value) |
| 250 | { |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 251 | |
| 252 | dbus_interface_t a; |
| 253 | int r; |
| 254 | sd_bus_error error = SD_BUS_ERROR_NULL; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 255 | sd_bus_message* m = NULL; |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 256 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 257 | fprintf(ipmidbus, |
| 258 | "Attempting to set a dbus Variant Sensor 0x%02x via %s with a " |
| 259 | "value of 0x%02x\n", |
| 260 | number, method, value); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 261 | |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 262 | r = find_openbmc_path(number, &a); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 263 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 264 | if (r < 0) |
| 265 | { |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 266 | fprintf(stderr, "Failed to find Sensor 0x%02x\n", number); |
| 267 | return 0; |
| 268 | } |
| 269 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 270 | r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface, |
| 271 | method); |
| 272 | if (r < 0) |
| 273 | { |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 274 | fprintf(stderr, "Failed to create a method call: %s", strerror(-r)); |
| 275 | goto final; |
| 276 | } |
| 277 | |
| 278 | r = sd_bus_message_append(m, "v", "i", value); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 279 | if (r < 0) |
| 280 | { |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 281 | fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r)); |
| 282 | goto final; |
| 283 | } |
| 284 | |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 285 | r = sd_bus_call(bus, m, 0, &error, NULL); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 286 | if (r < 0) |
| 287 | { |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 288 | fprintf(stderr, "12 Failed to call the method: %s", strerror(-r)); |
| 289 | } |
| 290 | |
| 291 | final: |
| 292 | sd_bus_error_free(&error); |
| 293 | m = sd_bus_message_unref(m); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 298 | uint8_t dbus_to_sensor_type(char* p) |
| 299 | { |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 300 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 301 | sensorTypemap_t* s = g_SensorTypeMap; |
| 302 | char r = 0; |
| 303 | while (s->number != 0xFF) |
| 304 | { |
| 305 | if (!strcmp(s->dbusname, p)) |
| 306 | { |
Tom Joseph | 558184e | 2017-09-01 13:45:05 +0530 | [diff] [blame] | 307 | r = s->typecode; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 308 | break; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 309 | } |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 310 | s++; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 313 | if (s->number == 0xFF) |
| 314 | printf("Failed to find Sensor Type %s\n", p); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 315 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 316 | return r; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 317 | } |
| 318 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 319 | uint8_t get_type_from_interface(dbus_interface_t dbus_if) |
| 320 | { |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 321 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 322 | char* p; |
Brad Bishop | 5600345 | 2016-10-05 21:49:19 -0400 | [diff] [blame] | 323 | uint8_t type; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 324 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 325 | // This is where sensors that do not exist in dbus but do |
| 326 | // exist in the host code stop. This should indicate it |
| 327 | // is not a supported sensor |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 328 | if (dbus_if.interface[0] == 0) |
| 329 | { |
| 330 | return 0; |
| 331 | } |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 332 | |
Emily Shaffer | 7117441 | 2017-04-05 15:10:40 -0700 | [diff] [blame] | 333 | // Fetch type from interface itself. |
| 334 | if (dbus_if.sensortype != 0) |
| 335 | { |
| 336 | type = dbus_if.sensortype; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 337 | } |
| 338 | else |
| 339 | { |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 340 | // Non InventoryItems |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 341 | p = strrchr(dbus_if.path, '/'); |
| 342 | type = dbus_to_sensor_type(p + 1); |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 343 | } |
| 344 | |
Brad Bishop | 5600345 | 2016-10-05 21:49:19 -0400 | [diff] [blame] | 345 | return type; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 346 | } |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 347 | |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 348 | // Replaces find_sensor |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 349 | uint8_t find_type_for_sensor_number(uint8_t num) |
| 350 | { |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 351 | int r; |
| 352 | dbus_interface_t dbus_if; |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 353 | r = find_openbmc_path(num, &dbus_if); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 354 | if (r < 0) |
| 355 | { |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 356 | fprintf(stderr, "Could not find sensor %d\n", num); |
Lei YU | 91875f7 | 2018-04-03 15:14:49 +0800 | [diff] [blame] | 357 | return 0; |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 358 | } |
| 359 | return get_type_from_interface(dbus_if); |
| 360 | } |
| 361 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 362 | ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 363 | ipmi_request_t request, |
| 364 | ipmi_response_t response, |
| 365 | ipmi_data_len_t data_len, |
| 366 | ipmi_context_t context) |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 367 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 368 | sensor_data_t* reqptr = (sensor_data_t*)request; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 369 | ipmi_ret_t rc = IPMI_CC_OK; |
| 370 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 371 | printf("IPMI GET_SENSOR_TYPE [0x%02X]\n", reqptr->sennum); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 372 | |
| 373 | // TODO Not sure what the System-event-sensor is suppose to return |
| 374 | // need to ask Hostboot team |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 375 | unsigned char buf[] = {0x00, 0x6F}; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 376 | |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 377 | buf[0] = find_type_for_sensor_number(reqptr->sennum); |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 378 | |
| 379 | // HACK UNTIL Dbus gets updated or we find a better way |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 380 | if (buf[0] == 0) |
| 381 | { |
Chris Austen | 800ba71 | 2015-12-03 15:31:00 -0600 | [diff] [blame] | 382 | rc = IPMI_CC_SENSOR_INVALID; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 383 | } |
| 384 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 385 | *data_len = sizeof(buf); |
| 386 | memcpy(response, &buf, *data_len); |
| 387 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 388 | return rc; |
| 389 | } |
| 390 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 391 | const std::set<std::string> analogSensorInterfaces = { |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 392 | "xyz.openbmc_project.Sensor.Value", |
Patrick Venture | e9a6405 | 2017-08-18 19:17:27 -0700 | [diff] [blame] | 393 | "xyz.openbmc_project.Control.FanPwm", |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | bool isAnalogSensor(const std::string& interface) |
| 397 | { |
| 398 | return (analogSensorInterfaces.count(interface)); |
| 399 | } |
| 400 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 401 | ipmi_ret_t setSensorReading(void* request) |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 402 | { |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 403 | ipmi::sensor::SetSensorReadingReq cmdData = |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 404 | *(static_cast<ipmi::sensor::SetSensorReadingReq*>(request)); |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 405 | |
| 406 | // Check if the Sensor Number is present |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 407 | const auto iter = sensors.find(cmdData.number); |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 408 | if (iter == sensors.end()) |
| 409 | { |
| 410 | return IPMI_CC_SENSOR_INVALID; |
| 411 | } |
| 412 | |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 413 | try |
| 414 | { |
Jayanth Othayoth | 0922bde | 2018-04-02 07:59:34 -0500 | [diff] [blame] | 415 | if (ipmi::sensor::Mutability::Write != |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 416 | (iter->second.mutability & ipmi::sensor::Mutability::Write)) |
Jayanth Othayoth | 0922bde | 2018-04-02 07:59:34 -0500 | [diff] [blame] | 417 | { |
| 418 | log<level::ERR>("Sensor Set operation is not allowed", |
| 419 | entry("SENSOR_NUM=%d", cmdData.number)); |
| 420 | return IPMI_CC_ILLEGAL_COMMAND; |
| 421 | } |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 422 | return iter->second.updateFunc(cmdData, iter->second); |
| 423 | } |
| 424 | catch (InternalFailure& e) |
| 425 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 426 | log<level::ERR>("Set sensor failed", |
| 427 | entry("SENSOR_NUM=%d", cmdData.number)); |
| 428 | commit<InternalFailure>(); |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 429 | } |
Tom Joseph | 8202432 | 2017-09-28 20:07:29 +0530 | [diff] [blame] | 430 | catch (const std::runtime_error& e) |
| 431 | { |
| 432 | log<level::ERR>(e.what()); |
| 433 | } |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 434 | |
| 435 | return IPMI_CC_UNSPECIFIED_ERROR; |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 436 | } |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 437 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 438 | ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 439 | ipmi_request_t request, ipmi_response_t response, |
| 440 | ipmi_data_len_t data_len, ipmi_context_t context) |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 441 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 442 | sensor_data_t* reqptr = (sensor_data_t*)request; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 443 | |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 444 | log<level::DEBUG>("IPMI SET_SENSOR", |
| 445 | entry("SENSOR_NUM=0x%02x", reqptr->sennum)); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 446 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 447 | /* |
| 448 | * This would support the Set Sensor Reading command for the presence |
| 449 | * and functional state of Processor, Core & DIMM. For the remaining |
| 450 | * sensors the existing support is invoked. |
| 451 | */ |
| 452 | auto ipmiRC = setSensorReading(request); |
| 453 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 454 | if (ipmiRC == IPMI_CC_SENSOR_INVALID) |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 455 | { |
| 456 | updateSensorRecordFromSSRAESC(reqptr); |
| 457 | ipmiRC = IPMI_CC_OK; |
| 458 | } |
Chris Austen | 8a45e7c | 2015-10-15 00:31:46 -0500 | [diff] [blame] | 459 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 460 | *data_len = 0; |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 461 | return ipmiRC; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 462 | } |
| 463 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 464 | ipmi_ret_t legacyGetSensorReading(uint8_t sensorNum, ipmi_response_t response, |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 465 | ipmi_data_len_t data_len) |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 466 | { |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 467 | int r; |
| 468 | dbus_interface_t a; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 469 | sd_bus* bus = ipmid_get_sd_bus_connection(); |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 470 | ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID; |
| 471 | uint8_t type = 0; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 472 | sd_bus_message* reply = NULL; |
Adriana Kobylak | 9312598 | 2016-03-01 12:48:10 -0600 | [diff] [blame] | 473 | int reading = 0; |
Dhruvaraj Subhashchandran | 6244f93 | 2017-11-23 01:08:46 -0600 | [diff] [blame] | 474 | char* assertion = NULL; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 475 | sensorreadingresp_t* resp = (sensorreadingresp_t*)response; |
| 476 | *data_len = 0; |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 477 | |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 478 | r = find_openbmc_path(sensorNum, &a); |
Tom Joseph | 40c35b1 | 2017-09-07 02:04:42 +0530 | [diff] [blame] | 479 | if (r < 0) |
| 480 | { |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 481 | sd_journal_print(LOG_ERR, "Failed to find Sensor 0x%02x\n", sensorNum); |
| 482 | return IPMI_CC_SENSOR_INVALID; |
Nan Li | 36deb76 | 2016-05-12 10:23:41 +0800 | [diff] [blame] | 483 | } |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 484 | |
| 485 | type = get_type_from_interface(a); |
| 486 | if (type == 0) |
Tom Joseph | 40c35b1 | 2017-09-07 02:04:42 +0530 | [diff] [blame] | 487 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 488 | sd_journal_print(LOG_ERR, "Failed to find Sensor 0x%02x\n", sensorNum); |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 489 | return IPMI_CC_SENSOR_INVALID; |
Brad Bishop | 5600345 | 2016-10-05 21:49:19 -0400 | [diff] [blame] | 490 | } |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 491 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 492 | switch (type) |
| 493 | { |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 494 | case 0xC2: |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 495 | r = sd_bus_get_property(bus, a.bus, a.path, a.interface, "value", |
| 496 | NULL, &reply, "i"); |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 497 | if (r < 0) |
| 498 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 499 | sd_journal_print(LOG_ERR, |
| 500 | "Failed to call sd_bus_get_property:" |
| 501 | " %d, %s\n", |
| 502 | r, strerror(-r)); |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 503 | sd_journal_print(LOG_ERR, "Bus: %s, Path: %s, Interface: %s\n", |
| 504 | a.bus, a.path, a.interface); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 505 | break; |
| 506 | } |
| 507 | |
Adriana Kobylak | 9312598 | 2016-03-01 12:48:10 -0600 | [diff] [blame] | 508 | r = sd_bus_message_read(reply, "i", &reading); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 509 | if (r < 0) |
| 510 | { |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 511 | sd_journal_print(LOG_ERR, "Failed to read sensor: %s\n", |
| 512 | strerror(-r)); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 513 | break; |
| 514 | } |
| 515 | |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 516 | rc = IPMI_CC_OK; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 517 | *data_len = sizeof(sensorreadingresp_t); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 518 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 519 | resp->value = (uint8_t)reading; |
| 520 | resp->operation = 0; |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 521 | resp->indication[0] = 0; |
| 522 | resp->indication[1] = 0; |
| 523 | break; |
| 524 | |
Nagaraju Goruganti | e98d146 | 2018-05-08 06:42:57 -0500 | [diff] [blame] | 525 | case 0xC8: |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 526 | r = sd_bus_get_property(bus, a.bus, a.path, a.interface, "value", |
| 527 | NULL, &reply, "i"); |
Nagaraju Goruganti | e98d146 | 2018-05-08 06:42:57 -0500 | [diff] [blame] | 528 | if (r < 0) |
| 529 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 530 | sd_journal_print(LOG_ERR, |
| 531 | "Failed to call sd_bus_get_property:" |
| 532 | " %d, %s\n", |
| 533 | r, strerror(-r)); |
Nagaraju Goruganti | e98d146 | 2018-05-08 06:42:57 -0500 | [diff] [blame] | 534 | sd_journal_print(LOG_ERR, "Bus: %s, Path: %s, Interface: %s\n", |
| 535 | a.bus, a.path, a.interface); |
| 536 | break; |
| 537 | } |
| 538 | |
| 539 | r = sd_bus_message_read(reply, "i", &reading); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 540 | if (r < 0) |
| 541 | { |
Nagaraju Goruganti | e98d146 | 2018-05-08 06:42:57 -0500 | [diff] [blame] | 542 | sd_journal_print(LOG_ERR, "Failed to read sensor: %s\n", |
| 543 | strerror(-r)); |
| 544 | break; |
| 545 | } |
| 546 | |
| 547 | rc = IPMI_CC_OK; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 548 | *data_len = sizeof(sensorreadingresp_t); |
Nagaraju Goruganti | e98d146 | 2018-05-08 06:42:57 -0500 | [diff] [blame] | 549 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 550 | resp->value = 0; |
| 551 | resp->operation = 0; |
Nagaraju Goruganti | e98d146 | 2018-05-08 06:42:57 -0500 | [diff] [blame] | 552 | resp->indication[0] = (uint8_t)reading; |
| 553 | resp->indication[1] = 0; |
| 554 | break; |
| 555 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 556 | // TODO openbmc/openbmc#2154 Move this sensor to right place. |
Dhruvaraj Subhashchandran | 6244f93 | 2017-11-23 01:08:46 -0600 | [diff] [blame] | 557 | case 0xCA: |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 558 | r = sd_bus_get_property(bus, a.bus, a.path, a.interface, "value", |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 559 | NULL, &reply, "s"); |
| 560 | if (r < 0) |
| 561 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 562 | sd_journal_print(LOG_ERR, |
| 563 | "Failed to call sd_bus_get_property:" |
| 564 | " %d, %s\n", |
| 565 | r, strerror(-r)); |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 566 | sd_journal_print(LOG_ERR, "Bus: %s, Path: %s, Interface: %s\n", |
| 567 | a.bus, a.path, a.interface); |
Dhruvaraj Subhashchandran | 6244f93 | 2017-11-23 01:08:46 -0600 | [diff] [blame] | 568 | break; |
| 569 | } |
| 570 | |
| 571 | r = sd_bus_message_read(reply, "s", &assertion); |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 572 | if (r < 0) |
| 573 | { |
| 574 | sd_journal_print(LOG_ERR, "Failed to read sensor: %s\n", |
| 575 | strerror(-r)); |
Dhruvaraj Subhashchandran | 6244f93 | 2017-11-23 01:08:46 -0600 | [diff] [blame] | 576 | break; |
| 577 | } |
| 578 | |
| 579 | rc = IPMI_CC_OK; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 580 | *data_len = sizeof(sensorreadingresp_t); |
Dhruvaraj Subhashchandran | 6244f93 | 2017-11-23 01:08:46 -0600 | [diff] [blame] | 581 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 582 | resp->value = 0; |
| 583 | resp->operation = 0; |
| 584 | if (strcmp(assertion, "Enabled") == 0) |
Dhruvaraj Subhashchandran | 6244f93 | 2017-11-23 01:08:46 -0600 | [diff] [blame] | 585 | { |
| 586 | resp->indication[0] = 0x02; |
| 587 | } |
| 588 | else |
| 589 | { |
| 590 | resp->indication[0] = 0x1; |
| 591 | } |
| 592 | resp->indication[1] = 0; |
Dhruvaraj Subhashchandran | 6244f93 | 2017-11-23 01:08:46 -0600 | [diff] [blame] | 593 | break; |
| 594 | |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 595 | default: |
Tom Joseph | 14c1546 | 2017-09-07 02:16:51 +0530 | [diff] [blame] | 596 | { |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 597 | return IPMI_CC_SENSOR_INVALID; |
Tom Joseph | 14c1546 | 2017-09-07 02:16:51 +0530 | [diff] [blame] | 598 | } |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 599 | } |
| 600 | |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 601 | reply = sd_bus_message_unref(reply); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 602 | |
| 603 | return rc; |
| 604 | } |
| 605 | |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 606 | ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 607 | ipmi_request_t request, |
| 608 | ipmi_response_t response, |
| 609 | ipmi_data_len_t data_len, |
| 610 | ipmi_context_t context) |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 611 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 612 | sensor_data_t* reqptr = (sensor_data_t*)request; |
| 613 | sensorreadingresp_t* resp = (sensorreadingresp_t*)response; |
| 614 | ipmi::sensor::GetSensorResponse getResponse{}; |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 615 | static constexpr auto scanningEnabledBit = 6; |
| 616 | |
| 617 | const auto iter = sensors.find(reqptr->sennum); |
| 618 | if (iter == sensors.end()) |
| 619 | { |
| 620 | return legacyGetSensorReading(reqptr->sennum, response, data_len); |
| 621 | } |
| 622 | if (ipmi::sensor::Mutability::Read != |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 623 | (iter->second.mutability & ipmi::sensor::Mutability::Read)) |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 624 | { |
Jayanth Othayoth | 6ccf881 | 2018-04-05 22:58:48 -0500 | [diff] [blame] | 625 | return IPMI_CC_ILLEGAL_COMMAND; |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | try |
| 629 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 630 | getResponse = iter->second.getFunc(iter->second); |
Tom Joseph | 3ee668f | 2018-03-02 19:49:17 +0530 | [diff] [blame] | 631 | *data_len = getResponse.size(); |
| 632 | memcpy(resp, getResponse.data(), *data_len); |
| 633 | resp->operation = 1 << scanningEnabledBit; |
| 634 | return IPMI_CC_OK; |
| 635 | } |
| 636 | catch (const std::exception& e) |
| 637 | { |
| 638 | *data_len = getResponse.size(); |
| 639 | memcpy(resp, getResponse.data(), *data_len); |
| 640 | return IPMI_CC_OK; |
| 641 | } |
| 642 | } |
| 643 | |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 644 | void getSensorThresholds(uint8_t sensorNum, |
| 645 | get_sdr::GetSensorThresholdsResponse* response) |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 646 | { |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 647 | constexpr auto warningThreshIntf = |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 648 | "xyz.openbmc_project.Sensor.Threshold.Warning"; |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 649 | constexpr auto criticalThreshIntf = |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 650 | "xyz.openbmc_project.Sensor.Threshold.Critical"; |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 651 | |
| 652 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 653 | |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 654 | const auto iter = sensors.find(sensorNum); |
| 655 | const auto info = iter->second; |
| 656 | |
| 657 | auto service = ipmi::getService(bus, info.sensorInterface, info.sensorPath); |
| 658 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 659 | auto warnThresholds = ipmi::getAllDbusProperties( |
| 660 | bus, service, info.sensorPath, warningThreshIntf); |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 661 | |
James Feist | 1e12112 | 2018-07-31 11:44:09 -0700 | [diff] [blame] | 662 | double warnLow = mapbox::util::apply_visitor(ipmi::VariantToDoubleVisitor(), |
| 663 | warnThresholds["WarningLow"]); |
| 664 | double warnHigh = mapbox::util::apply_visitor( |
| 665 | ipmi::VariantToDoubleVisitor(), warnThresholds["WarningHigh"]); |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 666 | |
| 667 | if (warnLow != 0) |
| 668 | { |
| 669 | warnLow *= pow(10, info.scale - info.exponentR); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 670 | response->lowerNonCritical = static_cast<uint8_t>( |
| 671 | (warnLow - info.scaledOffset) / info.coefficientM); |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 672 | response->validMask |= static_cast<uint8_t>( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 673 | ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK); |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | if (warnHigh != 0) |
| 677 | { |
| 678 | warnHigh *= pow(10, info.scale - info.exponentR); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 679 | response->upperNonCritical = static_cast<uint8_t>( |
| 680 | (warnHigh - info.scaledOffset) / info.coefficientM); |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 681 | response->validMask |= static_cast<uint8_t>( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 682 | ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK); |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 683 | } |
| 684 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 685 | auto critThresholds = ipmi::getAllDbusProperties( |
| 686 | bus, service, info.sensorPath, criticalThreshIntf); |
James Feist | 1e12112 | 2018-07-31 11:44:09 -0700 | [diff] [blame] | 687 | double critLow = mapbox::util::apply_visitor(ipmi::VariantToDoubleVisitor(), |
| 688 | critThresholds["CriticalLow"]); |
| 689 | double critHigh = mapbox::util::apply_visitor( |
| 690 | ipmi::VariantToDoubleVisitor(), critThresholds["CriticalHigh"]); |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 691 | |
| 692 | if (critLow != 0) |
| 693 | { |
| 694 | critLow *= pow(10, info.scale - info.exponentR); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 695 | response->lowerCritical = static_cast<uint8_t>( |
| 696 | (critLow - info.scaledOffset) / info.coefficientM); |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 697 | response->validMask |= static_cast<uint8_t>( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 698 | ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK); |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | if (critHigh != 0) |
| 702 | { |
| 703 | critHigh *= pow(10, info.scale - info.exponentR); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 704 | response->upperCritical = static_cast<uint8_t>( |
| 705 | (critHigh - info.scaledOffset) / info.coefficientM); |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 706 | response->validMask |= static_cast<uint8_t>( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 707 | ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK); |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
| 711 | ipmi_ret_t ipmi_sen_get_sensor_thresholds(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 712 | ipmi_request_t request, |
| 713 | ipmi_response_t response, |
| 714 | ipmi_data_len_t data_len, |
| 715 | ipmi_context_t context) |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 716 | { |
| 717 | constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value"; |
| 718 | |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 719 | if (*data_len != sizeof(uint8_t)) |
| 720 | { |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 721 | *data_len = 0; |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 722 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 723 | } |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 724 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 725 | auto sensorNum = *(reinterpret_cast<const uint8_t*>(request)); |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 726 | *data_len = 0; |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 727 | |
| 728 | const auto iter = sensors.find(sensorNum); |
| 729 | if (iter == sensors.end()) |
| 730 | { |
| 731 | return IPMI_CC_SENSOR_INVALID; |
| 732 | } |
| 733 | |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 734 | const auto info = iter->second; |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 735 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 736 | // Proceed only if the sensor value interface is implemented. |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 737 | if (info.propertyInterfaces.find(valueInterface) == |
| 738 | info.propertyInterfaces.end()) |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 739 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 740 | // return with valid mask as 0 |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 741 | return IPMI_CC_OK; |
| 742 | } |
| 743 | |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 744 | auto responseData = |
| 745 | reinterpret_cast<get_sdr::GetSensorThresholdsResponse*>(response); |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 746 | |
| 747 | try |
| 748 | { |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 749 | getSensorThresholds(sensorNum, responseData); |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 750 | } |
Tom Joseph | 0ac0dd2 | 2018-02-16 09:14:45 +0530 | [diff] [blame] | 751 | catch (std::exception& e) |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 752 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 753 | // Mask if the property is not present |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 754 | responseData->validMask = 0; |
| 755 | } |
| 756 | |
| 757 | *data_len = sizeof(get_sdr::GetSensorThresholdsResponse); |
| 758 | return IPMI_CC_OK; |
| 759 | } |
| 760 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 761 | ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 762 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 763 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 764 | { |
Nan Li | 70aa8d9 | 2016-08-29 00:11:10 +0800 | [diff] [blame] | 765 | ipmi_ret_t rc = IPMI_CC_INVALID; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 766 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 767 | printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n", netfn, cmd); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 768 | *data_len = 0; |
| 769 | |
| 770 | return rc; |
| 771 | } |
| 772 | |
Emily Shaffer | d06e0e7 | 2017-04-05 09:08:57 -0700 | [diff] [blame] | 773 | ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 774 | ipmi_request_t request, |
| 775 | ipmi_response_t response, |
| 776 | ipmi_data_len_t data_len, |
| 777 | ipmi_context_t context) |
| 778 | { |
| 779 | auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response); |
| 780 | if (request == nullptr || |
| 781 | get_sdr_info::request::get_count(request) == false) |
| 782 | { |
| 783 | // Get Sensor Count |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 784 | resp->count = sensors.size() + frus.size(); |
Emily Shaffer | d06e0e7 | 2017-04-05 09:08:57 -0700 | [diff] [blame] | 785 | } |
| 786 | else |
| 787 | { |
| 788 | resp->count = 1; |
| 789 | } |
| 790 | |
| 791 | // Multiple LUNs not supported. |
| 792 | namespace response = get_sdr_info::response; |
| 793 | response::set_lun_present(0, &(resp->luns_and_dynamic_population)); |
| 794 | response::set_lun_not_present(1, &(resp->luns_and_dynamic_population)); |
| 795 | response::set_lun_not_present(2, &(resp->luns_and_dynamic_population)); |
| 796 | response::set_lun_not_present(3, &(resp->luns_and_dynamic_population)); |
| 797 | response::set_static_population(&(resp->luns_and_dynamic_population)); |
| 798 | |
| 799 | *data_len = SDR_INFO_RESP_SIZE; |
| 800 | |
| 801 | return IPMI_CC_OK; |
| 802 | } |
| 803 | |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 804 | ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 805 | ipmi_request_t request, |
| 806 | ipmi_response_t response, |
| 807 | ipmi_data_len_t data_len, |
| 808 | ipmi_context_t context) |
| 809 | { |
| 810 | // A constant reservation ID is okay until we implement add/remove SDR. |
| 811 | const uint16_t reservation_id = 1; |
| 812 | *(uint16_t*)response = reservation_id; |
Emily Shaffer | 5a5a628 | 2017-08-15 11:17:17 -0700 | [diff] [blame] | 813 | *data_len = sizeof(uint16_t); |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 814 | |
| 815 | printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response); |
| 816 | return IPMI_CC_OK; |
| 817 | } |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 818 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 819 | void setUnitFieldsForObject(const ipmi::sensor::Info* info, |
| 820 | get_sdr::SensorDataFullRecordBody* body) |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 821 | { |
Tom Joseph | dc212b2 | 2018-02-16 09:59:57 +0530 | [diff] [blame] | 822 | namespace server = sdbusplus::xyz::openbmc_project::Sensor::server; |
| 823 | try |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 824 | { |
Tom Joseph | dc212b2 | 2018-02-16 09:59:57 +0530 | [diff] [blame] | 825 | auto unit = server::Value::convertUnitFromString(info->unit); |
| 826 | // Unit strings defined in |
| 827 | // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml |
| 828 | switch (unit) |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 829 | { |
Tom Joseph | dc212b2 | 2018-02-16 09:59:57 +0530 | [diff] [blame] | 830 | case server::Value::Unit::DegreesC: |
| 831 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C; |
| 832 | break; |
| 833 | case server::Value::Unit::RPMS: |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 834 | body->sensor_units_2_base = |
| 835 | get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions |
Tom Joseph | dc212b2 | 2018-02-16 09:59:57 +0530 | [diff] [blame] | 836 | get_sdr::body::set_rate_unit(0b100, body); // per minute |
| 837 | break; |
| 838 | case server::Value::Unit::Volts: |
| 839 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS; |
| 840 | break; |
| 841 | case server::Value::Unit::Meters: |
| 842 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS; |
| 843 | break; |
| 844 | case server::Value::Unit::Amperes: |
| 845 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES; |
| 846 | break; |
| 847 | case server::Value::Unit::Joules: |
| 848 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES; |
| 849 | break; |
| 850 | case server::Value::Unit::Watts: |
| 851 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_WATTS; |
| 852 | break; |
| 853 | default: |
| 854 | // Cannot be hit. |
| 855 | fprintf(stderr, "Unknown value unit type: = %s\n", |
| 856 | info->unit.c_str()); |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 857 | } |
| 858 | } |
Tom Joseph | dc212b2 | 2018-02-16 09:59:57 +0530 | [diff] [blame] | 859 | catch (sdbusplus::exception::InvalidEnumString e) |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 860 | { |
Tom Joseph | dc212b2 | 2018-02-16 09:59:57 +0530 | [diff] [blame] | 861 | log<level::WARNING>("Warning: no unit provided for sensor!"); |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 862 | } |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 865 | ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody* body, |
| 866 | const ipmi::sensor::Info* info, |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 867 | ipmi_data_len_t data_len) |
| 868 | { |
| 869 | /* Functional sensor case */ |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 870 | if (isAnalogSensor(info->propertyInterfaces.begin()->first)) |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 871 | { |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 872 | |
| 873 | body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a % |
| 874 | |
| 875 | /* Unit info */ |
Tom Joseph | dc212b2 | 2018-02-16 09:59:57 +0530 | [diff] [blame] | 876 | setUnitFieldsForObject(info, body); |
Emily Shaffer | 10f4959 | 2017-05-10 12:01:10 -0700 | [diff] [blame] | 877 | |
| 878 | get_sdr::body::set_b(info->coefficientB, body); |
| 879 | get_sdr::body::set_m(info->coefficientM, body); |
| 880 | get_sdr::body::set_b_exp(info->exponentB, body); |
Tom Joseph | dc212b2 | 2018-02-16 09:59:57 +0530 | [diff] [blame] | 881 | get_sdr::body::set_r_exp(info->exponentR, body); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 882 | |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 883 | get_sdr::body::set_id_type(0b00, body); // 00 = unicode |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Tom Joseph | 9642391 | 2018-01-25 00:14:34 +0530 | [diff] [blame] | 886 | /* ID string */ |
| 887 | auto id_string = info->sensorNameFunc(*info); |
| 888 | |
| 889 | if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH) |
| 890 | { |
| 891 | get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body); |
| 892 | } |
| 893 | else |
| 894 | { |
| 895 | get_sdr::body::set_id_strlen(id_string.length(), body); |
| 896 | } |
| 897 | strncpy(body->id_string, id_string.c_str(), |
| 898 | get_sdr::body::get_id_strlen(body)); |
| 899 | |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 900 | return IPMI_CC_OK; |
| 901 | }; |
| 902 | |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 903 | ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response, |
| 904 | ipmi_data_len_t data_len) |
| 905 | { |
| 906 | auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request); |
| 907 | auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 908 | get_sdr::SensorDataFruRecord record{}; |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 909 | auto dataLength = 0; |
| 910 | |
| 911 | auto fru = frus.begin(); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 912 | uint8_t fruID{}; |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 913 | auto recordID = get_sdr::request::get_record_id(req); |
| 914 | |
| 915 | fruID = recordID - FRU_RECORD_ID_START; |
| 916 | fru = frus.find(fruID); |
| 917 | if (fru == frus.end()) |
| 918 | { |
| 919 | return IPMI_CC_SENSOR_INVALID; |
| 920 | } |
| 921 | |
| 922 | /* Header */ |
| 923 | get_sdr::header::set_record_id(recordID, &(record.header)); |
| 924 | record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1 |
| 925 | record.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD; |
| 926 | record.header.record_length = sizeof(record.key) + sizeof(record.body); |
| 927 | |
| 928 | /* Key */ |
| 929 | record.key.fruID = fruID; |
| 930 | record.key.accessLun |= IPMI_LOGICAL_FRU; |
| 931 | record.key.deviceAddress = BMCSlaveAddress; |
| 932 | |
| 933 | /* Body */ |
| 934 | record.body.entityID = fru->second[0].entityID; |
| 935 | record.body.entityInstance = fru->second[0].entityInstance; |
| 936 | record.body.deviceType = fruInventoryDevice; |
| 937 | record.body.deviceTypeModifier = IPMIFruInventory; |
| 938 | |
| 939 | /* Device ID string */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 940 | auto deviceID = |
| 941 | fru->second[0].path.substr(fru->second[0].path.find_last_of('/') + 1, |
| 942 | fru->second[0].path.length()); |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 943 | |
| 944 | if (deviceID.length() > get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH) |
| 945 | { |
| 946 | get_sdr::body::set_device_id_strlen( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 947 | get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH, &(record.body)); |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 948 | } |
| 949 | else |
| 950 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 951 | get_sdr::body::set_device_id_strlen(deviceID.length(), &(record.body)); |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | strncpy(record.body.deviceID, deviceID.c_str(), |
| 955 | get_sdr::body::get_device_id_strlen(&(record.body))); |
| 956 | |
| 957 | if (++fru == frus.end()) |
| 958 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 959 | get_sdr::response::set_next_record_id(END_OF_RECORD, |
| 960 | resp); // last record |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 961 | } |
| 962 | else |
| 963 | { |
| 964 | get_sdr::response::set_next_record_id( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 965 | (FRU_RECORD_ID_START + fru->first), resp); |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | if (req->bytes_to_read > (sizeof(*resp) - req->offset)) |
| 969 | { |
| 970 | dataLength = (sizeof(*resp) - req->offset); |
| 971 | } |
| 972 | else |
| 973 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 974 | dataLength = req->bytes_to_read; |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | if (dataLength <= 0) |
| 978 | { |
| 979 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 980 | } |
| 981 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 982 | memcpy(resp->record_data, reinterpret_cast<uint8_t*>(&record) + req->offset, |
| 983 | (dataLength)); |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 984 | |
| 985 | *data_len = dataLength; |
| 986 | *data_len += 2; // additional 2 bytes for next record ID |
| 987 | |
| 988 | return IPMI_CC_OK; |
| 989 | } |
| 990 | |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 991 | ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 992 | ipmi_request_t request, ipmi_response_t response, |
| 993 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 994 | { |
| 995 | ipmi_ret_t ret = IPMI_CC_OK; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 996 | get_sdr::GetSdrReq* req = (get_sdr::GetSdrReq*)request; |
| 997 | get_sdr::GetSdrResp* resp = (get_sdr::GetSdrResp*)response; |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 998 | get_sdr::SensorDataFullRecord record = {0}; |
| 999 | if (req != NULL) |
| 1000 | { |
| 1001 | // Note: we use an iterator so we can provide the next ID at the end of |
| 1002 | // the call. |
| 1003 | auto sensor = sensors.begin(); |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 1004 | auto recordID = get_sdr::request::get_record_id(req); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1005 | |
| 1006 | // At the beginning of a scan, the host side will send us id=0. |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 1007 | if (recordID != 0) |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1008 | { |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 1009 | // recordID greater then 255,it means it is a FRU record. |
| 1010 | // Currently we are supporting two record types either FULL record |
| 1011 | // or FRU record. |
| 1012 | if (recordID >= FRU_RECORD_ID_START) |
| 1013 | { |
| 1014 | return ipmi_fru_get_sdr(request, response, data_len); |
| 1015 | } |
| 1016 | else |
| 1017 | { |
| 1018 | sensor = sensors.find(recordID); |
| 1019 | if (sensor == sensors.end()) |
| 1020 | { |
| 1021 | return IPMI_CC_SENSOR_INVALID; |
| 1022 | } |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | uint8_t sensor_id = sensor->first; |
| 1027 | |
| 1028 | /* Header */ |
| 1029 | get_sdr::header::set_record_id(sensor_id, &(record.header)); |
| 1030 | record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1 |
| 1031 | record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD; |
| 1032 | record.header.record_length = sizeof(get_sdr::SensorDataFullRecord); |
| 1033 | |
| 1034 | /* Key */ |
Tom Joseph | 9642391 | 2018-01-25 00:14:34 +0530 | [diff] [blame] | 1035 | get_sdr::key::set_owner_id_bmc(&(record.key)); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1036 | record.key.sensor_number = sensor_id; |
| 1037 | |
| 1038 | /* Body */ |
Tom Joseph | 9642391 | 2018-01-25 00:14:34 +0530 | [diff] [blame] | 1039 | record.body.entity_id = sensor->second.entityType; |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1040 | record.body.sensor_type = sensor->second.sensorType; |
| 1041 | record.body.event_reading_type = sensor->second.sensorReadingType; |
Tom Joseph | 9642391 | 2018-01-25 00:14:34 +0530 | [diff] [blame] | 1042 | record.body.entity_instance = sensor->second.instance; |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1043 | |
| 1044 | // Set the type-specific details given the DBus interface |
| 1045 | ret = populate_record_from_dbus(&(record.body), &(sensor->second), |
| 1046 | data_len); |
| 1047 | |
| 1048 | if (++sensor == sensors.end()) |
| 1049 | { |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 1050 | // we have reached till end of sensor, so assign the next record id |
| 1051 | // to 256(Max Sensor ID = 255) + FRU ID(may start with 0). |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1052 | auto next_record_id = |
| 1053 | (frus.size()) ? frus.begin()->first + FRU_RECORD_ID_START |
| 1054 | : END_OF_RECORD; |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 1055 | |
| 1056 | get_sdr::response::set_next_record_id(next_record_id, resp); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1057 | } |
| 1058 | else |
| 1059 | { |
| 1060 | get_sdr::response::set_next_record_id(sensor->first, resp); |
| 1061 | } |
| 1062 | |
| 1063 | *data_len = sizeof(get_sdr::GetSdrResp) - req->offset; |
| 1064 | memcpy(resp->record_data, (char*)&record + req->offset, |
| 1065 | sizeof(get_sdr::SensorDataFullRecord) - req->offset); |
| 1066 | } |
| 1067 | |
| 1068 | return ret; |
| 1069 | } |
| 1070 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 1071 | void register_netfn_sen_functions() |
| 1072 | { |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1073 | // <Wildcard Command> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1074 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD, nullptr, |
| 1075 | ipmi_sen_wildcard, PRIVILEGE_USER); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 1076 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1077 | // <Get Sensor Type> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1078 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE, nullptr, |
| 1079 | ipmi_sen_get_sensor_type, PRIVILEGE_USER); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 1080 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1081 | // <Set Sensor Reading and Event Status> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1082 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR, nullptr, |
| 1083 | ipmi_sen_set_sensor, PRIVILEGE_OPERATOR); |
Chris Austen | 8a45e7c | 2015-10-15 00:31:46 -0500 | [diff] [blame] | 1084 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1085 | // <Get Sensor Reading> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1086 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING, nullptr, |
| 1087 | ipmi_sen_get_sensor_reading, PRIVILEGE_USER); |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 1088 | |
Tom Joseph | 5ca5095 | 2018-02-22 00:33:38 +0530 | [diff] [blame] | 1089 | // <Reserve Device SDR Repository> |
| 1090 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_DEVICE_SDR_REPO, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1091 | nullptr, ipmi_sen_reserve_sdr, PRIVILEGE_USER); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 1092 | |
Tom Joseph | 5ca5095 | 2018-02-22 00:33:38 +0530 | [diff] [blame] | 1093 | // <Get Device SDR Info> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1094 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR_INFO, nullptr, |
| 1095 | ipmi_sen_get_sdr_info, PRIVILEGE_USER); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1096 | |
Tom Joseph | 5ca5095 | 2018-02-22 00:33:38 +0530 | [diff] [blame] | 1097 | // <Get Device SDR> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1098 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR, nullptr, |
| 1099 | ipmi_sen_get_sdr, PRIVILEGE_USER); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1100 | |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 1101 | // <Get Sensor Thresholds> |
| 1102 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_THRESHOLDS, |
| 1103 | nullptr, ipmi_sen_get_sensor_thresholds, |
| 1104 | PRIVILEGE_USER); |
| 1105 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 1106 | return; |
| 1107 | } |