File: fsp-elog-write.c

package info (click to toggle)
skiboot 5.3.3-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 13,280 kB
  • ctags: 13,840
  • sloc: ansic: 77,199; asm: 1,002; sh: 997; cpp: 894; tcl: 408; makefile: 325; python: 166; pascal: 65
file content (442 lines) | stat: -rw-r--r-- 11,700 bytes parent folder | download
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
/* Copyright 2013-2014 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/*
 * This code will enable generation and pushing of error log
 * from powernv, sapphire to FSP
 * Critical events from sapphire that needs to be reported
 * will be pushed on to FSP after converting the
 * error log to Platform Error Log (PEL) format.
 * This is termed as WRITE action to FSP.
 */

#include <skiboot.h>
#include <fsp.h>
#include <cpu.h>
#include <lock.h>
#include <errno.h>
#include <fsp-elog.h>
#include <timebase.h>
#include <pel.h>
#include <pool.h>
#include <opal-api.h>

static LIST_HEAD(elog_write_to_fsp_pending);
static LIST_HEAD(elog_write_to_host_pending);
static LIST_HEAD(elog_write_to_host_processed);

static struct lock elog_write_lock = LOCK_UNLOCKED;
static struct lock elog_panic_write_lock = LOCK_UNLOCKED;
static struct lock elog_write_to_host_lock = LOCK_UNLOCKED;

/* log buffer  to copy FSP log for READ */
#define ELOG_WRITE_TO_FSP_BUFFER_SIZE	0x00004000
static void *elog_write_to_fsp_buffer;

#define ELOG_PANIC_WRITE_BUFFER_SIZE	0x00004000
static void *elog_panic_write_buffer;

#define ELOG_WRITE_TO_HOST_BUFFER_SIZE	0x00004000
static void *elog_write_to_host_buffer;

static uint32_t elog_write_retries;

/* Manipulate this only with write_lock held */
static uint32_t elog_plid_fsp_commit = -1;
static enum elog_head_state elog_write_to_host_head_state = ELOG_STATE_NONE;

/* Need forward declaration because of Circular dependency */
static int opal_send_elog_to_fsp(void);

static void remove_elog_head_entry(void)
{
	struct errorlog *head, *entry;

	lock(&elog_write_lock);
	if (!list_empty(&elog_write_to_fsp_pending)) {
		head = list_top(&elog_write_to_fsp_pending,
					struct errorlog, link);
		if (head->plid == elog_plid_fsp_commit) {
			entry = list_pop(&elog_write_to_fsp_pending,
					struct errorlog, link);
			opal_elog_complete(entry, elog_write_retries < MAX_RETRIES);
			/* Reset the counter */
			elog_plid_fsp_commit = -1;
		}
	}
	elog_write_retries = 0;
	unlock(&elog_write_lock);
}

static void opal_fsp_write_complete(struct fsp_msg *read_msg)
{
	uint8_t val;

	val = (read_msg->resp->word1 >> 8) & 0xff;
	fsp_freemsg(read_msg);

	switch (val) {
	case FSP_STATUS_SUCCESS:
			remove_elog_head_entry();
			break;

	default:
		if (elog_write_retries++ >= MAX_RETRIES) {
			remove_elog_head_entry();
			prerror("ELOG: Error in writing to FSP (0x%x)!\n", val);
		}
		break;
	}

	if (opal_send_elog_to_fsp() != OPAL_SUCCESS)
		prerror("ELOG: Error sending elog to FSP !\n");
}

/* write PEL format hex dump of the log to FSP */
static int64_t fsp_opal_elog_write(size_t opal_elog_size)
{
	struct fsp_msg *elog_msg;

	elog_msg = fsp_mkmsg(FSP_CMD_CREATE_ERRLOG, 3, opal_elog_size,
						 0, PSI_DMA_ERRLOG_WRITE_BUF);
	if (!elog_msg) {
		prerror("ELOG: Failed to create message for WRITE to FSP\n");
		return OPAL_INTERNAL_ERROR;
	}
	if (fsp_queue_msg(elog_msg, opal_fsp_write_complete)) {
		fsp_freemsg(elog_msg);
		elog_msg = NULL;
		prerror("FSP: Error queueing elog update\n");
		return OPAL_INTERNAL_ERROR;
	}
	return OPAL_SUCCESS;
}

/* This should be called with elog_write_to_host_lock lock */
static inline void fsp_elog_write_set_head_state(enum elog_head_state state)
{
	elog_set_head_state(true, state);
	elog_write_to_host_head_state = state;
}

