1 // SPDX-License-Identifier: GPL-2.0-only
3 * APEI Error Record Serialization Table support
5 * ERST is a way provided by APEI to save and retrieve hardware error
6 * information to and from a persistent store.
8 * For more information about ERST, please refer to ACPI Specification
9 * version 4.0, section 17.4.
11 * Copyright 2010 Intel Corp.
12 * Author: Huang Ying <ying.huang@intel.com>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
20 #include <linux/acpi.h>
21 #include <linux/uaccess.h>
22 #include <linux/cper.h>
23 #include <linux/nmi.h>
24 #include <linux/hardirq.h>
25 #include <linux/pstore.h>
26 #include <linux/vmalloc.h>
27 #include <linux/mm.h> /* kvfree() */
28 #include <acpi/apei.h>
30 #include "apei-internal.h"
33 #define pr_fmt(fmt) "ERST: " fmt
35 /* ERST command status */
36 #define ERST_STATUS_SUCCESS 0x0
37 #define ERST_STATUS_NOT_ENOUGH_SPACE 0x1
38 #define ERST_STATUS_HARDWARE_NOT_AVAILABLE 0x2
39 #define ERST_STATUS_FAILED 0x3
40 #define ERST_STATUS_RECORD_STORE_EMPTY 0x4
41 #define ERST_STATUS_RECORD_NOT_FOUND 0x5
43 #define ERST_TAB_ENTRY(tab) \
44 ((struct acpi_whea_header *)((char *)(tab) + \
45 sizeof(struct acpi_table_erst)))
47 #define SPIN_UNIT 100 /* 100ns */
48 /* Firmware should respond within 1 milliseconds */
49 #define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
50 #define FIRMWARE_MAX_STALL 50 /* 50us */
53 EXPORT_SYMBOL_GPL(erst_disable
);
55 static struct acpi_table_erst
*erst_tab
;
57 /* ERST Error Log Address Range attributes */
58 #define ERST_RANGE_RESERVED 0x0001
59 #define ERST_RANGE_NVRAM 0x0002
60 #define ERST_RANGE_SLOW 0x0004
62 /* ERST Exec max timings */
63 #define ERST_EXEC_TIMING_MAX_MASK 0xFFFFFFFF00000000
64 #define ERST_EXEC_TIMING_MAX_SHIFT 32
67 * ERST Error Log Address Range, used as buffer for reading/writing
70 static struct erst_erange
{
79 * Prevent ERST interpreter to run simultaneously, because the
80 * corresponding firmware implementation may not work properly when
81 * invoked simultaneously.
83 * It is used to provide exclusive accessing for ERST Error Log
86 static DEFINE_RAW_SPINLOCK(erst_lock
);
88 static inline int erst_errno(int command_status
)
90 switch (command_status
) {
91 case ERST_STATUS_SUCCESS
:
93 case ERST_STATUS_HARDWARE_NOT_AVAILABLE
:
95 case ERST_STATUS_NOT_ENOUGH_SPACE
:
97 case ERST_STATUS_RECORD_STORE_EMPTY
:
98 case ERST_STATUS_RECORD_NOT_FOUND
:
105 static inline u64
erst_get_timeout(void)
107 u64 timeout
= FIRMWARE_TIMEOUT
;
109 if (erst_erange
.attr
& ERST_RANGE_SLOW
) {
110 timeout
= ((erst_erange
.timings
& ERST_EXEC_TIMING_MAX_MASK
) >>
111 ERST_EXEC_TIMING_MAX_SHIFT
) * NSEC_PER_MSEC
;
112 if (timeout
< FIRMWARE_TIMEOUT
)
113 timeout
= FIRMWARE_TIMEOUT
;
118 static int erst_timedout(u64
*t
, u64 spin_unit
)
120 if ((s64
)*t
< spin_unit
) {
121 pr_warn(FW_WARN
"Firmware does not respond in time.\n");
126 touch_nmi_watchdog();
130 static int erst_exec_load_var1(struct apei_exec_context
*ctx
,
131 struct acpi_whea_header
*entry
)
133 return __apei_exec_read_register(entry
, &ctx
->var1
);
136 static int erst_exec_load_var2(struct apei_exec_context
*ctx
,
137 struct acpi_whea_header
*entry
)
139 return __apei_exec_read_register(entry
, &ctx
->var2
);
142 static int erst_exec_store_var1(struct apei_exec_context
*ctx
,
143 struct acpi_whea_header
*entry
)
145 return __apei_exec_write_register(entry
, ctx
->var1
);
148 static int erst_exec_add(struct apei_exec_context
*ctx
,
149 struct acpi_whea_header
*entry
)
151 ctx
->var1
+= ctx
->var2
;
155 static int erst_exec_subtract(struct apei_exec_context
*ctx
,
156 struct acpi_whea_header
*entry
)
158 ctx
->var1
-= ctx
->var2
;
162 static int erst_exec_add_value(struct apei_exec_context
*ctx
,
163 struct acpi_whea_header
*entry
)
168 rc
= __apei_exec_read_register(entry
, &val
);
172 rc
= __apei_exec_write_register(entry
, val
);
176 static int erst_exec_subtract_value(struct apei_exec_context
*ctx
,
177 struct acpi_whea_header
*entry
)
182 rc
= __apei_exec_read_register(entry
, &val
);
186 rc
= __apei_exec_write_register(entry
, val
);
190 static int erst_exec_stall(struct apei_exec_context
*ctx
,
191 struct acpi_whea_header
*entry
)
195 if (ctx
->value
> FIRMWARE_MAX_STALL
) {
198 "Too long stall time for stall instruction: 0x%llx.\n",
200 stall_time
= FIRMWARE_MAX_STALL
;
202 stall_time
= ctx
->value
;
207 static int erst_exec_stall_while_true(struct apei_exec_context
*ctx
,
208 struct acpi_whea_header
*entry
)
215 timeout
= erst_get_timeout();
217 if (ctx
->var1
> FIRMWARE_MAX_STALL
) {
220 "Too long stall time for stall while true instruction: 0x%llx.\n",
222 stall_time
= FIRMWARE_MAX_STALL
;
224 stall_time
= ctx
->var1
;
227 rc
= __apei_exec_read_register(entry
, &val
);
230 if (val
!= ctx
->value
)
232 if (erst_timedout(&timeout
, stall_time
* NSEC_PER_USEC
))
238 static int erst_exec_skip_next_instruction_if_true(
239 struct apei_exec_context
*ctx
,
240 struct acpi_whea_header
*entry
)
245 rc
= __apei_exec_read_register(entry
, &val
);
248 if (val
== ctx
->value
) {
250 return APEI_EXEC_SET_IP
;
256 static int erst_exec_goto(struct apei_exec_context
*ctx
,
257 struct acpi_whea_header
*entry
)
259 ctx
->ip
= ctx
->value
;
260 return APEI_EXEC_SET_IP
;
263 static int erst_exec_set_src_address_base(struct apei_exec_context
*ctx
,
264 struct acpi_whea_header
*entry
)
266 return __apei_exec_read_register(entry
, &ctx
->src_base
);
269 static int erst_exec_set_dst_address_base(struct apei_exec_context
*ctx
,
270 struct acpi_whea_header
*entry
)
272 return __apei_exec_read_register(entry
, &ctx
->dst_base
);
275 static int erst_exec_move_data(struct apei_exec_context
*ctx
,
276 struct acpi_whea_header
*entry
)
282 /* ioremap does not work in interrupt context */
283 if (in_interrupt()) {
284 pr_warn("MOVE_DATA can not be used in interrupt context.\n");
288 rc
= __apei_exec_read_register(entry
, &offset
);
292 src
= ioremap(ctx
->src_base
+ offset
, ctx
->var2
);
295 dst
= ioremap(ctx
->dst_base
+ offset
, ctx
->var2
);
301 memmove(dst
, src
, ctx
->var2
);
309 static struct apei_exec_ins_type erst_ins_type
[] = {
310 [ACPI_ERST_READ_REGISTER
] = {
311 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
312 .run
= apei_exec_read_register
,
314 [ACPI_ERST_READ_REGISTER_VALUE
] = {
315 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
316 .run
= apei_exec_read_register_value
,
318 [ACPI_ERST_WRITE_REGISTER
] = {
319 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
320 .run
= apei_exec_write_register
,
322 [ACPI_ERST_WRITE_REGISTER_VALUE
] = {
323 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
324 .run
= apei_exec_write_register_value
,
328 .run
= apei_exec_noop
,
330 [ACPI_ERST_LOAD_VAR1
] = {
331 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
332 .run
= erst_exec_load_var1
,
334 [ACPI_ERST_LOAD_VAR2
] = {
335 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
336 .run
= erst_exec_load_var2
,
338 [ACPI_ERST_STORE_VAR1
] = {
339 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
340 .run
= erst_exec_store_var1
,
344 .run
= erst_exec_add
,
346 [ACPI_ERST_SUBTRACT
] = {
348 .run
= erst_exec_subtract
,
350 [ACPI_ERST_ADD_VALUE
] = {
351 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
352 .run
= erst_exec_add_value
,
354 [ACPI_ERST_SUBTRACT_VALUE
] = {
355 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
356 .run
= erst_exec_subtract_value
,
358 [ACPI_ERST_STALL
] = {
360 .run
= erst_exec_stall
,
362 [ACPI_ERST_STALL_WHILE_TRUE
] = {
363 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
364 .run
= erst_exec_stall_while_true
,
366 [ACPI_ERST_SKIP_NEXT_IF_TRUE
] = {
367 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
368 .run
= erst_exec_skip_next_instruction_if_true
,
372 .run
= erst_exec_goto
,
374 [ACPI_ERST_SET_SRC_ADDRESS_BASE
] = {
375 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
376 .run
= erst_exec_set_src_address_base
,
378 [ACPI_ERST_SET_DST_ADDRESS_BASE
] = {
379 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
380 .run
= erst_exec_set_dst_address_base
,
382 [ACPI_ERST_MOVE_DATA
] = {
383 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
384 .run
= erst_exec_move_data
,
388 static inline void erst_exec_ctx_init(struct apei_exec_context
*ctx
)
390 apei_exec_ctx_init(ctx
, erst_ins_type
, ARRAY_SIZE(erst_ins_type
),
391 ERST_TAB_ENTRY(erst_tab
), erst_tab
->entries
);
394 static int erst_get_erange(struct erst_erange
*range
)
396 struct apei_exec_context ctx
;
399 erst_exec_ctx_init(&ctx
);
400 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_ERROR_RANGE
);
403 range
->base
= apei_exec_ctx_get_output(&ctx
);
404 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_ERROR_LENGTH
);
407 range
->size
= apei_exec_ctx_get_output(&ctx
);
408 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_ERROR_ATTRIBUTES
);
411 range
->attr
= apei_exec_ctx_get_output(&ctx
);
412 rc
= apei_exec_run(&ctx
, ACPI_ERST_EXECUTE_TIMINGS
);
414 range
->timings
= apei_exec_ctx_get_output(&ctx
);
415 else if (rc
== -ENOENT
)
423 static ssize_t
__erst_get_record_count(void)
425 struct apei_exec_context ctx
;
428 erst_exec_ctx_init(&ctx
);
429 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_RECORD_COUNT
);
432 return apei_exec_ctx_get_output(&ctx
);
435 ssize_t
erst_get_record_count(void)
443 raw_spin_lock_irqsave(&erst_lock
, flags
);
444 count
= __erst_get_record_count();
445 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
449 EXPORT_SYMBOL_GPL(erst_get_record_count
);
451 #define ERST_RECORD_ID_CACHE_SIZE_MIN 16
452 #define ERST_RECORD_ID_CACHE_SIZE_MAX 1024
454 struct erst_record_id_cache
{
462 static struct erst_record_id_cache erst_record_id_cache
= {
463 .lock
= __MUTEX_INITIALIZER(erst_record_id_cache
.lock
),
467 static int __erst_get_next_record_id(u64
*record_id
)
469 struct apei_exec_context ctx
;
472 erst_exec_ctx_init(&ctx
);
473 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_RECORD_ID
);
476 *record_id
= apei_exec_ctx_get_output(&ctx
);
481 int erst_get_record_id_begin(int *pos
)
488 rc
= mutex_lock_interruptible(&erst_record_id_cache
.lock
);
491 erst_record_id_cache
.refcount
++;
492 mutex_unlock(&erst_record_id_cache
.lock
);
498 EXPORT_SYMBOL_GPL(erst_get_record_id_begin
);
500 /* erst_record_id_cache.lock must be held by caller */
501 static int __erst_record_id_cache_add_one(void)
503 u64 id
, prev_id
, first_id
;
508 id
= prev_id
= first_id
= APEI_ERST_INVALID_RECORD_ID
;
510 raw_spin_lock_irqsave(&erst_lock
, flags
);
511 rc
= __erst_get_next_record_id(&id
);
512 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
517 if (id
== APEI_ERST_INVALID_RECORD_ID
)
519 /* can not skip current ID, or loop back to first ID */
520 if (id
== prev_id
|| id
== first_id
)
522 if (first_id
== APEI_ERST_INVALID_RECORD_ID
)
526 entries
= erst_record_id_cache
.entries
;
527 for (i
= 0; i
< erst_record_id_cache
.len
; i
++) {
528 if (entries
[i
] == id
)
531 /* record id already in cache, try next */
532 if (i
< erst_record_id_cache
.len
)
534 if (erst_record_id_cache
.len
>= erst_record_id_cache
.size
) {
538 new_size
= erst_record_id_cache
.size
* 2;
539 new_size
= clamp_val(new_size
, ERST_RECORD_ID_CACHE_SIZE_MIN
,
540 ERST_RECORD_ID_CACHE_SIZE_MAX
);
541 if (new_size
<= erst_record_id_cache
.size
) {
542 if (printk_ratelimit())
543 pr_warn(FW_WARN
"too many record IDs!\n");
546 new_entries
= kvmalloc_array(new_size
, sizeof(entries
[0]),
550 memcpy(new_entries
, entries
,
551 erst_record_id_cache
.len
* sizeof(entries
[0]));
553 erst_record_id_cache
.entries
= entries
= new_entries
;
554 erst_record_id_cache
.size
= new_size
;
557 erst_record_id_cache
.len
++;
563 * Get the record ID of an existing error record on the persistent
564 * storage. If there is no error record on the persistent storage, the
565 * returned record_id is APEI_ERST_INVALID_RECORD_ID.
567 int erst_get_record_id_next(int *pos
, u64
*record_id
)
575 /* must be enclosed by erst_get_record_id_begin/end */
576 BUG_ON(!erst_record_id_cache
.refcount
);
577 BUG_ON(*pos
< 0 || *pos
> erst_record_id_cache
.len
);
579 mutex_lock(&erst_record_id_cache
.lock
);
580 entries
= erst_record_id_cache
.entries
;
581 for (; *pos
< erst_record_id_cache
.len
; (*pos
)++)
582 if (entries
[*pos
] != APEI_ERST_INVALID_RECORD_ID
)
584 /* found next record id in cache */
585 if (*pos
< erst_record_id_cache
.len
) {
586 *record_id
= entries
[*pos
];
591 /* Try to add one more record ID to cache */
592 rc
= __erst_record_id_cache_add_one();
595 /* successfully add one new ID */
597 *record_id
= erst_record_id_cache
.entries
[*pos
];
602 *record_id
= APEI_ERST_INVALID_RECORD_ID
;
605 mutex_unlock(&erst_record_id_cache
.lock
);
609 EXPORT_SYMBOL_GPL(erst_get_record_id_next
);
611 /* erst_record_id_cache.lock must be held by caller */
612 static void __erst_record_id_cache_compact(void)
617 if (erst_record_id_cache
.refcount
)
620 entries
= erst_record_id_cache
.entries
;
621 for (i
= 0; i
< erst_record_id_cache
.len
; i
++) {
622 if (entries
[i
] == APEI_ERST_INVALID_RECORD_ID
)
625 entries
[wpos
] = entries
[i
];
628 erst_record_id_cache
.len
= wpos
;
631 void erst_get_record_id_end(void)
634 * erst_disable != 0 should be detected by invoker via the
635 * return value of erst_get_record_id_begin/next, so this
636 * function should not be called for erst_disable != 0.
638 BUG_ON(erst_disable
);
640 mutex_lock(&erst_record_id_cache
.lock
);
641 erst_record_id_cache
.refcount
--;
642 BUG_ON(erst_record_id_cache
.refcount
< 0);
643 __erst_record_id_cache_compact();
644 mutex_unlock(&erst_record_id_cache
.lock
);
646 EXPORT_SYMBOL_GPL(erst_get_record_id_end
);
648 static int __erst_write_to_storage(u64 offset
)
650 struct apei_exec_context ctx
;
655 timeout
= erst_get_timeout();
657 erst_exec_ctx_init(&ctx
);
658 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_BEGIN_WRITE
);
661 apei_exec_ctx_set_input(&ctx
, offset
);
662 rc
= apei_exec_run(&ctx
, ACPI_ERST_SET_RECORD_OFFSET
);
665 rc
= apei_exec_run(&ctx
, ACPI_ERST_EXECUTE_OPERATION
);
669 rc
= apei_exec_run(&ctx
, ACPI_ERST_CHECK_BUSY_STATUS
);
672 val
= apei_exec_ctx_get_output(&ctx
);
675 if (erst_timedout(&timeout
, SPIN_UNIT
))
678 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_COMMAND_STATUS
);
681 val
= apei_exec_ctx_get_output(&ctx
);
682 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_END
);
686 return erst_errno(val
);
689 static int __erst_read_from_storage(u64 record_id
, u64 offset
)
691 struct apei_exec_context ctx
;
696 timeout
= erst_get_timeout();
698 erst_exec_ctx_init(&ctx
);
699 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_BEGIN_READ
);
702 apei_exec_ctx_set_input(&ctx
, offset
);
703 rc
= apei_exec_run(&ctx
, ACPI_ERST_SET_RECORD_OFFSET
);
706 apei_exec_ctx_set_input(&ctx
, record_id
);
707 rc
= apei_exec_run(&ctx
, ACPI_ERST_SET_RECORD_ID
);
710 rc
= apei_exec_run(&ctx
, ACPI_ERST_EXECUTE_OPERATION
);
714 rc
= apei_exec_run(&ctx
, ACPI_ERST_CHECK_BUSY_STATUS
);
717 val
= apei_exec_ctx_get_output(&ctx
);
720 if (erst_timedout(&timeout
, SPIN_UNIT
))
723 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_COMMAND_STATUS
);
726 val
= apei_exec_ctx_get_output(&ctx
);
727 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_END
);
731 return erst_errno(val
);
734 static int __erst_clear_from_storage(u64 record_id
)
736 struct apei_exec_context ctx
;
741 timeout
= erst_get_timeout();
743 erst_exec_ctx_init(&ctx
);
744 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_BEGIN_CLEAR
);
747 apei_exec_ctx_set_input(&ctx
, record_id
);
748 rc
= apei_exec_run(&ctx
, ACPI_ERST_SET_RECORD_ID
);
751 rc
= apei_exec_run(&ctx
, ACPI_ERST_EXECUTE_OPERATION
);
755 rc
= apei_exec_run(&ctx
, ACPI_ERST_CHECK_BUSY_STATUS
);
758 val
= apei_exec_ctx_get_output(&ctx
);
761 if (erst_timedout(&timeout
, SPIN_UNIT
))
764 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_COMMAND_STATUS
);
767 val
= apei_exec_ctx_get_output(&ctx
);
768 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_END
);
772 return erst_errno(val
);
775 /* NVRAM ERST Error Log Address Range is not supported yet */
776 static void pr_unimpl_nvram(void)
778 if (printk_ratelimit())
779 pr_warn("NVRAM ERST Log Address Range not implemented yet.\n");
782 static int __erst_write_to_nvram(const struct cper_record_header
*record
)
784 /* do not print message, because printk is not safe for NMI */
788 static int __erst_read_to_erange_from_nvram(u64 record_id
, u64
*offset
)
794 static int __erst_clear_from_nvram(u64 record_id
)
800 int erst_write(const struct cper_record_header
*record
)
804 struct cper_record_header
*rcd_erange
;
809 if (memcmp(record
->signature
, CPER_SIG_RECORD
, CPER_SIG_SIZE
))
812 if (erst_erange
.attr
& ERST_RANGE_NVRAM
) {
813 if (!raw_spin_trylock_irqsave(&erst_lock
, flags
))
815 rc
= __erst_write_to_nvram(record
);
816 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
820 if (record
->record_length
> erst_erange
.size
)
823 if (!raw_spin_trylock_irqsave(&erst_lock
, flags
))
825 memcpy(erst_erange
.vaddr
, record
, record
->record_length
);
826 rcd_erange
= erst_erange
.vaddr
;
827 /* signature for serialization system */
828 memcpy(&rcd_erange
->persistence_information
, "ER", 2);
830 rc
= __erst_write_to_storage(0);
831 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
835 EXPORT_SYMBOL_GPL(erst_write
);
837 static int __erst_read_to_erange(u64 record_id
, u64
*offset
)
841 if (erst_erange
.attr
& ERST_RANGE_NVRAM
)
842 return __erst_read_to_erange_from_nvram(
845 rc
= __erst_read_from_storage(record_id
, 0);
853 static ssize_t
__erst_read(u64 record_id
, struct cper_record_header
*record
,
858 struct cper_record_header
*rcd_tmp
;
860 rc
= __erst_read_to_erange(record_id
, &offset
);
863 rcd_tmp
= erst_erange
.vaddr
+ offset
;
864 len
= rcd_tmp
->record_length
;
866 memcpy(record
, rcd_tmp
, len
);
872 * If return value > buflen, the buffer size is not big enough,
873 * else if return value < 0, something goes wrong,
874 * else everything is OK, and return value is record length
876 ssize_t
erst_read(u64 record_id
, struct cper_record_header
*record
,
885 raw_spin_lock_irqsave(&erst_lock
, flags
);
886 len
= __erst_read(record_id
, record
, buflen
);
887 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
890 EXPORT_SYMBOL_GPL(erst_read
);
892 static void erst_clear_cache(u64 record_id
)
897 mutex_lock(&erst_record_id_cache
.lock
);
899 entries
= erst_record_id_cache
.entries
;
900 for (i
= 0; i
< erst_record_id_cache
.len
; i
++) {
901 if (entries
[i
] == record_id
)
902 entries
[i
] = APEI_ERST_INVALID_RECORD_ID
;
904 __erst_record_id_cache_compact();
906 mutex_unlock(&erst_record_id_cache
.lock
);
909 ssize_t
erst_read_record(u64 record_id
, struct cper_record_header
*record
,
910 size_t buflen
, size_t recordlen
, const guid_t
*creatorid
)
915 * if creatorid is NULL, read any record for erst-dbg module
917 if (creatorid
== NULL
) {
918 len
= erst_read(record_id
, record
, buflen
);
920 erst_clear_cache(record_id
);
925 len
= erst_read(record_id
, record
, buflen
);
927 * if erst_read return value is -ENOENT skip to next record_id,
928 * and clear the record_id cache.
930 if (len
== -ENOENT
) {
931 erst_clear_cache(record_id
);
939 * if erst_read return value is less than record head length,
940 * consider it as -EIO, and clear the record_id cache.
942 if (len
< recordlen
) {
944 erst_clear_cache(record_id
);
949 * if creatorid is not wanted, consider it as not found,
950 * for skipping to next record_id.
952 if (!guid_equal(&record
->creator_id
, creatorid
))
958 EXPORT_SYMBOL_GPL(erst_read_record
);
960 int erst_clear(u64 record_id
)
969 rc
= mutex_lock_interruptible(&erst_record_id_cache
.lock
);
972 raw_spin_lock_irqsave(&erst_lock
, flags
);
973 if (erst_erange
.attr
& ERST_RANGE_NVRAM
)
974 rc
= __erst_clear_from_nvram(record_id
);
976 rc
= __erst_clear_from_storage(record_id
);
977 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
980 entries
= erst_record_id_cache
.entries
;
981 for (i
= 0; i
< erst_record_id_cache
.len
; i
++) {
982 if (entries
[i
] == record_id
)
983 entries
[i
] = APEI_ERST_INVALID_RECORD_ID
;
985 __erst_record_id_cache_compact();
987 mutex_unlock(&erst_record_id_cache
.lock
);
990 EXPORT_SYMBOL_GPL(erst_clear
);
992 static int __init
setup_erst_disable(char *str
)
998 __setup("erst_disable", setup_erst_disable
);
1000 static int erst_check_table(struct acpi_table_erst
*erst_tab
)
1002 if ((erst_tab
->header_length
!=
1003 (sizeof(struct acpi_table_erst
) - sizeof(erst_tab
->header
)))
1004 && (erst_tab
->header_length
!= sizeof(struct acpi_table_erst
)))
1006 if (erst_tab
->header
.length
< sizeof(struct acpi_table_erst
))
1008 if (erst_tab
->entries
!=
1009 (erst_tab
->header
.length
- sizeof(struct acpi_table_erst
)) /
1010 sizeof(struct acpi_erst_entry
))
1016 static int erst_open_pstore(struct pstore_info
*psi
);
1017 static int erst_close_pstore(struct pstore_info
*psi
);
1018 static ssize_t
erst_reader(struct pstore_record
*record
);
1019 static int erst_writer(struct pstore_record
*record
);
1020 static int erst_clearer(struct pstore_record
*record
);
1022 static struct pstore_info erst_info
= {
1023 .owner
= THIS_MODULE
,
1025 .flags
= PSTORE_FLAGS_DMESG
,
1026 .open
= erst_open_pstore
,
1027 .close
= erst_close_pstore
,
1028 .read
= erst_reader
,
1029 .write
= erst_writer
,
1030 .erase
= erst_clearer
1033 #define CPER_CREATOR_PSTORE \
1034 GUID_INIT(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
1035 0x64, 0x90, 0xb8, 0x9d)
1036 #define CPER_SECTION_TYPE_DMESG \
1037 GUID_INIT(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54, \
1038 0x94, 0x19, 0xeb, 0x12)
1039 #define CPER_SECTION_TYPE_DMESG_Z \
1040 GUID_INIT(0x4f118707, 0x04dd, 0x4055, 0xb5, 0xdd, 0x95, 0x6d, \
1041 0x34, 0xdd, 0xfa, 0xc6)
1042 #define CPER_SECTION_TYPE_MCE \
1043 GUID_INIT(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
1044 0x04, 0x4a, 0x38, 0xfc)
1046 struct cper_pstore_record
{
1047 struct cper_record_header hdr
;
1048 struct cper_section_descriptor sec_hdr
;
1052 static int reader_pos
;
1054 static int erst_open_pstore(struct pstore_info
*psi
)
1059 return erst_get_record_id_begin(&reader_pos
);
1062 static int erst_close_pstore(struct pstore_info
*psi
)
1064 erst_get_record_id_end();
1069 static ssize_t
erst_reader(struct pstore_record
*record
)
1074 struct cper_pstore_record
*rcd
;
1075 size_t rcd_len
= sizeof(*rcd
) + erst_info
.bufsize
;
1080 rcd
= kmalloc(rcd_len
, GFP_KERNEL
);
1086 rc
= erst_get_record_id_next(&reader_pos
, &record_id
);
1090 /* no more record */
1091 if (record_id
== APEI_ERST_INVALID_RECORD_ID
) {
1096 len
= erst_read_record(record_id
, &rcd
->hdr
, rcd_len
, sizeof(*rcd
),
1097 &CPER_CREATOR_PSTORE
);
1098 /* The record may be cleared by others, try read next record */
1104 record
->buf
= kmalloc(len
, GFP_KERNEL
);
1105 if (record
->buf
== NULL
) {
1109 memcpy(record
->buf
, rcd
->data
, len
- sizeof(*rcd
));
1110 record
->id
= record_id
;
1111 record
->compressed
= false;
1112 record
->ecc_notice_size
= 0;
1113 if (guid_equal(&rcd
->sec_hdr
.section_type
, &CPER_SECTION_TYPE_DMESG_Z
)) {
1114 record
->type
= PSTORE_TYPE_DMESG
;
1115 record
->compressed
= true;
1116 } else if (guid_equal(&rcd
->sec_hdr
.section_type
, &CPER_SECTION_TYPE_DMESG
))
1117 record
->type
= PSTORE_TYPE_DMESG
;
1118 else if (guid_equal(&rcd
->sec_hdr
.section_type
, &CPER_SECTION_TYPE_MCE
))
1119 record
->type
= PSTORE_TYPE_MCE
;
1121 record
->type
= PSTORE_TYPE_MAX
;
1123 if (rcd
->hdr
.validation_bits
& CPER_VALID_TIMESTAMP
)
1124 record
->time
.tv_sec
= rcd
->hdr
.timestamp
;
1126 record
->time
.tv_sec
= 0;
1127 record
->time
.tv_nsec
= 0;
1131 return (rc
< 0) ? rc
: (len
- sizeof(*rcd
));
1134 static int erst_writer(struct pstore_record
*record
)
1136 struct cper_pstore_record
*rcd
= (struct cper_pstore_record
*)
1137 (erst_info
.buf
- sizeof(*rcd
));
1140 memset(rcd
, 0, sizeof(*rcd
));
1141 memcpy(rcd
->hdr
.signature
, CPER_SIG_RECORD
, CPER_SIG_SIZE
);
1142 rcd
->hdr
.revision
= CPER_RECORD_REV
;
1143 rcd
->hdr
.signature_end
= CPER_SIG_END
;
1144 rcd
->hdr
.section_count
= 1;
1145 rcd
->hdr
.error_severity
= CPER_SEV_FATAL
;
1146 /* timestamp valid. platform_id, partition_id are invalid */
1147 rcd
->hdr
.validation_bits
= CPER_VALID_TIMESTAMP
;
1148 rcd
->hdr
.timestamp
= ktime_get_real_seconds();
1149 rcd
->hdr
.record_length
= sizeof(*rcd
) + record
->size
;
1150 rcd
->hdr
.creator_id
= CPER_CREATOR_PSTORE
;
1151 rcd
->hdr
.notification_type
= CPER_NOTIFY_MCE
;
1152 rcd
->hdr
.record_id
= cper_next_record_id();
1153 rcd
->hdr
.flags
= CPER_HW_ERROR_FLAGS_PREVERR
;
1155 rcd
->sec_hdr
.section_offset
= sizeof(*rcd
);
1156 rcd
->sec_hdr
.section_length
= record
->size
;
1157 rcd
->sec_hdr
.revision
= CPER_SEC_REV
;
1158 /* fru_id and fru_text is invalid */
1159 rcd
->sec_hdr
.validation_bits
= 0;
1160 rcd
->sec_hdr
.flags
= CPER_SEC_PRIMARY
;
1161 switch (record
->type
) {
1162 case PSTORE_TYPE_DMESG
:
1163 if (record
->compressed
)
1164 rcd
->sec_hdr
.section_type
= CPER_SECTION_TYPE_DMESG_Z
;
1166 rcd
->sec_hdr
.section_type
= CPER_SECTION_TYPE_DMESG
;
1168 case PSTORE_TYPE_MCE
:
1169 rcd
->sec_hdr
.section_type
= CPER_SECTION_TYPE_MCE
;
1174 rcd
->sec_hdr
.section_severity
= CPER_SEV_FATAL
;
1176 ret
= erst_write(&rcd
->hdr
);
1177 record
->id
= rcd
->hdr
.record_id
;
1182 static int erst_clearer(struct pstore_record
*record
)
1184 return erst_clear(record
->id
);
1187 static int __init
erst_init(void)
1191 struct apei_exec_context ctx
;
1192 struct apei_resources erst_resources
;
1201 "Error Record Serialization Table (ERST) support is disabled.\n");
1205 status
= acpi_get_table(ACPI_SIG_ERST
, 0,
1206 (struct acpi_table_header
**)&erst_tab
);
1207 if (status
== AE_NOT_FOUND
)
1209 else if (ACPI_FAILURE(status
)) {
1210 const char *msg
= acpi_format_exception(status
);
1211 pr_err("Failed to get table, %s\n", msg
);
1216 rc
= erst_check_table(erst_tab
);
1218 pr_err(FW_BUG
"ERST table is invalid.\n");
1219 goto err_put_erst_tab
;
1222 apei_resources_init(&erst_resources
);
1223 erst_exec_ctx_init(&ctx
);
1224 rc
= apei_exec_collect_resources(&ctx
, &erst_resources
);
1227 rc
= apei_resources_request(&erst_resources
, "APEI ERST");
1230 rc
= apei_exec_pre_map_gars(&ctx
);
1233 rc
= erst_get_erange(&erst_erange
);
1237 "The corresponding hardware device or firmware implementation "
1238 "is not available.\n");
1240 pr_err("Failed to get Error Log Address Range.\n");
1244 r
= request_mem_region(erst_erange
.base
, erst_erange
.size
, "APEI ERST");
1246 pr_err("Can not request [mem %#010llx-%#010llx] for ERST.\n",
1247 (unsigned long long)erst_erange
.base
,
1248 (unsigned long long)erst_erange
.base
+ erst_erange
.size
- 1);
1253 erst_erange
.vaddr
= ioremap_cache(erst_erange
.base
,
1255 if (!erst_erange
.vaddr
)
1256 goto err_release_erange
;
1259 "Error Record Serialization Table (ERST) support is initialized.\n");
1261 buf
= kmalloc(erst_erange
.size
, GFP_KERNEL
);
1263 erst_info
.buf
= buf
+ sizeof(struct cper_pstore_record
);
1264 erst_info
.bufsize
= erst_erange
.size
-
1265 sizeof(struct cper_pstore_record
);
1266 rc
= pstore_register(&erst_info
);
1270 "Could not register with persistent store.\n");
1271 erst_info
.buf
= NULL
;
1272 erst_info
.bufsize
= 0;
1277 "Failed to allocate %lld bytes for persistent store error log.\n",
1280 /* Cleanup ERST Resources */
1281 apei_resources_fini(&erst_resources
);
1286 release_mem_region(erst_erange
.base
, erst_erange
.size
);
1288 apei_exec_post_unmap_gars(&ctx
);
1290 apei_resources_release(&erst_resources
);
1292 apei_resources_fini(&erst_resources
);
1294 acpi_put_table((struct acpi_table_header
*)erst_tab
);
1300 device_initcall(erst_init
);