1 #include "sensorhandler.hpp"
2 
3 #include <malloc.h>
4 
5 #include <phosphor-logging/lg2.hpp>
6 
7 extern uint8_t find_type_for_sensor_number(uint8_t);
8 
9 struct sensorRES_t
10 {
11     uint8_t sensor_number;
12     uint8_t operation;
13     uint8_t sensor_reading;
14     uint8_t assert_state7_0;
15     uint8_t assert_state14_8;
16     uint8_t deassert_state7_0;
17     uint8_t deassert_state14_8;
18     uint8_t event_data1;
19     uint8_t event_data2;
20     uint8_t event_data3;
21 } __attribute__((packed));
22 
23 #define ISBITSET(x, y) (((x) >> (y)) & 0x01)
24 #define ASSERTINDEX 0
25 #define DEASSERTINDEX 1
26 
27 // Sensor Type,  Offset, function handler, Dbus Method, Assert value, Deassert
28 // value
29 struct lookup_t
30 {
31     uint8_t sensor_type;
32     uint8_t offset;
33     int (*func)(const sensorRES_t*, const lookup_t*, const char*);
34     char member[16];
35     char assertion[64];
36     char deassertion[64];
37 };
38 
39 extern int updateDbusInterface(uint8_t, const char*, const char*);
40 
set_sensor_dbus_state_simple(const sensorRES_t * pRec,const lookup_t * pTable,const char * value)41 int set_sensor_dbus_state_simple(const sensorRES_t* pRec,
42                                  const lookup_t* pTable, const char* value)
43 {
44     return set_sensor_dbus_state_s(pRec->sensor_number, pTable->member, value);
45 }
46 
47 struct event_data_t
48 {
49     uint8_t data;
50     char text[64];
51 };
52 
53 event_data_t g_fwprogress02h[] = {
54     {0x00, "Unspecified"},
55     {0x01, "Memory Init"},
56     {0x02, "HD Init"},
57     {0x03, "Secondary Proc Init"},
58     {0x04, "User Authentication"},
59     {0x05, "User init system setup"},
60     {0x06, "USB configuration"},
61     {0x07, "PCI configuration"},
62     {0x08, "Option ROM Init"},
63     {0x09, "Video Init"},
64     {0x0A, "Cache Init"},
65     {0x0B, "SM Bus init"},
66     {0x0C, "Keyboard Init"},
67     {0x0D, "Embedded ctrl init"},
68     {0x0E, "Docking station attachment"},
69     {0x0F, "Enable docking station"},
70     {0x10, "Docking station ejection"},
71     {0x11, "Disabling docking station"},
72     {0x12, "Calling OS Wakeup"},
73     {0x13, "Starting OS"},
74     {0x14, "Baseboard Init"},
75     {0x15, ""},
76     {0x16, "Floppy Init"},
77     {0x17, "Keyboard Test"},
78     {0x18, "Pointing Device Test"},
79     {0x19, "Primary Proc Init"},
80     {0xFF, "Unknown"}};
81 
82 event_data_t g_fwprogress00h[] = {
83     {0x00, "Unspecified."},
84     {0x01, "No system memory detected"},
85     {0x02, "No usable system memory"},
86     {0x03, "Unrecoverable hard-disk/ATAPI/IDE"},
87     {0x04, "Unrecoverable system-board"},
88     {0x05, "Unrecoverable diskette"},
89     {0x06, "Unrecoverable hard-disk controller"},
90     {0x07, "Unrecoverable PS/2 or USB keyboard"},
91     {0x08, "Removable boot media not found"},
92     {0x09, "Unrecoverable video controller"},
93     {0x0A, "No video device detected"},
94     {0x0B, "Firmware ROM corruption detected"},
95     {0x0C, "CPU voltage mismatch"},
96     {0x0D, "CPU speed matching"},
97     {0xFF, "unknown"},
98 };
99 
event_data_lookup(event_data_t * p,uint8_t b)100 char* event_data_lookup(event_data_t* p, uint8_t b)
101 {
102     while (p->data != 0xFF)
103     {
104         if (p->data == b)
105         {
106             break;
107         }
108         p++;
109     }
110 
111     return p->text;
112 }
113 
114 //  The fw progress sensor contains some additional information that needs to be
115 //  processed prior to calling the dbus code.
set_sensor_dbus_state_fwprogress(const sensorRES_t * pRec,const lookup_t * pTable,const char *)116 int set_sensor_dbus_state_fwprogress(const sensorRES_t* pRec,
117                                      const lookup_t* pTable, const char*)
118 {
119     char valuestring[128];
120     char* p = valuestring;
121 
122     switch (pTable->offset)
123     {
124         case 0x00:
125             std::snprintf(
126                 p, sizeof(valuestring), "POST Error, %s",
127                 event_data_lookup(g_fwprogress00h, pRec->event_data2));
128             break;
129         case 0x01: /* Using g_fwprogress02h for 0x01 because that's what the
130                       ipmi spec says to do */
131             std::snprintf(
132                 p, sizeof(valuestring), "FW Hang, %s",
133                 event_data_lookup(g_fwprogress02h, pRec->event_data2));
134             break;
135         case 0x02:
136             std::snprintf(
137                 p, sizeof(valuestring), "FW Progress, %s",
138                 event_data_lookup(g_fwprogress02h, pRec->event_data2));
139             break;
140         default:
141             std::snprintf(
142                 p, sizeof(valuestring),
143                 "Internal warning, fw_progres offset unknown (0x%02x)",
144                 pTable->offset);
145             break;
146     }
147 
148     return set_sensor_dbus_state_s(pRec->sensor_number, pTable->member, p);
149 }
150 
151 // Handling this special OEM sensor by coping what is in byte 4.  I also think
152 // that is odd considering byte 3 is for sensor reading.  This seems like a
153 // misuse of the IPMI spec
set_sensor_dbus_state_osbootcount(const sensorRES_t * pRec,const lookup_t *,const char *)154 int set_sensor_dbus_state_osbootcount(const sensorRES_t* pRec, const lookup_t*,
155                                       const char*)
156 {
157     return set_sensor_dbus_state_y(pRec->sensor_number, "setValue",
158                                    pRec->assert_state7_0);
159 }
160 
set_sensor_dbus_state_system_event(const sensorRES_t * pRec,const lookup_t * pTable,const char *)161 int set_sensor_dbus_state_system_event(const sensorRES_t* pRec,
162                                        const lookup_t* pTable, const char*)
163 {
164     char valuestring[128];
165     char* p = valuestring;
166 
167     switch (pTable->offset)
168     {
169         case 0x00:
170             std::snprintf(p, sizeof(valuestring), "System Reconfigured");
171             break;
172         case 0x01:
173             std::snprintf(p, sizeof(valuestring), "OEM Boot Event");
174             break;
175         case 0x02:
176             std::snprintf(p, sizeof(valuestring),
177                           "Undetermined System Hardware Failure");
178             break;
179         case 0x03:
180             std::snprintf(
181                 p, sizeof(valuestring),
182                 "System Failure see error log for more details (0x%02x)",
183                 pRec->event_data2);
184             break;
185         case 0x04:
186             std::snprintf(
187                 p, sizeof(valuestring),
188                 "System Failure see PEF error log for more details (0x%02x)",
189                 pRec->event_data2);
190             break;
191         default:
192             std::snprintf(
193                 p, sizeof(valuestring),
194                 "Internal warning, system_event offset unknown (0x%02x)",
195                 pTable->offset);
196             break;
197     }
198 
199     return set_sensor_dbus_state_s(pRec->sensor_number, pTable->member, p);
200 }
201 
202 //  This table lists only senors we care about telling dbus about.
203 //  Offset definition cab be found in section 42.2 of the IPMI 2.0
204 //  spec.  Add more if/when there are more items of interest.
205 lookup_t g_ipmidbuslookup[] = {
206 
207     {0xe9, 0x00, set_sensor_dbus_state_simple, "setValue", "Disabled",
208      ""}, // OCC Inactive 0
209     {0xe9, 0x01, set_sensor_dbus_state_simple, "setValue", "Enabled",
210      ""}, // OCC Active 1
211     // Turbo Allowed
212     {0xda, 0x00, set_sensor_dbus_state_simple, "setValue", "True", "False"},
213     // Power Supply Derating
214     {0xb4, 0x00, set_sensor_dbus_state_simple, "setValue", "", ""},
215     // Power Cap
216     {0xC2, 0x00, set_sensor_dbus_state_simple, "setValue", "", ""},
217     {0x07, 0x07, set_sensor_dbus_state_simple, "setPresent", "True", "False"},
218     {0x07, 0x08, set_sensor_dbus_state_simple, "setFault", "True", "False"},
219     {0x0C, 0x06, set_sensor_dbus_state_simple, "setPresent", "True", "False"},
220     {0x0C, 0x04, set_sensor_dbus_state_simple, "setFault", "True", "False"},
221     {0x0F, 0x02, set_sensor_dbus_state_fwprogress, "setValue", "True", "False"},
222     {0x0F, 0x01, set_sensor_dbus_state_fwprogress, "setValue", "True", "False"},
223     {0x0F, 0x00, set_sensor_dbus_state_fwprogress, "setValue", "True", "False"},
224     {0xC7, 0x01, set_sensor_dbus_state_simple, "setFault", "True", "False"},
225     {0xc3, 0x00, set_sensor_dbus_state_osbootcount, "setValue", "", ""},
226     {0x1F, 0x00, set_sensor_dbus_state_simple, "setValue",
227      "Boot completed (00)", ""},
228     {0x1F, 0x01, set_sensor_dbus_state_simple, "setValue",
229      "Boot completed (01)", ""},
230     {0x1F, 0x02, set_sensor_dbus_state_simple, "setValue", "PXE boot completed",
231      ""},
232     {0x1F, 0x03, set_sensor_dbus_state_simple, "setValue",
233      "Diagnostic boot completed", ""},
234     {0x1F, 0x04, set_sensor_dbus_state_simple, "setValue",
235      "CD-ROM boot completed", ""},
236     {0x1F, 0x05, set_sensor_dbus_state_simple, "setValue", "ROM boot completed",
237      ""},
238     {0x1F, 0x06, set_sensor_dbus_state_simple, "setValue",
239      "Boot completed (06)", ""},
240     {0x12, 0x00, set_sensor_dbus_state_system_event, "setValue", "", ""},
241     {0x12, 0x01, set_sensor_dbus_state_system_event, "setValue", "", ""},
242     {0x12, 0x02, set_sensor_dbus_state_system_event, "setValue", "", ""},
243     {0x12, 0x03, set_sensor_dbus_state_system_event, "setValue", "", ""},
244     {0x12, 0x04, set_sensor_dbus_state_system_event, "setValue", "", ""},
245     {0xCA, 0x00, set_sensor_dbus_state_simple, "setValue", "Disabled", ""},
246     {0xCA, 0x01, set_sensor_dbus_state_simple, "setValue", "Enabled", ""},
247     {0xFF, 0xFF, NULL, "", "", ""}};
248 
reportSensorEventAssert(const sensorRES_t * pRec,int index)249 void reportSensorEventAssert(const sensorRES_t* pRec, int index)
250 {
251     lookup_t* pTable = &g_ipmidbuslookup[index];
252     (*pTable->func)(pRec, pTable, pTable->assertion);
253 }
reportSensorEventDeassert(const sensorRES_t * pRec,int index)254 void reportSensorEventDeassert(const sensorRES_t* pRec, int index)
255 {
256     lookup_t* pTable = &g_ipmidbuslookup[index];
257     (*pTable->func)(pRec, pTable, pTable->deassertion);
258 }
259 
findindex(const uint8_t sensor_type,int offset,int * index)260 int findindex(const uint8_t sensor_type, int offset, int* index)
261 {
262     int i = 0, rc = 0;
263     lookup_t* pTable = g_ipmidbuslookup;
264 
265     do
266     {
267         if (((pTable + i)->sensor_type == sensor_type) &&
268             ((pTable + i)->offset == offset))
269         {
270             rc = 1;
271             *index = i;
272             break;
273         }
274         i++;
275     } while ((pTable + i)->sensor_type != 0xFF);
276 
277     return rc;
278 }
279 
shouldReport(uint8_t sensorType,int offset,int * index)280 bool shouldReport(uint8_t sensorType, int offset, int* index)
281 {
282     bool rc = false;
283 
284     if (findindex(sensorType, offset, index))
285     {
286         rc = true;
287     }
288     if (rc == false)
289     {
290 #ifdef __IPMI_DEBUG__
291         lg2::debug("LOOKATME: Sensor should not be reported, "
292                    "sensor type: {SENSORTYPE}, offset: {OFFSET}",
293                    SENSORTYPE, lg2::hex, sensorType, "OFFSET", lg2::hex,
294                    offset);
295 #endif
296     }
297 
298     return rc;
299 }
300 
updateSensorRecordFromSSRAESC(const void * record)301 int updateSensorRecordFromSSRAESC(const void* record)
302 {
303     auto pRec = static_cast<const sensorRES_t*>(record);
304     uint8_t stype;
305     int index;
306 
307     stype = find_type_for_sensor_number(pRec->sensor_number);
308 
309     // 0xC3 types use the assertion7_0 for the value to be set
310     // so skip the reseach and call the correct event reporting
311     // function
312     if (stype == 0xC3)
313     {
314         shouldReport(stype, 0x00, &index);
315         reportSensorEventAssert(pRec, index);
316     }
317     else
318     {
319         // Scroll through each bit position .  Determine
320         // if any bit is either asserted or Deasserted.
321         for (int i = 0; i < 8; i++)
322         {
323             if ((ISBITSET(pRec->assert_state7_0, i)) &&
324                 (shouldReport(stype, i, &index)))
325             {
326                 reportSensorEventAssert(pRec, index);
327             }
328             if ((ISBITSET(pRec->assert_state14_8, i)) &&
329                 (shouldReport(stype, i + 8, &index)))
330             {
331                 reportSensorEventAssert(pRec, index);
332             }
333             if ((ISBITSET(pRec->deassert_state7_0, i)) &&
334                 (shouldReport(stype, i, &index)))
335             {
336                 reportSensorEventDeassert(pRec, index);
337             }
338             if ((ISBITSET(pRec->deassert_state14_8, i)) &&
339                 (shouldReport(stype, i + 8, &index)))
340             {
341                 reportSensorEventDeassert(pRec, index);
342             }
343         }
344     }
345 
346     return 0;
347 }
348