Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 1 | #include "sensorhandler.h" |
| 2 | #include "ipmid-api.h" |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <stdint.h> |
| 6 | |
Chris Austen | 8a45e7c | 2015-10-15 00:31:46 -0500 | [diff] [blame] | 7 | extern int updateSensorRecordFromSSRAESC(const void *); |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 8 | extern int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) ; |
| 9 | extern int find_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) ; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 10 | |
| 11 | void register_netfn_sen_functions() __attribute__((constructor)); |
| 12 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 13 | struct sensorTypemap_t { |
| 14 | uint8_t number; |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 15 | uint8_t typecode; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 16 | char dbusname[32]; |
| 17 | } ; |
| 18 | |
| 19 | |
| 20 | sensorTypemap_t g_SensorTypeMap[] = { |
| 21 | |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 22 | {0x01, 0x6F, "Temp"}, |
| 23 | {0x0C, 0x6F, "DIMM"}, |
| 24 | {0x0C, 0x6F, "MEMORY_BUFFER"}, |
| 25 | {0x07, 0x6F, "PROC"}, |
| 26 | {0x07, 0x6F, "CORE"}, |
| 27 | {0x07, 0x6F, "CPU"}, |
| 28 | {0x0F, 0x6F, "BootProgress"}, |
| 29 | {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor type code os 0x09 |
| 30 | {0xC3, 0x6F, "BootCount"}, |
| 31 | {0x1F, 0x6F, "OperatingSystemStatus"}, |
| 32 | {0xFF, 0x00, ""}, |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 36 | |
| 37 | struct sensor_data_t { |
| 38 | uint8_t sennum; |
| 39 | } __attribute__ ((packed)) ; |
| 40 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 41 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 42 | uint8_t dbus_to_sensor_type(char *p) { |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 43 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 44 | sensorTypemap_t *s = g_SensorTypeMap; |
| 45 | char r=0; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 46 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 47 | while (s->number != 0xFF) { |
| 48 | if (!strcmp(s->dbusname,p)) { |
| 49 | r = s->number; |
| 50 | break; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 51 | } |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 52 | s++; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 55 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 56 | if (s->number == 0xFF) |
| 57 | printf("Failed to find Sensor Type %s\n", p); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 58 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 59 | return r; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 62 | |
| 63 | uint8_t dbus_to_sensor_type_from_dbus(dbus_interface_t *a) { |
| 64 | char fru_type_name[64]; |
| 65 | int r= 0; |
| 66 | |
| 67 | r = find_interface_property_fru_type(a, "fru_type", fru_type_name); |
| 68 | if (r<0) { |
| 69 | fprintf(stderr, "Failed to get a fru type: %s", strerror(-r)); |
| 70 | return -1; |
| 71 | } else { |
| 72 | return dbus_to_sensor_type(fru_type_name); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | uint8_t find_sensor(uint8_t sensor_number) { |
| 78 | |
| 79 | dbus_interface_t a; |
| 80 | char *p; |
| 81 | char r; |
| 82 | |
| 83 | r = find_openbmc_path("SENSOR", sensor_number, &a); |
| 84 | |
| 85 | if (r < 0) { return 0; } |
| 86 | |
| 87 | // This is where sensors that do not exist in dbus but do |
| 88 | // exist in the host code stop. This should indicate it |
| 89 | // is not a supported sensor |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 90 | if (a.interface[0] == 0) { return 0;} |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 91 | |
| 92 | if (strstr(a.interface, "InventoryItem")) { |
| 93 | // InventoryItems are real frus. So need to get the |
| 94 | // fru_type property |
| 95 | r = dbus_to_sensor_type_from_dbus(&a); |
| 96 | } else { |
| 97 | // Non InventoryItems |
| 98 | p = strrchr (a.path, '/'); |
| 99 | r = dbus_to_sensor_type(p+1); |
| 100 | } |
| 101 | |
| 102 | return r; |
| 103 | } |
| 104 | |
| 105 | ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 106 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 107 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 108 | { |
| 109 | sensor_data_t *reqptr = (sensor_data_t*)request; |
| 110 | ipmi_ret_t rc = IPMI_CC_OK; |
| 111 | |
| 112 | printf("IPMI GET_SENSOR_TYPE [0x%02X]\n",reqptr->sennum); |
| 113 | |
| 114 | // TODO Not sure what the System-event-sensor is suppose to return |
| 115 | // need to ask Hostboot team |
| 116 | unsigned char buf[] = {0x00,0x6F}; |
| 117 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 118 | buf[0] = find_sensor(reqptr->sennum); |
| 119 | |
| 120 | // HACK UNTIL Dbus gets updated or we find a better way |
| 121 | if (buf[0] == 0) { |
| 122 | |
| 123 | switch(reqptr->sennum) { |
| 124 | case 0x35 : buf[0] = 0x12; buf[1] = 0x6F; break; |
| 125 | case 0x37 : buf[0] = 0xC7; buf[1] = 0x03; break; |
| 126 | case 0x38 : buf[0] = 0xC7; buf[1] = 0x03; break; |
| 127 | case 0x39 : buf[0] = 0xC7; buf[1] = 0x03; break; |
| 128 | case 0x3A : buf[0] = 0xC7; buf[1] = 0x03; break; |
| 129 | default: rc = IPMI_CC_SENSOR_INVALID; |
| 130 | } |
| 131 | } |
| 132 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 133 | |
| 134 | *data_len = sizeof(buf); |
| 135 | memcpy(response, &buf, *data_len); |
| 136 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 137 | return rc; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 142 | ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 143 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 144 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 145 | { |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 146 | sensor_data_t *reqptr = (sensor_data_t*)request; |
| 147 | ipmi_ret_t rc = IPMI_CC_OK; |
| 148 | unsigned short rlen; |
| 149 | |
Chris Austen | 8a45e7c | 2015-10-15 00:31:46 -0500 | [diff] [blame] | 150 | rlen = (unsigned short) *data_len; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 151 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 152 | printf("IPMI SET_SENSOR [0x%02x]\n",reqptr->sennum); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 153 | |
Chris Austen | 8a45e7c | 2015-10-15 00:31:46 -0500 | [diff] [blame] | 154 | updateSensorRecordFromSSRAESC(reqptr); |
| 155 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 156 | *data_len=0; |
| 157 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 158 | return rc; |
| 159 | } |
| 160 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 161 | ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 162 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 163 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 164 | { |
| 165 | ipmi_ret_t rc = IPMI_CC_OK; |
| 166 | |
| 167 | printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd); |
| 168 | *data_len = 0; |
| 169 | |
| 170 | return rc; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | void register_netfn_sen_functions() |
| 175 | { |
| 176 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_SENSOR, IPMI_CMD_WILDCARD); |
| 177 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD, NULL, ipmi_sen_wildcard); |
| 178 | |
| 179 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE); |
| 180 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE, NULL, ipmi_sen_get_sensor_type); |
| 181 | |
| 182 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_SENSOR, IPMI_CMD_SET_SENSOR); |
| 183 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR, NULL, ipmi_sen_set_sensor); |
Chris Austen | 8a45e7c | 2015-10-15 00:31:46 -0500 | [diff] [blame] | 184 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 185 | return; |
| 186 | } |