bool opal_elog_info(uint64_t *opal_elog_id, uint64_t *opal_elog_size)
{
	struct errorlog *head;
	bool rc = false;

	lock(&elog_write_to_host_lock);
	if (elog_write_to_host_head_state == ELOG_STATE_FETCHED_DATA) {
		head = list_top(&elog_write_to_host_pending,
					struct errorlog, link);
		if (!head) {
			/**
			 * @fwts-label ElogListInconsistent
			 * @fwts-advice Bug in interaction between FSP and
			 * OPAL. The state maintained by OPAL didn't match
			 * what the FSP sent.
			 */
			prlog(PR_ERR,
			      "%s: Inconsistent internal list state !\n",
			      __func__);
			fsp_elog_write_set_head_state(ELOG_STATE_NONE);
		} else {
			*opal_elog_id = head->plid;
			*opal_elog_size = head->log_size;
			fsp_elog_write_set_head_state(ELOG_STATE_FETCHED_INFO);
			rc = true;
		}
	}
	unlock(&elog_write_to_host_lock);
	return rc;
}

static void opal_commit_elog_in_host(void)
{

	struct errorlog *buf;

	lock(&elog_write_to_host_lock);
	if (!list_empty(&elog_write_to_host_pending) &&
			(elog_write_to_host_head_state == ELOG_STATE_NONE)) {
		buf = list_top(&elog_write_to_host_pending,
				struct errorlog, link);
		buf->log_size = create_pel_log(buf,
					       (char *)elog_write_to_host_buffer,
					       ELOG_WRITE_TO_HOST_BUFFER_SIZE);
		fsp_elog_write_set_head_state(ELOG_STATE_FETCHED_DATA);
	}
	unlock(&elog_write_to_host_lock);
}


bool opal_elog_read(uint64_t *buffer, uint64_t opal_elog_size,
		    uint64_t opal_elog_id)
{
	struct errorlog *log_data;
	bool rc = false;

	lock(&elog_write_to_host_lock);
	if (elog_write_to_host_head_state == ELOG_STATE_FETCHED_INFO) {
		log_data = list_top(&elog_write_to_host_pending,
					struct errorlog, link);
		if (!log_data) {
			fsp_elog_write_set_head_state(ELOG_STATE_NONE);
			unlock(&elog_write_to_host_lock);
			return rc;
		}
		if ((opal_elog_id != log_data->plid) &&
		    (opal_elog_size != log_data->log_size)) {
			unlock(&elog_write_to_host_lock);
			return rc;
		}

		memcpy((void *)buffer, elog_write_to_host_buffer,
							opal_elog_size);

		list_del(&log_data->link);
		list_add(&elog_write_to_host_processed, &log_data->link);
		fsp_elog_write_set_head_state(ELOG_STATE_NONE);
		rc = true;
	}
	unlock(&elog_write_to_host_lock);
	opal_commit_elog_in_host();
	return rc;
}

bool opal_elog_ack(uint64_t ack_id)
{
	bool rc = false;
	struct errorlog *log_data;
	struct errorlog *record, *next_record;

	lock(&elog_write_to_host_lock);
	if (!list_empty(&elog_write_to_host_processed)) {
		list_for_each_safe(&elog_write_to_host_processed, record,
							next_record, link) {
			if (record->plid != ack_id)
				continue;
			list_del(&record->link);
			opal_elog_complete(record, true);
			rc = true;
		}
	}

	if ((!rc) && (!list_empty(&elog_write_to_host_pending))) {
		log_data = list_top(&elog_write_to_host_pending,
					struct errorlog, link);
		if (ack_id == log_data->plid)
			fsp_elog_write_set_head_state(ELOG_STATE_NONE);

		list_for_each_safe(&elog_write_to_host_pending, record,
							next_record, link) {
			if (record->plid != ack_id)
				continue;
			list_del(&record->link);
			opal_elog_complete(record, true);
			rc = true;
			unlock(&elog_write_to_host_lock);
			opal_commit_elog_in_host();
			return rc;
		}
	}
	unlock(&elog_write_to_host_lock);
	return rc;
}

void opal_resend_pending_logs(void)
{
	struct errorlog *record;

	lock(&elog_write_to_host_lock);
	while (!list_empty(&elog_write_to_host_processed)) {
		record = list_pop(&elog_write_to_host_processed,
					struct errorlog, link);
		list_add_tail(&elog_write_to_host_pending, &record->link);
	}
	fsp_elog_write_set_head_state(ELOG_STATE_NONE);
	unlock(&elog_write_to_host_lock);
	opal_commit_elog_in_host();
}

static int opal_send_elog_to_fsp(void)
{
	struct errorlog *head;
	int rc = OPAL_SUCCESS;

	/* Convert entry to PEL
	 * and push it down to FSP. We wait for the ack from
	 * FSP.
	 */
	lock(&elog_write_lock);
	if (!list_empty(&elog_write_to_fsp_pending)) {
		head = list_top(&elog_write_to_fsp_pending,
					 struct errorlog, link);
		elog_plid_fsp_commit = head->plid;
		head->log_size = create_pel_log(head,
						(char *)elog_write_to_fsp_buffer,
						ELOG_WRITE_TO_FSP_BUFFER_SIZE);
		rc = fsp_opal_elog_write(head->log_size);
		unlock(&elog_write_lock);
		return rc;
	}
	unlock(&elog_write_lock);
	return rc;
}

