Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * APEI Error Record Serialization Table support
0004  *
0005  * ERST is a way provided by APEI to save and retrieve hardware error
0006  * information to and from a persistent store.
0007  *
0008  * For more information about ERST, please refer to ACPI Specification
0009  * version 4.0, section 17.4.
0010  *
0011  * Copyright 2010 Intel Corp.
0012  *   Author: Huang Ying <ying.huang@intel.com>
0013  */
0014 
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/init.h>
0018 #include <linux/delay.h>
0019 #include <linux/io.h>
0020 #include <linux/acpi.h>
0021 #include <linux/uaccess.h>
0022 #include <linux/cper.h>
0023 #include <linux/nmi.h>
0024 #include <linux/hardirq.h>
0025 #include <linux/pstore.h>
0026 #include <linux/vmalloc.h>
0027 #include <linux/mm.h> /* kvfree() */
0028 #include <acpi/apei.h>
0029 
0030 #include "apei-internal.h"
0031 
0032 #undef pr_fmt
0033 #define pr_fmt(fmt) "ERST: " fmt
0034 
0035 /* ERST command status */
0036 #define ERST_STATUS_SUCCESS         0x0
0037 #define ERST_STATUS_NOT_ENOUGH_SPACE        0x1
0038 #define ERST_STATUS_HARDWARE_NOT_AVAILABLE  0x2
0039 #define ERST_STATUS_FAILED          0x3
0040 #define ERST_STATUS_RECORD_STORE_EMPTY      0x4
0041 #define ERST_STATUS_RECORD_NOT_FOUND        0x5
0042 
0043 #define ERST_TAB_ENTRY(tab)                     \
0044     ((struct acpi_whea_header *)((char *)(tab) +            \
0045                      sizeof(struct acpi_table_erst)))
0046 
0047 #define SPIN_UNIT       100         /* 100ns */
0048 /* Firmware should respond within 1 milliseconds */
0049 #define FIRMWARE_TIMEOUT    (1 * NSEC_PER_MSEC)
0050 #define FIRMWARE_MAX_STALL  50          /* 50us */
0051 
0052 int erst_disable;
0053 EXPORT_SYMBOL_GPL(erst_disable);
0054 
0055 static struct acpi_table_erst *erst_tab;
0056 
0057 /* ERST Error Log Address Range attributes */
0058 #define ERST_RANGE_RESERVED 0x0001
0059 #define ERST_RANGE_NVRAM    0x0002
0060 #define ERST_RANGE_SLOW     0x0004
0061 
0062 /*
0063  * ERST Error Log Address Range, used as buffer for reading/writing
0064  * error records.
0065  */
0066 static struct erst_erange {
0067     u64 base;
0068     u64 size;
0069     void __iomem *vaddr;
0070     u32 attr;
0071 } erst_erange;
0072 
0073 /*
0074  * Prevent ERST interpreter to run simultaneously, because the
0075  * corresponding firmware implementation may not work properly when
0076  * invoked simultaneously.
0077  *
0078  * It is used to provide exclusive accessing for ERST Error Log
0079  * Address Range too.
0080  */
0081 static DEFINE_RAW_SPINLOCK(erst_lock);
0082 
0083 static inline int erst_errno(int command_status)
0084 {
0085     switch (command_status) {
0086     case ERST_STATUS_SUCCESS:
0087         return 0;
0088     case ERST_STATUS_HARDWARE_NOT_AVAILABLE:
0089         return -ENODEV;
0090     case ERST_STATUS_NOT_ENOUGH_SPACE:
0091         return -ENOSPC;
0092     case ERST_STATUS_RECORD_STORE_EMPTY:
0093     case ERST_STATUS_RECORD_NOT_FOUND:
0094         return -ENOENT;
0095     default:
0096         return -EINVAL;
0097     }
0098 }
0099 
0100 static int erst_timedout(u64 *t, u64 spin_unit)
0101 {
0102     if ((s64)*t < spin_unit) {
0103         pr_warn(FW_WARN "Firmware does not respond in time.\n");
0104         return 1;
0105     }
0106     *t -= spin_unit;
0107     ndelay(spin_unit);
0108     touch_nmi_watchdog();
0109     return 0;
0110 }
0111 
0112 static int erst_exec_load_var1(struct apei_exec_context *ctx,
0113                    struct acpi_whea_header *entry)
0114 {
0115     return __apei_exec_read_register(entry, &ctx->var1);
0116 }
0117 
0118 static int erst_exec_load_var2(struct apei_exec_context *ctx,
0119                    struct acpi_whea_header *entry)
0120 {
0121     return __apei_exec_read_register(entry, &ctx->var2);
0122 }
0123 
0124 static int erst_exec_store_var1(struct apei_exec_context *ctx,
0125                 struct acpi_whea_header *entry)
0126 {
0127     return __apei_exec_write_register(entry, ctx->var1);
0128 }
0129 
0130 static int erst_exec_add(struct apei_exec_context *ctx,
0131              struct acpi_whea_header *entry)
0132 {
0133     ctx->var1 += ctx->var2;
0134     return 0;
0135 }
0136 
0137 static int erst_exec_subtract(struct apei_exec_context *ctx,
0138                   struct acpi_whea_header *entry)
0139 {
0140     ctx->var1 -= ctx->var2;
0141     return 0;
0142 }
0143 
0144 static int erst_exec_add_value(struct apei_exec_context *ctx,
0145                    struct acpi_whea_header *entry)
0146 {
0147     int rc;
0148     u64 val;
0149 
0150     rc = __apei_exec_read_register(entry, &val);
0151     if (rc)
0152         return rc;
0153     val += ctx->value;
0154     rc = __apei_exec_write_register(entry, val);
0155     return rc;
0156 }
0157 
0158 static int erst_exec_subtract_value(struct apei_exec_context *ctx,
0159                     struct acpi_whea_header *entry)
0160 {
0161     int rc;
0162     u64 val;
0163 
0164     rc = __apei_exec_read_register(entry, &val);
0165     if (rc)
0166         return rc;
0167     val -= ctx->value;
0168     rc = __apei_exec_write_register(entry, val);
0169     return rc;
0170 }
0171 
0172 static int erst_exec_stall(struct apei_exec_context *ctx,
0173                struct acpi_whea_header *entry)
0174 {
0175     u64 stall_time;
0176 
0177     if (ctx->value > FIRMWARE_MAX_STALL) {
0178         if (!in_nmi())
0179             pr_warn(FW_WARN
0180             "Too long stall time for stall instruction: 0x%llx.\n",
0181                    ctx->value);
0182         stall_time = FIRMWARE_MAX_STALL;
0183     } else
0184         stall_time = ctx->value;
0185     udelay(stall_time);
0186     return 0;
0187 }
0188 
0189 static int erst_exec_stall_while_true(struct apei_exec_context *ctx,
0190                       struct acpi_whea_header *entry)
0191 {
0192     int rc;
0193     u64 val;
0194     u64 timeout = FIRMWARE_TIMEOUT;
0195     u64 stall_time;
0196 
0197     if (ctx->var1 > FIRMWARE_MAX_STALL) {
0198         if (!in_nmi())
0199             pr_warn(FW_WARN
0200         "Too long stall time for stall while true instruction: 0x%llx.\n",
0201                    ctx->var1);
0202         stall_time = FIRMWARE_MAX_STALL;
0203     } else
0204         stall_time = ctx->var1;
0205 
0206     for (;;) {
0207         rc = __apei_exec_read_register(entry, &val);
0208         if (rc)
0209             return rc;
0210         if (val != ctx->value)
0211             break;
0212         if (erst_timedout(&timeout, stall_time * NSEC_PER_USEC))
0213             return -EIO;
0214     }
0215     return 0;
0216 }
0217 
0218 static int erst_exec_skip_next_instruction_if_true(
0219     struct apei_exec_context *ctx,
0220     struct acpi_whea_header *entry)
0221 {
0222     int rc;
0223     u64 val;
0224 
0225     rc = __apei_exec_read_register(entry, &val);
0226     if (rc)
0227         return rc;
0228     if (val == ctx->value) {
0229         ctx->ip += 2;
0230         return APEI_EXEC_SET_IP;
0231     }
0232 
0233     return 0;
0234 }
0235 
0236 static int erst_exec_goto(struct apei_exec_context *ctx,
0237               struct acpi_whea_header *entry)
0238 {
0239     ctx->ip = ctx->value;
0240     return APEI_EXEC_SET_IP;
0241 }
0242 
0243 static int erst_exec_set_src_address_base(struct apei_exec_context *ctx,
0244                       struct acpi_whea_header *entry)
0245 {
0246     return __apei_exec_read_register(entry, &ctx->src_base);
0247 }
0248 
0249 static int erst_exec_set_dst_address_base(struct apei_exec_context *ctx,
0250                       struct acpi_whea_header *entry)
0251 {
0252     return __apei_exec_read_register(entry, &ctx->dst_base);
0253 }
0254 
0255 static int erst_exec_move_data(struct apei_exec_context *ctx,
0256                    struct acpi_whea_header *entry)
0257 {
0258     int rc;
0259     u64 offset;
0260     void *src, *dst;
0261 
0262     /* ioremap does not work in interrupt context */
0263     if (in_interrupt()) {
0264         pr_warn("MOVE_DATA can not be used in interrupt context.\n");
0265         return -EBUSY;
0266     }
0267 
0268     rc = __apei_exec_read_register(entry, &offset);
0269     if (rc)
0270         return rc;
0271 
0272     src = ioremap(ctx->src_base + offset, ctx->var2);
0273     if (!src)
0274         return -ENOMEM;
0275     dst = ioremap(ctx->dst_base + offset, ctx->var2);
0276     if (!dst) {
0277         iounmap(src);
0278         return -ENOMEM;
0279     }
0280 
0281     memmove(dst, src, ctx->var2);
0282 
0283     iounmap(src);
0284     iounmap(dst);
0285 
0286     return 0;
0287 }
0288 
0289 static struct apei_exec_ins_type erst_ins_type[] = {
0290     [ACPI_ERST_READ_REGISTER] = {
0291         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0292         .run = apei_exec_read_register,
0293     },
0294     [ACPI_ERST_READ_REGISTER_VALUE] = {
0295         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0296         .run = apei_exec_read_register_value,
0297     },
0298     [ACPI_ERST_WRITE_REGISTER] = {
0299         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0300         .run = apei_exec_write_register,
0301     },
0302     [ACPI_ERST_WRITE_REGISTER_VALUE] = {
0303         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0304         .run = apei_exec_write_register_value,
0305     },
0306     [ACPI_ERST_NOOP] = {
0307         .flags = 0,
0308         .run = apei_exec_noop,
0309     },
0310     [ACPI_ERST_LOAD_VAR1] = {
0311         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0312         .run = erst_exec_load_var1,
0313     },
0314     [ACPI_ERST_LOAD_VAR2] = {
0315         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0316         .run = erst_exec_load_var2,
0317     },
0318     [ACPI_ERST_STORE_VAR1] = {
0319         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0320         .run = erst_exec_store_var1,
0321     },
0322     [ACPI_ERST_ADD] = {
0323         .flags = 0,
0324         .run = erst_exec_add,
0325     },
0326     [ACPI_ERST_SUBTRACT] = {
0327         .flags = 0,
0328         .run = erst_exec_subtract,
0329     },
0330     [ACPI_ERST_ADD_VALUE] = {
0331         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0332         .run = erst_exec_add_value,
0333     },
0334     [ACPI_ERST_SUBTRACT_VALUE] = {
0335         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0336         .run = erst_exec_subtract_value,
0337     },
0338     [ACPI_ERST_STALL] = {
0339         .flags = 0,
0340         .run = erst_exec_stall,
0341     },
0342     [ACPI_ERST_STALL_WHILE_TRUE] = {
0343         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0344         .run = erst_exec_stall_while_true,
0345     },
0346     [ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
0347         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0348         .run = erst_exec_skip_next_instruction_if_true,
0349     },
0350     [ACPI_ERST_GOTO] = {
0351         .flags = 0,
0352         .run = erst_exec_goto,
0353     },
0354     [ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
0355         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0356         .run = erst_exec_set_src_address_base,
0357     },
0358     [ACPI_ERST_SET_DST_ADDRESS_BASE] = {
0359         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0360         .run = erst_exec_set_dst_address_base,
0361     },
0362     [ACPI_ERST_MOVE_DATA] = {
0363         .flags = APEI_EXEC_INS_ACCESS_REGISTER,
0364         .run = erst_exec_move_data,
0365     },
0366 };
0367 
0368 static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
0369 {
0370     apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
0371                ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
0372 }
0373 
0374 static int erst_get_erange(struct erst_erange *range)
0375 {
0376     struct apei_exec_context ctx;
0377     int rc;
0378 
0379     erst_exec_ctx_init(&ctx);
0380     rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
0381     if (rc)
0382         return rc;
0383     range->base = apei_exec_ctx_get_output(&ctx);
0384     rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
0385     if (rc)
0386         return rc;
0387     range->size = apei_exec_ctx_get_output(&ctx);
0388     rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
0389     if (rc)
0390         return rc;
0391     range->attr = apei_exec_ctx_get_output(&ctx);
0392 
0393     return 0;
0394 }
0395 
0396 static ssize_t __erst_get_record_count(void)
0397 {
0398     struct apei_exec_context ctx;
0399     int rc;
0400 
0401     erst_exec_ctx_init(&ctx);
0402     rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
0403     if (rc)
0404         return rc;
0405     return apei_exec_ctx_get_output(&ctx);
0406 }
0407 
0408 ssize_t erst_get_record_count(void)
0409 {
0410     ssize_t count;
0411     unsigned long flags;
0412 
0413     if (erst_disable)
0414         return -ENODEV;
0415 
0416     raw_spin_lock_irqsave(&erst_lock, flags);
0417     count = __erst_get_record_count();
0418     raw_spin_unlock_irqrestore(&erst_lock, flags);
0419 
0420     return count;
0421 }
0422 EXPORT_SYMBOL_GPL(erst_get_record_count);
0423 
0424 #define ERST_RECORD_ID_CACHE_SIZE_MIN   16
0425 #define ERST_RECORD_ID_CACHE_SIZE_MAX   1024
0426 
0427 struct erst_record_id_cache {
0428     struct mutex lock;
0429     u64 *entries;
0430     int len;
0431     int size;
0432     int refcount;
0433 };
0434 
0435 static struct erst_record_id_cache erst_record_id_cache = {
0436     .lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock),
0437     .refcount = 0,
0438 };
0439 
0440 static int __erst_get_next_record_id(u64 *record_id)
0441 {
0442     struct apei_exec_context ctx;
0443     int rc;
0444 
0445     erst_exec_ctx_init(&ctx);
0446     rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
0447     if (rc)
0448         return rc;
0449     *record_id = apei_exec_ctx_get_output(&ctx);
0450 
0451     return 0;
0452 }
0453 
0454 int erst_get_record_id_begin(int *pos)
0455 {
0456     int rc;
0457 
0458     if (erst_disable)
0459         return -ENODEV;
0460 
0461     rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
0462     if (rc)
0463         return rc;
0464     erst_record_id_cache.refcount++;
0465     mutex_unlock(&erst_record_id_cache.lock);
0466 
0467     *pos = 0;
0468 
0469     return 0;
0470 }
0471 EXPORT_SYMBOL_GPL(erst_get_record_id_begin);
0472 
0473 /* erst_record_id_cache.lock must be held by caller */
0474 static int __erst_record_id_cache_add_one(void)
0475 {
0476     u64 id, prev_id, first_id;
0477     int i, rc;
0478     u64 *entries;
0479     unsigned long flags;
0480 
0481     id = prev_id = first_id = APEI_ERST_INVALID_RECORD_ID;
0482 retry:
0483     raw_spin_lock_irqsave(&erst_lock, flags);
0484     rc = __erst_get_next_record_id(&id);
0485     raw_spin_unlock_irqrestore(&erst_lock, flags);
0486     if (rc == -ENOENT)
0487         return 0;
0488     if (rc)
0489         return rc;
0490     if (id == APEI_ERST_INVALID_RECORD_ID)
0491         return 0;
0492     /* can not skip current ID, or loop back to first ID */
0493     if (id == prev_id || id == first_id)
0494         return 0;
0495     if (first_id == APEI_ERST_INVALID_RECORD_ID)
0496         first_id = id;
0497     prev_id = id;
0498 
0499     entries = erst_record_id_cache.entries;
0500     for (i = 0; i < erst_record_id_cache.len; i++) {
0501         if (entries[i] == id)
0502             break;
0503     }
0504     /* record id already in cache, try next */
0505     if (i < erst_record_id_cache.len)
0506         goto retry;
0507     if (erst_record_id_cache.len >= erst_record_id_cache.size) {
0508         int new_size;
0509         u64 *new_entries;
0510 
0511         new_size = erst_record_id_cache.size * 2;
0512         new_size = clamp_val(new_size, ERST_RECORD_ID_CACHE_SIZE_MIN,
0513                      ERST_RECORD_ID_CACHE_SIZE_MAX);
0514         if (new_size <= erst_record_id_cache.size) {
0515             if (printk_ratelimit())
0516                 pr_warn(FW_WARN "too many record IDs!\n");
0517             return 0;
0518         }
0519         new_entries = kvmalloc_array(new_size, sizeof(entries[0]),
0520                          GFP_KERNEL);
0521         if (!new_entries)
0522             return -ENOMEM;
0523         memcpy(new_entries, entries,
0524                erst_record_id_cache.len * sizeof(entries[0]));
0525         kvfree(entries);
0526         erst_record_id_cache.entries = entries = new_entries;
0527         erst_record_id_cache.size = new_size;
0528     }
0529     entries[i] = id;
0530     erst_record_id_cache.len++;
0531 
0532     return 1;
0533 }
0534 
0535 /*
0536  * Get the record ID of an existing error record on the persistent
0537  * storage. If there is no error record on the persistent storage, the
0538  * returned record_id is APEI_ERST_INVALID_RECORD_ID.
0539  */
0540 int erst_get_record_id_next(int *pos, u64 *record_id)
0541 {
0542     int rc = 0;
0543     u64 *entries;
0544 
0545     if (erst_disable)
0546         return -ENODEV;
0547 
0548     /* must be enclosed by erst_get_record_id_begin/end */
0549     BUG_ON(!erst_record_id_cache.refcount);
0550     BUG_ON(*pos < 0 || *pos > erst_record_id_cache.len);
0551 
0552     mutex_lock(&erst_record_id_cache.lock);
0553     entries = erst_record_id_cache.entries;
0554     for (; *pos < erst_record_id_cache.len; (*pos)++)
0555         if (entries[*pos] != APEI_ERST_INVALID_RECORD_ID)
0556             break;
0557     /* found next record id in cache */
0558     if (*pos < erst_record_id_cache.len) {
0559         *record_id = entries[*pos];
0560         (*pos)++;
0561         goto out_unlock;
0562     }
0563 
0564     /* Try to add one more record ID to cache */
0565     rc = __erst_record_id_cache_add_one();
0566     if (rc < 0)
0567         goto out_unlock;
0568     /* successfully add one new ID */
0569     if (rc == 1) {
0570         *record_id = erst_record_id_cache.entries[*pos];
0571         (*pos)++;
0572         rc = 0;
0573     } else {
0574         *pos = -1;
0575         *record_id = APEI_ERST_INVALID_RECORD_ID;
0576     }
0577 out_unlock:
0578     mutex_unlock(&erst_record_id_cache.lock);
0579 
0580     return rc;
0581 }
0582 EXPORT_SYMBOL_GPL(erst_get_record_id_next);
0583 
0584 /* erst_record_id_cache.lock must be held by caller */
0585 static void __erst_record_id_cache_compact(void)
0586 {
0587     int i, wpos = 0;
0588     u64 *entries;
0589 
0590     if (erst_record_id_cache.refcount)
0591         return;
0592 
0593     entries = erst_record_id_cache.entries;
0594     for (i = 0; i < erst_record_id_cache.len; i++) {
0595         if (entries[i] == APEI_ERST_INVALID_RECORD_ID)
0596             continue;
0597         if (wpos != i)
0598             entries[wpos] = entries[i];
0599         wpos++;
0600     }
0601     erst_record_id_cache.len = wpos;
0602 }
0603 
0604 void erst_get_record_id_end(void)
0605 {
0606     /*
0607      * erst_disable != 0 should be detected by invoker via the
0608      * return value of erst_get_record_id_begin/next, so this
0609      * function should not be called for erst_disable != 0.
0610      */
0611     BUG_ON(erst_disable);
0612 
0613     mutex_lock(&erst_record_id_cache.lock);
0614     erst_record_id_cache.refcount--;
0615     BUG_ON(erst_record_id_cache.refcount < 0);
0616     __erst_record_id_cache_compact();
0617     mutex_unlock(&erst_record_id_cache.lock);
0618 }
0619 EXPORT_SYMBOL_GPL(erst_get_record_id_end);
0620 
0621 static int __erst_write_to_storage(u64 offset)
0622 {
0623     struct apei_exec_context ctx;
0624     u64 timeout = FIRMWARE_TIMEOUT;
0625     u64 val;
0626     int rc;
0627 
0628     erst_exec_ctx_init(&ctx);
0629     rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_WRITE);
0630     if (rc)
0631         return rc;
0632     apei_exec_ctx_set_input(&ctx, offset);
0633     rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
0634     if (rc)
0635         return rc;
0636     rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
0637     if (rc)
0638         return rc;
0639     for (;;) {
0640         rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
0641         if (rc)
0642             return rc;
0643         val = apei_exec_ctx_get_output(&ctx);
0644         if (!val)
0645             break;
0646         if (erst_timedout(&timeout, SPIN_UNIT))
0647             return -EIO;
0648     }
0649     rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
0650     if (rc)
0651         return rc;
0652     val = apei_exec_ctx_get_output(&ctx);
0653     rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
0654     if (rc)
0655         return rc;
0656 
0657     return erst_errno(val);
0658 }
0659 
0660 static int __erst_read_from_storage(u64 record_id, u64 offset)
0661 {
0662     struct apei_exec_context ctx;
0663     u64 timeout = FIRMWARE_TIMEOUT;
0664     u64 val;
0665     int rc;
0666 
0667     erst_exec_ctx_init(&ctx);
0668     rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_READ);
0669     if (rc)
0670         return rc;
0671     apei_exec_ctx_set_input(&ctx, offset);
0672     rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
0673     if (rc)
0674         return rc;
0675     apei_exec_ctx_set_input(&ctx, record_id);
0676     rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
0677     if (rc)
0678         return rc;
0679     rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
0680     if (rc)
0681         return rc;
0682     for (;;) {
0683         rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
0684         if (rc)
0685             return rc;
0686         val = apei_exec_ctx_get_output(&ctx);
0687         if (!val)
0688             break;
0689         if (erst_timedout(&timeout, SPIN_UNIT))
0690             return -EIO;
0691     }
0692     rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
0693     if (rc)
0694         return rc;
0695     val = apei_exec_ctx_get_output(&ctx);
0696     rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
0697     if (rc)
0698         return rc;
0699 
0700     return erst_errno(val);
0701 }
0702 
0703 static int __erst_clear_from_storage(u64 record_id)
0704 {
0705     struct apei_exec_context ctx;
0706     u64 timeout = FIRMWARE_TIMEOUT;
0707     u64 val;
0708     int rc;
0709 
0710     erst_exec_ctx_init(&ctx);
0711     rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_CLEAR);
0712     if (rc)
0713         return rc;
0714     apei_exec_ctx_set_input(&ctx, record_id);
0715     rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
0716     if (rc)
0717         return rc;
0718     rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
0719     if (rc)
0720         return rc;
0721     for (;;) {
0722         rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
0723         if (rc)
0724             return rc;
0725         val = apei_exec_ctx_get_output(&ctx);
0726         if (!val)
0727             break;
0728         if (erst_timedout(&timeout, SPIN_UNIT))
0729             return -EIO;
0730     }
0731     rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
0732     if (rc)
0733         return rc;
0734     val = apei_exec_ctx_get_output(&ctx);
0735     rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
0736     if (rc)
0737         return rc;
0738 
0739     return erst_errno(val);
0740 }
0741 
0742 /* NVRAM ERST Error Log Address Range is not supported yet */
0743 static void pr_unimpl_nvram(void)
0744 {
0745     if (printk_ratelimit())
0746         pr_warn("NVRAM ERST Log Address Range not implemented yet.\n");
0747 }
0748 
0749 static int __erst_write_to_nvram(const struct cper_record_header *record)
0750 {
0751     /* do not print message, because printk is not safe for NMI */
0752     return -ENOSYS;
0753 }
0754 
0755 static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
0756 {
0757     pr_unimpl_nvram();
0758     return -ENOSYS;
0759 }
0760 
0761 static int __erst_clear_from_nvram(u64 record_id)
0762 {
0763     pr_unimpl_nvram();
0764     return -ENOSYS;
0765 }
0766 
0767 int erst_write(const struct cper_record_header *record)
0768 {
0769     int rc;
0770     unsigned long flags;
0771     struct cper_record_header *rcd_erange;
0772 
0773     if (erst_disable)
0774         return -ENODEV;
0775 
0776     if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
0777         return -EINVAL;
0778 
0779     if (erst_erange.attr & ERST_RANGE_NVRAM) {
0780         if (!raw_spin_trylock_irqsave(&erst_lock, flags))
0781             return -EBUSY;
0782         rc = __erst_write_to_nvram(record);
0783         raw_spin_unlock_irqrestore(&erst_lock, flags);
0784         return rc;
0785     }
0786 
0787     if (record->record_length > erst_erange.size)
0788         return -EINVAL;
0789 
0790     if (!raw_spin_trylock_irqsave(&erst_lock, flags))
0791         return -EBUSY;
0792     memcpy(erst_erange.vaddr, record, record->record_length);
0793     rcd_erange = erst_erange.vaddr;
0794     /* signature for serialization system */
0795     memcpy(&rcd_erange->persistence_information, "ER", 2);
0796 
0797     rc = __erst_write_to_storage(0);
0798     raw_spin_unlock_irqrestore(&erst_lock, flags);
0799 
0800     return rc;
0801 }
0802 EXPORT_SYMBOL_GPL(erst_write);
0803 
0804 static int __erst_read_to_erange(u64 record_id, u64 *offset)
0805 {
0806     int rc;
0807 
0808     if (erst_erange.attr & ERST_RANGE_NVRAM)
0809         return __erst_read_to_erange_from_nvram(
0810             record_id, offset);
0811 
0812     rc = __erst_read_from_storage(record_id, 0);
0813     if (rc)
0814         return rc;
0815     *offset = 0;
0816 
0817     return 0;
0818 }
0819 
0820 static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
0821                size_t buflen)
0822 {
0823     int rc;
0824     u64 offset, len = 0;
0825     struct cper_record_header *rcd_tmp;
0826 
0827     rc = __erst_read_to_erange(record_id, &offset);
0828     if (rc)
0829         return rc;
0830     rcd_tmp = erst_erange.vaddr + offset;
0831     len = rcd_tmp->record_length;
0832     if (len <= buflen)
0833         memcpy(record, rcd_tmp, len);
0834 
0835     return len;
0836 }
0837 
0838 /*
0839  * If return value > buflen, the buffer size is not big enough,
0840  * else if return value < 0, something goes wrong,
0841  * else everything is OK, and return value is record length
0842  */
0843 ssize_t erst_read(u64 record_id, struct cper_record_header *record,
0844           size_t buflen)
0845 {
0846     ssize_t len;
0847     unsigned long flags;
0848 
0849     if (erst_disable)
0850         return -ENODEV;
0851 
0852     raw_spin_lock_irqsave(&erst_lock, flags);
0853     len = __erst_read(record_id, record, buflen);
0854     raw_spin_unlock_irqrestore(&erst_lock, flags);
0855     return len;
0856 }
0857 EXPORT_SYMBOL_GPL(erst_read);
0858 
0859 static void erst_clear_cache(u64 record_id)
0860 {
0861     int i;
0862     u64 *entries;
0863 
0864     mutex_lock(&erst_record_id_cache.lock);
0865 
0866     entries = erst_record_id_cache.entries;
0867     for (i = 0; i < erst_record_id_cache.len; i++) {
0868         if (entries[i] == record_id)
0869             entries[i] = APEI_ERST_INVALID_RECORD_ID;
0870     }
0871     __erst_record_id_cache_compact();
0872 
0873     mutex_unlock(&erst_record_id_cache.lock);
0874 }
0875 
0876 ssize_t erst_read_record(u64 record_id, struct cper_record_header *record,
0877         size_t buflen, size_t recordlen, const guid_t *creatorid)
0878 {
0879     ssize_t len;
0880 
0881     /*
0882      * if creatorid is NULL, read any record for erst-dbg module
0883      */
0884     if (creatorid == NULL) {
0885         len = erst_read(record_id, record, buflen);
0886         if (len == -ENOENT)
0887             erst_clear_cache(record_id);
0888 
0889         return len;
0890     }
0891 
0892     len = erst_read(record_id, record, buflen);
0893     /*
0894      * if erst_read return value is -ENOENT skip to next record_id,
0895      * and clear the record_id cache.
0896      */
0897     if (len == -ENOENT) {
0898         erst_clear_cache(record_id);
0899         goto out;
0900     }
0901 
0902     if (len < 0)
0903         goto out;
0904 
0905     /*
0906      * if erst_read return value is less than record head length,
0907      * consider it as -EIO, and clear the record_id cache.
0908      */
0909     if (len < recordlen) {
0910         len = -EIO;
0911         erst_clear_cache(record_id);
0912         goto out;
0913     }
0914 
0915     /*
0916      * if creatorid is not wanted, consider it as not found,
0917      * for skipping to next record_id.
0918      */
0919     if (!guid_equal(&record->creator_id, creatorid))
0920         len = -ENOENT;
0921 
0922 out:
0923     return len;
0924 }
0925 EXPORT_SYMBOL_GPL(erst_read_record);
0926 
0927 int erst_clear(u64 record_id)
0928 {
0929     int rc, i;
0930     unsigned long flags;
0931     u64 *entries;
0932 
0933     if (erst_disable)
0934         return -ENODEV;
0935 
0936     rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
0937     if (rc)
0938         return rc;
0939     raw_spin_lock_irqsave(&erst_lock, flags);
0940     if (erst_erange.attr & ERST_RANGE_NVRAM)
0941         rc = __erst_clear_from_nvram(record_id);
0942     else
0943         rc = __erst_clear_from_storage(record_id);
0944     raw_spin_unlock_irqrestore(&erst_lock, flags);
0945     if (rc)
0946         goto out;
0947     entries = erst_record_id_cache.entries;
0948     for (i = 0; i < erst_record_id_cache.len; i++) {
0949         if (entries[i] == record_id)
0950             entries[i] = APEI_ERST_INVALID_RECORD_ID;
0951     }
0952     __erst_record_id_cache_compact();
0953 out:
0954     mutex_unlock(&erst_record_id_cache.lock);
0955     return rc;
0956 }
0957 EXPORT_SYMBOL_GPL(erst_clear);
0958 
0959 static int __init setup_erst_disable(char *str)
0960 {
0961     erst_disable = 1;
0962     return 1;
0963 }
0964 
0965 __setup("erst_disable", setup_erst_disable);
0966 
0967 static int erst_check_table(struct acpi_table_erst *erst_tab)
0968 {
0969     if ((erst_tab->header_length !=
0970          (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
0971         && (erst_tab->header_length != sizeof(struct acpi_table_erst)))
0972         return -EINVAL;
0973     if (erst_tab->header.length < sizeof(struct acpi_table_erst))
0974         return -EINVAL;
0975     if (erst_tab->entries !=
0976         (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
0977         sizeof(struct acpi_erst_entry))
0978         return -EINVAL;
0979 
0980     return 0;
0981 }
0982 
0983 static int erst_open_pstore(struct pstore_info *psi);
0984 static int erst_close_pstore(struct pstore_info *psi);
0985 static ssize_t erst_reader(struct pstore_record *record);
0986 static int erst_writer(struct pstore_record *record);
0987 static int erst_clearer(struct pstore_record *record);
0988 
0989 static struct pstore_info erst_info = {
0990     .owner      = THIS_MODULE,
0991     .name       = "erst",
0992     .flags      = PSTORE_FLAGS_DMESG,
0993     .open       = erst_open_pstore,
0994     .close      = erst_close_pstore,
0995     .read       = erst_reader,
0996     .write      = erst_writer,
0997     .erase      = erst_clearer
0998 };
0999 
1000 #define CPER_CREATOR_PSTORE                     \
1001     GUID_INIT(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c,   \
1002           0x64, 0x90, 0xb8, 0x9d)
1003 #define CPER_SECTION_TYPE_DMESG                     \
1004     GUID_INIT(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54,   \
1005           0x94, 0x19, 0xeb, 0x12)
1006 #define CPER_SECTION_TYPE_DMESG_Z                   \
1007     GUID_INIT(0x4f118707, 0x04dd, 0x4055, 0xb5, 0xdd, 0x95, 0x6d,   \
1008           0x34, 0xdd, 0xfa, 0xc6)
1009 #define CPER_SECTION_TYPE_MCE                       \
1010     GUID_INIT(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96,   \
1011           0x04, 0x4a, 0x38, 0xfc)
1012 
1013 struct cper_pstore_record {
1014     struct cper_record_header hdr;
1015     struct cper_section_descriptor sec_hdr;
1016     char data[];
1017 } __packed;
1018 
1019 static int reader_pos;
1020 
1021 static int erst_open_pstore(struct pstore_info *psi)
1022 {
1023     int rc;
1024 
1025     if (erst_disable)
1026         return -ENODEV;
1027 
1028     rc = erst_get_record_id_begin(&reader_pos);
1029 
1030     return rc;
1031 }
1032 
1033 static int erst_close_pstore(struct pstore_info *psi)
1034 {
1035     erst_get_record_id_end();
1036 
1037     return 0;
1038 }
1039 
1040 static ssize_t erst_reader(struct pstore_record *record)
1041 {
1042     int rc;
1043     ssize_t len = 0;
1044     u64 record_id;
1045     struct cper_pstore_record *rcd;
1046     size_t rcd_len = sizeof(*rcd) + erst_info.bufsize;
1047 
1048     if (erst_disable)
1049         return -ENODEV;
1050 
1051     rcd = kmalloc(rcd_len, GFP_KERNEL);
1052     if (!rcd) {
1053         rc = -ENOMEM;
1054         goto out;
1055     }
1056 skip:
1057     rc = erst_get_record_id_next(&reader_pos, &record_id);
1058     if (rc)
1059         goto out;
1060 
1061     /* no more record */
1062     if (record_id == APEI_ERST_INVALID_RECORD_ID) {
1063         rc = -EINVAL;
1064         goto out;
1065     }
1066 
1067     len = erst_read_record(record_id, &rcd->hdr, rcd_len, sizeof(*rcd),
1068             &CPER_CREATOR_PSTORE);
1069     /* The record may be cleared by others, try read next record */
1070     if (len == -ENOENT)
1071         goto skip;
1072     else if (len < 0)
1073         goto out;
1074 
1075     record->buf = kmalloc(len, GFP_KERNEL);
1076     if (record->buf == NULL) {
1077         rc = -ENOMEM;
1078         goto out;
1079     }
1080     memcpy(record->buf, rcd->data, len - sizeof(*rcd));
1081     record->id = record_id;
1082     record->compressed = false;
1083     record->ecc_notice_size = 0;
1084     if (guid_equal(&rcd->sec_hdr.section_type, &CPER_SECTION_TYPE_DMESG_Z)) {
1085         record->type = PSTORE_TYPE_DMESG;
1086         record->compressed = true;
1087     } else if (guid_equal(&rcd->sec_hdr.section_type, &CPER_SECTION_TYPE_DMESG))
1088         record->type = PSTORE_TYPE_DMESG;
1089     else if (guid_equal(&rcd->sec_hdr.section_type, &CPER_SECTION_TYPE_MCE))
1090         record->type = PSTORE_TYPE_MCE;
1091     else
1092         record->type = PSTORE_TYPE_MAX;
1093 
1094     if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
1095         record->time.tv_sec = rcd->hdr.timestamp;
1096     else
1097         record->time.tv_sec = 0;
1098     record->time.tv_nsec = 0;
1099 
1100 out:
1101     kfree(rcd);
1102     return (rc < 0) ? rc : (len - sizeof(*rcd));
1103 }
1104 
1105 static int erst_writer(struct pstore_record *record)
1106 {
1107     struct cper_pstore_record *rcd = (struct cper_pstore_record *)
1108                     (erst_info.buf - sizeof(*rcd));
1109     int ret;
1110 
1111     memset(rcd, 0, sizeof(*rcd));
1112     memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
1113     rcd->hdr.revision = CPER_RECORD_REV;
1114     rcd->hdr.signature_end = CPER_SIG_END;
1115     rcd->hdr.section_count = 1;
1116     rcd->hdr.error_severity = CPER_SEV_FATAL;
1117     /* timestamp valid. platform_id, partition_id are invalid */
1118     rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
1119     rcd->hdr.timestamp = ktime_get_real_seconds();
1120     rcd->hdr.record_length = sizeof(*rcd) + record->size;
1121     rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
1122     rcd->hdr.notification_type = CPER_NOTIFY_MCE;
1123     rcd->hdr.record_id = cper_next_record_id();
1124     rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
1125 
1126     rcd->sec_hdr.section_offset = sizeof(*rcd);
1127     rcd->sec_hdr.section_length = record->size;
1128     rcd->sec_hdr.revision = CPER_SEC_REV;
1129     /* fru_id and fru_text is invalid */
1130     rcd->sec_hdr.validation_bits = 0;
1131     rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
1132     switch (record->type) {
1133     case PSTORE_TYPE_DMESG:
1134         if (record->compressed)
1135             rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG_Z;
1136         else
1137             rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
1138         break;
1139     case PSTORE_TYPE_MCE:
1140         rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
1141         break;
1142     default:
1143         return -EINVAL;
1144     }
1145     rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
1146 
1147     ret = erst_write(&rcd->hdr);
1148     record->id = rcd->hdr.record_id;
1149 
1150     return ret;
1151 }
1152 
1153 static int erst_clearer(struct pstore_record *record)
1154 {
1155     return erst_clear(record->id);
1156 }
1157 
1158 static int __init erst_init(void)
1159 {
1160     int rc = 0;
1161     acpi_status status;
1162     struct apei_exec_context ctx;
1163     struct apei_resources erst_resources;
1164     struct resource *r;
1165     char *buf;
1166 
1167     if (acpi_disabled)
1168         goto err;
1169 
1170     if (erst_disable) {
1171         pr_info(
1172     "Error Record Serialization Table (ERST) support is disabled.\n");
1173         goto err;
1174     }
1175 
1176     status = acpi_get_table(ACPI_SIG_ERST, 0,
1177                 (struct acpi_table_header **)&erst_tab);
1178     if (status == AE_NOT_FOUND)
1179         goto err;
1180     else if (ACPI_FAILURE(status)) {
1181         const char *msg = acpi_format_exception(status);
1182         pr_err("Failed to get table, %s\n", msg);
1183         rc = -EINVAL;
1184         goto err;
1185     }
1186 
1187     rc = erst_check_table(erst_tab);
1188     if (rc) {
1189         pr_err(FW_BUG "ERST table is invalid.\n");
1190         goto err_put_erst_tab;
1191     }
1192 
1193     apei_resources_init(&erst_resources);
1194     erst_exec_ctx_init(&ctx);
1195     rc = apei_exec_collect_resources(&ctx, &erst_resources);
1196     if (rc)
1197         goto err_fini;
1198     rc = apei_resources_request(&erst_resources, "APEI ERST");
1199     if (rc)
1200         goto err_fini;
1201     rc = apei_exec_pre_map_gars(&ctx);
1202     if (rc)
1203         goto err_release;
1204     rc = erst_get_erange(&erst_erange);
1205     if (rc) {
1206         if (rc == -ENODEV)
1207             pr_info(
1208     "The corresponding hardware device or firmware implementation "
1209     "is not available.\n");
1210         else
1211             pr_err("Failed to get Error Log Address Range.\n");
1212         goto err_unmap_reg;
1213     }
1214 
1215     r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
1216     if (!r) {
1217         pr_err("Can not request [mem %#010llx-%#010llx] for ERST.\n",
1218                (unsigned long long)erst_erange.base,
1219                (unsigned long long)erst_erange.base + erst_erange.size - 1);
1220         rc = -EIO;
1221         goto err_unmap_reg;
1222     }
1223     rc = -ENOMEM;
1224     erst_erange.vaddr = ioremap_cache(erst_erange.base,
1225                       erst_erange.size);
1226     if (!erst_erange.vaddr)
1227         goto err_release_erange;
1228 
1229     pr_info(
1230     "Error Record Serialization Table (ERST) support is initialized.\n");
1231 
1232     buf = kmalloc(erst_erange.size, GFP_KERNEL);
1233     if (buf) {
1234         erst_info.buf = buf + sizeof(struct cper_pstore_record);
1235         erst_info.bufsize = erst_erange.size -
1236                     sizeof(struct cper_pstore_record);
1237         rc = pstore_register(&erst_info);
1238         if (rc) {
1239             if (rc != -EPERM)
1240                 pr_info(
1241                 "Could not register with persistent store.\n");
1242             erst_info.buf = NULL;
1243             erst_info.bufsize = 0;
1244             kfree(buf);
1245         }
1246     } else
1247         pr_err(
1248         "Failed to allocate %lld bytes for persistent store error log.\n",
1249         erst_erange.size);
1250 
1251     /* Cleanup ERST Resources */
1252     apei_resources_fini(&erst_resources);
1253 
1254     return 0;
1255 
1256 err_release_erange:
1257     release_mem_region(erst_erange.base, erst_erange.size);
1258 err_unmap_reg:
1259     apei_exec_post_unmap_gars(&ctx);
1260 err_release:
1261     apei_resources_release(&erst_resources);
1262 err_fini:
1263     apei_resources_fini(&erst_resources);
1264 err_put_erst_tab:
1265     acpi_put_table((struct acpi_table_header *)erst_tab);
1266 err:
1267     erst_disable = 1;
1268     return rc;
1269 }
1270 
1271 device_initcall(erst_init);