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 atrributes */
58 #define ERST_RANGE_RESERVED 0x0001
59 #define ERST_RANGE_NVRAM 0x0002
60 #define ERST_RANGE_SLOW 0x0004
63 * ERST Error Log Address Range, used as buffer for reading/writing
66 static struct erst_erange
{
74 * Prevent ERST interpreter to run simultaneously, because the
75 * corresponding firmware implementation may not work properly when
76 * invoked simultaneously.
78 * It is used to provide exclusive accessing for ERST Error Log
81 static DEFINE_RAW_SPINLOCK(erst_lock
);
83 static inline int erst_errno(int command_status
)
85 switch (command_status
) {
86 case ERST_STATUS_SUCCESS
:
88 case ERST_STATUS_HARDWARE_NOT_AVAILABLE
:
90 case ERST_STATUS_NOT_ENOUGH_SPACE
:
92 case ERST_STATUS_RECORD_STORE_EMPTY
:
93 case ERST_STATUS_RECORD_NOT_FOUND
:
100 static int erst_timedout(u64
*t
, u64 spin_unit
)
102 if ((s64
)*t
< spin_unit
) {
103 pr_warn(FW_WARN
"Firmware does not respond in time.\n");
108 touch_nmi_watchdog();
112 static int erst_exec_load_var1(struct apei_exec_context
*ctx
,
113 struct acpi_whea_header
*entry
)
115 return __apei_exec_read_register(entry
, &ctx
->var1
);
118 static int erst_exec_load_var2(struct apei_exec_context
*ctx
,
119 struct acpi_whea_header
*entry
)
121 return __apei_exec_read_register(entry
, &ctx
->var2
);
124 static int erst_exec_store_var1(struct apei_exec_context
*ctx
,
125 struct acpi_whea_header
*entry
)
127 return __apei_exec_write_register(entry
, ctx
->var1
);
130 static int erst_exec_add(struct apei_exec_context
*ctx
,
131 struct acpi_whea_header
*entry
)
133 ctx
->var1
+= ctx
->var2
;
137 static int erst_exec_subtract(struct apei_exec_context
*ctx
,
138 struct acpi_whea_header
*entry
)
140 ctx
->var1
-= ctx
->var2
;
144 static int erst_exec_add_value(struct apei_exec_context
*ctx
,
145 struct acpi_whea_header
*entry
)
150 rc
= __apei_exec_read_register(entry
, &val
);
154 rc
= __apei_exec_write_register(entry
, val
);
158 static int erst_exec_subtract_value(struct apei_exec_context
*ctx
,
159 struct acpi_whea_header
*entry
)
164 rc
= __apei_exec_read_register(entry
, &val
);
168 rc
= __apei_exec_write_register(entry
, val
);
172 static int erst_exec_stall(struct apei_exec_context
*ctx
,
173 struct acpi_whea_header
*entry
)
177 if (ctx
->value
> FIRMWARE_MAX_STALL
) {
180 "Too long stall time for stall instruction: 0x%llx.\n",
182 stall_time
= FIRMWARE_MAX_STALL
;
184 stall_time
= ctx
->value
;
189 static int erst_exec_stall_while_true(struct apei_exec_context
*ctx
,
190 struct acpi_whea_header
*entry
)
194 u64 timeout
= FIRMWARE_TIMEOUT
;
197 if (ctx
->var1
> FIRMWARE_MAX_STALL
) {
200 "Too long stall time for stall while true instruction: 0x%llx.\n",
202 stall_time
= FIRMWARE_MAX_STALL
;
204 stall_time
= ctx
->var1
;
207 rc
= __apei_exec_read_register(entry
, &val
);
210 if (val
!= ctx
->value
)
212 if (erst_timedout(&timeout
, stall_time
* NSEC_PER_USEC
))
218 static int erst_exec_skip_next_instruction_if_true(
219 struct apei_exec_context
*ctx
,
220 struct acpi_whea_header
*entry
)
225 rc
= __apei_exec_read_register(entry
, &val
);
228 if (val
== ctx
->value
) {
230 return APEI_EXEC_SET_IP
;
236 static int erst_exec_goto(struct apei_exec_context
*ctx
,
237 struct acpi_whea_header
*entry
)
239 ctx
->ip
= ctx
->value
;
240 return APEI_EXEC_SET_IP
;
243 static int erst_exec_set_src_address_base(struct apei_exec_context
*ctx
,
244 struct acpi_whea_header
*entry
)
246 return __apei_exec_read_register(entry
, &ctx
->src_base
);
249 static int erst_exec_set_dst_address_base(struct apei_exec_context
*ctx
,
250 struct acpi_whea_header
*entry
)
252 return __apei_exec_read_register(entry
, &ctx
->dst_base
);
255 static int erst_exec_move_data(struct apei_exec_context
*ctx
,
256 struct acpi_whea_header
*entry
)
262 /* ioremap does not work in interrupt context */
263 if (in_interrupt()) {
264 pr_warn("MOVE_DATA can not be used in interrupt context.\n");
268 rc
= __apei_exec_read_register(entry
, &offset
);
272 src
= ioremap(ctx
->src_base
+ offset
, ctx
->var2
);
275 dst
= ioremap(ctx
->dst_base
+ offset
, ctx
->var2
);
281 memmove(dst
, src
, ctx
->var2
);
289 static struct apei_exec_ins_type erst_ins_type
[] = {
290 [ACPI_ERST_READ_REGISTER
] = {
291 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
292 .run
= apei_exec_read_register
,
294 [ACPI_ERST_READ_REGISTER_VALUE
] = {
295 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
296 .run
= apei_exec_read_register_value
,
298 [ACPI_ERST_WRITE_REGISTER
] = {
299 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
300 .run
= apei_exec_write_register
,
302 [ACPI_ERST_WRITE_REGISTER_VALUE
] = {
303 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
304 .run
= apei_exec_write_register_value
,
308 .run
= apei_exec_noop
,
310 [ACPI_ERST_LOAD_VAR1
] = {
311 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
312 .run
= erst_exec_load_var1
,
314 [ACPI_ERST_LOAD_VAR2
] = {
315 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
316 .run
= erst_exec_load_var2
,
318 [ACPI_ERST_STORE_VAR1
] = {
319 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
320 .run
= erst_exec_store_var1
,
324 .run
= erst_exec_add
,
326 [ACPI_ERST_SUBTRACT
] = {
328 .run
= erst_exec_subtract
,
330 [ACPI_ERST_ADD_VALUE
] = {
331 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
332 .run
= erst_exec_add_value
,
334 [ACPI_ERST_SUBTRACT_VALUE
] = {
335 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
336 .run
= erst_exec_subtract_value
,
338 [ACPI_ERST_STALL
] = {
340 .run
= erst_exec_stall
,
342 [ACPI_ERST_STALL_WHILE_TRUE
] = {
343 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
344 .run
= erst_exec_stall_while_true
,
346 [ACPI_ERST_SKIP_NEXT_IF_TRUE
] = {
347 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
348 .run
= erst_exec_skip_next_instruction_if_true
,
352 .run
= erst_exec_goto
,
354 [ACPI_ERST_SET_SRC_ADDRESS_BASE
] = {
355 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
356 .run
= erst_exec_set_src_address_base
,
358 [ACPI_ERST_SET_DST_ADDRESS_BASE
] = {
359 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
360 .run
= erst_exec_set_dst_address_base
,
362 [ACPI_ERST_MOVE_DATA
] = {
363 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
364 .run
= erst_exec_move_data
,
368 static inline void erst_exec_ctx_init(struct apei_exec_context
*ctx
)
370 apei_exec_ctx_init(ctx
, erst_ins_type
, ARRAY_SIZE(erst_ins_type
),
371 ERST_TAB_ENTRY(erst_tab
), erst_tab
->entries
);
374 static int erst_get_erange(struct erst_erange
*range
)
376 struct apei_exec_context ctx
;
379 erst_exec_ctx_init(&ctx
);
380 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_ERROR_RANGE
);
383 range
->base
= apei_exec_ctx_get_output(&ctx
);
384 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_ERROR_LENGTH
);
387 range
->size
= apei_exec_ctx_get_output(&ctx
);
388 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_ERROR_ATTRIBUTES
);
391 range
->attr
= apei_exec_ctx_get_output(&ctx
);
396 static ssize_t
__erst_get_record_count(void)
398 struct apei_exec_context ctx
;
401 erst_exec_ctx_init(&ctx
);
402 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_RECORD_COUNT
);
405 return apei_exec_ctx_get_output(&ctx
);
408 ssize_t
erst_get_record_count(void)
416 raw_spin_lock_irqsave(&erst_lock
, flags
);
417 count
= __erst_get_record_count();
418 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
422 EXPORT_SYMBOL_GPL(erst_get_record_count
);
424 #define ERST_RECORD_ID_CACHE_SIZE_MIN 16
425 #define ERST_RECORD_ID_CACHE_SIZE_MAX 1024
427 struct erst_record_id_cache
{
435 static struct erst_record_id_cache erst_record_id_cache
= {
436 .lock
= __MUTEX_INITIALIZER(erst_record_id_cache
.lock
),
440 static int __erst_get_next_record_id(u64
*record_id
)
442 struct apei_exec_context ctx
;
445 erst_exec_ctx_init(&ctx
);
446 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_RECORD_ID
);
449 *record_id
= apei_exec_ctx_get_output(&ctx
);
454 int erst_get_record_id_begin(int *pos
)
461 rc
= mutex_lock_interruptible(&erst_record_id_cache
.lock
);
464 erst_record_id_cache
.refcount
++;
465 mutex_unlock(&erst_record_id_cache
.lock
);
471 EXPORT_SYMBOL_GPL(erst_get_record_id_begin
);
473 /* erst_record_id_cache.lock must be held by caller */
474 static int __erst_record_id_cache_add_one(void)
476 u64 id
, prev_id
, first_id
;
481 id
= prev_id
= first_id
= APEI_ERST_INVALID_RECORD_ID
;
483 raw_spin_lock_irqsave(&erst_lock
, flags
);
484 rc
= __erst_get_next_record_id(&id
);
485 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
490 if (id
== APEI_ERST_INVALID_RECORD_ID
)
492 /* can not skip current ID, or loop back to first ID */
493 if (id
== prev_id
|| id
== first_id
)
495 if (first_id
== APEI_ERST_INVALID_RECORD_ID
)
499 entries
= erst_record_id_cache
.entries
;
500 for (i
= 0; i
< erst_record_id_cache
.len
; i
++) {
501 if (entries
[i
] == id
)
504 /* record id already in cache, try next */
505 if (i
< erst_record_id_cache
.len
)
507 if (erst_record_id_cache
.len
>= erst_record_id_cache
.size
) {
511 new_size
= erst_record_id_cache
.size
* 2;
512 new_size
= clamp_val(new_size
, ERST_RECORD_ID_CACHE_SIZE_MIN
,
513 ERST_RECORD_ID_CACHE_SIZE_MAX
);
514 if (new_size
<= erst_record_id_cache
.size
) {
515 if (printk_ratelimit())
516 pr_warn(FW_WARN
"too many record IDs!\n");
519 new_entries
= kvmalloc_array(new_size
, sizeof(entries
[0]),
523 memcpy(new_entries
, entries
,
524 erst_record_id_cache
.len
* sizeof(entries
[0]));
526 erst_record_id_cache
.entries
= entries
= new_entries
;
527 erst_record_id_cache
.size
= new_size
;
530 erst_record_id_cache
.len
++;
536 * Get the record ID of an existing error record on the persistent
537 * storage. If there is no error record on the persistent storage, the
538 * returned record_id is APEI_ERST_INVALID_RECORD_ID.
540 int erst_get_record_id_next(int *pos
, u64
*record_id
)
548 /* must be enclosed by erst_get_record_id_begin/end */
549 BUG_ON(!erst_record_id_cache
.refcount
);
550 BUG_ON(*pos
< 0 || *pos
> erst_record_id_cache
.len
);
552 mutex_lock(&erst_record_id_cache
.lock
);
553 entries
= erst_record_id_cache
.entries
;
554 for (; *pos
< erst_record_id_cache
.len
; (*pos
)++)
555 if (entries
[*pos
] != APEI_ERST_INVALID_RECORD_ID
)
557 /* found next record id in cache */
558 if (*pos
< erst_record_id_cache
.len
) {
559 *record_id
= entries
[*pos
];
564 /* Try to add one more record ID to cache */
565 rc
= __erst_record_id_cache_add_one();
568 /* successfully add one new ID */
570 *record_id
= erst_record_id_cache
.entries
[*pos
];
575 *record_id
= APEI_ERST_INVALID_RECORD_ID
;
578 mutex_unlock(&erst_record_id_cache
.lock
);
582 EXPORT_SYMBOL_GPL(erst_get_record_id_next
);
584 /* erst_record_id_cache.lock must be held by caller */
585 static void __erst_record_id_cache_compact(void)
590 if (erst_record_id_cache
.refcount
)
593 entries
= erst_record_id_cache
.entries
;
594 for (i
= 0; i
< erst_record_id_cache
.len
; i
++) {
595 if (entries
[i
] == APEI_ERST_INVALID_RECORD_ID
)
598 entries
[wpos
] = entries
[i
];
601 erst_record_id_cache
.len
= wpos
;
604 void erst_get_record_id_end(void)
607 * erst_disable != 0 should be detected by invoker via the
608 * return value of erst_get_record_id_begin/next, so this
609 * function should not be called for erst_disable != 0.
611 BUG_ON(erst_disable
);
613 mutex_lock(&erst_record_id_cache
.lock
);
614 erst_record_id_cache
.refcount
--;
615 BUG_ON(erst_record_id_cache
.refcount
< 0);
616 __erst_record_id_cache_compact();
617 mutex_unlock(&erst_record_id_cache
.lock
);
619 EXPORT_SYMBOL_GPL(erst_get_record_id_end
);
621 static int __erst_write_to_storage(u64 offset
)
623 struct apei_exec_context ctx
;
624 u64 timeout
= FIRMWARE_TIMEOUT
;
628 erst_exec_ctx_init(&ctx
);
629 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_BEGIN_WRITE
);
632 apei_exec_ctx_set_input(&ctx
, offset
);
633 rc
= apei_exec_run(&ctx
, ACPI_ERST_SET_RECORD_OFFSET
);
636 rc
= apei_exec_run(&ctx
, ACPI_ERST_EXECUTE_OPERATION
);
640 rc
= apei_exec_run(&ctx
, ACPI_ERST_CHECK_BUSY_STATUS
);
643 val
= apei_exec_ctx_get_output(&ctx
);
646 if (erst_timedout(&timeout
, SPIN_UNIT
))
649 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_COMMAND_STATUS
);
652 val
= apei_exec_ctx_get_output(&ctx
);
653 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_END
);
657 return erst_errno(val
);
660 static int __erst_read_from_storage(u64 record_id
, u64 offset
)
662 struct apei_exec_context ctx
;
663 u64 timeout
= FIRMWARE_TIMEOUT
;
667 erst_exec_ctx_init(&ctx
);
668 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_BEGIN_READ
);
671 apei_exec_ctx_set_input(&ctx
, offset
);
672 rc
= apei_exec_run(&ctx
, ACPI_ERST_SET_RECORD_OFFSET
);
675 apei_exec_ctx_set_input(&ctx
, record_id
);
676 rc
= apei_exec_run(&ctx
, ACPI_ERST_SET_RECORD_ID
);
679 rc
= apei_exec_run(&ctx
, ACPI_ERST_EXECUTE_OPERATION
);
683 rc
= apei_exec_run(&ctx
, ACPI_ERST_CHECK_BUSY_STATUS
);
686 val
= apei_exec_ctx_get_output(&ctx
);
689 if (erst_timedout(&timeout
, SPIN_UNIT
))
692 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_COMMAND_STATUS
);
695 val
= apei_exec_ctx_get_output(&ctx
);
696 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_END
);
700 return erst_errno(val
);
703 static int __erst_clear_from_storage(u64 record_id
)
705 struct apei_exec_context ctx
;
706 u64 timeout
= FIRMWARE_TIMEOUT
;
710 erst_exec_ctx_init(&ctx
);
711 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_BEGIN_CLEAR
);
714 apei_exec_ctx_set_input(&ctx
, record_id
);
715 rc
= apei_exec_run(&ctx
, ACPI_ERST_SET_RECORD_ID
);
718 rc
= apei_exec_run(&ctx
, ACPI_ERST_EXECUTE_OPERATION
);
722 rc
= apei_exec_run(&ctx
, ACPI_ERST_CHECK_BUSY_STATUS
);
725 val
= apei_exec_ctx_get_output(&ctx
);
728 if (erst_timedout(&timeout
, SPIN_UNIT
))
731 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_COMMAND_STATUS
);
734 val
= apei_exec_ctx_get_output(&ctx
);
735 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_END
);
739 return erst_errno(val
);
742 /* NVRAM ERST Error Log Address Range is not supported yet */
743 static void pr_unimpl_nvram(void)
745 if (printk_ratelimit())
746 pr_warn("NVRAM ERST Log Address Range not implemented yet.\n");
749 static int __erst_write_to_nvram(const struct cper_record_header
*record
)
751 /* do not print message, because printk is not safe for NMI */
755 static int __erst_read_to_erange_from_nvram(u64 record_id
, u64
*offset
)
761 static int __erst_clear_from_nvram(u64 record_id
)
767 int erst_write(const struct cper_record_header
*record
)
771 struct cper_record_header
*rcd_erange
;
776 if (memcmp(record
->signature
, CPER_SIG_RECORD
, CPER_SIG_SIZE
))
779 if (erst_erange
.attr
& ERST_RANGE_NVRAM
) {
780 if (!raw_spin_trylock_irqsave(&erst_lock
, flags
))
782 rc
= __erst_write_to_nvram(record
);
783 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
787 if (record
->record_length
> erst_erange
.size
)
790 if (!raw_spin_trylock_irqsave(&erst_lock
, flags
))
792 memcpy(erst_erange
.vaddr
, record
, record
->record_length
);
793 rcd_erange
= erst_erange
.vaddr
;
794 /* signature for serialization system */
795 memcpy(&rcd_erange
->persistence_information
, "ER", 2);
797 rc
= __erst_write_to_storage(0);
798 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
802 EXPORT_SYMBOL_GPL(erst_write
);
804 static int __erst_read_to_erange(u64 record_id
, u64
*offset
)
808 if (erst_erange
.attr
& ERST_RANGE_NVRAM
)
809 return __erst_read_to_erange_from_nvram(
812 rc
= __erst_read_from_storage(record_id
, 0);
820 static ssize_t
__erst_read(u64 record_id
, struct cper_record_header
*record
,
825 struct cper_record_header
*rcd_tmp
;
827 rc
= __erst_read_to_erange(record_id
, &offset
);
830 rcd_tmp
= erst_erange
.vaddr
+ offset
;
831 len
= rcd_tmp
->record_length
;
833 memcpy(record
, rcd_tmp
, len
);
839 * If return value > buflen, the buffer size is not big enough,
840 * else if return value < 0, something goes wrong,
841 * else everything is OK, and return value is record length
843 ssize_t
erst_read(u64 record_id
, struct cper_record_header
*record
,
852 raw_spin_lock_irqsave(&erst_lock
, flags
);
853 len
= __erst_read(record_id
, record
, buflen
);
854 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
857 EXPORT_SYMBOL_GPL(erst_read
);
859 int erst_clear(u64 record_id
)
868 rc
= mutex_lock_interruptible(&erst_record_id_cache
.lock
);
871 raw_spin_lock_irqsave(&erst_lock
, flags
);
872 if (erst_erange
.attr
& ERST_RANGE_NVRAM
)
873 rc
= __erst_clear_from_nvram(record_id
);
875 rc
= __erst_clear_from_storage(record_id
);
876 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
879 entries
= erst_record_id_cache
.entries
;
880 for (i
= 0; i
< erst_record_id_cache
.len
; i
++) {
881 if (entries
[i
] == record_id
)
882 entries
[i
] = APEI_ERST_INVALID_RECORD_ID
;
884 __erst_record_id_cache_compact();
886 mutex_unlock(&erst_record_id_cache
.lock
);
889 EXPORT_SYMBOL_GPL(erst_clear
);
891 static int __init
setup_erst_disable(char *str
)
897 __setup("erst_disable", setup_erst_disable
);
899 static int erst_check_table(struct acpi_table_erst
*erst_tab
)
901 if ((erst_tab
->header_length
!=
902 (sizeof(struct acpi_table_erst
) - sizeof(erst_tab
->header
)))
903 && (erst_tab
->header_length
!= sizeof(struct acpi_table_erst
)))
905 if (erst_tab
->header
.length
< sizeof(struct acpi_table_erst
))
907 if (erst_tab
->entries
!=
908 (erst_tab
->header
.length
- sizeof(struct acpi_table_erst
)) /
909 sizeof(struct acpi_erst_entry
))
915 static int erst_open_pstore(struct pstore_info
*psi
);
916 static int erst_close_pstore(struct pstore_info
*psi
);
917 static ssize_t
erst_reader(struct pstore_record
*record
);
918 static int erst_writer(struct pstore_record
*record
);
919 static int erst_clearer(struct pstore_record
*record
);
921 static struct pstore_info erst_info
= {
922 .owner
= THIS_MODULE
,
924 .flags
= PSTORE_FLAGS_DMESG
,
925 .open
= erst_open_pstore
,
926 .close
= erst_close_pstore
,
928 .write
= erst_writer
,
929 .erase
= erst_clearer
932 #define CPER_CREATOR_PSTORE \
933 GUID_INIT(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
934 0x64, 0x90, 0xb8, 0x9d)
935 #define CPER_SECTION_TYPE_DMESG \
936 GUID_INIT(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54, \
937 0x94, 0x19, 0xeb, 0x12)
938 #define CPER_SECTION_TYPE_DMESG_Z \
939 GUID_INIT(0x4f118707, 0x04dd, 0x4055, 0xb5, 0xdd, 0x95, 0x6d, \
940 0x34, 0xdd, 0xfa, 0xc6)
941 #define CPER_SECTION_TYPE_MCE \
942 GUID_INIT(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
943 0x04, 0x4a, 0x38, 0xfc)
945 struct cper_pstore_record
{
946 struct cper_record_header hdr
;
947 struct cper_section_descriptor sec_hdr
;
951 static int reader_pos
;
953 static int erst_open_pstore(struct pstore_info
*psi
)
960 rc
= erst_get_record_id_begin(&reader_pos
);
965 static int erst_close_pstore(struct pstore_info
*psi
)
967 erst_get_record_id_end();
972 static ssize_t
erst_reader(struct pstore_record
*record
)
977 struct cper_pstore_record
*rcd
;
978 size_t rcd_len
= sizeof(*rcd
) + erst_info
.bufsize
;
983 rcd
= kmalloc(rcd_len
, GFP_KERNEL
);
989 rc
= erst_get_record_id_next(&reader_pos
, &record_id
);
994 if (record_id
== APEI_ERST_INVALID_RECORD_ID
) {
999 len
= erst_read(record_id
, &rcd
->hdr
, rcd_len
);
1000 /* The record may be cleared by others, try read next record */
1003 else if (len
< 0 || len
< sizeof(*rcd
)) {
1007 if (!guid_equal(&rcd
->hdr
.creator_id
, &CPER_CREATOR_PSTORE
))
1010 record
->buf
= kmalloc(len
, GFP_KERNEL
);
1011 if (record
->buf
== NULL
) {
1015 memcpy(record
->buf
, rcd
->data
, len
- sizeof(*rcd
));
1016 record
->id
= record_id
;
1017 record
->compressed
= false;
1018 record
->ecc_notice_size
= 0;
1019 if (guid_equal(&rcd
->sec_hdr
.section_type
, &CPER_SECTION_TYPE_DMESG_Z
)) {
1020 record
->type
= PSTORE_TYPE_DMESG
;
1021 record
->compressed
= true;
1022 } else if (guid_equal(&rcd
->sec_hdr
.section_type
, &CPER_SECTION_TYPE_DMESG
))
1023 record
->type
= PSTORE_TYPE_DMESG
;
1024 else if (guid_equal(&rcd
->sec_hdr
.section_type
, &CPER_SECTION_TYPE_MCE
))
1025 record
->type
= PSTORE_TYPE_MCE
;
1027 record
->type
= PSTORE_TYPE_MAX
;
1029 if (rcd
->hdr
.validation_bits
& CPER_VALID_TIMESTAMP
)
1030 record
->time
.tv_sec
= rcd
->hdr
.timestamp
;
1032 record
->time
.tv_sec
= 0;
1033 record
->time
.tv_nsec
= 0;
1037 return (rc
< 0) ? rc
: (len
- sizeof(*rcd
));
1040 static int erst_writer(struct pstore_record
*record
)
1042 struct cper_pstore_record
*rcd
= (struct cper_pstore_record
*)
1043 (erst_info
.buf
- sizeof(*rcd
));
1046 memset(rcd
, 0, sizeof(*rcd
));
1047 memcpy(rcd
->hdr
.signature
, CPER_SIG_RECORD
, CPER_SIG_SIZE
);
1048 rcd
->hdr
.revision
= CPER_RECORD_REV
;
1049 rcd
->hdr
.signature_end
= CPER_SIG_END
;
1050 rcd
->hdr
.section_count
= 1;
1051 rcd
->hdr
.error_severity
= CPER_SEV_FATAL
;
1052 /* timestamp valid. platform_id, partition_id are invalid */
1053 rcd
->hdr
.validation_bits
= CPER_VALID_TIMESTAMP
;
1054 rcd
->hdr
.timestamp
= ktime_get_real_seconds();
1055 rcd
->hdr
.record_length
= sizeof(*rcd
) + record
->size
;
1056 rcd
->hdr
.creator_id
= CPER_CREATOR_PSTORE
;
1057 rcd
->hdr
.notification_type
= CPER_NOTIFY_MCE
;
1058 rcd
->hdr
.record_id
= cper_next_record_id();
1059 rcd
->hdr
.flags
= CPER_HW_ERROR_FLAGS_PREVERR
;
1061 rcd
->sec_hdr
.section_offset
= sizeof(*rcd
);
1062 rcd
->sec_hdr
.section_length
= record
->size
;
1063 rcd
->sec_hdr
.revision
= CPER_SEC_REV
;
1064 /* fru_id and fru_text is invalid */
1065 rcd
->sec_hdr
.validation_bits
= 0;
1066 rcd
->sec_hdr
.flags
= CPER_SEC_PRIMARY
;
1067 switch (record
->type
) {
1068 case PSTORE_TYPE_DMESG
:
1069 if (record
->compressed
)
1070 rcd
->sec_hdr
.section_type
= CPER_SECTION_TYPE_DMESG_Z
;
1072 rcd
->sec_hdr
.section_type
= CPER_SECTION_TYPE_DMESG
;
1074 case PSTORE_TYPE_MCE
:
1075 rcd
->sec_hdr
.section_type
= CPER_SECTION_TYPE_MCE
;
1080 rcd
->sec_hdr
.section_severity
= CPER_SEV_FATAL
;
1082 ret
= erst_write(&rcd
->hdr
);
1083 record
->id
= rcd
->hdr
.record_id
;
1088 static int erst_clearer(struct pstore_record
*record
)
1090 return erst_clear(record
->id
);
1093 static int __init
erst_init(void)
1097 struct apei_exec_context ctx
;
1098 struct apei_resources erst_resources
;
1107 "Error Record Serialization Table (ERST) support is disabled.\n");
1111 status
= acpi_get_table(ACPI_SIG_ERST
, 0,
1112 (struct acpi_table_header
**)&erst_tab
);
1113 if (status
== AE_NOT_FOUND
)
1115 else if (ACPI_FAILURE(status
)) {
1116 const char *msg
= acpi_format_exception(status
);
1117 pr_err("Failed to get table, %s\n", msg
);
1122 rc
= erst_check_table(erst_tab
);
1124 pr_err(FW_BUG
"ERST table is invalid.\n");
1125 goto err_put_erst_tab
;
1128 apei_resources_init(&erst_resources
);
1129 erst_exec_ctx_init(&ctx
);
1130 rc
= apei_exec_collect_resources(&ctx
, &erst_resources
);
1133 rc
= apei_resources_request(&erst_resources
, "APEI ERST");
1136 rc
= apei_exec_pre_map_gars(&ctx
);
1139 rc
= erst_get_erange(&erst_erange
);
1143 "The corresponding hardware device or firmware implementation "
1144 "is not available.\n");
1146 pr_err("Failed to get Error Log Address Range.\n");
1150 r
= request_mem_region(erst_erange
.base
, erst_erange
.size
, "APEI ERST");
1152 pr_err("Can not request [mem %#010llx-%#010llx] for ERST.\n",
1153 (unsigned long long)erst_erange
.base
,
1154 (unsigned long long)erst_erange
.base
+ erst_erange
.size
- 1);
1159 erst_erange
.vaddr
= ioremap_cache(erst_erange
.base
,
1161 if (!erst_erange
.vaddr
)
1162 goto err_release_erange
;
1165 "Error Record Serialization Table (ERST) support is initialized.\n");
1167 buf
= kmalloc(erst_erange
.size
, GFP_KERNEL
);
1169 erst_info
.buf
= buf
+ sizeof(struct cper_pstore_record
);
1170 erst_info
.bufsize
= erst_erange
.size
-
1171 sizeof(struct cper_pstore_record
);
1172 rc
= pstore_register(&erst_info
);
1176 "Could not register with persistent store.\n");
1177 erst_info
.buf
= NULL
;
1178 erst_info
.bufsize
= 0;
1183 "Failed to allocate %lld bytes for persistent store error log.\n",
1186 /* Cleanup ERST Resources */
1187 apei_resources_fini(&erst_resources
);
1192 release_mem_region(erst_erange
.base
, erst_erange
.size
);
1194 apei_exec_post_unmap_gars(&ctx
);
1196 apei_resources_release(&erst_resources
);
1198 apei_resources_fini(&erst_resources
);
1200 acpi_put_table((struct acpi_table_header
*)erst_tab
);
1206 device_initcall(erst_init
);