blob: 2c462beee5513ec40448f6faf0179e621644558f [file] [log] [blame]
Huang Yinga08f82d2010-05-18 14:35:21 +08001/*
2 * APEI Error Record Serialization Table support
3 *
4 * ERST is a way provided by APEI to save and retrieve hardware error
Lucas De Marchi58f87ed2010-09-07 12:49:45 -04005 * information to and from a persistent store.
Huang Yinga08f82d2010-05-18 14:35:21 +08006 *
7 * For more information about ERST, please refer to ACPI Specification
8 * version 4.0, section 17.4.
9 *
10 * Copyright 2010 Intel Corp.
11 * Author: Huang Ying <ying.huang@intel.com>
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License version
15 * 2 as published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
Huang Yinga08f82d2010-05-18 14:35:21 +080021 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/io.h>
28#include <linux/acpi.h>
29#include <linux/uaccess.h>
30#include <linux/cper.h>
31#include <linux/nmi.h>
Thomas Gleixner0a7992c2010-08-11 14:17:29 -070032#include <linux/hardirq.h>
Tony Luck0bb77c42011-01-03 14:22:11 -080033#include <linux/pstore.h>
Stephen Rothwelld6472302015-06-02 19:01:38 +100034#include <linux/vmalloc.h>
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -080035#include <linux/mm.h> /* kvfree() */
Huang Yinga08f82d2010-05-18 14:35:21 +080036#include <acpi/apei.h>
37
38#include "apei-internal.h"
39
Borislav Petkovcb82a2e2013-07-29 15:51:35 +020040#undef pr_fmt
41#define pr_fmt(fmt) "ERST: " fmt
Huang Yinga08f82d2010-05-18 14:35:21 +080042
43/* ERST command status */
44#define ERST_STATUS_SUCCESS 0x0
45#define ERST_STATUS_NOT_ENOUGH_SPACE 0x1
46#define ERST_STATUS_HARDWARE_NOT_AVAILABLE 0x2
47#define ERST_STATUS_FAILED 0x3
48#define ERST_STATUS_RECORD_STORE_EMPTY 0x4
49#define ERST_STATUS_RECORD_NOT_FOUND 0x5
50
51#define ERST_TAB_ENTRY(tab) \
52 ((struct acpi_whea_header *)((char *)(tab) + \
53 sizeof(struct acpi_table_erst)))
54
55#define SPIN_UNIT 100 /* 100ns */
Stefan Weile8a8b252011-01-02 15:12:42 +010056/* Firmware should respond within 1 milliseconds */
Huang Yinga08f82d2010-05-18 14:35:21 +080057#define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
58#define FIRMWARE_MAX_STALL 50 /* 50us */
59
60int erst_disable;
61EXPORT_SYMBOL_GPL(erst_disable);
62
63static struct acpi_table_erst *erst_tab;
64
65/* ERST Error Log Address Range atrributes */
66#define ERST_RANGE_RESERVED 0x0001
67#define ERST_RANGE_NVRAM 0x0002
68#define ERST_RANGE_SLOW 0x0004
69
70/*
71 * ERST Error Log Address Range, used as buffer for reading/writing
72 * error records.
73 */
74static struct erst_erange {
75 u64 base;
76 u64 size;
77 void __iomem *vaddr;
78 u32 attr;
79} erst_erange;
80
81/*
82 * Prevent ERST interpreter to run simultaneously, because the
83 * corresponding firmware implementation may not work properly when
84 * invoked simultaneously.
85 *
86 * It is used to provide exclusive accessing for ERST Error Log
87 * Address Range too.
88 */
Huang Ying3b38bb52010-12-02 10:40:53 +080089static DEFINE_RAW_SPINLOCK(erst_lock);
Huang Yinga08f82d2010-05-18 14:35:21 +080090
91static inline int erst_errno(int command_status)
92{
93 switch (command_status) {
94 case ERST_STATUS_SUCCESS:
95 return 0;
96 case ERST_STATUS_HARDWARE_NOT_AVAILABLE:
97 return -ENODEV;
98 case ERST_STATUS_NOT_ENOUGH_SPACE:
99 return -ENOSPC;
100 case ERST_STATUS_RECORD_STORE_EMPTY:
101 case ERST_STATUS_RECORD_NOT_FOUND:
102 return -ENOENT;
103 default:
104 return -EINVAL;
105 }
106}
107
108static int erst_timedout(u64 *t, u64 spin_unit)
109{
110 if ((s64)*t < spin_unit) {
Borislav Petkovcb82a2e2013-07-29 15:51:35 +0200111 pr_warn(FW_WARN "Firmware does not respond in time.\n");
Huang Yinga08f82d2010-05-18 14:35:21 +0800112 return 1;
113 }
114 *t -= spin_unit;
115 ndelay(spin_unit);
116 touch_nmi_watchdog();
117 return 0;
118}
119
120static int erst_exec_load_var1(struct apei_exec_context *ctx,
121 struct acpi_whea_header *entry)
122{
123 return __apei_exec_read_register(entry, &ctx->var1);
124}
125
126static int erst_exec_load_var2(struct apei_exec_context *ctx,
127 struct acpi_whea_header *entry)
128{
129 return __apei_exec_read_register(entry, &ctx->var2);
130}
131
132static int erst_exec_store_var1(struct apei_exec_context *ctx,
133 struct acpi_whea_header *entry)
134{
135 return __apei_exec_write_register(entry, ctx->var1);
136}
137
138static int erst_exec_add(struct apei_exec_context *ctx,
139 struct acpi_whea_header *entry)
140{
141 ctx->var1 += ctx->var2;
142 return 0;
143}
144
145static int erst_exec_subtract(struct apei_exec_context *ctx,
146 struct acpi_whea_header *entry)
147{
148 ctx->var1 -= ctx->var2;
149 return 0;
150}
151
152static int erst_exec_add_value(struct apei_exec_context *ctx,
153 struct acpi_whea_header *entry)
154{
155 int rc;
156 u64 val;
157
158 rc = __apei_exec_read_register(entry, &val);
159 if (rc)
160 return rc;
161 val += ctx->value;
162 rc = __apei_exec_write_register(entry, val);
163 return rc;
164}
165
166static int erst_exec_subtract_value(struct apei_exec_context *ctx,
167 struct acpi_whea_header *entry)
168{
169 int rc;
170 u64 val;
171
172 rc = __apei_exec_read_register(entry, &val);
173 if (rc)
174 return rc;
175 val -= ctx->value;
176 rc = __apei_exec_write_register(entry, val);
177 return rc;
178}
179
180static int erst_exec_stall(struct apei_exec_context *ctx,
181 struct acpi_whea_header *entry)
182{
183 u64 stall_time;
184
185 if (ctx->value > FIRMWARE_MAX_STALL) {
186 if (!in_nmi())
Borislav Petkovcb82a2e2013-07-29 15:51:35 +0200187 pr_warn(FW_WARN
188 "Too long stall time for stall instruction: 0x%llx.\n",
Huang Yinga08f82d2010-05-18 14:35:21 +0800189 ctx->value);
190 stall_time = FIRMWARE_MAX_STALL;
191 } else
192 stall_time = ctx->value;
193 udelay(stall_time);
194 return 0;
195}
196
197static int erst_exec_stall_while_true(struct apei_exec_context *ctx,
198 struct acpi_whea_header *entry)
199{
200 int rc;
201 u64 val;
202 u64 timeout = FIRMWARE_TIMEOUT;
203 u64 stall_time;
204
205 if (ctx->var1 > FIRMWARE_MAX_STALL) {
206 if (!in_nmi())
Borislav Petkovcb82a2e2013-07-29 15:51:35 +0200207 pr_warn(FW_WARN
208 "Too long stall time for stall while true instruction: 0x%llx.\n",
Huang Yinga08f82d2010-05-18 14:35:21 +0800209 ctx->var1);
210 stall_time = FIRMWARE_MAX_STALL;
211 } else
212 stall_time = ctx->var1;
213
214 for (;;) {
215 rc = __apei_exec_read_register(entry, &val);
216 if (rc)
217 return rc;
218 if (val != ctx->value)
219 break;
220 if (erst_timedout(&timeout, stall_time * NSEC_PER_USEC))
221 return -EIO;
222 }
223 return 0;
224}
225
226static int erst_exec_skip_next_instruction_if_true(
227 struct apei_exec_context *ctx,
228 struct acpi_whea_header *entry)
229{
230 int rc;
231 u64 val;
232
233 rc = __apei_exec_read_register(entry, &val);
234 if (rc)
235 return rc;
236 if (val == ctx->value) {
237 ctx->ip += 2;
238 return APEI_EXEC_SET_IP;
239 }
240
241 return 0;
242}
243
244static int erst_exec_goto(struct apei_exec_context *ctx,
245 struct acpi_whea_header *entry)
246{
247 ctx->ip = ctx->value;
248 return APEI_EXEC_SET_IP;
249}
250
251static int erst_exec_set_src_address_base(struct apei_exec_context *ctx,
252 struct acpi_whea_header *entry)
253{
254 return __apei_exec_read_register(entry, &ctx->src_base);
255}
256
257static int erst_exec_set_dst_address_base(struct apei_exec_context *ctx,
258 struct acpi_whea_header *entry)
259{
260 return __apei_exec_read_register(entry, &ctx->dst_base);
261}
262
263static int erst_exec_move_data(struct apei_exec_context *ctx,
264 struct acpi_whea_header *entry)
265{
266 int rc;
267 u64 offset;
Huang Ying0bbba382010-09-29 19:53:55 +0800268 void *src, *dst;
269
270 /* ioremap does not work in interrupt context */
271 if (in_interrupt()) {
Borislav Petkovcb82a2e2013-07-29 15:51:35 +0200272 pr_warn("MOVE_DATA can not be used in interrupt context.\n");
Huang Ying0bbba382010-09-29 19:53:55 +0800273 return -EBUSY;
274 }
Huang Yinga08f82d2010-05-18 14:35:21 +0800275
276 rc = __apei_exec_read_register(entry, &offset);
277 if (rc)
278 return rc;
Huang Ying0bbba382010-09-29 19:53:55 +0800279
280 src = ioremap(ctx->src_base + offset, ctx->var2);
281 if (!src)
282 return -ENOMEM;
283 dst = ioremap(ctx->dst_base + offset, ctx->var2);
Wei Yongjun08b326d2013-07-02 09:02:32 +0800284 if (!dst) {
285 iounmap(src);
Huang Ying0bbba382010-09-29 19:53:55 +0800286 return -ENOMEM;
Wei Yongjun08b326d2013-07-02 09:02:32 +0800287 }
Huang Ying0bbba382010-09-29 19:53:55 +0800288
289 memmove(dst, src, ctx->var2);
290
291 iounmap(src);
292 iounmap(dst);
Huang Yinga08f82d2010-05-18 14:35:21 +0800293
294 return 0;
295}
296
297static struct apei_exec_ins_type erst_ins_type[] = {
298 [ACPI_ERST_READ_REGISTER] = {
299 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
300 .run = apei_exec_read_register,
301 },
302 [ACPI_ERST_READ_REGISTER_VALUE] = {
303 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
304 .run = apei_exec_read_register_value,
305 },
306 [ACPI_ERST_WRITE_REGISTER] = {
307 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
308 .run = apei_exec_write_register,
309 },
310 [ACPI_ERST_WRITE_REGISTER_VALUE] = {
311 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
312 .run = apei_exec_write_register_value,
313 },
314 [ACPI_ERST_NOOP] = {
315 .flags = 0,
316 .run = apei_exec_noop,
317 },
318 [ACPI_ERST_LOAD_VAR1] = {
319 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
320 .run = erst_exec_load_var1,
321 },
322 [ACPI_ERST_LOAD_VAR2] = {
323 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
324 .run = erst_exec_load_var2,
325 },
326 [ACPI_ERST_STORE_VAR1] = {
327 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
328 .run = erst_exec_store_var1,
329 },
330 [ACPI_ERST_ADD] = {
331 .flags = 0,
332 .run = erst_exec_add,
333 },
334 [ACPI_ERST_SUBTRACT] = {
335 .flags = 0,
336 .run = erst_exec_subtract,
337 },
338 [ACPI_ERST_ADD_VALUE] = {
339 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
340 .run = erst_exec_add_value,
341 },
342 [ACPI_ERST_SUBTRACT_VALUE] = {
343 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
344 .run = erst_exec_subtract_value,
345 },
346 [ACPI_ERST_STALL] = {
347 .flags = 0,
348 .run = erst_exec_stall,
349 },
350 [ACPI_ERST_STALL_WHILE_TRUE] = {
351 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
352 .run = erst_exec_stall_while_true,
353 },
354 [ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
355 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
356 .run = erst_exec_skip_next_instruction_if_true,
357 },
358 [ACPI_ERST_GOTO] = {
359 .flags = 0,
360 .run = erst_exec_goto,
361 },
362 [ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
363 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
364 .run = erst_exec_set_src_address_base,
365 },
366 [ACPI_ERST_SET_DST_ADDRESS_BASE] = {
367 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
368 .run = erst_exec_set_dst_address_base,
369 },
370 [ACPI_ERST_MOVE_DATA] = {
371 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
372 .run = erst_exec_move_data,
373 },
374};
375
376static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
377{
378 apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
379 ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
380}
381
382static int erst_get_erange(struct erst_erange *range)
383{
384 struct apei_exec_context ctx;
385 int rc;
386
387 erst_exec_ctx_init(&ctx);
388 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
389 if (rc)
390 return rc;
391 range->base = apei_exec_ctx_get_output(&ctx);
392 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
393 if (rc)
394 return rc;
395 range->size = apei_exec_ctx_get_output(&ctx);
396 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
397 if (rc)
398 return rc;
399 range->attr = apei_exec_ctx_get_output(&ctx);
400
401 return 0;
402}
403
404static ssize_t __erst_get_record_count(void)
405{
406 struct apei_exec_context ctx;
407 int rc;
408
409 erst_exec_ctx_init(&ctx);
410 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
411 if (rc)
412 return rc;
413 return apei_exec_ctx_get_output(&ctx);
414}
415
416ssize_t erst_get_record_count(void)
417{
418 ssize_t count;
419 unsigned long flags;
420
421 if (erst_disable)
422 return -ENODEV;
423
Huang Ying3b38bb52010-12-02 10:40:53 +0800424 raw_spin_lock_irqsave(&erst_lock, flags);
Huang Yinga08f82d2010-05-18 14:35:21 +0800425 count = __erst_get_record_count();
Huang Ying3b38bb52010-12-02 10:40:53 +0800426 raw_spin_unlock_irqrestore(&erst_lock, flags);
Huang Yinga08f82d2010-05-18 14:35:21 +0800427
428 return count;
429}
430EXPORT_SYMBOL_GPL(erst_get_record_count);
431
Huang Ying885b9762011-02-21 13:54:41 +0800432#define ERST_RECORD_ID_CACHE_SIZE_MIN 16
433#define ERST_RECORD_ID_CACHE_SIZE_MAX 1024
434
435struct erst_record_id_cache {
436 struct mutex lock;
437 u64 *entries;
438 int len;
439 int size;
440 int refcount;
441};
442
443static struct erst_record_id_cache erst_record_id_cache = {
444 .lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock),
445 .refcount = 0,
446};
447
Huang Yinga08f82d2010-05-18 14:35:21 +0800448static int __erst_get_next_record_id(u64 *record_id)
449{
450 struct apei_exec_context ctx;
451 int rc;
452
453 erst_exec_ctx_init(&ctx);
454 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
455 if (rc)
456 return rc;
457 *record_id = apei_exec_ctx_get_output(&ctx);
458
459 return 0;
460}
461
Huang Ying885b9762011-02-21 13:54:41 +0800462int erst_get_record_id_begin(int *pos)
463{
464 int rc;
465
466 if (erst_disable)
467 return -ENODEV;
468
469 rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
470 if (rc)
471 return rc;
472 erst_record_id_cache.refcount++;
473 mutex_unlock(&erst_record_id_cache.lock);
474
475 *pos = 0;
476
477 return 0;
478}
479EXPORT_SYMBOL_GPL(erst_get_record_id_begin);
480
481/* erst_record_id_cache.lock must be held by caller */
482static int __erst_record_id_cache_add_one(void)
483{
484 u64 id, prev_id, first_id;
485 int i, rc;
486 u64 *entries;
487 unsigned long flags;
488
489 id = prev_id = first_id = APEI_ERST_INVALID_RECORD_ID;
490retry:
491 raw_spin_lock_irqsave(&erst_lock, flags);
492 rc = __erst_get_next_record_id(&id);
493 raw_spin_unlock_irqrestore(&erst_lock, flags);
494 if (rc == -ENOENT)
495 return 0;
496 if (rc)
497 return rc;
498 if (id == APEI_ERST_INVALID_RECORD_ID)
499 return 0;
500 /* can not skip current ID, or loop back to first ID */
501 if (id == prev_id || id == first_id)
502 return 0;
503 if (first_id == APEI_ERST_INVALID_RECORD_ID)
504 first_id = id;
505 prev_id = id;
506
507 entries = erst_record_id_cache.entries;
508 for (i = 0; i < erst_record_id_cache.len; i++) {
509 if (entries[i] == id)
510 break;
511 }
512 /* record id already in cache, try next */
513 if (i < erst_record_id_cache.len)
514 goto retry;
515 if (erst_record_id_cache.len >= erst_record_id_cache.size) {
Michal Hocko752ade62017-05-08 15:57:27 -0700516 int new_size;
Huang Ying885b9762011-02-21 13:54:41 +0800517 u64 *new_entries;
518
519 new_size = erst_record_id_cache.size * 2;
520 new_size = clamp_val(new_size, ERST_RECORD_ID_CACHE_SIZE_MIN,
521 ERST_RECORD_ID_CACHE_SIZE_MAX);
522 if (new_size <= erst_record_id_cache.size) {
523 if (printk_ratelimit())
Borislav Petkovcb82a2e2013-07-29 15:51:35 +0200524 pr_warn(FW_WARN "too many record IDs!\n");
Huang Ying885b9762011-02-21 13:54:41 +0800525 return 0;
526 }
Michal Hocko752ade62017-05-08 15:57:27 -0700527 new_entries = kvmalloc(new_size * sizeof(entries[0]), GFP_KERNEL);
Huang Ying885b9762011-02-21 13:54:41 +0800528 if (!new_entries)
529 return -ENOMEM;
530 memcpy(new_entries, entries,
531 erst_record_id_cache.len * sizeof(entries[0]));
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800532 kvfree(entries);
Huang Ying885b9762011-02-21 13:54:41 +0800533 erst_record_id_cache.entries = entries = new_entries;
534 erst_record_id_cache.size = new_size;
535 }
536 entries[i] = id;
537 erst_record_id_cache.len++;
538
539 return 1;
540}
541
Huang Yinga08f82d2010-05-18 14:35:21 +0800542/*
543 * Get the record ID of an existing error record on the persistent
544 * storage. If there is no error record on the persistent storage, the
545 * returned record_id is APEI_ERST_INVALID_RECORD_ID.
546 */
Huang Ying885b9762011-02-21 13:54:41 +0800547int erst_get_record_id_next(int *pos, u64 *record_id)
Huang Yinga08f82d2010-05-18 14:35:21 +0800548{
Huang Ying885b9762011-02-21 13:54:41 +0800549 int rc = 0;
550 u64 *entries;
Huang Yinga08f82d2010-05-18 14:35:21 +0800551
552 if (erst_disable)
553 return -ENODEV;
554
Huang Ying885b9762011-02-21 13:54:41 +0800555 /* must be enclosed by erst_get_record_id_begin/end */
556 BUG_ON(!erst_record_id_cache.refcount);
557 BUG_ON(*pos < 0 || *pos > erst_record_id_cache.len);
558
559 mutex_lock(&erst_record_id_cache.lock);
560 entries = erst_record_id_cache.entries;
561 for (; *pos < erst_record_id_cache.len; (*pos)++)
562 if (entries[*pos] != APEI_ERST_INVALID_RECORD_ID)
563 break;
564 /* found next record id in cache */
565 if (*pos < erst_record_id_cache.len) {
566 *record_id = entries[*pos];
567 (*pos)++;
568 goto out_unlock;
569 }
570
571 /* Try to add one more record ID to cache */
572 rc = __erst_record_id_cache_add_one();
573 if (rc < 0)
574 goto out_unlock;
575 /* successfully add one new ID */
576 if (rc == 1) {
577 *record_id = erst_record_id_cache.entries[*pos];
578 (*pos)++;
579 rc = 0;
580 } else {
581 *pos = -1;
582 *record_id = APEI_ERST_INVALID_RECORD_ID;
583 }
584out_unlock:
585 mutex_unlock(&erst_record_id_cache.lock);
Huang Yinga08f82d2010-05-18 14:35:21 +0800586
587 return rc;
588}
Huang Ying885b9762011-02-21 13:54:41 +0800589EXPORT_SYMBOL_GPL(erst_get_record_id_next);
590
591/* erst_record_id_cache.lock must be held by caller */
592static void __erst_record_id_cache_compact(void)
593{
594 int i, wpos = 0;
595 u64 *entries;
596
597 if (erst_record_id_cache.refcount)
598 return;
599
600 entries = erst_record_id_cache.entries;
601 for (i = 0; i < erst_record_id_cache.len; i++) {
602 if (entries[i] == APEI_ERST_INVALID_RECORD_ID)
603 continue;
604 if (wpos != i)
Chen, Gongd3ab3ed2013-12-18 01:30:49 -0500605 entries[wpos] = entries[i];
Huang Ying885b9762011-02-21 13:54:41 +0800606 wpos++;
607 }
608 erst_record_id_cache.len = wpos;
609}
610
611void erst_get_record_id_end(void)
612{
613 /*
614 * erst_disable != 0 should be detected by invoker via the
615 * return value of erst_get_record_id_begin/next, so this
616 * function should not be called for erst_disable != 0.
617 */
618 BUG_ON(erst_disable);
619
620 mutex_lock(&erst_record_id_cache.lock);
621 erst_record_id_cache.refcount--;
622 BUG_ON(erst_record_id_cache.refcount < 0);
623 __erst_record_id_cache_compact();
624 mutex_unlock(&erst_record_id_cache.lock);
625}
626EXPORT_SYMBOL_GPL(erst_get_record_id_end);
Huang Yinga08f82d2010-05-18 14:35:21 +0800627
628static int __erst_write_to_storage(u64 offset)
629{
630 struct apei_exec_context ctx;
631 u64 timeout = FIRMWARE_TIMEOUT;
632 u64 val;
633 int rc;
634
635 erst_exec_ctx_init(&ctx);
Huang Ying392913d2011-07-13 13:14:17 +0800636 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_WRITE);
Huang Yinga08f82d2010-05-18 14:35:21 +0800637 if (rc)
638 return rc;
639 apei_exec_ctx_set_input(&ctx, offset);
640 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
641 if (rc)
642 return rc;
643 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
644 if (rc)
645 return rc;
646 for (;;) {
647 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
648 if (rc)
649 return rc;
650 val = apei_exec_ctx_get_output(&ctx);
651 if (!val)
652 break;
653 if (erst_timedout(&timeout, SPIN_UNIT))
654 return -EIO;
655 }
656 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
657 if (rc)
658 return rc;
659 val = apei_exec_ctx_get_output(&ctx);
Huang Ying392913d2011-07-13 13:14:17 +0800660 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
Huang Yinga08f82d2010-05-18 14:35:21 +0800661 if (rc)
662 return rc;
663
664 return erst_errno(val);
665}
666
667static int __erst_read_from_storage(u64 record_id, u64 offset)
668{
669 struct apei_exec_context ctx;
670 u64 timeout = FIRMWARE_TIMEOUT;
671 u64 val;
672 int rc;
673
674 erst_exec_ctx_init(&ctx);
Huang Ying392913d2011-07-13 13:14:17 +0800675 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_READ);
Huang Yinga08f82d2010-05-18 14:35:21 +0800676 if (rc)
677 return rc;
678 apei_exec_ctx_set_input(&ctx, offset);
679 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
680 if (rc)
681 return rc;
682 apei_exec_ctx_set_input(&ctx, record_id);
683 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
684 if (rc)
685 return rc;
686 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
687 if (rc)
688 return rc;
689 for (;;) {
690 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
691 if (rc)
692 return rc;
693 val = apei_exec_ctx_get_output(&ctx);
694 if (!val)
695 break;
696 if (erst_timedout(&timeout, SPIN_UNIT))
697 return -EIO;
698 };
699 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
700 if (rc)
701 return rc;
702 val = apei_exec_ctx_get_output(&ctx);
Huang Ying392913d2011-07-13 13:14:17 +0800703 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
Huang Yinga08f82d2010-05-18 14:35:21 +0800704 if (rc)
705 return rc;
706
707 return erst_errno(val);
708}
709
710static int __erst_clear_from_storage(u64 record_id)
711{
712 struct apei_exec_context ctx;
713 u64 timeout = FIRMWARE_TIMEOUT;
714 u64 val;
715 int rc;
716
717 erst_exec_ctx_init(&ctx);
Huang Ying392913d2011-07-13 13:14:17 +0800718 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_CLEAR);
Huang Yinga08f82d2010-05-18 14:35:21 +0800719 if (rc)
720 return rc;
721 apei_exec_ctx_set_input(&ctx, record_id);
722 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
723 if (rc)
724 return rc;
725 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
726 if (rc)
727 return rc;
728 for (;;) {
729 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
730 if (rc)
731 return rc;
732 val = apei_exec_ctx_get_output(&ctx);
733 if (!val)
734 break;
735 if (erst_timedout(&timeout, SPIN_UNIT))
736 return -EIO;
737 }
738 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
739 if (rc)
740 return rc;
741 val = apei_exec_ctx_get_output(&ctx);
Huang Ying392913d2011-07-13 13:14:17 +0800742 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
Huang Yinga08f82d2010-05-18 14:35:21 +0800743 if (rc)
744 return rc;
745
746 return erst_errno(val);
747}
748
749/* NVRAM ERST Error Log Address Range is not supported yet */
750static void pr_unimpl_nvram(void)
751{
752 if (printk_ratelimit())
Borislav Petkovcb82a2e2013-07-29 15:51:35 +0200753 pr_warn("NVRAM ERST Log Address Range not implemented yet.\n");
Huang Yinga08f82d2010-05-18 14:35:21 +0800754}
755
756static int __erst_write_to_nvram(const struct cper_record_header *record)
757{
758 /* do not print message, because printk is not safe for NMI */
759 return -ENOSYS;
760}
761
762static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
763{
764 pr_unimpl_nvram();
765 return -ENOSYS;
766}
767
768static int __erst_clear_from_nvram(u64 record_id)
769{
770 pr_unimpl_nvram();
771 return -ENOSYS;
772}
773
774int erst_write(const struct cper_record_header *record)
775{
776 int rc;
777 unsigned long flags;
778 struct cper_record_header *rcd_erange;
779
780 if (erst_disable)
781 return -ENODEV;
782
783 if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
784 return -EINVAL;
785
786 if (erst_erange.attr & ERST_RANGE_NVRAM) {
Huang Ying3b38bb52010-12-02 10:40:53 +0800787 if (!raw_spin_trylock_irqsave(&erst_lock, flags))
Huang Yinga08f82d2010-05-18 14:35:21 +0800788 return -EBUSY;
789 rc = __erst_write_to_nvram(record);
Huang Ying3b38bb52010-12-02 10:40:53 +0800790 raw_spin_unlock_irqrestore(&erst_lock, flags);
Huang Yinga08f82d2010-05-18 14:35:21 +0800791 return rc;
792 }
793
794 if (record->record_length > erst_erange.size)
795 return -EINVAL;
796
Huang Ying3b38bb52010-12-02 10:40:53 +0800797 if (!raw_spin_trylock_irqsave(&erst_lock, flags))
Huang Yinga08f82d2010-05-18 14:35:21 +0800798 return -EBUSY;
799 memcpy(erst_erange.vaddr, record, record->record_length);
800 rcd_erange = erst_erange.vaddr;
801 /* signature for serialization system */
802 memcpy(&rcd_erange->persistence_information, "ER", 2);
803
804 rc = __erst_write_to_storage(0);
Huang Ying3b38bb52010-12-02 10:40:53 +0800805 raw_spin_unlock_irqrestore(&erst_lock, flags);
Huang Yinga08f82d2010-05-18 14:35:21 +0800806
807 return rc;
808}
809EXPORT_SYMBOL_GPL(erst_write);
810
811static int __erst_read_to_erange(u64 record_id, u64 *offset)
812{
813 int rc;
814
815 if (erst_erange.attr & ERST_RANGE_NVRAM)
816 return __erst_read_to_erange_from_nvram(
817 record_id, offset);
818
819 rc = __erst_read_from_storage(record_id, 0);
820 if (rc)
821 return rc;
822 *offset = 0;
823
824 return 0;
825}
826
827static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
828 size_t buflen)
829{
830 int rc;
831 u64 offset, len = 0;
832 struct cper_record_header *rcd_tmp;
833
834 rc = __erst_read_to_erange(record_id, &offset);
835 if (rc)
836 return rc;
837 rcd_tmp = erst_erange.vaddr + offset;
838 len = rcd_tmp->record_length;
839 if (len <= buflen)
840 memcpy(record, rcd_tmp, len);
841
842 return len;
843}
844
845/*
846 * If return value > buflen, the buffer size is not big enough,
847 * else if return value < 0, something goes wrong,
848 * else everything is OK, and return value is record length
849 */
850ssize_t erst_read(u64 record_id, struct cper_record_header *record,
851 size_t buflen)
852{
853 ssize_t len;
854 unsigned long flags;
855
856 if (erst_disable)
857 return -ENODEV;
858
Huang Ying3b38bb52010-12-02 10:40:53 +0800859 raw_spin_lock_irqsave(&erst_lock, flags);
Huang Yinga08f82d2010-05-18 14:35:21 +0800860 len = __erst_read(record_id, record, buflen);
Huang Ying3b38bb52010-12-02 10:40:53 +0800861 raw_spin_unlock_irqrestore(&erst_lock, flags);
Huang Yinga08f82d2010-05-18 14:35:21 +0800862 return len;
863}
864EXPORT_SYMBOL_GPL(erst_read);
865
Huang Yinga08f82d2010-05-18 14:35:21 +0800866int erst_clear(u64 record_id)
867{
Huang Ying885b9762011-02-21 13:54:41 +0800868 int rc, i;
Huang Yinga08f82d2010-05-18 14:35:21 +0800869 unsigned long flags;
Huang Ying885b9762011-02-21 13:54:41 +0800870 u64 *entries;
Huang Yinga08f82d2010-05-18 14:35:21 +0800871
872 if (erst_disable)
873 return -ENODEV;
874
Huang Ying885b9762011-02-21 13:54:41 +0800875 rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
876 if (rc)
877 return rc;
Huang Ying3b38bb52010-12-02 10:40:53 +0800878 raw_spin_lock_irqsave(&erst_lock, flags);
Huang Yinga08f82d2010-05-18 14:35:21 +0800879 if (erst_erange.attr & ERST_RANGE_NVRAM)
880 rc = __erst_clear_from_nvram(record_id);
881 else
882 rc = __erst_clear_from_storage(record_id);
Huang Ying3b38bb52010-12-02 10:40:53 +0800883 raw_spin_unlock_irqrestore(&erst_lock, flags);
Huang Ying885b9762011-02-21 13:54:41 +0800884 if (rc)
885 goto out;
886 entries = erst_record_id_cache.entries;
887 for (i = 0; i < erst_record_id_cache.len; i++) {
888 if (entries[i] == record_id)
889 entries[i] = APEI_ERST_INVALID_RECORD_ID;
890 }
891 __erst_record_id_cache_compact();
892out:
893 mutex_unlock(&erst_record_id_cache.lock);
Huang Yinga08f82d2010-05-18 14:35:21 +0800894 return rc;
895}
896EXPORT_SYMBOL_GPL(erst_clear);
897
898static int __init setup_erst_disable(char *str)
899{
900 erst_disable = 1;
901 return 0;
902}
903
904__setup("erst_disable", setup_erst_disable);
905
906static int erst_check_table(struct acpi_table_erst *erst_tab)
907{
Huang Ying3a78f962010-09-29 19:53:51 +0800908 if ((erst_tab->header_length !=
909 (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
Jiang Liu7ed28f22012-03-07 22:15:06 +0800910 && (erst_tab->header_length != sizeof(struct acpi_table_erst)))
Huang Yinga08f82d2010-05-18 14:35:21 +0800911 return -EINVAL;
912 if (erst_tab->header.length < sizeof(struct acpi_table_erst))
913 return -EINVAL;
914 if (erst_tab->entries !=
915 (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
916 sizeof(struct acpi_erst_entry))
917 return -EINVAL;
918
919 return 0;
920}
921
Chen Gong06cf91b2011-05-16 11:00:27 -0700922static int erst_open_pstore(struct pstore_info *psi);
923static int erst_close_pstore(struct pstore_info *psi);
Kees Cook125cc422017-03-03 22:09:18 -0800924static ssize_t erst_reader(struct pstore_record *record);
Kees Cook76cc9582017-03-03 23:28:53 -0800925static int erst_writer(struct pstore_record *record);
Kees Cooka61072a2017-03-04 23:31:19 -0800926static int erst_clearer(struct pstore_record *record);
Tony Luck0bb77c42011-01-03 14:22:11 -0800927
928static struct pstore_info erst_info = {
929 .owner = THIS_MODULE,
930 .name = "erst",
Namhyung Kimc950fd62016-07-28 00:08:25 +0900931 .flags = PSTORE_FLAGS_DMESG,
Chen Gong06cf91b2011-05-16 11:00:27 -0700932 .open = erst_open_pstore,
933 .close = erst_close_pstore,
Tony Luck0bb77c42011-01-03 14:22:11 -0800934 .read = erst_reader,
935 .write = erst_writer,
Matthew Garrett638c1fd2011-07-21 16:57:52 -0400936 .erase = erst_clearer
Tony Luck0bb77c42011-01-03 14:22:11 -0800937};
938
939#define CPER_CREATOR_PSTORE \
940 UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
941 0x64, 0x90, 0xb8, 0x9d)
942#define CPER_SECTION_TYPE_DMESG \
943 UUID_LE(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54, \
944 0x94, 0x19, 0xeb, 0x12)
Aruna Balakrishnaiah901037b2013-08-16 13:57:26 -0700945#define CPER_SECTION_TYPE_DMESG_Z \
946 UUID_LE(0x4f118707, 0x04dd, 0x4055, 0xb5, 0xdd, 0x95, 0x6d, \
947 0x34, 0xdd, 0xfa, 0xc6)
Tony Luck0bb77c42011-01-03 14:22:11 -0800948#define CPER_SECTION_TYPE_MCE \
949 UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
950 0x04, 0x4a, 0x38, 0xfc)
951
952struct cper_pstore_record {
953 struct cper_record_header hdr;
954 struct cper_section_descriptor sec_hdr;
955 char data[];
956} __packed;
957
Chen Gong06cf91b2011-05-16 11:00:27 -0700958static int reader_pos;
959
960static int erst_open_pstore(struct pstore_info *psi)
961{
962 int rc;
963
964 if (erst_disable)
965 return -ENODEV;
966
967 rc = erst_get_record_id_begin(&reader_pos);
968
969 return rc;
970}
971
972static int erst_close_pstore(struct pstore_info *psi)
973{
974 erst_get_record_id_end();
975
976 return 0;
977}
978
Kees Cook125cc422017-03-03 22:09:18 -0800979static ssize_t erst_reader(struct pstore_record *record)
Tony Luck0bb77c42011-01-03 14:22:11 -0800980{
981 int rc;
Chen Gong06cf91b2011-05-16 11:00:27 -0700982 ssize_t len = 0;
Tony Luck0bb77c42011-01-03 14:22:11 -0800983 u64 record_id;
Kees Cookf6f82852011-11-17 12:58:07 -0800984 struct cper_pstore_record *rcd;
985 size_t rcd_len = sizeof(*rcd) + erst_info.bufsize;
Tony Luck0bb77c42011-01-03 14:22:11 -0800986
987 if (erst_disable)
988 return -ENODEV;
989
Kees Cookf6f82852011-11-17 12:58:07 -0800990 rcd = kmalloc(rcd_len, GFP_KERNEL);
991 if (!rcd) {
992 rc = -ENOMEM;
993 goto out;
994 }
Tony Luck0bb77c42011-01-03 14:22:11 -0800995skip:
Chen Gong06cf91b2011-05-16 11:00:27 -0700996 rc = erst_get_record_id_next(&reader_pos, &record_id);
997 if (rc)
998 goto out;
999
Tony Luck0bb77c42011-01-03 14:22:11 -08001000 /* no more record */
1001 if (record_id == APEI_ERST_INVALID_RECORD_ID) {
Kees Cookf6f82852011-11-17 12:58:07 -08001002 rc = -EINVAL;
Chen Gong06cf91b2011-05-16 11:00:27 -07001003 goto out;
Tony Luck0bb77c42011-01-03 14:22:11 -08001004 }
1005
Kees Cookf6f82852011-11-17 12:58:07 -08001006 len = erst_read(record_id, &rcd->hdr, rcd_len);
Chen Gongf5ec25d2011-05-16 11:01:39 -07001007 /* The record may be cleared by others, try read next record */
1008 if (len == -ENOENT)
1009 goto skip;
Kees Cookf6f82852011-11-17 12:58:07 -08001010 else if (len < sizeof(*rcd)) {
1011 rc = -EIO;
Chen Gongf5ec25d2011-05-16 11:01:39 -07001012 goto out;
1013 }
Tony Luck0bb77c42011-01-03 14:22:11 -08001014 if (uuid_le_cmp(rcd->hdr.creator_id, CPER_CREATOR_PSTORE) != 0)
1015 goto skip;
Tony Luck0bb77c42011-01-03 14:22:11 -08001016
Kees Cook125cc422017-03-03 22:09:18 -08001017 record->buf = kmalloc(len, GFP_KERNEL);
1018 if (record->buf == NULL) {
Kees Cookf6f82852011-11-17 12:58:07 -08001019 rc = -ENOMEM;
1020 goto out;
1021 }
Kees Cook125cc422017-03-03 22:09:18 -08001022 memcpy(record->buf, rcd->data, len - sizeof(*rcd));
1023 record->id = record_id;
1024 record->compressed = false;
1025 record->ecc_notice_size = 0;
Tony Luck0bb77c42011-01-03 14:22:11 -08001026 if (uuid_le_cmp(rcd->sec_hdr.section_type,
Aruna Balakrishnaiah901037b2013-08-16 13:57:26 -07001027 CPER_SECTION_TYPE_DMESG_Z) == 0) {
Kees Cook125cc422017-03-03 22:09:18 -08001028 record->type = PSTORE_TYPE_DMESG;
1029 record->compressed = true;
Aruna Balakrishnaiah901037b2013-08-16 13:57:26 -07001030 } else if (uuid_le_cmp(rcd->sec_hdr.section_type,
Tony Luck0bb77c42011-01-03 14:22:11 -08001031 CPER_SECTION_TYPE_DMESG) == 0)
Kees Cook125cc422017-03-03 22:09:18 -08001032 record->type = PSTORE_TYPE_DMESG;
Tony Luck0bb77c42011-01-03 14:22:11 -08001033 else if (uuid_le_cmp(rcd->sec_hdr.section_type,
1034 CPER_SECTION_TYPE_MCE) == 0)
Kees Cook125cc422017-03-03 22:09:18 -08001035 record->type = PSTORE_TYPE_MCE;
Tony Luck0bb77c42011-01-03 14:22:11 -08001036 else
Kees Cook125cc422017-03-03 22:09:18 -08001037 record->type = PSTORE_TYPE_UNKNOWN;
Tony Luck0bb77c42011-01-03 14:22:11 -08001038
1039 if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
Kees Cook125cc422017-03-03 22:09:18 -08001040 record->time.tv_sec = rcd->hdr.timestamp;
Tony Luck0bb77c42011-01-03 14:22:11 -08001041 else
Kees Cook125cc422017-03-03 22:09:18 -08001042 record->time.tv_sec = 0;
1043 record->time.tv_nsec = 0;
Tony Luck0bb77c42011-01-03 14:22:11 -08001044
Chen Gong06cf91b2011-05-16 11:00:27 -07001045out:
Kees Cookf6f82852011-11-17 12:58:07 -08001046 kfree(rcd);
Chen Gong06cf91b2011-05-16 11:00:27 -07001047 return (rc < 0) ? rc : (len - sizeof(*rcd));
Tony Luck0bb77c42011-01-03 14:22:11 -08001048}
1049
Kees Cook76cc9582017-03-03 23:28:53 -08001050static int erst_writer(struct pstore_record *record)
Tony Luck0bb77c42011-01-03 14:22:11 -08001051{
1052 struct cper_pstore_record *rcd = (struct cper_pstore_record *)
1053 (erst_info.buf - sizeof(*rcd));
Chen Gongb238b8f2011-10-12 09:17:24 -07001054 int ret;
Tony Luck0bb77c42011-01-03 14:22:11 -08001055
1056 memset(rcd, 0, sizeof(*rcd));
1057 memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
1058 rcd->hdr.revision = CPER_RECORD_REV;
1059 rcd->hdr.signature_end = CPER_SIG_END;
1060 rcd->hdr.section_count = 1;
1061 rcd->hdr.error_severity = CPER_SEV_FATAL;
1062 /* timestamp valid. platform_id, partition_id are invalid */
1063 rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
1064 rcd->hdr.timestamp = get_seconds();
Kees Cook76cc9582017-03-03 23:28:53 -08001065 rcd->hdr.record_length = sizeof(*rcd) + record->size;
Tony Luck0bb77c42011-01-03 14:22:11 -08001066 rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
1067 rcd->hdr.notification_type = CPER_NOTIFY_MCE;
1068 rcd->hdr.record_id = cper_next_record_id();
1069 rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
1070
1071 rcd->sec_hdr.section_offset = sizeof(*rcd);
Kees Cook76cc9582017-03-03 23:28:53 -08001072 rcd->sec_hdr.section_length = record->size;
Tony Luck0bb77c42011-01-03 14:22:11 -08001073 rcd->sec_hdr.revision = CPER_SEC_REV;
1074 /* fru_id and fru_text is invalid */
1075 rcd->sec_hdr.validation_bits = 0;
1076 rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
Kees Cook76cc9582017-03-03 23:28:53 -08001077 switch (record->type) {
Tony Luck0bb77c42011-01-03 14:22:11 -08001078 case PSTORE_TYPE_DMESG:
Kees Cook76cc9582017-03-03 23:28:53 -08001079 if (record->compressed)
Aruna Balakrishnaiah901037b2013-08-16 13:57:26 -07001080 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG_Z;
1081 else
1082 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
Tony Luck0bb77c42011-01-03 14:22:11 -08001083 break;
1084 case PSTORE_TYPE_MCE:
1085 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
1086 break;
1087 default:
1088 return -EINVAL;
1089 }
1090 rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
1091
Chen Gongb238b8f2011-10-12 09:17:24 -07001092 ret = erst_write(&rcd->hdr);
Kees Cook76cc9582017-03-03 23:28:53 -08001093 record->id = rcd->hdr.record_id;
Tony Luck0bb77c42011-01-03 14:22:11 -08001094
Chen Gongb238b8f2011-10-12 09:17:24 -07001095 return ret;
Tony Luck0bb77c42011-01-03 14:22:11 -08001096}
1097
Kees Cooka61072a2017-03-04 23:31:19 -08001098static int erst_clearer(struct pstore_record *record)
Matthew Garrett638c1fd2011-07-21 16:57:52 -04001099{
Kees Cooka61072a2017-03-04 23:31:19 -08001100 return erst_clear(record->id);
Matthew Garrett638c1fd2011-07-21 16:57:52 -04001101}
1102
Huang Yinga08f82d2010-05-18 14:35:21 +08001103static int __init erst_init(void)
1104{
1105 int rc = 0;
1106 acpi_status status;
1107 struct apei_exec_context ctx;
1108 struct apei_resources erst_resources;
1109 struct resource *r;
Tony Luck0bb77c42011-01-03 14:22:11 -08001110 char *buf;
Huang Yinga08f82d2010-05-18 14:35:21 +08001111
1112 if (acpi_disabled)
1113 goto err;
1114
1115 if (erst_disable) {
Borislav Petkovcb82a2e2013-07-29 15:51:35 +02001116 pr_info(
Huang Yinga08f82d2010-05-18 14:35:21 +08001117 "Error Record Serialization Table (ERST) support is disabled.\n");
1118 goto err;
1119 }
1120
1121 status = acpi_get_table(ACPI_SIG_ERST, 0,
1122 (struct acpi_table_header **)&erst_tab);
Huang Yingad686152011-12-08 11:25:43 +08001123 if (status == AE_NOT_FOUND)
Huang Yinga08f82d2010-05-18 14:35:21 +08001124 goto err;
Huang Yingad686152011-12-08 11:25:43 +08001125 else if (ACPI_FAILURE(status)) {
Huang Yinga08f82d2010-05-18 14:35:21 +08001126 const char *msg = acpi_format_exception(status);
Borislav Petkovcb82a2e2013-07-29 15:51:35 +02001127 pr_err("Failed to get table, %s\n", msg);
Huang Yinga08f82d2010-05-18 14:35:21 +08001128 rc = -EINVAL;
1129 goto err;
1130 }
1131
1132 rc = erst_check_table(erst_tab);
1133 if (rc) {
Borislav Petkovcb82a2e2013-07-29 15:51:35 +02001134 pr_err(FW_BUG "ERST table is invalid.\n");
Huang Yinga08f82d2010-05-18 14:35:21 +08001135 goto err;
1136 }
1137
1138 apei_resources_init(&erst_resources);
1139 erst_exec_ctx_init(&ctx);
1140 rc = apei_exec_collect_resources(&ctx, &erst_resources);
1141 if (rc)
1142 goto err_fini;
1143 rc = apei_resources_request(&erst_resources, "APEI ERST");
1144 if (rc)
1145 goto err_fini;
1146 rc = apei_exec_pre_map_gars(&ctx);
1147 if (rc)
1148 goto err_release;
1149 rc = erst_get_erange(&erst_erange);
1150 if (rc) {
1151 if (rc == -ENODEV)
Borislav Petkovcb82a2e2013-07-29 15:51:35 +02001152 pr_info(
Huang Yinga08f82d2010-05-18 14:35:21 +08001153 "The corresponding hardware device or firmware implementation "
1154 "is not available.\n");
1155 else
Borislav Petkovcb82a2e2013-07-29 15:51:35 +02001156 pr_err("Failed to get Error Log Address Range.\n");
Huang Yinga08f82d2010-05-18 14:35:21 +08001157 goto err_unmap_reg;
1158 }
1159
1160 r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
1161 if (!r) {
Borislav Petkovcb82a2e2013-07-29 15:51:35 +02001162 pr_err("Can not request [mem %#010llx-%#010llx] for ERST.\n",
1163 (unsigned long long)erst_erange.base,
1164 (unsigned long long)erst_erange.base + erst_erange.size - 1);
Huang Yinga08f82d2010-05-18 14:35:21 +08001165 rc = -EIO;
1166 goto err_unmap_reg;
1167 }
1168 rc = -ENOMEM;
1169 erst_erange.vaddr = ioremap_cache(erst_erange.base,
1170 erst_erange.size);
1171 if (!erst_erange.vaddr)
1172 goto err_release_erange;
1173
Borislav Petkovcb82a2e2013-07-29 15:51:35 +02001174 pr_info(
Lenny Szubowicz74fd6c62013-06-28 16:14:10 -04001175 "Error Record Serialization Table (ERST) support is initialized.\n");
1176
Tony Luck0bb77c42011-01-03 14:22:11 -08001177 buf = kmalloc(erst_erange.size, GFP_KERNEL);
Don Zickusabd4d552011-08-12 10:54:51 -07001178 spin_lock_init(&erst_info.buf_lock);
Tony Luck0bb77c42011-01-03 14:22:11 -08001179 if (buf) {
1180 erst_info.buf = buf + sizeof(struct cper_pstore_record);
1181 erst_info.bufsize = erst_erange.size -
1182 sizeof(struct cper_pstore_record);
Lenny Szubowicz74fd6c62013-06-28 16:14:10 -04001183 rc = pstore_register(&erst_info);
1184 if (rc) {
1185 if (rc != -EPERM)
Borislav Petkovcb82a2e2013-07-29 15:51:35 +02001186 pr_info(
1187 "Could not register with persistent store.\n");
Lenny Szubowicz74fd6c62013-06-28 16:14:10 -04001188 erst_info.buf = NULL;
1189 erst_info.bufsize = 0;
Tony Luck0bb77c42011-01-03 14:22:11 -08001190 kfree(buf);
1191 }
Lenny Szubowicz74fd6c62013-06-28 16:14:10 -04001192 } else
Borislav Petkovcb82a2e2013-07-29 15:51:35 +02001193 pr_err(
1194 "Failed to allocate %lld bytes for persistent store error log.\n",
Lenny Szubowicz74fd6c62013-06-28 16:14:10 -04001195 erst_erange.size);
Huang Yinga08f82d2010-05-18 14:35:21 +08001196
Josh Hunt3a03d122016-03-08 10:52:12 -05001197 /* Cleanup ERST Resources */
1198 apei_resources_fini(&erst_resources);
1199
Huang Yinga08f82d2010-05-18 14:35:21 +08001200 return 0;
1201
1202err_release_erange:
1203 release_mem_region(erst_erange.base, erst_erange.size);
1204err_unmap_reg:
1205 apei_exec_post_unmap_gars(&ctx);
1206err_release:
1207 apei_resources_release(&erst_resources);
1208err_fini:
1209 apei_resources_fini(&erst_resources);
1210err:
1211 erst_disable = 1;
1212 return rc;
1213}
1214
1215device_initcall(erst_init);