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 <acpi/apei.h>
37 #include "apei-internal.h"
40 #define pr_fmt(fmt) "ERST: " fmt
42 /* ERST command status */
43 #define ERST_STATUS_SUCCESS 0x0
44 #define ERST_STATUS_NOT_ENOUGH_SPACE 0x1
45 #define ERST_STATUS_HARDWARE_NOT_AVAILABLE 0x2
46 #define ERST_STATUS_FAILED 0x3
47 #define ERST_STATUS_RECORD_STORE_EMPTY 0x4
48 #define ERST_STATUS_RECORD_NOT_FOUND 0x5
50 #define ERST_TAB_ENTRY(tab) \
51 ((struct acpi_whea_header *)((char *)(tab) + \
52 sizeof(struct acpi_table_erst)))
54 #define SPIN_UNIT 100 /* 100ns */
55 /* Firmware should respond within 1 milliseconds */
56 #define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
57 #define FIRMWARE_MAX_STALL 50 /* 50us */
60 EXPORT_SYMBOL_GPL(erst_disable);
62 static struct acpi_table_erst *erst_tab;
64 /* ERST Error Log Address Range atrributes */
65 #define ERST_RANGE_RESERVED 0x0001
66 #define ERST_RANGE_NVRAM 0x0002
67 #define ERST_RANGE_SLOW 0x0004
70 * ERST Error Log Address Range, used as buffer for reading/writing
73 static struct erst_erange {
81 * Prevent ERST interpreter to run simultaneously, because the
82 * corresponding firmware implementation may not work properly when
83 * invoked simultaneously.
85 * It is used to provide exclusive accessing for ERST Error Log
88 static DEFINE_RAW_SPINLOCK(erst_lock);
90 static inline int erst_errno(int command_status)
92 switch (command_status) {
93 case ERST_STATUS_SUCCESS:
95 case ERST_STATUS_HARDWARE_NOT_AVAILABLE:
97 case ERST_STATUS_NOT_ENOUGH_SPACE:
99 case ERST_STATUS_RECORD_STORE_EMPTY:
100 case ERST_STATUS_RECORD_NOT_FOUND:
107 static int erst_timedout(u64 *t, u64 spin_unit)
109 if ((s64)*t < spin_unit) {
110 pr_warn(FW_WARN "Firmware does not respond in time.\n");
115 touch_nmi_watchdog();
119 static int erst_exec_load_var1(struct apei_exec_context *ctx,
120 struct acpi_whea_header *entry)
122 return __apei_exec_read_register(entry, &ctx->var1);
125 static int erst_exec_load_var2(struct apei_exec_context *ctx,
126 struct acpi_whea_header *entry)
128 return __apei_exec_read_register(entry, &ctx->var2);
131 static int erst_exec_store_var1(struct apei_exec_context *ctx,
132 struct acpi_whea_header *entry)
134 return __apei_exec_write_register(entry, ctx->var1);
137 static int erst_exec_add(struct apei_exec_context *ctx,
138 struct acpi_whea_header *entry)
140 ctx->var1 += ctx->var2;
144 static int erst_exec_subtract(struct apei_exec_context *ctx,
145 struct acpi_whea_header *entry)
147 ctx->var1 -= ctx->var2;
151 static int erst_exec_add_value(struct apei_exec_context *ctx,
152 struct acpi_whea_header *entry)
157 rc = __apei_exec_read_register(entry, &val);
161 rc = __apei_exec_write_register(entry, val);
165 static int erst_exec_subtract_value(struct apei_exec_context *ctx,
166 struct acpi_whea_header *entry)
171 rc = __apei_exec_read_register(entry, &val);
175 rc = __apei_exec_write_register(entry, val);
179 static int erst_exec_stall(struct apei_exec_context *ctx,
180 struct acpi_whea_header *entry)
184 if (ctx->value > FIRMWARE_MAX_STALL) {
187 "Too long stall time for stall instruction: 0x%llx.\n",
189 stall_time = FIRMWARE_MAX_STALL;
191 stall_time = ctx->value;
196 static int erst_exec_stall_while_true(struct apei_exec_context *ctx,
197 struct acpi_whea_header *entry)
201 u64 timeout = FIRMWARE_TIMEOUT;
204 if (ctx->var1 > FIRMWARE_MAX_STALL) {
207 "Too long stall time for stall while true instruction: 0x%llx.\n",
209 stall_time = FIRMWARE_MAX_STALL;
211 stall_time = ctx->var1;
214 rc = __apei_exec_read_register(entry, &val);
217 if (val != ctx->value)
219 if (erst_timedout(&timeout, stall_time * NSEC_PER_USEC))
225 static int erst_exec_skip_next_instruction_if_true(
226 struct apei_exec_context *ctx,
227 struct acpi_whea_header *entry)
232 rc = __apei_exec_read_register(entry, &val);
235 if (val == ctx->value) {
237 return APEI_EXEC_SET_IP;
243 static int erst_exec_goto(struct apei_exec_context *ctx,
244 struct acpi_whea_header *entry)
246 ctx->ip = ctx->value;
247 return APEI_EXEC_SET_IP;
250 static int erst_exec_set_src_address_base(struct apei_exec_context *ctx,
251 struct acpi_whea_header *entry)
253 return __apei_exec_read_register(entry, &ctx->src_base);
256 static int erst_exec_set_dst_address_base(struct apei_exec_context *ctx,
257 struct acpi_whea_header *entry)
259 return __apei_exec_read_register(entry, &ctx->dst_base);
262 static int erst_exec_move_data(struct apei_exec_context *ctx,
263 struct acpi_whea_header *entry)
269 /* ioremap does not work in interrupt context */
270 if (in_interrupt()) {
271 pr_warn("MOVE_DATA can not be used in interrupt context.\n");
275 rc = __apei_exec_read_register(entry, &offset);
279 src = ioremap(ctx->src_base + offset, ctx->var2);
282 dst = ioremap(ctx->dst_base + offset, ctx->var2);
288 memmove(dst, src, ctx->var2);
296 static struct apei_exec_ins_type erst_ins_type[] = {
297 [ACPI_ERST_READ_REGISTER] = {
298 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
299 .run = apei_exec_read_register,
301 [ACPI_ERST_READ_REGISTER_VALUE] = {
302 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
303 .run = apei_exec_read_register_value,
305 [ACPI_ERST_WRITE_REGISTER] = {
306 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
307 .run = apei_exec_write_register,
309 [ACPI_ERST_WRITE_REGISTER_VALUE] = {
310 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
311 .run = apei_exec_write_register_value,
315 .run = apei_exec_noop,
317 [ACPI_ERST_LOAD_VAR1] = {
318 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
319 .run = erst_exec_load_var1,
321 [ACPI_ERST_LOAD_VAR2] = {
322 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
323 .run = erst_exec_load_var2,
325 [ACPI_ERST_STORE_VAR1] = {
326 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
327 .run = erst_exec_store_var1,
331 .run = erst_exec_add,
333 [ACPI_ERST_SUBTRACT] = {
335 .run = erst_exec_subtract,
337 [ACPI_ERST_ADD_VALUE] = {
338 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
339 .run = erst_exec_add_value,
341 [ACPI_ERST_SUBTRACT_VALUE] = {
342 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
343 .run = erst_exec_subtract_value,
345 [ACPI_ERST_STALL] = {
347 .run = erst_exec_stall,
349 [ACPI_ERST_STALL_WHILE_TRUE] = {
350 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
351 .run = erst_exec_stall_while_true,
353 [ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
354 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
355 .run = erst_exec_skip_next_instruction_if_true,
359 .run = erst_exec_goto,
361 [ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
362 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
363 .run = erst_exec_set_src_address_base,
365 [ACPI_ERST_SET_DST_ADDRESS_BASE] = {
366 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
367 .run = erst_exec_set_dst_address_base,
369 [ACPI_ERST_MOVE_DATA] = {
370 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
371 .run = erst_exec_move_data,
375 static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
377 apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
378 ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
381 static int erst_get_erange(struct erst_erange *range)
383 struct apei_exec_context ctx;
386 erst_exec_ctx_init(&ctx);
387 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
390 range->base = apei_exec_ctx_get_output(&ctx);
391 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
394 range->size = apei_exec_ctx_get_output(&ctx);
395 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
398 range->attr = apei_exec_ctx_get_output(&ctx);
403 static ssize_t __erst_get_record_count(void)
405 struct apei_exec_context ctx;
408 erst_exec_ctx_init(&ctx);
409 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
412 return apei_exec_ctx_get_output(&ctx);
415 ssize_t erst_get_record_count(void)
423 raw_spin_lock_irqsave(&erst_lock, flags);
424 count = __erst_get_record_count();
425 raw_spin_unlock_irqrestore(&erst_lock, flags);
429 EXPORT_SYMBOL_GPL(erst_get_record_count);
431 #define ERST_RECORD_ID_CACHE_SIZE_MIN 16
432 #define ERST_RECORD_ID_CACHE_SIZE_MAX 1024
434 struct erst_record_id_cache {
442 static struct erst_record_id_cache erst_record_id_cache = {
443 .lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock),
447 static int __erst_get_next_record_id(u64 *record_id)
449 struct apei_exec_context ctx;
452 erst_exec_ctx_init(&ctx);
453 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
456 *record_id = apei_exec_ctx_get_output(&ctx);
461 int erst_get_record_id_begin(int *pos)
468 rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
471 erst_record_id_cache.refcount++;
472 mutex_unlock(&erst_record_id_cache.lock);
478 EXPORT_SYMBOL_GPL(erst_get_record_id_begin);
480 /* erst_record_id_cache.lock must be held by caller */
481 static int __erst_record_id_cache_add_one(void)
483 u64 id, prev_id, first_id;
488 id = prev_id = first_id = APEI_ERST_INVALID_RECORD_ID;
490 raw_spin_lock_irqsave(&erst_lock, flags);
491 rc = __erst_get_next_record_id(&id);
492 raw_spin_unlock_irqrestore(&erst_lock, flags);
497 if (id == APEI_ERST_INVALID_RECORD_ID)
499 /* can not skip current ID, or loop back to first ID */
500 if (id == prev_id || id == first_id)
502 if (first_id == APEI_ERST_INVALID_RECORD_ID)
506 entries = erst_record_id_cache.entries;
507 for (i = 0; i < erst_record_id_cache.len; i++) {
508 if (entries[i] == id)
511 /* record id already in cache, try next */
512 if (i < erst_record_id_cache.len)
514 if (erst_record_id_cache.len >= erst_record_id_cache.size) {
515 int new_size, alloc_size;
518 new_size = erst_record_id_cache.size * 2;
519 new_size = clamp_val(new_size, ERST_RECORD_ID_CACHE_SIZE_MIN,
520 ERST_RECORD_ID_CACHE_SIZE_MAX);
521 if (new_size <= erst_record_id_cache.size) {
522 if (printk_ratelimit())
523 pr_warn(FW_WARN "too many record IDs!\n");
526 alloc_size = new_size * sizeof(entries[0]);
527 if (alloc_size < PAGE_SIZE)
528 new_entries = kmalloc(alloc_size, GFP_KERNEL);
530 new_entries = vmalloc(alloc_size);
533 memcpy(new_entries, entries,
534 erst_record_id_cache.len * sizeof(entries[0]));
535 if (erst_record_id_cache.size < PAGE_SIZE)
539 erst_record_id_cache.entries = entries = new_entries;
540 erst_record_id_cache.size = new_size;
543 erst_record_id_cache.len++;
549 * Get the record ID of an existing error record on the persistent
550 * storage. If there is no error record on the persistent storage, the
551 * returned record_id is APEI_ERST_INVALID_RECORD_ID.
553 int erst_get_record_id_next(int *pos, u64 *record_id)
561 /* must be enclosed by erst_get_record_id_begin/end */
562 BUG_ON(!erst_record_id_cache.refcount);
563 BUG_ON(*pos < 0 || *pos > erst_record_id_cache.len);
565 mutex_lock(&erst_record_id_cache.lock);
566 entries = erst_record_id_cache.entries;
567 for (; *pos < erst_record_id_cache.len; (*pos)++)
568 if (entries[*pos] != APEI_ERST_INVALID_RECORD_ID)
570 /* found next record id in cache */
571 if (*pos < erst_record_id_cache.len) {
572 *record_id = entries[*pos];
577 /* Try to add one more record ID to cache */
578 rc = __erst_record_id_cache_add_one();
581 /* successfully add one new ID */
583 *record_id = erst_record_id_cache.entries[*pos];
588 *record_id = APEI_ERST_INVALID_RECORD_ID;
591 mutex_unlock(&erst_record_id_cache.lock);
595 EXPORT_SYMBOL_GPL(erst_get_record_id_next);
597 /* erst_record_id_cache.lock must be held by caller */
598 static void __erst_record_id_cache_compact(void)
603 if (erst_record_id_cache.refcount)
606 entries = erst_record_id_cache.entries;
607 for (i = 0; i < erst_record_id_cache.len; i++) {
608 if (entries[i] == APEI_ERST_INVALID_RECORD_ID)
611 entries[wpos] = entries[i];
614 erst_record_id_cache.len = wpos;
617 void erst_get_record_id_end(void)
620 * erst_disable != 0 should be detected by invoker via the
621 * return value of erst_get_record_id_begin/next, so this
622 * function should not be called for erst_disable != 0.
624 BUG_ON(erst_disable);
626 mutex_lock(&erst_record_id_cache.lock);
627 erst_record_id_cache.refcount--;
628 BUG_ON(erst_record_id_cache.refcount < 0);
629 __erst_record_id_cache_compact();
630 mutex_unlock(&erst_record_id_cache.lock);
632 EXPORT_SYMBOL_GPL(erst_get_record_id_end);
634 static int __erst_write_to_storage(u64 offset)
636 struct apei_exec_context ctx;
637 u64 timeout = FIRMWARE_TIMEOUT;
641 erst_exec_ctx_init(&ctx);
642 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_WRITE);
645 apei_exec_ctx_set_input(&ctx, offset);
646 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
649 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
653 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
656 val = apei_exec_ctx_get_output(&ctx);
659 if (erst_timedout(&timeout, SPIN_UNIT))
662 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
665 val = apei_exec_ctx_get_output(&ctx);
666 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
670 return erst_errno(val);
673 static int __erst_read_from_storage(u64 record_id, u64 offset)
675 struct apei_exec_context ctx;
676 u64 timeout = FIRMWARE_TIMEOUT;
680 erst_exec_ctx_init(&ctx);
681 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_READ);
684 apei_exec_ctx_set_input(&ctx, offset);
685 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
688 apei_exec_ctx_set_input(&ctx, record_id);
689 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
692 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
696 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
699 val = apei_exec_ctx_get_output(&ctx);
702 if (erst_timedout(&timeout, SPIN_UNIT))
705 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
708 val = apei_exec_ctx_get_output(&ctx);
709 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
713 return erst_errno(val);
716 static int __erst_clear_from_storage(u64 record_id)
718 struct apei_exec_context ctx;
719 u64 timeout = FIRMWARE_TIMEOUT;
723 erst_exec_ctx_init(&ctx);
724 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_CLEAR);
727 apei_exec_ctx_set_input(&ctx, record_id);
728 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
731 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
735 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
738 val = apei_exec_ctx_get_output(&ctx);
741 if (erst_timedout(&timeout, SPIN_UNIT))
744 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
747 val = apei_exec_ctx_get_output(&ctx);
748 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
752 return erst_errno(val);
755 /* NVRAM ERST Error Log Address Range is not supported yet */
756 static void pr_unimpl_nvram(void)
758 if (printk_ratelimit())
759 pr_warn("NVRAM ERST Log Address Range not implemented yet.\n");
762 static int __erst_write_to_nvram(const struct cper_record_header *record)
764 /* do not print message, because printk is not safe for NMI */
768 static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
774 static int __erst_clear_from_nvram(u64 record_id)
780 int erst_write(const struct cper_record_header *record)
784 struct cper_record_header *rcd_erange;
789 if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
792 if (erst_erange.attr & ERST_RANGE_NVRAM) {
793 if (!raw_spin_trylock_irqsave(&erst_lock, flags))
795 rc = __erst_write_to_nvram(record);
796 raw_spin_unlock_irqrestore(&erst_lock, flags);
800 if (record->record_length > erst_erange.size)
803 if (!raw_spin_trylock_irqsave(&erst_lock, flags))
805 memcpy(erst_erange.vaddr, record, record->record_length);
806 rcd_erange = erst_erange.vaddr;
807 /* signature for serialization system */
808 memcpy(&rcd_erange->persistence_information, "ER", 2);
810 rc = __erst_write_to_storage(0);
811 raw_spin_unlock_irqrestore(&erst_lock, flags);
815 EXPORT_SYMBOL_GPL(erst_write);
817 static int __erst_read_to_erange(u64 record_id, u64 *offset)
821 if (erst_erange.attr & ERST_RANGE_NVRAM)
822 return __erst_read_to_erange_from_nvram(
825 rc = __erst_read_from_storage(record_id, 0);
833 static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
838 struct cper_record_header *rcd_tmp;
840 rc = __erst_read_to_erange(record_id, &offset);
843 rcd_tmp = erst_erange.vaddr + offset;
844 len = rcd_tmp->record_length;
846 memcpy(record, rcd_tmp, len);
852 * If return value > buflen, the buffer size is not big enough,
853 * else if return value < 0, something goes wrong,
854 * else everything is OK, and return value is record length
856 ssize_t erst_read(u64 record_id, struct cper_record_header *record,
865 raw_spin_lock_irqsave(&erst_lock, flags);
866 len = __erst_read(record_id, record, buflen);
867 raw_spin_unlock_irqrestore(&erst_lock, flags);
870 EXPORT_SYMBOL_GPL(erst_read);
872 int erst_clear(u64 record_id)
881 rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
884 raw_spin_lock_irqsave(&erst_lock, flags);
885 if (erst_erange.attr & ERST_RANGE_NVRAM)
886 rc = __erst_clear_from_nvram(record_id);
888 rc = __erst_clear_from_storage(record_id);
889 raw_spin_unlock_irqrestore(&erst_lock, flags);
892 entries = erst_record_id_cache.entries;
893 for (i = 0; i < erst_record_id_cache.len; i++) {
894 if (entries[i] == record_id)
895 entries[i] = APEI_ERST_INVALID_RECORD_ID;
897 __erst_record_id_cache_compact();
899 mutex_unlock(&erst_record_id_cache.lock);
902 EXPORT_SYMBOL_GPL(erst_clear);
904 static int __init setup_erst_disable(char *str)
910 __setup("erst_disable", setup_erst_disable);
912 static int erst_check_table(struct acpi_table_erst *erst_tab)
914 if ((erst_tab->header_length !=
915 (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
916 && (erst_tab->header_length != sizeof(struct acpi_table_erst)))
918 if (erst_tab->header.length < sizeof(struct acpi_table_erst))
920 if (erst_tab->entries !=
921 (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
922 sizeof(struct acpi_erst_entry))
928 static int erst_open_pstore(struct pstore_info *psi);
929 static int erst_close_pstore(struct pstore_info *psi);
930 static ssize_t erst_reader(u64 *id, enum pstore_type_id *type, int *count,
931 struct timespec *time, char **buf,
932 bool *compressed, struct pstore_info *psi);
933 static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason,
934 u64 *id, unsigned int part, int count, bool compressed,
935 size_t size, struct pstore_info *psi);
936 static int erst_clearer(enum pstore_type_id type, u64 id, int count,
937 struct timespec time, struct pstore_info *psi);
939 static struct pstore_info erst_info = {
940 .owner = THIS_MODULE,
942 .flags = PSTORE_FLAGS_FRAGILE,
943 .open = erst_open_pstore,
944 .close = erst_close_pstore,
946 .write = erst_writer,
947 .erase = erst_clearer
950 #define CPER_CREATOR_PSTORE \
951 UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
952 0x64, 0x90, 0xb8, 0x9d)
953 #define CPER_SECTION_TYPE_DMESG \
954 UUID_LE(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54, \
955 0x94, 0x19, 0xeb, 0x12)
956 #define CPER_SECTION_TYPE_DMESG_Z \
957 UUID_LE(0x4f118707, 0x04dd, 0x4055, 0xb5, 0xdd, 0x95, 0x6d, \
958 0x34, 0xdd, 0xfa, 0xc6)
959 #define CPER_SECTION_TYPE_MCE \
960 UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
961 0x04, 0x4a, 0x38, 0xfc)
963 struct cper_pstore_record {
964 struct cper_record_header hdr;
965 struct cper_section_descriptor sec_hdr;
969 static int reader_pos;
971 static int erst_open_pstore(struct pstore_info *psi)
978 rc = erst_get_record_id_begin(&reader_pos);
983 static int erst_close_pstore(struct pstore_info *psi)
985 erst_get_record_id_end();
990 static ssize_t erst_reader(u64 *id, enum pstore_type_id *type, int *count,
991 struct timespec *time, char **buf,
992 bool *compressed, struct pstore_info *psi)
997 struct cper_pstore_record *rcd;
998 size_t rcd_len = sizeof(*rcd) + erst_info.bufsize;
1003 rcd = kmalloc(rcd_len, GFP_KERNEL);
1009 rc = erst_get_record_id_next(&reader_pos, &record_id);
1013 /* no more record */
1014 if (record_id == APEI_ERST_INVALID_RECORD_ID) {
1019 len = erst_read(record_id, &rcd->hdr, rcd_len);
1020 /* The record may be cleared by others, try read next record */
1023 else if (len < sizeof(*rcd)) {
1027 if (uuid_le_cmp(rcd->hdr.creator_id, CPER_CREATOR_PSTORE) != 0)
1030 *buf = kmalloc(len, GFP_KERNEL);
1035 memcpy(*buf, rcd->data, len - sizeof(*rcd));
1037 *compressed = false;
1038 if (uuid_le_cmp(rcd->sec_hdr.section_type,
1039 CPER_SECTION_TYPE_DMESG_Z) == 0) {
1040 *type = PSTORE_TYPE_DMESG;
1042 } else if (uuid_le_cmp(rcd->sec_hdr.section_type,
1043 CPER_SECTION_TYPE_DMESG) == 0)
1044 *type = PSTORE_TYPE_DMESG;
1045 else if (uuid_le_cmp(rcd->sec_hdr.section_type,
1046 CPER_SECTION_TYPE_MCE) == 0)
1047 *type = PSTORE_TYPE_MCE;
1049 *type = PSTORE_TYPE_UNKNOWN;
1051 if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
1052 time->tv_sec = rcd->hdr.timestamp;
1059 return (rc < 0) ? rc : (len - sizeof(*rcd));
1062 static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason,
1063 u64 *id, unsigned int part, int count, bool compressed,
1064 size_t size, struct pstore_info *psi)
1066 struct cper_pstore_record *rcd = (struct cper_pstore_record *)
1067 (erst_info.buf - sizeof(*rcd));
1070 memset(rcd, 0, sizeof(*rcd));
1071 memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
1072 rcd->hdr.revision = CPER_RECORD_REV;
1073 rcd->hdr.signature_end = CPER_SIG_END;
1074 rcd->hdr.section_count = 1;
1075 rcd->hdr.error_severity = CPER_SEV_FATAL;
1076 /* timestamp valid. platform_id, partition_id are invalid */
1077 rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
1078 rcd->hdr.timestamp = get_seconds();
1079 rcd->hdr.record_length = sizeof(*rcd) + size;
1080 rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
1081 rcd->hdr.notification_type = CPER_NOTIFY_MCE;
1082 rcd->hdr.record_id = cper_next_record_id();
1083 rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
1085 rcd->sec_hdr.section_offset = sizeof(*rcd);
1086 rcd->sec_hdr.section_length = size;
1087 rcd->sec_hdr.revision = CPER_SEC_REV;
1088 /* fru_id and fru_text is invalid */
1089 rcd->sec_hdr.validation_bits = 0;
1090 rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
1092 case PSTORE_TYPE_DMESG:
1094 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG_Z;
1096 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
1098 case PSTORE_TYPE_MCE:
1099 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
1104 rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
1106 ret = erst_write(&rcd->hdr);
1107 *id = rcd->hdr.record_id;
1112 static int erst_clearer(enum pstore_type_id type, u64 id, int count,
1113 struct timespec time, struct pstore_info *psi)
1115 return erst_clear(id);
1118 static int __init erst_init(void)
1122 struct apei_exec_context ctx;
1123 struct apei_resources erst_resources;
1132 "Error Record Serialization Table (ERST) support is disabled.\n");
1136 status = acpi_get_table(ACPI_SIG_ERST, 0,
1137 (struct acpi_table_header **)&erst_tab);
1138 if (status == AE_NOT_FOUND)
1140 else if (ACPI_FAILURE(status)) {
1141 const char *msg = acpi_format_exception(status);
1142 pr_err("Failed to get table, %s\n", msg);
1147 rc = erst_check_table(erst_tab);
1149 pr_err(FW_BUG "ERST table is invalid.\n");
1153 apei_resources_init(&erst_resources);
1154 erst_exec_ctx_init(&ctx);
1155 rc = apei_exec_collect_resources(&ctx, &erst_resources);
1158 rc = apei_resources_request(&erst_resources, "APEI ERST");
1161 rc = apei_exec_pre_map_gars(&ctx);
1164 rc = erst_get_erange(&erst_erange);
1168 "The corresponding hardware device or firmware implementation "
1169 "is not available.\n");
1171 pr_err("Failed to get Error Log Address Range.\n");
1175 r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
1177 pr_err("Can not request [mem %#010llx-%#010llx] for ERST.\n",
1178 (unsigned long long)erst_erange.base,
1179 (unsigned long long)erst_erange.base + erst_erange.size - 1);
1184 erst_erange.vaddr = ioremap_cache(erst_erange.base,
1186 if (!erst_erange.vaddr)
1187 goto err_release_erange;
1190 "Error Record Serialization Table (ERST) support is initialized.\n");
1192 buf = kmalloc(erst_erange.size, GFP_KERNEL);
1193 spin_lock_init(&erst_info.buf_lock);
1195 erst_info.buf = buf + sizeof(struct cper_pstore_record);
1196 erst_info.bufsize = erst_erange.size -
1197 sizeof(struct cper_pstore_record);
1198 rc = pstore_register(&erst_info);
1202 "Could not register with persistent store.\n");
1203 erst_info.buf = NULL;
1204 erst_info.bufsize = 0;
1209 "Failed to allocate %lld bytes for persistent store error log.\n",
1215 release_mem_region(erst_erange.base, erst_erange.size);
1217 apei_exec_post_unmap_gars(&ctx);
1219 apei_resources_release(&erst_resources);
1221 apei_resources_fini(&erst_resources);
1227 device_initcall(erst_init);