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