static int opal_push_logs_sync_to_fsp(struct errorlog *buf)
{
	struct fsp_msg *elog_msg;
	int opal_elog_size = 0;
	int rc = OPAL_SUCCESS;

	lock(&elog_panic_write_lock);
	opal_elog_size = create_pel_log(buf,
					(char *)elog_panic_write_buffer,
					ELOG_PANIC_WRITE_BUFFER_SIZE);

	elog_msg = fsp_mkmsg(FSP_CMD_CREATE_ERRLOG, 3, opal_elog_size,
					0, PSI_DMA_ELOG_PANIC_WRITE_BUF);
	if (!elog_msg) {
		prerror("ELOG: PLID: 0x%x Failed to create message for WRITE "
							"to FSP\n", buf->plid);
		unlock(&elog_panic_write_lock);
		opal_elog_complete(buf, false);
		return OPAL_INTERNAL_ERROR;
	}

	if (fsp_sync_msg(elog_msg, false)) {
		fsp_freemsg(elog_msg);
		rc = OPAL_INTERNAL_ERROR;
	} else {
		rc = (elog_msg->resp->word1 >> 8) & 0xff;
		fsp_freemsg(elog_msg);
	}
	unlock(&elog_panic_write_lock);

	if (rc != OPAL_SUCCESS)
		opal_elog_complete(buf, false);
	else
		opal_elog_complete(buf, true);
	return rc;
}

static inline u64 get_elog_timeout(void)
{
	return (mftb() + secs_to_tb(ERRORLOG_TIMEOUT_INTERVAL));
}

int elog_fsp_commit(struct errorlog *buf)
{
	int rc = OPAL_SUCCESS;

	/* Error needs to be committed, update the time out value */
	buf->elog_timeout = get_elog_timeout();

	if (buf->event_severity == OPAL_ERROR_PANIC) {
		rc = opal_push_logs_sync_to_fsp(buf);
		return rc;
	}

	lock(&elog_write_lock);
	if (list_empty(&elog_write_to_fsp_pending)) {
		list_add_tail(&elog_write_to_fsp_pending, &buf->link);
		unlock(&elog_write_lock);
		rc = opal_send_elog_to_fsp();
		return rc;
	}
	list_add_tail(&elog_write_to_fsp_pending, &buf->link);
	unlock(&elog_write_lock);
	return rc;
}

static void elog_append_write_to_host(struct errorlog *buf)
{

	lock(&elog_write_to_host_lock);
	if (list_empty(&elog_write_to_host_pending)) {
		list_add(&elog_write_to_host_pending, &buf->link);
		unlock(&elog_write_to_host_lock);
		opal_commit_elog_in_host();
	} else {
		list_add_tail(&elog_write_to_host_pending, &buf->link);
		unlock(&elog_write_to_host_lock);
	}
}

static void elog_timeout_poll(void *data __unused)
{
	uint64_t now;
	struct errorlog *head, *entry;

	lock(&elog_write_lock);
	if (list_empty(&elog_write_to_fsp_pending)) {
		unlock(&elog_write_lock);
		return;
	} else {
		head = list_top(&elog_write_to_fsp_pending,
					struct errorlog, link);
		now = mftb();
		if ((tb_compare(now, head->elog_timeout) == TB_AAFTERB) ||
			(tb_compare(now, head->elog_timeout) == TB_AEQUALB)) {
				entry = list_pop(&elog_write_to_fsp_pending,
						struct errorlog, link);
				unlock(&elog_write_lock);
				elog_append_write_to_host(entry);
		} else
			unlock(&elog_write_lock);
	}
}

/* fsp elog init function */
void fsp_elog_write_init(void)
{
	if (!fsp_present())
		return;

	elog_panic_write_buffer = memalign(TCE_PSIZE,
					   ELOG_PANIC_WRITE_BUFFER_SIZE);
	if (!elog_panic_write_buffer) {
		prerror("FSP: could not allocate ELOG_PANIC_WRITE_BUFFER!\n");
		return;
	}

	elog_write_to_fsp_buffer = memalign(TCE_PSIZE,
						ELOG_WRITE_TO_FSP_BUFFER_SIZE);
	if (!elog_write_to_fsp_buffer) {
		prerror("FSP: could not allocate ELOG_WRITE_BUFFER!\n");
		return;
	}

	elog_write_to_host_buffer = memalign(TCE_PSIZE,
					ELOG_WRITE_TO_HOST_BUFFER_SIZE);
	if (!elog_write_to_host_buffer) {
		prerror("FSP: could not allocate ELOG_WRITE_TO_HOST_BUFFER!\n");
		return;
	}

	/* Map TCEs */
	fsp_tce_map(PSI_DMA_ELOG_PANIC_WRITE_BUF, elog_panic_write_buffer,
					PSI_DMA_ELOG_PANIC_WRITE_BUF_SZ);

	fsp_tce_map(PSI_DMA_ERRLOG_WRITE_BUF, elog_write_to_fsp_buffer,
					PSI_DMA_ERRLOG_WRITE_BUF_SZ);

	elog_init();

	/* Add a poller */
	opal_add_poller(elog_timeout_poll, NULL);
}