2 * APEI Error Record Serialization Table support
4 * ERST is a way provided by APEI to save and retrieve hardware error
5 * information to and from a persistent store.
7 * For more information about ERST, please refer to ACPI Specification
8 * version 4.0, section 17.4.
10 * Copyright 2010 Intel Corp.
11 * Author: Huang Ying <ying.huang@intel.com>
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.
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.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
28 #include <linux/acpi.h>
29 #include <linux/uaccess.h>
30 #include <linux/cper.h>
31 #include <linux/nmi.h>
32 #include <linux/hardirq.h>
33 #include <linux/pstore.h>
34 #include <linux/vmalloc.h>
35 #include <linux/mm.h> /* kvfree() */
36 #include <acpi/apei.h>
38 #include "apei-internal.h"
41 #define pr_fmt(fmt) "ERST: " fmt
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
51 #define ERST_TAB_ENTRY(tab) \
52 ((struct acpi_whea_header *)((char *)(tab) + \
53 sizeof(struct acpi_table_erst)))
55 #define SPIN_UNIT 100 /* 100ns */
56 /* Firmware should respond within 1 milliseconds */
57 #define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
58 #define FIRMWARE_MAX_STALL 50 /* 50us */
61 EXPORT_SYMBOL_GPL(erst_disable
);
63 static struct acpi_table_erst
*erst_tab
;
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
71 * ERST Error Log Address Range, used as buffer for reading/writing
74 static struct erst_erange
{
82 * Prevent ERST interpreter to run simultaneously, because the
83 * corresponding firmware implementation may not work properly when
84 * invoked simultaneously.
86 * It is used to provide exclusive accessing for ERST Error Log
89 static DEFINE_RAW_SPINLOCK(erst_lock
);
91 static inline int erst_errno(int command_status
)
93 switch (command_status
) {
94 case ERST_STATUS_SUCCESS
:
96 case ERST_STATUS_HARDWARE_NOT_AVAILABLE
:
98 case ERST_STATUS_NOT_ENOUGH_SPACE
:
100 case ERST_STATUS_RECORD_STORE_EMPTY
:
101 case ERST_STATUS_RECORD_NOT_FOUND
:
108 static int erst_timedout(u64
*t
, u64 spin_unit
)
110 if ((s64
)*t
< spin_unit
) {
111 pr_warn(FW_WARN
"Firmware does not respond in time.\n");
116 touch_nmi_watchdog();
120 static int erst_exec_load_var1(struct apei_exec_context
*ctx
,
121 struct acpi_whea_header
*entry
)
123 return __apei_exec_read_register(entry
, &ctx
->var1
);
126 static int erst_exec_load_var2(struct apei_exec_context
*ctx
,
127 struct acpi_whea_header
*entry
)
129 return __apei_exec_read_register(entry
, &ctx
->var2
);
132 static int erst_exec_store_var1(struct apei_exec_context
*ctx
,
133 struct acpi_whea_header
*entry
)
135 return __apei_exec_write_register(entry
, ctx
->var1
);
138 static int erst_exec_add(struct apei_exec_context
*ctx
,
139 struct acpi_whea_header
*entry
)
141 ctx
->var1
+= ctx
->var2
;
145 static int erst_exec_subtract(struct apei_exec_context
*ctx
,
146 struct acpi_whea_header
*entry
)
148 ctx
->var1
-= ctx
->var2
;
152 static int erst_exec_add_value(struct apei_exec_context
*ctx
,
153 struct acpi_whea_header
*entry
)
158 rc
= __apei_exec_read_register(entry
, &val
);
162 rc
= __apei_exec_write_register(entry
, val
);
166 static int erst_exec_subtract_value(struct apei_exec_context
*ctx
,
167 struct acpi_whea_header
*entry
)
172 rc
= __apei_exec_read_register(entry
, &val
);
176 rc
= __apei_exec_write_register(entry
, val
);
180 static int erst_exec_stall(struct apei_exec_context
*ctx
,
181 struct acpi_whea_header
*entry
)
185 if (ctx
->value
> FIRMWARE_MAX_STALL
) {
188 "Too long stall time for stall instruction: 0x%llx.\n",
190 stall_time
= FIRMWARE_MAX_STALL
;
192 stall_time
= ctx
->value
;
197 static int erst_exec_stall_while_true(struct apei_exec_context
*ctx
,
198 struct acpi_whea_header
*entry
)
202 u64 timeout
= FIRMWARE_TIMEOUT
;
205 if (ctx
->var1
> FIRMWARE_MAX_STALL
) {
208 "Too long stall time for stall while true instruction: 0x%llx.\n",
210 stall_time
= FIRMWARE_MAX_STALL
;
212 stall_time
= ctx
->var1
;
215 rc
= __apei_exec_read_register(entry
, &val
);
218 if (val
!= ctx
->value
)
220 if (erst_timedout(&timeout
, stall_time
* NSEC_PER_USEC
))
226 static int erst_exec_skip_next_instruction_if_true(
227 struct apei_exec_context
*ctx
,
228 struct acpi_whea_header
*entry
)
233 rc
= __apei_exec_read_register(entry
, &val
);
236 if (val
== ctx
->value
) {
238 return APEI_EXEC_SET_IP
;
244 static int erst_exec_goto(struct apei_exec_context
*ctx
,
245 struct acpi_whea_header
*entry
)
247 ctx
->ip
= ctx
->value
;
248 return APEI_EXEC_SET_IP
;
251 static int erst_exec_set_src_address_base(struct apei_exec_context
*ctx
,
252 struct acpi_whea_header
*entry
)
254 return __apei_exec_read_register(entry
, &ctx
->src_base
);
257 static int erst_exec_set_dst_address_base(struct apei_exec_context
*ctx
,
258 struct acpi_whea_header
*entry
)
260 return __apei_exec_read_register(entry
, &ctx
->dst_base
);
263 static int erst_exec_move_data(struct apei_exec_context
*ctx
,
264 struct acpi_whea_header
*entry
)
270 /* ioremap does not work in interrupt context */
271 if (in_interrupt()) {
272 pr_warn("MOVE_DATA can not be used in interrupt context.\n");
276 rc
= __apei_exec_read_register(entry
, &offset
);
280 src
= ioremap(ctx
->src_base
+ offset
, ctx
->var2
);
283 dst
= ioremap(ctx
->dst_base
+ offset
, ctx
->var2
);
289 memmove(dst
, src
, ctx
->var2
);
297 static 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
,
302 [ACPI_ERST_READ_REGISTER_VALUE
] = {
303 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
304 .run
= apei_exec_read_register_value
,
306 [ACPI_ERST_WRITE_REGISTER
] = {
307 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
308 .run
= apei_exec_write_register
,
310 [ACPI_ERST_WRITE_REGISTER_VALUE
] = {
311 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
312 .run
= apei_exec_write_register_value
,
316 .run
= apei_exec_noop
,
318 [ACPI_ERST_LOAD_VAR1
] = {
319 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
320 .run
= erst_exec_load_var1
,
322 [ACPI_ERST_LOAD_VAR2
] = {
323 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
324 .run
= erst_exec_load_var2
,
326 [ACPI_ERST_STORE_VAR1
] = {
327 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
328 .run
= erst_exec_store_var1
,
332 .run
= erst_exec_add
,
334 [ACPI_ERST_SUBTRACT
] = {
336 .run
= erst_exec_subtract
,
338 [ACPI_ERST_ADD_VALUE
] = {
339 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
340 .run
= erst_exec_add_value
,
342 [ACPI_ERST_SUBTRACT_VALUE
] = {
343 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
344 .run
= erst_exec_subtract_value
,
346 [ACPI_ERST_STALL
] = {
348 .run
= erst_exec_stall
,
350 [ACPI_ERST_STALL_WHILE_TRUE
] = {
351 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
352 .run
= erst_exec_stall_while_true
,
354 [ACPI_ERST_SKIP_NEXT_IF_TRUE
] = {
355 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
356 .run
= erst_exec_skip_next_instruction_if_true
,
360 .run
= erst_exec_goto
,
362 [ACPI_ERST_SET_SRC_ADDRESS_BASE
] = {
363 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
364 .run
= erst_exec_set_src_address_base
,
366 [ACPI_ERST_SET_DST_ADDRESS_BASE
] = {
367 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
368 .run
= erst_exec_set_dst_address_base
,
370 [ACPI_ERST_MOVE_DATA
] = {
371 .flags
= APEI_EXEC_INS_ACCESS_REGISTER
,
372 .run
= erst_exec_move_data
,
376 static inline void erst_exec_ctx_init(struct apei_exec_context
*ctx
)
378 apei_exec_ctx_init(ctx
, erst_ins_type
, ARRAY_SIZE(erst_ins_type
),
379 ERST_TAB_ENTRY(erst_tab
), erst_tab
->entries
);
382 static int erst_get_erange(struct erst_erange
*range
)
384 struct apei_exec_context ctx
;
387 erst_exec_ctx_init(&ctx
);
388 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_ERROR_RANGE
);
391 range
->base
= apei_exec_ctx_get_output(&ctx
);
392 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_ERROR_LENGTH
);
395 range
->size
= apei_exec_ctx_get_output(&ctx
);
396 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_ERROR_ATTRIBUTES
);
399 range
->attr
= apei_exec_ctx_get_output(&ctx
);
404 static ssize_t
__erst_get_record_count(void)
406 struct apei_exec_context ctx
;
409 erst_exec_ctx_init(&ctx
);
410 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_RECORD_COUNT
);
413 return apei_exec_ctx_get_output(&ctx
);
416 ssize_t
erst_get_record_count(void)
424 raw_spin_lock_irqsave(&erst_lock
, flags
);
425 count
= __erst_get_record_count();
426 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
430 EXPORT_SYMBOL_GPL(erst_get_record_count
);
432 #define ERST_RECORD_ID_CACHE_SIZE_MIN 16
433 #define ERST_RECORD_ID_CACHE_SIZE_MAX 1024
435 struct erst_record_id_cache
{
443 static struct erst_record_id_cache erst_record_id_cache
= {
444 .lock
= __MUTEX_INITIALIZER(erst_record_id_cache
.lock
),
448 static int __erst_get_next_record_id(u64
*record_id
)
450 struct apei_exec_context ctx
;
453 erst_exec_ctx_init(&ctx
);
454 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_RECORD_ID
);
457 *record_id
= apei_exec_ctx_get_output(&ctx
);
462 int erst_get_record_id_begin(int *pos
)
469 rc
= mutex_lock_interruptible(&erst_record_id_cache
.lock
);
472 erst_record_id_cache
.refcount
++;
473 mutex_unlock(&erst_record_id_cache
.lock
);
479 EXPORT_SYMBOL_GPL(erst_get_record_id_begin
);
481 /* erst_record_id_cache.lock must be held by caller */
482 static int __erst_record_id_cache_add_one(void)
484 u64 id
, prev_id
, first_id
;
489 id
= prev_id
= first_id
= APEI_ERST_INVALID_RECORD_ID
;
491 raw_spin_lock_irqsave(&erst_lock
, flags
);
492 rc
= __erst_get_next_record_id(&id
);
493 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
498 if (id
== APEI_ERST_INVALID_RECORD_ID
)
500 /* can not skip current ID, or loop back to first ID */
501 if (id
== prev_id
|| id
== first_id
)
503 if (first_id
== APEI_ERST_INVALID_RECORD_ID
)
507 entries
= erst_record_id_cache
.entries
;
508 for (i
= 0; i
< erst_record_id_cache
.len
; i
++) {
509 if (entries
[i
] == id
)
512 /* record id already in cache, try next */
513 if (i
< erst_record_id_cache
.len
)
515 if (erst_record_id_cache
.len
>= erst_record_id_cache
.size
) {
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())
524 pr_warn(FW_WARN
"too many record IDs!\n");
527 new_entries
= kvmalloc(new_size
* sizeof(entries
[0]), GFP_KERNEL
);
530 memcpy(new_entries
, entries
,
531 erst_record_id_cache
.len
* sizeof(entries
[0]));
533 erst_record_id_cache
.entries
= entries
= new_entries
;
534 erst_record_id_cache
.size
= new_size
;
537 erst_record_id_cache
.len
++;
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.
547 int erst_get_record_id_next(int *pos
, u64
*record_id
)
555 /* 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
);
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
)
564 /* found next record id in cache */
565 if (*pos
< erst_record_id_cache
.len
) {
566 *record_id
= entries
[*pos
];
571 /* Try to add one more record ID to cache */
572 rc
= __erst_record_id_cache_add_one();
575 /* successfully add one new ID */
577 *record_id
= erst_record_id_cache
.entries
[*pos
];
582 *record_id
= APEI_ERST_INVALID_RECORD_ID
;
585 mutex_unlock(&erst_record_id_cache
.lock
);
589 EXPORT_SYMBOL_GPL(erst_get_record_id_next
);
591 /* erst_record_id_cache.lock must be held by caller */
592 static void __erst_record_id_cache_compact(void)
597 if (erst_record_id_cache
.refcount
)
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
)
605 entries
[wpos
] = entries
[i
];
608 erst_record_id_cache
.len
= wpos
;
611 void erst_get_record_id_end(void)
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.
618 BUG_ON(erst_disable
);
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
);
626 EXPORT_SYMBOL_GPL(erst_get_record_id_end
);
628 static int __erst_write_to_storage(u64 offset
)
630 struct apei_exec_context ctx
;
631 u64 timeout
= FIRMWARE_TIMEOUT
;
635 erst_exec_ctx_init(&ctx
);
636 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_BEGIN_WRITE
);
639 apei_exec_ctx_set_input(&ctx
, offset
);
640 rc
= apei_exec_run(&ctx
, ACPI_ERST_SET_RECORD_OFFSET
);
643 rc
= apei_exec_run(&ctx
, ACPI_ERST_EXECUTE_OPERATION
);
647 rc
= apei_exec_run(&ctx
, ACPI_ERST_CHECK_BUSY_STATUS
);
650 val
= apei_exec_ctx_get_output(&ctx
);
653 if (erst_timedout(&timeout
, SPIN_UNIT
))
656 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_COMMAND_STATUS
);
659 val
= apei_exec_ctx_get_output(&ctx
);
660 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_END
);
664 return erst_errno(val
);
667 static int __erst_read_from_storage(u64 record_id
, u64 offset
)
669 struct apei_exec_context ctx
;
670 u64 timeout
= FIRMWARE_TIMEOUT
;
674 erst_exec_ctx_init(&ctx
);
675 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_BEGIN_READ
);
678 apei_exec_ctx_set_input(&ctx
, offset
);
679 rc
= apei_exec_run(&ctx
, ACPI_ERST_SET_RECORD_OFFSET
);
682 apei_exec_ctx_set_input(&ctx
, record_id
);
683 rc
= apei_exec_run(&ctx
, ACPI_ERST_SET_RECORD_ID
);
686 rc
= apei_exec_run(&ctx
, ACPI_ERST_EXECUTE_OPERATION
);
690 rc
= apei_exec_run(&ctx
, ACPI_ERST_CHECK_BUSY_STATUS
);
693 val
= apei_exec_ctx_get_output(&ctx
);
696 if (erst_timedout(&timeout
, SPIN_UNIT
))
699 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_COMMAND_STATUS
);
702 val
= apei_exec_ctx_get_output(&ctx
);
703 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_END
);
707 return erst_errno(val
);
710 static int __erst_clear_from_storage(u64 record_id
)
712 struct apei_exec_context ctx
;
713 u64 timeout
= FIRMWARE_TIMEOUT
;
717 erst_exec_ctx_init(&ctx
);
718 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_BEGIN_CLEAR
);
721 apei_exec_ctx_set_input(&ctx
, record_id
);
722 rc
= apei_exec_run(&ctx
, ACPI_ERST_SET_RECORD_ID
);
725 rc
= apei_exec_run(&ctx
, ACPI_ERST_EXECUTE_OPERATION
);
729 rc
= apei_exec_run(&ctx
, ACPI_ERST_CHECK_BUSY_STATUS
);
732 val
= apei_exec_ctx_get_output(&ctx
);
735 if (erst_timedout(&timeout
, SPIN_UNIT
))
738 rc
= apei_exec_run(&ctx
, ACPI_ERST_GET_COMMAND_STATUS
);
741 val
= apei_exec_ctx_get_output(&ctx
);
742 rc
= apei_exec_run_optional(&ctx
, ACPI_ERST_END
);
746 return erst_errno(val
);
749 /* NVRAM ERST Error Log Address Range is not supported yet */
750 static void pr_unimpl_nvram(void)
752 if (printk_ratelimit())
753 pr_warn("NVRAM ERST Log Address Range not implemented yet.\n");
756 static int __erst_write_to_nvram(const struct cper_record_header
*record
)
758 /* do not print message, because printk is not safe for NMI */
762 static int __erst_read_to_erange_from_nvram(u64 record_id
, u64
*offset
)
768 static int __erst_clear_from_nvram(u64 record_id
)
774 int erst_write(const struct cper_record_header
*record
)
778 struct cper_record_header
*rcd_erange
;
783 if (memcmp(record
->signature
, CPER_SIG_RECORD
, CPER_SIG_SIZE
))
786 if (erst_erange
.attr
& ERST_RANGE_NVRAM
) {
787 if (!raw_spin_trylock_irqsave(&erst_lock
, flags
))
789 rc
= __erst_write_to_nvram(record
);
790 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
794 if (record
->record_length
> erst_erange
.size
)
797 if (!raw_spin_trylock_irqsave(&erst_lock
, flags
))
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);
804 rc
= __erst_write_to_storage(0);
805 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
809 EXPORT_SYMBOL_GPL(erst_write
);
811 static int __erst_read_to_erange(u64 record_id
, u64
*offset
)
815 if (erst_erange
.attr
& ERST_RANGE_NVRAM
)
816 return __erst_read_to_erange_from_nvram(
819 rc
= __erst_read_from_storage(record_id
, 0);
827 static ssize_t
__erst_read(u64 record_id
, struct cper_record_header
*record
,
832 struct cper_record_header
*rcd_tmp
;
834 rc
= __erst_read_to_erange(record_id
, &offset
);
837 rcd_tmp
= erst_erange
.vaddr
+ offset
;
838 len
= rcd_tmp
->record_length
;
840 memcpy(record
, rcd_tmp
, len
);
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
850 ssize_t
erst_read(u64 record_id
, struct cper_record_header
*record
,
859 raw_spin_lock_irqsave(&erst_lock
, flags
);
860 len
= __erst_read(record_id
, record
, buflen
);
861 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
864 EXPORT_SYMBOL_GPL(erst_read
);
866 int erst_clear(u64 record_id
)
875 rc
= mutex_lock_interruptible(&erst_record_id_cache
.lock
);
878 raw_spin_lock_irqsave(&erst_lock
, flags
);
879 if (erst_erange
.attr
& ERST_RANGE_NVRAM
)
880 rc
= __erst_clear_from_nvram(record_id
);
882 rc
= __erst_clear_from_storage(record_id
);
883 raw_spin_unlock_irqrestore(&erst_lock
, flags
);
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
;
891 __erst_record_id_cache_compact();
893 mutex_unlock(&erst_record_id_cache
.lock
);
896 EXPORT_SYMBOL_GPL(erst_clear
);
898 static int __init
setup_erst_disable(char *str
)
904 __setup("erst_disable", setup_erst_disable
);
906 static int erst_check_table(struct acpi_table_erst
*erst_tab
)
908 if ((erst_tab
->header_length
!=
909 (sizeof(struct acpi_table_erst
) - sizeof(erst_tab
->header
)))
910 && (erst_tab
->header_length
!= sizeof(struct acpi_table_erst
)))
912 if (erst_tab
->header
.length
< sizeof(struct acpi_table_erst
))
914 if (erst_tab
->entries
!=
915 (erst_tab
->header
.length
- sizeof(struct acpi_table_erst
)) /
916 sizeof(struct acpi_erst_entry
))
922 static int erst_open_pstore(struct pstore_info
*psi
);
923 static int erst_close_pstore(struct pstore_info
*psi
);
924 static ssize_t
erst_reader(struct pstore_record
*record
);
925 static int erst_writer(struct pstore_record
*record
);
926 static int erst_clearer(struct pstore_record
*record
);
928 static struct pstore_info erst_info
= {
929 .owner
= THIS_MODULE
,
931 .flags
= PSTORE_FLAGS_DMESG
,
932 .open
= erst_open_pstore
,
933 .close
= erst_close_pstore
,
935 .write
= erst_writer
,
936 .erase
= erst_clearer
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)
945 #define CPER_SECTION_TYPE_DMESG_Z \
946 UUID_LE(0x4f118707, 0x04dd, 0x4055, 0xb5, 0xdd, 0x95, 0x6d, \
947 0x34, 0xdd, 0xfa, 0xc6)
948 #define CPER_SECTION_TYPE_MCE \
949 UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
950 0x04, 0x4a, 0x38, 0xfc)
952 struct cper_pstore_record
{
953 struct cper_record_header hdr
;
954 struct cper_section_descriptor sec_hdr
;
958 static int reader_pos
;
960 static int erst_open_pstore(struct pstore_info
*psi
)
967 rc
= erst_get_record_id_begin(&reader_pos
);
972 static int erst_close_pstore(struct pstore_info
*psi
)
974 erst_get_record_id_end();
979 static ssize_t
erst_reader(struct pstore_record
*record
)
984 struct cper_pstore_record
*rcd
;
985 size_t rcd_len
= sizeof(*rcd
) + erst_info
.bufsize
;
990 rcd
= kmalloc(rcd_len
, GFP_KERNEL
);
996 rc
= erst_get_record_id_next(&reader_pos
, &record_id
);
1000 /* no more record */
1001 if (record_id
== APEI_ERST_INVALID_RECORD_ID
) {
1006 len
= erst_read(record_id
, &rcd
->hdr
, rcd_len
);
1007 /* The record may be cleared by others, try read next record */
1010 else if (len
< 0 || len
< sizeof(*rcd
)) {
1014 if (uuid_le_cmp(rcd
->hdr
.creator_id
, CPER_CREATOR_PSTORE
) != 0)
1017 record
->buf
= kmalloc(len
, GFP_KERNEL
);
1018 if (record
->buf
== NULL
) {
1022 memcpy(record
->buf
, rcd
->data
, len
- sizeof(*rcd
));
1023 record
->id
= record_id
;
1024 record
->compressed
= false;
1025 record
->ecc_notice_size
= 0;
1026 if (uuid_le_cmp(rcd
->sec_hdr
.section_type
,
1027 CPER_SECTION_TYPE_DMESG_Z
) == 0) {
1028 record
->type
= PSTORE_TYPE_DMESG
;
1029 record
->compressed
= true;
1030 } else if (uuid_le_cmp(rcd
->sec_hdr
.section_type
,
1031 CPER_SECTION_TYPE_DMESG
) == 0)
1032 record
->type
= PSTORE_TYPE_DMESG
;
1033 else if (uuid_le_cmp(rcd
->sec_hdr
.section_type
,
1034 CPER_SECTION_TYPE_MCE
) == 0)
1035 record
->type
= PSTORE_TYPE_MCE
;
1037 record
->type
= PSTORE_TYPE_UNKNOWN
;
1039 if (rcd
->hdr
.validation_bits
& CPER_VALID_TIMESTAMP
)
1040 record
->time
.tv_sec
= rcd
->hdr
.timestamp
;
1042 record
->time
.tv_sec
= 0;
1043 record
->time
.tv_nsec
= 0;
1047 return (rc
< 0) ? rc
: (len
- sizeof(*rcd
));
1050 static int erst_writer(struct pstore_record
*record
)
1052 struct cper_pstore_record
*rcd
= (struct cper_pstore_record
*)
1053 (erst_info
.buf
- sizeof(*rcd
));
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
= ktime_get_real_seconds();
1065 rcd
->hdr
.record_length
= sizeof(*rcd
) + record
->size
;
1066 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
;
1071 rcd
->sec_hdr
.section_offset
= sizeof(*rcd
);
1072 rcd
->sec_hdr
.section_length
= record
->size
;
1073 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
;
1077 switch (record
->type
) {
1078 case PSTORE_TYPE_DMESG
:
1079 if (record
->compressed
)
1080 rcd
->sec_hdr
.section_type
= CPER_SECTION_TYPE_DMESG_Z
;
1082 rcd
->sec_hdr
.section_type
= CPER_SECTION_TYPE_DMESG
;
1084 case PSTORE_TYPE_MCE
:
1085 rcd
->sec_hdr
.section_type
= CPER_SECTION_TYPE_MCE
;
1090 rcd
->sec_hdr
.section_severity
= CPER_SEV_FATAL
;
1092 ret
= erst_write(&rcd
->hdr
);
1093 record
->id
= rcd
->hdr
.record_id
;
1098 static int erst_clearer(struct pstore_record
*record
)
1100 return erst_clear(record
->id
);
1103 static int __init
erst_init(void)
1107 struct apei_exec_context ctx
;
1108 struct apei_resources erst_resources
;
1117 "Error Record Serialization Table (ERST) support is disabled.\n");
1121 status
= acpi_get_table(ACPI_SIG_ERST
, 0,
1122 (struct acpi_table_header
**)&erst_tab
);
1123 if (status
== AE_NOT_FOUND
)
1125 else if (ACPI_FAILURE(status
)) {
1126 const char *msg
= acpi_format_exception(status
);
1127 pr_err("Failed to get table, %s\n", msg
);
1132 rc
= erst_check_table(erst_tab
);
1134 pr_err(FW_BUG
"ERST table is invalid.\n");
1138 apei_resources_init(&erst_resources
);
1139 erst_exec_ctx_init(&ctx
);
1140 rc
= apei_exec_collect_resources(&ctx
, &erst_resources
);
1143 rc
= apei_resources_request(&erst_resources
, "APEI ERST");
1146 rc
= apei_exec_pre_map_gars(&ctx
);
1149 rc
= erst_get_erange(&erst_erange
);
1153 "The corresponding hardware device or firmware implementation "
1154 "is not available.\n");
1156 pr_err("Failed to get Error Log Address Range.\n");
1160 r
= request_mem_region(erst_erange
.base
, erst_erange
.size
, "APEI ERST");
1162 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);
1169 erst_erange
.vaddr
= ioremap_cache(erst_erange
.base
,
1171 if (!erst_erange
.vaddr
)
1172 goto err_release_erange
;
1175 "Error Record Serialization Table (ERST) support is initialized.\n");
1177 buf
= kmalloc(erst_erange
.size
, GFP_KERNEL
);
1178 spin_lock_init(&erst_info
.buf_lock
);
1180 erst_info
.buf
= buf
+ sizeof(struct cper_pstore_record
);
1181 erst_info
.bufsize
= erst_erange
.size
-
1182 sizeof(struct cper_pstore_record
);
1183 rc
= pstore_register(&erst_info
);
1187 "Could not register with persistent store.\n");
1188 erst_info
.buf
= NULL
;
1189 erst_info
.bufsize
= 0;
1194 "Failed to allocate %lld bytes for persistent store error log.\n",
1197 /* Cleanup ERST Resources */
1198 apei_resources_fini(&erst_resources
);
1203 release_mem_region(erst_erange
.base
, erst_erange
.size
);
1205 apei_exec_post_unmap_gars(&ctx
);
1207 apei_resources_release(&erst_resources
);
1209 apei_resources_fini(&erst_resources
);
1215 device_initcall(erst_init
);