-
Notifications
You must be signed in to change notification settings - Fork 4
/
healthmon.c
481 lines (402 loc) · 10.6 KB
/
healthmon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* healthmon
*
* Copyright 2018-present LinkedIn. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <syslog.h>
#include <sys/file.h>
#include <fcntl.h>
#include <openbmc/obmc-i2c.h>
#include <openbmc/gpio.h>
#include <facebook/powershelf.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <time.h>
#define HEALTHMON_INTERVAL 10
#define MAX_RETRIES 10
#define BMC_POLL_WAIT_TIME 2
/*
* Start to send GPIO low-high pulse after 60s
*/
#define BMC_SEND_PULSE_WAIT 60
/*
* 3 minute boot timeout: 9 GPIO LOW<->HIGH pulse (20s)
*/
#define BMC_BOOT_TIMEOUT 9
/*
* BMC HEARTBEAT GPIOs:
* HEARTBEAT_OUT_GPIO(C1)
* HEARTBEAT_IN_GPIO(Y0)
*/
#define BMC_HEARTBEAT_OUT_GPIO_NUM 17
#define BMC_HEARTBEAT_IN_GPIO_NUM 192
/*
* BMC Identity GPIO(J2):
*/
#define BMC_ID_GPIO_NUM 74
/*
* Cached Data
*/
efuse_info_t efuse_info_cache[MAX_EFUSE_NUM];
psu_info_t psu_info_cache[MAX_PSU_NUM];
psu_info_t psu_info_mem[MAX_PSU_NUM];
fan_info_t fan_info_cache;
int *mmap_addr;
int fd_mmap;
int bmc_id;
/*
* BMC_ID_GPIO_NUM - GPIO for BMC0 or BMC1
*/
int which_bmc ()
{
return gpio_get(BMC_ID_GPIO_NUM);
}
void set_gpio_out_direction(uint8_t gpio_num);
/*
* save efuse info to cached table
*/
int
save_efuse_info (efuse_info_t *efuse_info)
{
if (!efuse_info) {
syslog(LOG_WARNING, "Invalid input: NULL \n");
return -1;
}
if ((efuse_info->efuse_num < 1) || efuse_info->efuse_num > MAX_EFUSE_NUM) {
syslog(LOG_WARNING, "efuse%d is out of range(1-50)", efuse_info->efuse_num);
return -1;
}
memcpy(&efuse_info_cache[efuse_info->efuse_num - 1], efuse_info, sizeof(efuse_info_t));
return 0;
}
/*
* save to info table
*/
int
save_psu_info (psu_info_t *psu_info)
{
if (!psu_info) {
syslog(LOG_WARNING, "Invalid input: NULL \n");
return -1;
}
if ((psu_info->psu_num < 1) || psu_info->psu_num > MAX_PSU_NUM) {
syslog(LOG_WARNING, "psu %d is out of range", psu_info->psu_num);
return -1;
}
get_mmap_info((int*)&psu_info_mem, FILE_PSU, sizeof(psu_info_mem));
for(int i=0; i < PSU_STATUS_MAX; i++) {
psu_info->status_cntr[i] += psu_info_mem[psu_info->psu_num - 1].status_cntr[i];
}
memcpy(&psu_info_cache[psu_info->psu_num - 1], psu_info, sizeof(psu_info_t));
return 0;
}
/*
* save to info cache table
*/
int
save_fan_info (fan_info_t *fan_info)
{
if (!fan_info) {
syslog(LOG_WARNING, "Invalid input: NULL \n");
return -1;
}
memcpy(&fan_info_cache, fan_info, sizeof(fan_info_t));
return 0;
}
/*
* Write to mmap file
*/
int
write_mmap_file(uint8_t fd)
{
uint8_t retries = 5;
int done = 0, offset, len;
for(int attempt; attempt < retries; ++attempt) {
done = flock(fd, LOCK_EX | LOCK_NB);
if (done != 0) {
syslog(LOG_WARNING, "Failed to lock mmap file, errno: %d", errno);
/*
* lock held by another proc
*/
sleep(1);
continue;
}
else
break;
}
if (done != 0) {
syslog(LOG_WARNING, "Failed to lock mmap file\n");
return -1;
}
/*
* write fan info to the mmaped file
*/
offset = get_file_offset(FILE_FAN);
len = sizeof(fan_info_cache) + 1;
memcpy(mmap_addr+offset, &fan_info_cache, len);
/*
* write psu info to the mmaped file
*/
offset = get_file_offset(FILE_PSU);
len = sizeof(psu_info_cache);
memcpy(mmap_addr+offset, psu_info_cache, len);
/*
* write efuse info to the mmaped file
*/
offset = get_file_offset(FILE_EFUSE);
len = sizeof(efuse_info_cache);
memcpy(mmap_addr + offset, efuse_info_cache, len);
flock(fd, LOCK_UN);
return 0;
}
void init_psus (int *mmap_addr)
{
psu_info_t *psu_info;
if (!mmap_addr)
return;
psu_info = (psu_info_t *) malloc(sizeof(psu_info_t));
if (!psu_info)
return;
for (int i = 1; i < MAX_PSU_NUM + 1; i++) {
memset(psu_info, 0, sizeof(psu_info_t));
psu_info->psu_num = i;
save_psu_info(psu_info);
}
free(psu_info);
}
int init_poll (int filesize)
{
int mode = 0x0777;
bmc_id = which_bmc();
if (fd_mmap == -1) {
syslog(LOG_WARNING, "failed to open mmap file");
return -1;
}
/* go to the location corresponding to the last byte */
if (lseek (fd_mmap, filesize - 1, SEEK_SET) == -1)
{
syslog(LOG_WARNING, "mmap file lseek error");
return -1;
}
/* write a dummy byte at the last location */
if (write (fd_mmap, "", 1) != 1) {
syslog(LOG_WARNING, "mmap file write error");
return -1;
}
/* Now the file is ready to be mmapped.
*/
mmap_addr = mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd_mmap, 0);
if (mmap_addr == MAP_FAILED) {
syslog(LOG_WARNING, "failed to mmap()");
return -1;
}
init_psus(mmap_addr);
return 0;
}
/*
* Poll the Hardware and save to structures
*/
void poll_psu_info (int *mmap_addr, uint8_t fd)
{
int len;
int offset;
psu_info_t *psu_info;
if (!mmap_addr)
return;
psu_info = (psu_info_t *) malloc(sizeof(psu_info_t));
if (!psu_info)
return;
/*
* check PSU presense, AC input and all faults and warning
*/
for (int i = 1; i < MAX_PSU_NUM + 1; i++) {
memset(psu_info, 0, sizeof(psu_info_t));
psu_info->psu_num = i;
if (psu_poll_info(psu_info) == -1) {
syslog(LOG_WARNING, "failed to poll psu%d info", i);
continue;
}
save_psu_info(psu_info);
/*
* check PSU presense, AC input
*/
if (psu_info->operation_state == PSU_NOT_PRESENT) {
syslog(LOG_WARNING, "psu%d not present", i);
continue;
}
else if (psu_info->operation_state == PSU_OFF) {
syslog(LOG_WARNING, "psu%d is off", i);
continue;
}
else if (psu_info->operation_state == PSU_NO_AC_INPUT) {
syslog(LOG_WARNING, "psu%d has no ac input", i);
continue;
}
}
free(psu_info);
}
void poll_efuse_info(int *mmap_addr, uint8_t fd)
{
int i2c_bus, selector_addr, len;
int offset;
efuse_info_t *efuse_info;
if (!mmap_addr) {
syslog(LOG_WARNING, "healthmon: efuse mmap: NULL");
return;
}
efuse_info = (efuse_info_t *) malloc(sizeof(efuse_info_t));
if (!efuse_info) {
syslog(LOG_WARNING, "healthmon: efuse memory alloc failed");
return;
}
/*
* check faults and warning
*/
for (int i = 1; i < MAX_EFUSE_NUM + 1; i++) {
memset(efuse_info, 0, sizeof(efuse_info_t));
efuse_info->efuse_num = i;
if (efuse_poll_info(efuse_info) == -1) {
continue;
}
save_efuse_info(efuse_info);
}
free(efuse_info);
return;
}
/*
* Poll FAN Hardware info
* save hardware data into cached data table
*/
void poll_fan_info (int *mmap_addr, uint8_t fd)
{
int len, offset;
fan_info_t *fan_info;
fan_info = (fan_info_t *) malloc(sizeof(fan_info_t));
if (!fan_info)
return;
memset(fan_info, 0, sizeof(fan_info_t));
if (fan_poll_info(fan_info) == -1) {
syslog(LOG_WARNING, "failed to poll fan info");
free(fan_info);
return;
}
save_fan_info(fan_info);
free(fan_info);
}
/*
* Set GPIO direction to OUT
*/
void set_gpio_out_direction(uint8_t gpio_num)
{
gpio_st g;
gpio_direction_en dir;
/*
* GPIO current direction
*/
g.gs_gpio = gpio_num;
gpio_current_direction(&g, &dir);
if (dir == GPIO_DIRECTION_IN) {
gpio_change_direction(&g, GPIO_DIRECTION_OUT);
}
return;
}
int do_poll(int timedout_flag, int fd)
{
int randomTime = 0, ret = 0;
time_t endwait;
time_t start = time(NULL);
time_t seconds = HEALTHMON_INTERVAL;
/*
* set GPIO OUT to LOW
*/
sleep(1);
/*
* Own the i2c buses, start transaction of HW access
*/
sleep(BMC_POLL_WAIT_TIME);
/*
* Get HW data
*/
poll_efuse_info(mmap_addr, fd);
poll_psu_info(mmap_addr, fd);
poll_fan_info(mmap_addr, fd);
if (write_mmap_file(fd) == -1) {
syslog(LOG_WARNING, "failed to write to mmap");
}
/*
* Done access HW, set GPIO OUT to HIGH
*/
sleep(BMC_POLL_WAIT_TIME);
return 0;
}
int main(int argc, char **argv) {
int filesize;
struct stat statbuf;
int retry = 3;
int boot_timeout_count = BMC_BOOT_TIMEOUT;
int timedout_flag = 0;
int in_gpio_value_prev = -1;
int in_gpio_value_current;
int count = 0, timeout_count = 0;
int start_send_pulse = 0;
int gpio_pulse_count = 0;
int gpio_low_count = 0;
int gpio_value = GPIO_VALUE_HIGH;
int mode = 0x0777;
filesize = sizeof(efuse_info_cache) + sizeof(psu_info_cache) + sizeof(fan_info_cache);
/*
* Open a file for writing efuse info.
*/
fd_mmap = open(MMAP_FILEPATH, O_RDWR | O_CREAT | O_TRUNC, mode);
/*
* initialization
*/
init_poll(filesize);
if (bmc_id == 0)
{
in_gpio_value_prev = 0;
}
else
{
// do not run on BMC1
return 0;
}
while (1) {
sleep(1);
/*
* timeout check
*/
do_poll(timedout_flag, fd_mmap);
}
/* Don't forget to free the mmapped memory
*/
if (munmap(mmap_addr, filesize) == -1) {
syslog(LOG_WARNING, "Error un-mmapping the efuse file");
}
/*
* Un-mmaping doesn't close the file, so we still need to do that.
*/
close(fd_mmap);
}