-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmmcinfo.c
291 lines (263 loc) · 8.54 KB
/
mmcinfo.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
/***************************************************************************
* ____ _____________ __ __ __ _ _____ ___ _ *
* / __ \/ ____/ ___/\ \/ / | \/ (_)__ _ _ __|_ _/ __| /_\ (R) *
* / / / / __/ \__ \ \ / | |\/| | / _| '_/ _ \| || (__ / _ \ *
* / /_/ / /___ ___/ / / / |_| |_|_\__|_| \___/|_| \___/_/ \_\ *
* /_____/_____//____/ /_/ T E C H N O L O G Y L A B *
* *
* Copyright 2022 Deutsches Elektronen-Synchrotron DESY. *
* All rights reserved. *
* *
***************************************************************************/
#include <fcntl.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mmcmb/mmcmb.h"
static char* uptime_format(uint32_t t, char* buf, size_t len)
{
#define SECS_PER_MIN 60
#define MIN_PER_HOUR 60
#define HOURS_PER_DAY 24
static const struct {
const char* name;
uint32_t divisor, modulo;
} units[] = {
{"day", HOURS_PER_DAY * MIN_PER_HOUR * SECS_PER_MIN, 0},
{"hour", MIN_PER_HOUR * SECS_PER_MIN, HOURS_PER_DAY},
{"minute", SECS_PER_MIN, MIN_PER_HOUR},
{"second", 1, SECS_PER_MIN},
};
*buf = '\0';
size_t l = 0;
for (size_t i = 0; i < (sizeof(units) / sizeof(units[0])); i++) {
unsigned int num = (t / units[i].divisor);
if (units[i].modulo) {
num %= units[i].modulo;
}
if (l || num) {
l += snprintf(buf + l,
len - l,
"%s%d %s%s",
l ? ", " : "",
num,
units[i].name,
num == 1 ? "" : "s");
}
}
return buf;
}
static void dump_str(const char* desc, int rjust, const char* str, int maxlen)
{
printf("%-*s: %.*s\n", rjust, desc, maxlen, *str ? str : "N/A");
}
static void dump_mmc_information(void)
{
mb_mmc_information_t info;
if (!mb_get_mmc_information(&info)) {
exit(1);
}
printf("MMC information\n");
printf("---------------\n");
printf("%-16s: %d.%d\n",
"App version",
info.application_version.major,
info.application_version.minor);
printf("%-16s: %d.%d\n", "Lib version", info.library_version.major, info.library_version.minor);
printf("%-16s: %d.%d\n",
"CPLD board ver.",
info.cpld_board_version.major,
info.cpld_board_version.minor);
printf("%-16s: %d.%d\n",
"CPLD lib ver.",
info.cpld_library_version.major,
info.cpld_library_version.minor);
printf("%-16s: Rev. %c\n", "STAMP revision", info.stamp_hw_revision);
printf("%-16s: %d\n", "AMC slot", info.amc_slot_nr);
printf("%-16s: 0x%02x\n", "IPMB addr", info.ipmb_addr);
dump_str("Board name", 16, info.board_name, sizeof(info.board_name));
printf("%-16s: 0x%04x\n", "IANA Vendor ID", info.vendor_id);
printf("%-16s: 0x%04x\n", "IANA Product ID", info.product_id);
char tmp[60];
printf("%-16s: %s\n", "Uptime", uptime_format(info.mmc_uptime, tmp, sizeof(tmp)));
}
static void dump_mmc_sensors(void)
{
mb_mmc_sensor_t sen[MAX_SENS_MMC];
if (!mb_get_mmc_sensors(sen, 0, MAX_SENS_MMC)) {
exit(1);
}
printf("MMC sensors\n");
printf("-----------\n");
for (size_t i = 0; i < MAX_SENS_MMC && sen[i].name[0]; i++) {
printf("%-13.*s: %g\n", (int)sizeof(sen[i].name), sen[i].name, sen[i].reading);
}
}
static void dump_fru_description(size_t fru_id)
{
mb_fru_description_t desc;
if (!mb_get_fru_description(&desc, fru_id)) {
exit(1);
}
printf("FRU %zu description\n", fru_id);
printf("-----------------\n");
char uid_str[6 * 2 + 1] = "N/A";
uint8_t uid_zero[sizeof(desc.uid)] = {0};
if (memcmp(desc.uid, uid_zero, sizeof(desc.uid))) {
snprintf(uid_str,
sizeof(uid_str),
"%02X%02X%02X%02X%02X%02X",
desc.uid[0],
desc.uid[1],
desc.uid[2],
desc.uid[3],
desc.uid[4],
desc.uid[5]);
}
printf("%-14s: %s\n", "UID", uid_str);
dump_str("Manufacturer", 14, desc.manufacturer, sizeof(desc.manufacturer));
dump_str("Product name", 14, desc.product, sizeof(desc.product));
dump_str("Part number", 14, desc.part_nr, sizeof(desc.part_nr));
dump_str("Serial number", 14, desc.serial_nr, sizeof(desc.serial_nr));
dump_str("Version", 14, desc.version, sizeof(desc.version));
}
static void dump_fru_status(size_t fru_id)
{
mb_fru_status_t stat;
if (!mb_get_fru_status(&stat, fru_id)) {
exit(1);
}
printf("FRU %zu status\n", fru_id);
printf("-----------------\n");
printf("%-14s: %cPresent %cCompatible %cPowered %cFailure\n",
"Flags",
stat.present ? '+' : '-',
stat.compatible ? '+' : '-',
stat.powered ? '+' : '-',
stat.failure ? '+' : '-');
// Dump FMC-specific flags if applicable
if (stat.present && stat.powered && (fru_id == 2 || fru_id == 3)) {
printf("%-14s: Type: %s, ClkDir: %s, PG_M2C: %s\n",
"FMC status",
!stat.ext.fmc.hspc_prsnt ? "FMC+" : "FMC",
stat.ext.fmc.clk_dir ? "C2M" : "M2C",
stat.ext.fmc.pg_m2c ? "asserted" : "deasserted");
}
for (size_t i = 0; i < stat.num_temp_sensors; i++) {
if (stat.temperature[i] != FRU_TEMP_INVALID) {
const float temp = (float)stat.temperature[i] / 100.f;
printf("Temperature %zu : %g C\n", i + 1, temp);
} else {
printf("Temperature %zu : N/A\n", i + 1);
}
}
}
static bool fru_present(size_t fru_id)
{
mb_fru_status_t stat;
if (!mb_get_fru_status(&stat, fru_id)) {
exit(1);
}
return stat.present;
}
typedef struct dump_enable {
bool mmc;
bool sensors;
bool fru[NUM_FRUS];
bool fpga;
} dump_enable_t;
static void lf(void)
{
static bool first_call = true;
if (!first_call) {
printf("\n");
}
first_call = false;
}
static void dump_mmcmb(dump_enable_t en)
{
if (en.mmc) {
lf();
dump_mmc_information();
}
if (en.sensors) {
lf();
dump_mmc_sensors();
}
for (size_t fru_id = 0; fru_id < NUM_FRUS; fru_id++) {
if (en.fru[fru_id]) {
if (fru_present(fru_id)) {
lf();
dump_fru_description(fru_id);
lf();
dump_fru_status(fru_id);
} else {
lf();
printf("FRU %zu not present\n", fru_id);
printf("-----------------\n");
}
}
}
if (en.fpga) {
mb_fpga_ctrl_t ctrl;
if (mb_get_fpga_ctrl(&ctrl)) {
lf();
printf("FPGA Ctrl: %cShdn %cPCIeReset\r\n",
ctrl.req_shutdown ? '+' : '-',
ctrl.req_pcie_reset ? '+' : '-');
}
}
}
int main(int argc, char** argv)
{
dump_enable_t en = {0};
const struct {
const char* opt;
bool* en;
} opt_map[] = {
{"mmc", &en.mmc},
{"sensors", &en.sensors},
{"fru0", &en.fru[0]},
{"fru1", &en.fru[1]},
{"fru2", &en.fru[2]},
{"fru3", &en.fru[3]},
{"amc", &en.fru[0]},
{"rtm", &en.fru[1]},
{"fmc1", &en.fru[2]},
{"fmc2", &en.fru[3]},
{"fpga", &en.fpga},
};
if (argc <= 1) {
// Dump all
en = (dump_enable_t){true, true, {true, true, true, true}, true};
} else {
for (int i = 1; i < argc; i++) {
bool opt_found = false;
for (size_t k = 0; k < (sizeof(opt_map) / sizeof(opt_map[0])); k++) {
if (!strcmp(argv[i], opt_map[k].opt)) {
*opt_map[k].en = true;
opt_found = true;
break;
}
}
if (!opt_found) {
goto usage;
}
}
}
if (!mb_check_magic()) {
fprintf(stderr, "Mailbox not available\r\n");
return 1;
}
dump_mmcmb(en);
return 0;
usage:
fprintf(stderr,
"usage: %s [mmc] [sensors] [fru0..3] [amc] [rtm] [fmc1] [fmc2] [fpga]\r\n",
argv[0]);
return 1;
}