summaryrefslogtreecommitdiffstats
path: root/common/recipes-core/ipmid/files/sel.c
blob: d598bb17fec9271dea0e06c791026e4f84fb9c2e (plain)
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
/*
 *
 * Copyright 2014-present Facebook. All Rights Reserved.
 *
 * This file represents platform specific implementation for storing
 * SEL logs and acts as back-end for IPMI stack
 *
 * TODO: Optimize the file handling to keep file open always instead of
 * current open/seek/close
 *
 *
 * 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 "sel.h"
#include "timestamp.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <syslog.h>
#include <string.h>

// SEL File.
#define SEL_LOG_FILE  "/mnt/data/sel.bin"

// SEL Header magic number
#define SEL_HDR_MAGIC 0xFBFBFBFB

// SEL Header version number
#define SEL_HDR_VERSION 0x01

// SEL Data offset from file beginning
#define SEL_DATA_OFFSET 0x100

// SEL reservation IDs can not be 0x00 or 0xFFFF
#define SEL_RSVID_MIN  0x01
#define SEL_RSVID_MAX  0xFFFE

// Number of SEL records before wrap
#define SEL_RECORDS_MAX 128 // TODO: Based on need we can make it bigger
#define SEL_ELEMS_MAX (SEL_RECORDS_MAX+1)

// Index for circular array
#define SEL_INDEX_MIN 0x00
#define SEL_INDEX_MAX SEL_RECORDS_MAX

// Record ID can not be 0x0 (IPMI/Section 31)
#define SEL_RECID_MIN (SEL_INDEX_MIN+1)
#define SEL_RECID_MAX (SEL_INDEX_MAX+1)

// Special RecID value for first and last (IPMI/Section 31)
#define SEL_RECID_FIRST 0x0000
#define SEL_RECID_LAST 0xFFFF

// SEL header struct to keep track of SEL Log entries
typedef struct {
  int magic; // Magic number to check validity
  int version; // version number of this header
  int begin; // index to the begining of the log
  int end; // index to end of the log
  time_stamp_t ts_add; // last addition time stamp
  time_stamp_t ts_erase; // last erase time stamp
} sel_hdr_t;

// Keep track of last Reservation ID
static int g_rsv_id = 0x01;

// Cached version of SEL Header and data
static sel_hdr_t g_sel_hdr;
static sel_msg_t g_sel_data[SEL_ELEMS_MAX];

// Local helper functions to interact with file system
static int
file_get_sel_hdr(void) {
  FILE *fp;

  fp = fopen(SEL_LOG_FILE, "r");
  if (fp == NULL) {
    return -1;
  }

  if (fread(&g_sel_hdr, sizeof(sel_hdr_t), 1, fp) <= 0) {
    syslog(LOG_ALERT, "file_get_sel_hdr: fread\n");
    fclose (fp);
    return -1;
  }

  fclose(fp);
  return 0;
}

static int
file_get_sel_data(void) {
  FILE *fp;
  int i, j;

  fp = fopen(SEL_LOG_FILE, "r");
  if (fp == NULL) {
    syslog(LOG_ALERT, "file_get_sel_data: fopen\n");
    return -1;
  }

  if (fseek(fp, SEL_DATA_OFFSET, SEEK_SET)) {
    syslog(LOG_ALERT, "file_get_sel_data: fseek\n");
    fclose(fp);
    return -1;
  }

  unsigned char buf[SEL_ELEMS_MAX * 16];
  if (fread(buf, 1, SEL_ELEMS_MAX * sizeof(sel_msg_t), fp) <= 0) {
    syslog(LOG_ALERT, "file_get_sel_data: fread\n");
    fclose(fp);
    return -1;
  }

  fclose(fp);

  for (i = 0; i < SEL_ELEMS_MAX; i++) {
    for (j = 0; j < sizeof(sel_msg_t);j++) {
      g_sel_data[i].msg[j] = buf[i*16 + j];
    }
  }

  return 0;
}

static int
file_store_sel_hdr(void) {
  FILE *fp;

  fp = fopen(SEL_LOG_FILE, "r+");
  if (fp == NULL) {
    syslog(LOG_ALERT, "file_store_sel_hdr: fopen\n");
    return -1;
  }

  if (fwrite(&g_sel_hdr, sizeof(sel_hdr_t), 1, fp) <= 0) {
    syslog(LOG_ALERT, "file_store_sel_hdr: fwrite\n");
    fclose(fp);
    return -1;
  }

  fclose(fp);

  return 0;
}

static int
file_store_sel_data(int recId, sel_msg_t *data) {
  FILE *fp;
  int index;

  fp = fopen(SEL_LOG_FILE, "r+");
  if (fp == NULL) {
    syslog(LOG_ALERT, "file_store_sel_data: fopen\n");
    return -1;
  }

  // Records are stored using zero-based index
  index = (recId-1) * sizeof(sel_msg_t);

  if (fseek(fp, SEL_DATA_OFFSET+index, SEEK_SET)) {
    syslog(LOG_ALERT, "file_store_sel_data: fseek\n");
    fclose(fp);
    return -1;
  }

  if (fwrite(data->msg, sizeof(sel_msg_t), 1, fp) <= 0) {
    syslog(LOG_ALERT, "file_store_sel_data: fwrite\n");
    fclose(fp);
    return -1;
  }

  fclose(fp);

  return 0;
}

// Platform specific SEL API entry points
// Retrieve time stamp for recent add operation
void
sel_ts_recent_add(time_stamp_t *ts) {
  memcpy(ts->ts, g_sel_hdr.ts_add.ts, 0x04);
}

// Retrieve time stamp for recent erase operation
void
sel_ts_recent_erase(time_stamp_t *ts) {
  memcpy(ts->ts, g_sel_hdr.ts_erase.ts, 0x04);
}

// Retrieve total number of entries in SEL log
int
sel_num_entries(void) {
  if (g_sel_hdr.begin <= g_sel_hdr.end) {
      return (g_sel_hdr.end - g_sel_hdr.begin);
  } else {
    return (g_sel_hdr.end + (SEL_INDEX_MAX - g_sel_hdr.begin + 1));
  }
}

// Retrieve total free space available in SEL log
int
sel_free_space(void) {
  int total_space;
  int used_space;

  total_space = SEL_RECORDS_MAX * sizeof(sel_msg_t);
  used_space = sel_num_entries() * sizeof(sel_msg_t);

  return (total_space - used_space);
}

// Reserve an ID that will be used in later operations
// IPMI/Section 31.4
int
sel_rsv_id() {
  // Increment the current reservation ID and return
  if (g_rsv_id++ == SEL_RSVID_MAX) {
    g_rsv_id = SEL_RSVID_MIN;
  }

  return g_rsv_id;
}

// Get the SEL entry for a given record ID
// IPMI/Section 31.5
int
sel_get_entry(int read_rec_id, sel_msg_t *msg, int *next_rec_id) {

  int index;

  // Find the index in to array based on given index
  if (read_rec_id == SEL_RECID_FIRST) {
    index = g_sel_hdr.begin;
  } else if (read_rec_id == SEL_RECID_LAST) {
    if (g_sel_hdr.end) {
      index = g_sel_hdr.end - 1;
    } else {
      index = SEL_INDEX_MAX;
    }
  } else {
    index = read_rec_id - 1;
  }

  // If the log is empty return error
  if (sel_num_entries() == 0) {
    syslog(LOG_ALERT, "sel_get_entry: No entries\n");
    return -1;
  }

  // Check for boundary conditions
  if ((index < SEL_INDEX_MIN) || (index > SEL_INDEX_MAX)) {
    syslog(LOG_ALERT, "sel_get_entry: Invalid Record ID %d\n", read_rec_id);
    return -1;
  }

  // If begin < end, check to make sure the given id falls between
  if (g_sel_hdr.begin < g_sel_hdr.end) {
    if (index < g_sel_hdr.begin || index >= g_sel_hdr.end) {
      syslog(LOG_ALERT, "sel_get_entry: Wrong Record ID %d\n", read_rec_id);
      return -1;
    }
  }

  // If end < begin, check to make sure the given id is valid
  if (g_sel_hdr.begin > g_sel_hdr.end) {
    if (index >= g_sel_hdr.end && index < g_sel_hdr.begin) {
      syslog(LOG_ALERT, "sel_get_entry: Wrong Record ID2 %d\n", read_rec_id);
      return -1;
    }
  }

  memcpy(msg->msg, g_sel_data[index].msg, sizeof(sel_msg_t));

  // Return the next record ID in the log
  *next_rec_id = read_rec_id++;
  if (*next_rec_id > SEL_INDEX_MAX) {
    *next_rec_id = SEL_INDEX_MIN;
  }

  // If this is the last entry in the log, return 0xFFFF
  if (*next_rec_id == g_sel_hdr.end) {
    *next_rec_id = SEL_RECID_LAST;
  }

  return 0;
}

// Add a new entry in to SEL log
// IPMI/Section 31.6
int
sel_add_entry(sel_msg_t *msg, int *rec_id) {
  // If the SEL if full, roll over. To keep track of empty condition, use
  // one empty location less than the max records.
  if (sel_num_entries() == SEL_RECORDS_MAX) {
      syslog(LOG_ALERT, "sel_add_entry: SEL rollover\n");
    if (++g_sel_hdr.begin > SEL_INDEX_MAX) {
      g_sel_hdr.begin = SEL_INDEX_MIN;
    }
  }

  // Update message's time stamp starting at byte 4
  time_stamp_fill(&msg->msg[3]);

  // Add the enry at end
  memcpy(g_sel_data[g_sel_hdr.end].msg, msg->msg, sizeof(sel_msg_t));

  // Return the newly added record ID
  *rec_id = g_sel_hdr.end+1;

  if (file_store_sel_data(*rec_id, msg)) {
    syslog(LOG_ALERT, "sel_add_entry: file_store_sel_data\n");
    return -1;
  }

  // Increment the end pointer
  if (++g_sel_hdr.end > SEL_INDEX_MAX) {
    g_sel_hdr.end = SEL_INDEX_MIN;
  }

  // Update timestamp for add in header
  time_stamp_fill(g_sel_hdr.ts_add.ts);

  // Store the structure persistently
  if (file_store_sel_hdr()) {
    syslog(LOG_ALERT, "sel_add_entry: file_store_sel_hdr\n");
    return -1;
  }

  return 0;
}

// Erase the SEL completely
// IPMI/Section 31.9
// Note: To reduce wear/tear, instead of erasing, manipulating the metadata
int
sel_erase(int rsv_id) {
  if (rsv_id != g_rsv_id) {
    return -1;
  }

  // Erase SEL Logs
  g_sel_hdr.begin = SEL_INDEX_MIN;
  g_sel_hdr.end = SEL_INDEX_MIN;

  // Update timestamp for erase in header
  time_stamp_fill(g_sel_hdr.ts_erase.ts);

  // Store the structure persistently
  if (file_store_sel_hdr()) {
    syslog(LOG_ALERT, "sel_erase: file_store_sel_hdr\n");
    return -1;
  }

  return 0;
}

// To get the erase status while erase happens
// IPMI/Section 31.2
// Note: Since we are not doing offline erasing, need not return in-progress state
int
sel_erase_status(int rsv_id, sel_erase_stat_t *status) {
  if (rsv_id != g_rsv_id) {
    return -1;
  }

  // Since we do not do any offline erasing, always return erase done
  *status = SEL_ERASE_DONE;

  return 0;
}

// Initialize SEL log file
int
sel_init(void) {
  FILE *fp;
  int i;

  // Check if the file exists or not
  if (access(SEL_LOG_FILE, F_OK) == 0) {
    // Since file is present, fetch all the contents to cache
    if (file_get_sel_hdr()) {
      syslog(LOG_ALERT, "init_sel: file_get_sel_hdr\n");
      return -1;
    }

    if (file_get_sel_data()) {
      syslog(LOG_ALERT, "init_sel: file_get_sel_data\n");
      return -1;
    }

    return 0;
  }

  // File not present, so create the file
  fp = fopen(SEL_LOG_FILE, "w+");
  if (fp == NULL) {
    syslog(LOG_ALERT, "init_sel: fopen\n");
    return -1;
  }

  fclose (fp);

  // Populate SEL Header in to the file
  g_sel_hdr.magic = SEL_HDR_MAGIC;
  g_sel_hdr.version = SEL_HDR_VERSION;
  g_sel_hdr.begin = SEL_INDEX_MIN;
  g_sel_hdr.end = SEL_INDEX_MIN;
  memset(g_sel_hdr.ts_add.ts, 0x0, 4);
  memset(g_sel_hdr.ts_erase.ts, 0x0, 4);

  if (file_store_sel_hdr()) {
    syslog(LOG_ALERT, "init_sel: file_store_sel_hdr\n");
    return -1;
  }

  // Populate SEL Data in to the file
  for (i = 1;  i <= SEL_RECORDS_MAX; i++) {
    sel_msg_t msg = {0};
    if (file_store_sel_data(i, &msg)) {
      syslog(LOG_ALERT, "init_sel: file_store_sel_data\n");
      return -1;
    }
  }

  return 0;
}
OpenPOWER on IntegriCloud