Loading...
1/*
2 * APEI Error Record Serialization Table support
3 *
4 * ERST is a way provided by APEI to save and retrieve hardware error
5 * information to and from a persistent store.
6 *
7 * For more information about ERST, please refer to ACPI Specification
8 * version 4.0, section 17.4.
9 *
10 * Copyright 2010 Intel Corp.
11 * Author: Huang Ying <ying.huang@intel.com>
12 *
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.
16 *
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.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/delay.h>
31#include <linux/io.h>
32#include <linux/acpi.h>
33#include <linux/uaccess.h>
34#include <linux/cper.h>
35#include <linux/nmi.h>
36#include <linux/hardirq.h>
37#include <linux/pstore.h>
38#include <acpi/apei.h>
39
40#include "apei-internal.h"
41
42#define ERST_PFX "ERST: "
43
44/* ERST command status */
45#define ERST_STATUS_SUCCESS 0x0
46#define ERST_STATUS_NOT_ENOUGH_SPACE 0x1
47#define ERST_STATUS_HARDWARE_NOT_AVAILABLE 0x2
48#define ERST_STATUS_FAILED 0x3
49#define ERST_STATUS_RECORD_STORE_EMPTY 0x4
50#define ERST_STATUS_RECORD_NOT_FOUND 0x5
51
52#define ERST_TAB_ENTRY(tab) \
53 ((struct acpi_whea_header *)((char *)(tab) + \
54 sizeof(struct acpi_table_erst)))
55
56#define SPIN_UNIT 100 /* 100ns */
57/* Firmware should respond within 1 milliseconds */
58#define FIRMWARE_TIMEOUT (1 * NSEC_PER_MSEC)
59#define FIRMWARE_MAX_STALL 50 /* 50us */
60
61int erst_disable;
62EXPORT_SYMBOL_GPL(erst_disable);
63
64static struct acpi_table_erst *erst_tab;
65
66/* ERST Error Log Address Range atrributes */
67#define ERST_RANGE_RESERVED 0x0001
68#define ERST_RANGE_NVRAM 0x0002
69#define ERST_RANGE_SLOW 0x0004
70
71/*
72 * ERST Error Log Address Range, used as buffer for reading/writing
73 * error records.
74 */
75static struct erst_erange {
76 u64 base;
77 u64 size;
78 void __iomem *vaddr;
79 u32 attr;
80} erst_erange;
81
82/*
83 * Prevent ERST interpreter to run simultaneously, because the
84 * corresponding firmware implementation may not work properly when
85 * invoked simultaneously.
86 *
87 * It is used to provide exclusive accessing for ERST Error Log
88 * Address Range too.
89 */
90static DEFINE_RAW_SPINLOCK(erst_lock);
91
92static inline int erst_errno(int command_status)
93{
94 switch (command_status) {
95 case ERST_STATUS_SUCCESS:
96 return 0;
97 case ERST_STATUS_HARDWARE_NOT_AVAILABLE:
98 return -ENODEV;
99 case ERST_STATUS_NOT_ENOUGH_SPACE:
100 return -ENOSPC;
101 case ERST_STATUS_RECORD_STORE_EMPTY:
102 case ERST_STATUS_RECORD_NOT_FOUND:
103 return -ENOENT;
104 default:
105 return -EINVAL;
106 }
107}
108
109static int erst_timedout(u64 *t, u64 spin_unit)
110{
111 if ((s64)*t < spin_unit) {
112 pr_warning(FW_WARN ERST_PFX
113 "Firmware does not respond in time\n");
114 return 1;
115 }
116 *t -= spin_unit;
117 ndelay(spin_unit);
118 touch_nmi_watchdog();
119 return 0;
120}
121
122static int erst_exec_load_var1(struct apei_exec_context *ctx,
123 struct acpi_whea_header *entry)
124{
125 return __apei_exec_read_register(entry, &ctx->var1);
126}
127
128static int erst_exec_load_var2(struct apei_exec_context *ctx,
129 struct acpi_whea_header *entry)
130{
131 return __apei_exec_read_register(entry, &ctx->var2);
132}
133
134static int erst_exec_store_var1(struct apei_exec_context *ctx,
135 struct acpi_whea_header *entry)
136{
137 return __apei_exec_write_register(entry, ctx->var1);
138}
139
140static int erst_exec_add(struct apei_exec_context *ctx,
141 struct acpi_whea_header *entry)
142{
143 ctx->var1 += ctx->var2;
144 return 0;
145}
146
147static int erst_exec_subtract(struct apei_exec_context *ctx,
148 struct acpi_whea_header *entry)
149{
150 ctx->var1 -= ctx->var2;
151 return 0;
152}
153
154static int erst_exec_add_value(struct apei_exec_context *ctx,
155 struct acpi_whea_header *entry)
156{
157 int rc;
158 u64 val;
159
160 rc = __apei_exec_read_register(entry, &val);
161 if (rc)
162 return rc;
163 val += ctx->value;
164 rc = __apei_exec_write_register(entry, val);
165 return rc;
166}
167
168static int erst_exec_subtract_value(struct apei_exec_context *ctx,
169 struct acpi_whea_header *entry)
170{
171 int rc;
172 u64 val;
173
174 rc = __apei_exec_read_register(entry, &val);
175 if (rc)
176 return rc;
177 val -= ctx->value;
178 rc = __apei_exec_write_register(entry, val);
179 return rc;
180}
181
182static int erst_exec_stall(struct apei_exec_context *ctx,
183 struct acpi_whea_header *entry)
184{
185 u64 stall_time;
186
187 if (ctx->value > FIRMWARE_MAX_STALL) {
188 if (!in_nmi())
189 pr_warning(FW_WARN ERST_PFX
190 "Too long stall time for stall instruction: %llx.\n",
191 ctx->value);
192 stall_time = FIRMWARE_MAX_STALL;
193 } else
194 stall_time = ctx->value;
195 udelay(stall_time);
196 return 0;
197}
198
199static int erst_exec_stall_while_true(struct apei_exec_context *ctx,
200 struct acpi_whea_header *entry)
201{
202 int rc;
203 u64 val;
204 u64 timeout = FIRMWARE_TIMEOUT;
205 u64 stall_time;
206
207 if (ctx->var1 > FIRMWARE_MAX_STALL) {
208 if (!in_nmi())
209 pr_warning(FW_WARN ERST_PFX
210 "Too long stall time for stall while true instruction: %llx.\n",
211 ctx->var1);
212 stall_time = FIRMWARE_MAX_STALL;
213 } else
214 stall_time = ctx->var1;
215
216 for (;;) {
217 rc = __apei_exec_read_register(entry, &val);
218 if (rc)
219 return rc;
220 if (val != ctx->value)
221 break;
222 if (erst_timedout(&timeout, stall_time * NSEC_PER_USEC))
223 return -EIO;
224 }
225 return 0;
226}
227
228static int erst_exec_skip_next_instruction_if_true(
229 struct apei_exec_context *ctx,
230 struct acpi_whea_header *entry)
231{
232 int rc;
233 u64 val;
234
235 rc = __apei_exec_read_register(entry, &val);
236 if (rc)
237 return rc;
238 if (val == ctx->value) {
239 ctx->ip += 2;
240 return APEI_EXEC_SET_IP;
241 }
242
243 return 0;
244}
245
246static int erst_exec_goto(struct apei_exec_context *ctx,
247 struct acpi_whea_header *entry)
248{
249 ctx->ip = ctx->value;
250 return APEI_EXEC_SET_IP;
251}
252
253static int erst_exec_set_src_address_base(struct apei_exec_context *ctx,
254 struct acpi_whea_header *entry)
255{
256 return __apei_exec_read_register(entry, &ctx->src_base);
257}
258
259static int erst_exec_set_dst_address_base(struct apei_exec_context *ctx,
260 struct acpi_whea_header *entry)
261{
262 return __apei_exec_read_register(entry, &ctx->dst_base);
263}
264
265static int erst_exec_move_data(struct apei_exec_context *ctx,
266 struct acpi_whea_header *entry)
267{
268 int rc;
269 u64 offset;
270 void *src, *dst;
271
272 /* ioremap does not work in interrupt context */
273 if (in_interrupt()) {
274 pr_warning(ERST_PFX
275 "MOVE_DATA can not be used in interrupt context");
276 return -EBUSY;
277 }
278
279 rc = __apei_exec_read_register(entry, &offset);
280 if (rc)
281 return rc;
282
283 src = ioremap(ctx->src_base + offset, ctx->var2);
284 if (!src)
285 return -ENOMEM;
286 dst = ioremap(ctx->dst_base + offset, ctx->var2);
287 if (!dst)
288 return -ENOMEM;
289
290 memmove(dst, src, ctx->var2);
291
292 iounmap(src);
293 iounmap(dst);
294
295 return 0;
296}
297
298static struct apei_exec_ins_type erst_ins_type[] = {
299 [ACPI_ERST_READ_REGISTER] = {
300 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
301 .run = apei_exec_read_register,
302 },
303 [ACPI_ERST_READ_REGISTER_VALUE] = {
304 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
305 .run = apei_exec_read_register_value,
306 },
307 [ACPI_ERST_WRITE_REGISTER] = {
308 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
309 .run = apei_exec_write_register,
310 },
311 [ACPI_ERST_WRITE_REGISTER_VALUE] = {
312 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
313 .run = apei_exec_write_register_value,
314 },
315 [ACPI_ERST_NOOP] = {
316 .flags = 0,
317 .run = apei_exec_noop,
318 },
319 [ACPI_ERST_LOAD_VAR1] = {
320 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
321 .run = erst_exec_load_var1,
322 },
323 [ACPI_ERST_LOAD_VAR2] = {
324 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
325 .run = erst_exec_load_var2,
326 },
327 [ACPI_ERST_STORE_VAR1] = {
328 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
329 .run = erst_exec_store_var1,
330 },
331 [ACPI_ERST_ADD] = {
332 .flags = 0,
333 .run = erst_exec_add,
334 },
335 [ACPI_ERST_SUBTRACT] = {
336 .flags = 0,
337 .run = erst_exec_subtract,
338 },
339 [ACPI_ERST_ADD_VALUE] = {
340 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
341 .run = erst_exec_add_value,
342 },
343 [ACPI_ERST_SUBTRACT_VALUE] = {
344 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
345 .run = erst_exec_subtract_value,
346 },
347 [ACPI_ERST_STALL] = {
348 .flags = 0,
349 .run = erst_exec_stall,
350 },
351 [ACPI_ERST_STALL_WHILE_TRUE] = {
352 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
353 .run = erst_exec_stall_while_true,
354 },
355 [ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
356 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
357 .run = erst_exec_skip_next_instruction_if_true,
358 },
359 [ACPI_ERST_GOTO] = {
360 .flags = 0,
361 .run = erst_exec_goto,
362 },
363 [ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
364 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
365 .run = erst_exec_set_src_address_base,
366 },
367 [ACPI_ERST_SET_DST_ADDRESS_BASE] = {
368 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
369 .run = erst_exec_set_dst_address_base,
370 },
371 [ACPI_ERST_MOVE_DATA] = {
372 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
373 .run = erst_exec_move_data,
374 },
375};
376
377static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
378{
379 apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
380 ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
381}
382
383static int erst_get_erange(struct erst_erange *range)
384{
385 struct apei_exec_context ctx;
386 int rc;
387
388 erst_exec_ctx_init(&ctx);
389 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
390 if (rc)
391 return rc;
392 range->base = apei_exec_ctx_get_output(&ctx);
393 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
394 if (rc)
395 return rc;
396 range->size = apei_exec_ctx_get_output(&ctx);
397 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
398 if (rc)
399 return rc;
400 range->attr = apei_exec_ctx_get_output(&ctx);
401
402 return 0;
403}
404
405static ssize_t __erst_get_record_count(void)
406{
407 struct apei_exec_context ctx;
408 int rc;
409
410 erst_exec_ctx_init(&ctx);
411 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
412 if (rc)
413 return rc;
414 return apei_exec_ctx_get_output(&ctx);
415}
416
417ssize_t erst_get_record_count(void)
418{
419 ssize_t count;
420 unsigned long flags;
421
422 if (erst_disable)
423 return -ENODEV;
424
425 raw_spin_lock_irqsave(&erst_lock, flags);
426 count = __erst_get_record_count();
427 raw_spin_unlock_irqrestore(&erst_lock, flags);
428
429 return count;
430}
431EXPORT_SYMBOL_GPL(erst_get_record_count);
432
433#define ERST_RECORD_ID_CACHE_SIZE_MIN 16
434#define ERST_RECORD_ID_CACHE_SIZE_MAX 1024
435
436struct erst_record_id_cache {
437 struct mutex lock;
438 u64 *entries;
439 int len;
440 int size;
441 int refcount;
442};
443
444static struct erst_record_id_cache erst_record_id_cache = {
445 .lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock),
446 .refcount = 0,
447};
448
449static int __erst_get_next_record_id(u64 *record_id)
450{
451 struct apei_exec_context ctx;
452 int rc;
453
454 erst_exec_ctx_init(&ctx);
455 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
456 if (rc)
457 return rc;
458 *record_id = apei_exec_ctx_get_output(&ctx);
459
460 return 0;
461}
462
463int erst_get_record_id_begin(int *pos)
464{
465 int rc;
466
467 if (erst_disable)
468 return -ENODEV;
469
470 rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
471 if (rc)
472 return rc;
473 erst_record_id_cache.refcount++;
474 mutex_unlock(&erst_record_id_cache.lock);
475
476 *pos = 0;
477
478 return 0;
479}
480EXPORT_SYMBOL_GPL(erst_get_record_id_begin);
481
482/* erst_record_id_cache.lock must be held by caller */
483static int __erst_record_id_cache_add_one(void)
484{
485 u64 id, prev_id, first_id;
486 int i, rc;
487 u64 *entries;
488 unsigned long flags;
489
490 id = prev_id = first_id = APEI_ERST_INVALID_RECORD_ID;
491retry:
492 raw_spin_lock_irqsave(&erst_lock, flags);
493 rc = __erst_get_next_record_id(&id);
494 raw_spin_unlock_irqrestore(&erst_lock, flags);
495 if (rc == -ENOENT)
496 return 0;
497 if (rc)
498 return rc;
499 if (id == APEI_ERST_INVALID_RECORD_ID)
500 return 0;
501 /* can not skip current ID, or loop back to first ID */
502 if (id == prev_id || id == first_id)
503 return 0;
504 if (first_id == APEI_ERST_INVALID_RECORD_ID)
505 first_id = id;
506 prev_id = id;
507
508 entries = erst_record_id_cache.entries;
509 for (i = 0; i < erst_record_id_cache.len; i++) {
510 if (entries[i] == id)
511 break;
512 }
513 /* record id already in cache, try next */
514 if (i < erst_record_id_cache.len)
515 goto retry;
516 if (erst_record_id_cache.len >= erst_record_id_cache.size) {
517 int new_size, alloc_size;
518 u64 *new_entries;
519
520 new_size = erst_record_id_cache.size * 2;
521 new_size = clamp_val(new_size, ERST_RECORD_ID_CACHE_SIZE_MIN,
522 ERST_RECORD_ID_CACHE_SIZE_MAX);
523 if (new_size <= erst_record_id_cache.size) {
524 if (printk_ratelimit())
525 pr_warning(FW_WARN ERST_PFX
526 "too many record ID!\n");
527 return 0;
528 }
529 alloc_size = new_size * sizeof(entries[0]);
530 if (alloc_size < PAGE_SIZE)
531 new_entries = kmalloc(alloc_size, GFP_KERNEL);
532 else
533 new_entries = vmalloc(alloc_size);
534 if (!new_entries)
535 return -ENOMEM;
536 memcpy(new_entries, entries,
537 erst_record_id_cache.len * sizeof(entries[0]));
538 if (erst_record_id_cache.size < PAGE_SIZE)
539 kfree(entries);
540 else
541 vfree(entries);
542 erst_record_id_cache.entries = entries = new_entries;
543 erst_record_id_cache.size = new_size;
544 }
545 entries[i] = id;
546 erst_record_id_cache.len++;
547
548 return 1;
549}
550
551/*
552 * Get the record ID of an existing error record on the persistent
553 * storage. If there is no error record on the persistent storage, the
554 * returned record_id is APEI_ERST_INVALID_RECORD_ID.
555 */
556int erst_get_record_id_next(int *pos, u64 *record_id)
557{
558 int rc = 0;
559 u64 *entries;
560
561 if (erst_disable)
562 return -ENODEV;
563
564 /* must be enclosed by erst_get_record_id_begin/end */
565 BUG_ON(!erst_record_id_cache.refcount);
566 BUG_ON(*pos < 0 || *pos > erst_record_id_cache.len);
567
568 mutex_lock(&erst_record_id_cache.lock);
569 entries = erst_record_id_cache.entries;
570 for (; *pos < erst_record_id_cache.len; (*pos)++)
571 if (entries[*pos] != APEI_ERST_INVALID_RECORD_ID)
572 break;
573 /* found next record id in cache */
574 if (*pos < erst_record_id_cache.len) {
575 *record_id = entries[*pos];
576 (*pos)++;
577 goto out_unlock;
578 }
579
580 /* Try to add one more record ID to cache */
581 rc = __erst_record_id_cache_add_one();
582 if (rc < 0)
583 goto out_unlock;
584 /* successfully add one new ID */
585 if (rc == 1) {
586 *record_id = erst_record_id_cache.entries[*pos];
587 (*pos)++;
588 rc = 0;
589 } else {
590 *pos = -1;
591 *record_id = APEI_ERST_INVALID_RECORD_ID;
592 }
593out_unlock:
594 mutex_unlock(&erst_record_id_cache.lock);
595
596 return rc;
597}
598EXPORT_SYMBOL_GPL(erst_get_record_id_next);
599
600/* erst_record_id_cache.lock must be held by caller */
601static void __erst_record_id_cache_compact(void)
602{
603 int i, wpos = 0;
604 u64 *entries;
605
606 if (erst_record_id_cache.refcount)
607 return;
608
609 entries = erst_record_id_cache.entries;
610 for (i = 0; i < erst_record_id_cache.len; i++) {
611 if (entries[i] == APEI_ERST_INVALID_RECORD_ID)
612 continue;
613 if (wpos != i)
614 memcpy(&entries[wpos], &entries[i], sizeof(entries[i]));
615 wpos++;
616 }
617 erst_record_id_cache.len = wpos;
618}
619
620void erst_get_record_id_end(void)
621{
622 /*
623 * erst_disable != 0 should be detected by invoker via the
624 * return value of erst_get_record_id_begin/next, so this
625 * function should not be called for erst_disable != 0.
626 */
627 BUG_ON(erst_disable);
628
629 mutex_lock(&erst_record_id_cache.lock);
630 erst_record_id_cache.refcount--;
631 BUG_ON(erst_record_id_cache.refcount < 0);
632 __erst_record_id_cache_compact();
633 mutex_unlock(&erst_record_id_cache.lock);
634}
635EXPORT_SYMBOL_GPL(erst_get_record_id_end);
636
637static int __erst_write_to_storage(u64 offset)
638{
639 struct apei_exec_context ctx;
640 u64 timeout = FIRMWARE_TIMEOUT;
641 u64 val;
642 int rc;
643
644 erst_exec_ctx_init(&ctx);
645 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_WRITE);
646 if (rc)
647 return rc;
648 apei_exec_ctx_set_input(&ctx, offset);
649 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
650 if (rc)
651 return rc;
652 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
653 if (rc)
654 return rc;
655 for (;;) {
656 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
657 if (rc)
658 return rc;
659 val = apei_exec_ctx_get_output(&ctx);
660 if (!val)
661 break;
662 if (erst_timedout(&timeout, SPIN_UNIT))
663 return -EIO;
664 }
665 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
666 if (rc)
667 return rc;
668 val = apei_exec_ctx_get_output(&ctx);
669 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
670 if (rc)
671 return rc;
672
673 return erst_errno(val);
674}
675
676static int __erst_read_from_storage(u64 record_id, u64 offset)
677{
678 struct apei_exec_context ctx;
679 u64 timeout = FIRMWARE_TIMEOUT;
680 u64 val;
681 int rc;
682
683 erst_exec_ctx_init(&ctx);
684 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_READ);
685 if (rc)
686 return rc;
687 apei_exec_ctx_set_input(&ctx, offset);
688 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
689 if (rc)
690 return rc;
691 apei_exec_ctx_set_input(&ctx, record_id);
692 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
693 if (rc)
694 return rc;
695 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
696 if (rc)
697 return rc;
698 for (;;) {
699 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
700 if (rc)
701 return rc;
702 val = apei_exec_ctx_get_output(&ctx);
703 if (!val)
704 break;
705 if (erst_timedout(&timeout, SPIN_UNIT))
706 return -EIO;
707 };
708 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
709 if (rc)
710 return rc;
711 val = apei_exec_ctx_get_output(&ctx);
712 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
713 if (rc)
714 return rc;
715
716 return erst_errno(val);
717}
718
719static int __erst_clear_from_storage(u64 record_id)
720{
721 struct apei_exec_context ctx;
722 u64 timeout = FIRMWARE_TIMEOUT;
723 u64 val;
724 int rc;
725
726 erst_exec_ctx_init(&ctx);
727 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_CLEAR);
728 if (rc)
729 return rc;
730 apei_exec_ctx_set_input(&ctx, record_id);
731 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
732 if (rc)
733 return rc;
734 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
735 if (rc)
736 return rc;
737 for (;;) {
738 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
739 if (rc)
740 return rc;
741 val = apei_exec_ctx_get_output(&ctx);
742 if (!val)
743 break;
744 if (erst_timedout(&timeout, SPIN_UNIT))
745 return -EIO;
746 }
747 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
748 if (rc)
749 return rc;
750 val = apei_exec_ctx_get_output(&ctx);
751 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
752 if (rc)
753 return rc;
754
755 return erst_errno(val);
756}
757
758/* NVRAM ERST Error Log Address Range is not supported yet */
759static void pr_unimpl_nvram(void)
760{
761 if (printk_ratelimit())
762 pr_warning(ERST_PFX
763 "NVRAM ERST Log Address Range is not implemented yet\n");
764}
765
766static int __erst_write_to_nvram(const struct cper_record_header *record)
767{
768 /* do not print message, because printk is not safe for NMI */
769 return -ENOSYS;
770}
771
772static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
773{
774 pr_unimpl_nvram();
775 return -ENOSYS;
776}
777
778static int __erst_clear_from_nvram(u64 record_id)
779{
780 pr_unimpl_nvram();
781 return -ENOSYS;
782}
783
784int erst_write(const struct cper_record_header *record)
785{
786 int rc;
787 unsigned long flags;
788 struct cper_record_header *rcd_erange;
789
790 if (erst_disable)
791 return -ENODEV;
792
793 if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
794 return -EINVAL;
795
796 if (erst_erange.attr & ERST_RANGE_NVRAM) {
797 if (!raw_spin_trylock_irqsave(&erst_lock, flags))
798 return -EBUSY;
799 rc = __erst_write_to_nvram(record);
800 raw_spin_unlock_irqrestore(&erst_lock, flags);
801 return rc;
802 }
803
804 if (record->record_length > erst_erange.size)
805 return -EINVAL;
806
807 if (!raw_spin_trylock_irqsave(&erst_lock, flags))
808 return -EBUSY;
809 memcpy(erst_erange.vaddr, record, record->record_length);
810 rcd_erange = erst_erange.vaddr;
811 /* signature for serialization system */
812 memcpy(&rcd_erange->persistence_information, "ER", 2);
813
814 rc = __erst_write_to_storage(0);
815 raw_spin_unlock_irqrestore(&erst_lock, flags);
816
817 return rc;
818}
819EXPORT_SYMBOL_GPL(erst_write);
820
821static int __erst_read_to_erange(u64 record_id, u64 *offset)
822{
823 int rc;
824
825 if (erst_erange.attr & ERST_RANGE_NVRAM)
826 return __erst_read_to_erange_from_nvram(
827 record_id, offset);
828
829 rc = __erst_read_from_storage(record_id, 0);
830 if (rc)
831 return rc;
832 *offset = 0;
833
834 return 0;
835}
836
837static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
838 size_t buflen)
839{
840 int rc;
841 u64 offset, len = 0;
842 struct cper_record_header *rcd_tmp;
843
844 rc = __erst_read_to_erange(record_id, &offset);
845 if (rc)
846 return rc;
847 rcd_tmp = erst_erange.vaddr + offset;
848 len = rcd_tmp->record_length;
849 if (len <= buflen)
850 memcpy(record, rcd_tmp, len);
851
852 return len;
853}
854
855/*
856 * If return value > buflen, the buffer size is not big enough,
857 * else if return value < 0, something goes wrong,
858 * else everything is OK, and return value is record length
859 */
860ssize_t erst_read(u64 record_id, struct cper_record_header *record,
861 size_t buflen)
862{
863 ssize_t len;
864 unsigned long flags;
865
866 if (erst_disable)
867 return -ENODEV;
868
869 raw_spin_lock_irqsave(&erst_lock, flags);
870 len = __erst_read(record_id, record, buflen);
871 raw_spin_unlock_irqrestore(&erst_lock, flags);
872 return len;
873}
874EXPORT_SYMBOL_GPL(erst_read);
875
876int erst_clear(u64 record_id)
877{
878 int rc, i;
879 unsigned long flags;
880 u64 *entries;
881
882 if (erst_disable)
883 return -ENODEV;
884
885 rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
886 if (rc)
887 return rc;
888 raw_spin_lock_irqsave(&erst_lock, flags);
889 if (erst_erange.attr & ERST_RANGE_NVRAM)
890 rc = __erst_clear_from_nvram(record_id);
891 else
892 rc = __erst_clear_from_storage(record_id);
893 raw_spin_unlock_irqrestore(&erst_lock, flags);
894 if (rc)
895 goto out;
896 entries = erst_record_id_cache.entries;
897 for (i = 0; i < erst_record_id_cache.len; i++) {
898 if (entries[i] == record_id)
899 entries[i] = APEI_ERST_INVALID_RECORD_ID;
900 }
901 __erst_record_id_cache_compact();
902out:
903 mutex_unlock(&erst_record_id_cache.lock);
904 return rc;
905}
906EXPORT_SYMBOL_GPL(erst_clear);
907
908static int __init setup_erst_disable(char *str)
909{
910 erst_disable = 1;
911 return 0;
912}
913
914__setup("erst_disable", setup_erst_disable);
915
916static int erst_check_table(struct acpi_table_erst *erst_tab)
917{
918 if ((erst_tab->header_length !=
919 (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
920 && (erst_tab->header_length != sizeof(struct acpi_table_einj)))
921 return -EINVAL;
922 if (erst_tab->header.length < sizeof(struct acpi_table_erst))
923 return -EINVAL;
924 if (erst_tab->entries !=
925 (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
926 sizeof(struct acpi_erst_entry))
927 return -EINVAL;
928
929 return 0;
930}
931
932static int erst_open_pstore(struct pstore_info *psi);
933static int erst_close_pstore(struct pstore_info *psi);
934static ssize_t erst_reader(u64 *id, enum pstore_type_id *type,
935 struct timespec *time, struct pstore_info *psi);
936static u64 erst_writer(enum pstore_type_id type, unsigned int part,
937 size_t size, struct pstore_info *psi);
938static int erst_clearer(enum pstore_type_id type, u64 id,
939 struct pstore_info *psi);
940
941static struct pstore_info erst_info = {
942 .owner = THIS_MODULE,
943 .name = "erst",
944 .open = erst_open_pstore,
945 .close = erst_close_pstore,
946 .read = erst_reader,
947 .write = erst_writer,
948 .erase = erst_clearer
949};
950
951#define CPER_CREATOR_PSTORE \
952 UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
953 0x64, 0x90, 0xb8, 0x9d)
954#define CPER_SECTION_TYPE_DMESG \
955 UUID_LE(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54, \
956 0x94, 0x19, 0xeb, 0x12)
957#define CPER_SECTION_TYPE_MCE \
958 UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
959 0x04, 0x4a, 0x38, 0xfc)
960
961struct cper_pstore_record {
962 struct cper_record_header hdr;
963 struct cper_section_descriptor sec_hdr;
964 char data[];
965} __packed;
966
967static int reader_pos;
968
969static int erst_open_pstore(struct pstore_info *psi)
970{
971 int rc;
972
973 if (erst_disable)
974 return -ENODEV;
975
976 rc = erst_get_record_id_begin(&reader_pos);
977
978 return rc;
979}
980
981static int erst_close_pstore(struct pstore_info *psi)
982{
983 erst_get_record_id_end();
984
985 return 0;
986}
987
988static ssize_t erst_reader(u64 *id, enum pstore_type_id *type,
989 struct timespec *time, struct pstore_info *psi)
990{
991 int rc;
992 ssize_t len = 0;
993 u64 record_id;
994 struct cper_pstore_record *rcd = (struct cper_pstore_record *)
995 (erst_info.buf - sizeof(*rcd));
996
997 if (erst_disable)
998 return -ENODEV;
999
1000skip:
1001 rc = erst_get_record_id_next(&reader_pos, &record_id);
1002 if (rc)
1003 goto out;
1004
1005 /* no more record */
1006 if (record_id == APEI_ERST_INVALID_RECORD_ID) {
1007 rc = -1;
1008 goto out;
1009 }
1010
1011 len = erst_read(record_id, &rcd->hdr, sizeof(*rcd) +
1012 erst_info.bufsize);
1013 /* The record may be cleared by others, try read next record */
1014 if (len == -ENOENT)
1015 goto skip;
1016 else if (len < 0) {
1017 rc = -1;
1018 goto out;
1019 }
1020 if (uuid_le_cmp(rcd->hdr.creator_id, CPER_CREATOR_PSTORE) != 0)
1021 goto skip;
1022
1023 *id = record_id;
1024 if (uuid_le_cmp(rcd->sec_hdr.section_type,
1025 CPER_SECTION_TYPE_DMESG) == 0)
1026 *type = PSTORE_TYPE_DMESG;
1027 else if (uuid_le_cmp(rcd->sec_hdr.section_type,
1028 CPER_SECTION_TYPE_MCE) == 0)
1029 *type = PSTORE_TYPE_MCE;
1030 else
1031 *type = PSTORE_TYPE_UNKNOWN;
1032
1033 if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
1034 time->tv_sec = rcd->hdr.timestamp;
1035 else
1036 time->tv_sec = 0;
1037 time->tv_nsec = 0;
1038
1039out:
1040 return (rc < 0) ? rc : (len - sizeof(*rcd));
1041}
1042
1043static u64 erst_writer(enum pstore_type_id type, unsigned int part,
1044 size_t size, struct pstore_info *psi)
1045{
1046 struct cper_pstore_record *rcd = (struct cper_pstore_record *)
1047 (erst_info.buf - sizeof(*rcd));
1048
1049 memset(rcd, 0, sizeof(*rcd));
1050 memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
1051 rcd->hdr.revision = CPER_RECORD_REV;
1052 rcd->hdr.signature_end = CPER_SIG_END;
1053 rcd->hdr.section_count = 1;
1054 rcd->hdr.error_severity = CPER_SEV_FATAL;
1055 /* timestamp valid. platform_id, partition_id are invalid */
1056 rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
1057 rcd->hdr.timestamp = get_seconds();
1058 rcd->hdr.record_length = sizeof(*rcd) + size;
1059 rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
1060 rcd->hdr.notification_type = CPER_NOTIFY_MCE;
1061 rcd->hdr.record_id = cper_next_record_id();
1062 rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
1063
1064 rcd->sec_hdr.section_offset = sizeof(*rcd);
1065 rcd->sec_hdr.section_length = size;
1066 rcd->sec_hdr.revision = CPER_SEC_REV;
1067 /* fru_id and fru_text is invalid */
1068 rcd->sec_hdr.validation_bits = 0;
1069 rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
1070 switch (type) {
1071 case PSTORE_TYPE_DMESG:
1072 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
1073 break;
1074 case PSTORE_TYPE_MCE:
1075 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
1076 break;
1077 default:
1078 return -EINVAL;
1079 }
1080 rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
1081
1082 erst_write(&rcd->hdr);
1083
1084 return rcd->hdr.record_id;
1085}
1086
1087static int erst_clearer(enum pstore_type_id type, u64 id,
1088 struct pstore_info *psi)
1089{
1090 return erst_clear(id);
1091}
1092
1093static int __init erst_init(void)
1094{
1095 int rc = 0;
1096 acpi_status status;
1097 struct apei_exec_context ctx;
1098 struct apei_resources erst_resources;
1099 struct resource *r;
1100 char *buf;
1101
1102 if (acpi_disabled)
1103 goto err;
1104
1105 if (erst_disable) {
1106 pr_info(ERST_PFX
1107 "Error Record Serialization Table (ERST) support is disabled.\n");
1108 goto err;
1109 }
1110
1111 status = acpi_get_table(ACPI_SIG_ERST, 0,
1112 (struct acpi_table_header **)&erst_tab);
1113 if (status == AE_NOT_FOUND) {
1114 pr_info(ERST_PFX "Table is not found!\n");
1115 goto err;
1116 } else if (ACPI_FAILURE(status)) {
1117 const char *msg = acpi_format_exception(status);
1118 pr_err(ERST_PFX "Failed to get table, %s\n", msg);
1119 rc = -EINVAL;
1120 goto err;
1121 }
1122
1123 rc = erst_check_table(erst_tab);
1124 if (rc) {
1125 pr_err(FW_BUG ERST_PFX "ERST table is invalid\n");
1126 goto err;
1127 }
1128
1129 apei_resources_init(&erst_resources);
1130 erst_exec_ctx_init(&ctx);
1131 rc = apei_exec_collect_resources(&ctx, &erst_resources);
1132 if (rc)
1133 goto err_fini;
1134 rc = apei_resources_request(&erst_resources, "APEI ERST");
1135 if (rc)
1136 goto err_fini;
1137 rc = apei_exec_pre_map_gars(&ctx);
1138 if (rc)
1139 goto err_release;
1140 rc = erst_get_erange(&erst_erange);
1141 if (rc) {
1142 if (rc == -ENODEV)
1143 pr_info(ERST_PFX
1144 "The corresponding hardware device or firmware implementation "
1145 "is not available.\n");
1146 else
1147 pr_err(ERST_PFX
1148 "Failed to get Error Log Address Range.\n");
1149 goto err_unmap_reg;
1150 }
1151
1152 r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
1153 if (!r) {
1154 pr_err(ERST_PFX
1155 "Can not request iomem region <0x%16llx-0x%16llx> for ERST.\n",
1156 (unsigned long long)erst_erange.base,
1157 (unsigned long long)erst_erange.base + erst_erange.size);
1158 rc = -EIO;
1159 goto err_unmap_reg;
1160 }
1161 rc = -ENOMEM;
1162 erst_erange.vaddr = ioremap_cache(erst_erange.base,
1163 erst_erange.size);
1164 if (!erst_erange.vaddr)
1165 goto err_release_erange;
1166
1167 buf = kmalloc(erst_erange.size, GFP_KERNEL);
1168 mutex_init(&erst_info.buf_mutex);
1169 if (buf) {
1170 erst_info.buf = buf + sizeof(struct cper_pstore_record);
1171 erst_info.bufsize = erst_erange.size -
1172 sizeof(struct cper_pstore_record);
1173 if (pstore_register(&erst_info)) {
1174 pr_info(ERST_PFX "Could not register with persistent store\n");
1175 kfree(buf);
1176 }
1177 }
1178
1179 pr_info(ERST_PFX
1180 "Error Record Serialization Table (ERST) support is initialized.\n");
1181
1182 return 0;
1183
1184err_release_erange:
1185 release_mem_region(erst_erange.base, erst_erange.size);
1186err_unmap_reg:
1187 apei_exec_post_unmap_gars(&ctx);
1188err_release:
1189 apei_resources_release(&erst_resources);
1190err_fini:
1191 apei_resources_fini(&erst_resources);
1192err:
1193 erst_disable = 1;
1194 return rc;
1195}
1196
1197device_initcall(erst_init);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * APEI Error Record Serialization Table support
4 *
5 * ERST is a way provided by APEI to save and retrieve hardware error
6 * information to and from a persistent store.
7 *
8 * For more information about ERST, please refer to ACPI Specification
9 * version 4.0, section 17.4.
10 *
11 * Copyright 2010 Intel Corp.
12 * Author: Huang Ying <ying.huang@intel.com>
13 */
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/delay.h>
19#include <linux/io.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>
29
30#include "apei-internal.h"
31
32#undef pr_fmt
33#define pr_fmt(fmt) "ERST: " fmt
34
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
42
43#define ERST_TAB_ENTRY(tab) \
44 ((struct acpi_whea_header *)((char *)(tab) + \
45 sizeof(struct acpi_table_erst)))
46
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 */
51
52int erst_disable;
53EXPORT_SYMBOL_GPL(erst_disable);
54
55static struct acpi_table_erst *erst_tab;
56
57/* ERST Error Log Address Range attributes */
58#define ERST_RANGE_RESERVED 0x0001
59#define ERST_RANGE_NVRAM 0x0002
60#define ERST_RANGE_SLOW 0x0004
61
62/*
63 * ERST Error Log Address Range, used as buffer for reading/writing
64 * error records.
65 */
66static struct erst_erange {
67 u64 base;
68 u64 size;
69 void __iomem *vaddr;
70 u32 attr;
71} erst_erange;
72
73/*
74 * Prevent ERST interpreter to run simultaneously, because the
75 * corresponding firmware implementation may not work properly when
76 * invoked simultaneously.
77 *
78 * It is used to provide exclusive accessing for ERST Error Log
79 * Address Range too.
80 */
81static DEFINE_RAW_SPINLOCK(erst_lock);
82
83static inline int erst_errno(int command_status)
84{
85 switch (command_status) {
86 case ERST_STATUS_SUCCESS:
87 return 0;
88 case ERST_STATUS_HARDWARE_NOT_AVAILABLE:
89 return -ENODEV;
90 case ERST_STATUS_NOT_ENOUGH_SPACE:
91 return -ENOSPC;
92 case ERST_STATUS_RECORD_STORE_EMPTY:
93 case ERST_STATUS_RECORD_NOT_FOUND:
94 return -ENOENT;
95 default:
96 return -EINVAL;
97 }
98}
99
100static int erst_timedout(u64 *t, u64 spin_unit)
101{
102 if ((s64)*t < spin_unit) {
103 pr_warn(FW_WARN "Firmware does not respond in time.\n");
104 return 1;
105 }
106 *t -= spin_unit;
107 ndelay(spin_unit);
108 touch_nmi_watchdog();
109 return 0;
110}
111
112static int erst_exec_load_var1(struct apei_exec_context *ctx,
113 struct acpi_whea_header *entry)
114{
115 return __apei_exec_read_register(entry, &ctx->var1);
116}
117
118static int erst_exec_load_var2(struct apei_exec_context *ctx,
119 struct acpi_whea_header *entry)
120{
121 return __apei_exec_read_register(entry, &ctx->var2);
122}
123
124static int erst_exec_store_var1(struct apei_exec_context *ctx,
125 struct acpi_whea_header *entry)
126{
127 return __apei_exec_write_register(entry, ctx->var1);
128}
129
130static int erst_exec_add(struct apei_exec_context *ctx,
131 struct acpi_whea_header *entry)
132{
133 ctx->var1 += ctx->var2;
134 return 0;
135}
136
137static int erst_exec_subtract(struct apei_exec_context *ctx,
138 struct acpi_whea_header *entry)
139{
140 ctx->var1 -= ctx->var2;
141 return 0;
142}
143
144static int erst_exec_add_value(struct apei_exec_context *ctx,
145 struct acpi_whea_header *entry)
146{
147 int rc;
148 u64 val;
149
150 rc = __apei_exec_read_register(entry, &val);
151 if (rc)
152 return rc;
153 val += ctx->value;
154 rc = __apei_exec_write_register(entry, val);
155 return rc;
156}
157
158static int erst_exec_subtract_value(struct apei_exec_context *ctx,
159 struct acpi_whea_header *entry)
160{
161 int rc;
162 u64 val;
163
164 rc = __apei_exec_read_register(entry, &val);
165 if (rc)
166 return rc;
167 val -= ctx->value;
168 rc = __apei_exec_write_register(entry, val);
169 return rc;
170}
171
172static int erst_exec_stall(struct apei_exec_context *ctx,
173 struct acpi_whea_header *entry)
174{
175 u64 stall_time;
176
177 if (ctx->value > FIRMWARE_MAX_STALL) {
178 if (!in_nmi())
179 pr_warn(FW_WARN
180 "Too long stall time for stall instruction: 0x%llx.\n",
181 ctx->value);
182 stall_time = FIRMWARE_MAX_STALL;
183 } else
184 stall_time = ctx->value;
185 udelay(stall_time);
186 return 0;
187}
188
189static int erst_exec_stall_while_true(struct apei_exec_context *ctx,
190 struct acpi_whea_header *entry)
191{
192 int rc;
193 u64 val;
194 u64 timeout = FIRMWARE_TIMEOUT;
195 u64 stall_time;
196
197 if (ctx->var1 > FIRMWARE_MAX_STALL) {
198 if (!in_nmi())
199 pr_warn(FW_WARN
200 "Too long stall time for stall while true instruction: 0x%llx.\n",
201 ctx->var1);
202 stall_time = FIRMWARE_MAX_STALL;
203 } else
204 stall_time = ctx->var1;
205
206 for (;;) {
207 rc = __apei_exec_read_register(entry, &val);
208 if (rc)
209 return rc;
210 if (val != ctx->value)
211 break;
212 if (erst_timedout(&timeout, stall_time * NSEC_PER_USEC))
213 return -EIO;
214 }
215 return 0;
216}
217
218static int erst_exec_skip_next_instruction_if_true(
219 struct apei_exec_context *ctx,
220 struct acpi_whea_header *entry)
221{
222 int rc;
223 u64 val;
224
225 rc = __apei_exec_read_register(entry, &val);
226 if (rc)
227 return rc;
228 if (val == ctx->value) {
229 ctx->ip += 2;
230 return APEI_EXEC_SET_IP;
231 }
232
233 return 0;
234}
235
236static int erst_exec_goto(struct apei_exec_context *ctx,
237 struct acpi_whea_header *entry)
238{
239 ctx->ip = ctx->value;
240 return APEI_EXEC_SET_IP;
241}
242
243static int erst_exec_set_src_address_base(struct apei_exec_context *ctx,
244 struct acpi_whea_header *entry)
245{
246 return __apei_exec_read_register(entry, &ctx->src_base);
247}
248
249static int erst_exec_set_dst_address_base(struct apei_exec_context *ctx,
250 struct acpi_whea_header *entry)
251{
252 return __apei_exec_read_register(entry, &ctx->dst_base);
253}
254
255static int erst_exec_move_data(struct apei_exec_context *ctx,
256 struct acpi_whea_header *entry)
257{
258 int rc;
259 u64 offset;
260 void *src, *dst;
261
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");
265 return -EBUSY;
266 }
267
268 rc = __apei_exec_read_register(entry, &offset);
269 if (rc)
270 return rc;
271
272 src = ioremap(ctx->src_base + offset, ctx->var2);
273 if (!src)
274 return -ENOMEM;
275 dst = ioremap(ctx->dst_base + offset, ctx->var2);
276 if (!dst) {
277 iounmap(src);
278 return -ENOMEM;
279 }
280
281 memmove(dst, src, ctx->var2);
282
283 iounmap(src);
284 iounmap(dst);
285
286 return 0;
287}
288
289static 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,
293 },
294 [ACPI_ERST_READ_REGISTER_VALUE] = {
295 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
296 .run = apei_exec_read_register_value,
297 },
298 [ACPI_ERST_WRITE_REGISTER] = {
299 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
300 .run = apei_exec_write_register,
301 },
302 [ACPI_ERST_WRITE_REGISTER_VALUE] = {
303 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
304 .run = apei_exec_write_register_value,
305 },
306 [ACPI_ERST_NOOP] = {
307 .flags = 0,
308 .run = apei_exec_noop,
309 },
310 [ACPI_ERST_LOAD_VAR1] = {
311 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
312 .run = erst_exec_load_var1,
313 },
314 [ACPI_ERST_LOAD_VAR2] = {
315 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
316 .run = erst_exec_load_var2,
317 },
318 [ACPI_ERST_STORE_VAR1] = {
319 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
320 .run = erst_exec_store_var1,
321 },
322 [ACPI_ERST_ADD] = {
323 .flags = 0,
324 .run = erst_exec_add,
325 },
326 [ACPI_ERST_SUBTRACT] = {
327 .flags = 0,
328 .run = erst_exec_subtract,
329 },
330 [ACPI_ERST_ADD_VALUE] = {
331 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
332 .run = erst_exec_add_value,
333 },
334 [ACPI_ERST_SUBTRACT_VALUE] = {
335 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
336 .run = erst_exec_subtract_value,
337 },
338 [ACPI_ERST_STALL] = {
339 .flags = 0,
340 .run = erst_exec_stall,
341 },
342 [ACPI_ERST_STALL_WHILE_TRUE] = {
343 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
344 .run = erst_exec_stall_while_true,
345 },
346 [ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
347 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
348 .run = erst_exec_skip_next_instruction_if_true,
349 },
350 [ACPI_ERST_GOTO] = {
351 .flags = 0,
352 .run = erst_exec_goto,
353 },
354 [ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
355 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
356 .run = erst_exec_set_src_address_base,
357 },
358 [ACPI_ERST_SET_DST_ADDRESS_BASE] = {
359 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
360 .run = erst_exec_set_dst_address_base,
361 },
362 [ACPI_ERST_MOVE_DATA] = {
363 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
364 .run = erst_exec_move_data,
365 },
366};
367
368static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
369{
370 apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
371 ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
372}
373
374static int erst_get_erange(struct erst_erange *range)
375{
376 struct apei_exec_context ctx;
377 int rc;
378
379 erst_exec_ctx_init(&ctx);
380 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
381 if (rc)
382 return rc;
383 range->base = apei_exec_ctx_get_output(&ctx);
384 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
385 if (rc)
386 return rc;
387 range->size = apei_exec_ctx_get_output(&ctx);
388 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
389 if (rc)
390 return rc;
391 range->attr = apei_exec_ctx_get_output(&ctx);
392
393 return 0;
394}
395
396static ssize_t __erst_get_record_count(void)
397{
398 struct apei_exec_context ctx;
399 int rc;
400
401 erst_exec_ctx_init(&ctx);
402 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
403 if (rc)
404 return rc;
405 return apei_exec_ctx_get_output(&ctx);
406}
407
408ssize_t erst_get_record_count(void)
409{
410 ssize_t count;
411 unsigned long flags;
412
413 if (erst_disable)
414 return -ENODEV;
415
416 raw_spin_lock_irqsave(&erst_lock, flags);
417 count = __erst_get_record_count();
418 raw_spin_unlock_irqrestore(&erst_lock, flags);
419
420 return count;
421}
422EXPORT_SYMBOL_GPL(erst_get_record_count);
423
424#define ERST_RECORD_ID_CACHE_SIZE_MIN 16
425#define ERST_RECORD_ID_CACHE_SIZE_MAX 1024
426
427struct erst_record_id_cache {
428 struct mutex lock;
429 u64 *entries;
430 int len;
431 int size;
432 int refcount;
433};
434
435static struct erst_record_id_cache erst_record_id_cache = {
436 .lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock),
437 .refcount = 0,
438};
439
440static int __erst_get_next_record_id(u64 *record_id)
441{
442 struct apei_exec_context ctx;
443 int rc;
444
445 erst_exec_ctx_init(&ctx);
446 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
447 if (rc)
448 return rc;
449 *record_id = apei_exec_ctx_get_output(&ctx);
450
451 return 0;
452}
453
454int erst_get_record_id_begin(int *pos)
455{
456 int rc;
457
458 if (erst_disable)
459 return -ENODEV;
460
461 rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
462 if (rc)
463 return rc;
464 erst_record_id_cache.refcount++;
465 mutex_unlock(&erst_record_id_cache.lock);
466
467 *pos = 0;
468
469 return 0;
470}
471EXPORT_SYMBOL_GPL(erst_get_record_id_begin);
472
473/* erst_record_id_cache.lock must be held by caller */
474static int __erst_record_id_cache_add_one(void)
475{
476 u64 id, prev_id, first_id;
477 int i, rc;
478 u64 *entries;
479 unsigned long flags;
480
481 id = prev_id = first_id = APEI_ERST_INVALID_RECORD_ID;
482retry:
483 raw_spin_lock_irqsave(&erst_lock, flags);
484 rc = __erst_get_next_record_id(&id);
485 raw_spin_unlock_irqrestore(&erst_lock, flags);
486 if (rc == -ENOENT)
487 return 0;
488 if (rc)
489 return rc;
490 if (id == APEI_ERST_INVALID_RECORD_ID)
491 return 0;
492 /* can not skip current ID, or loop back to first ID */
493 if (id == prev_id || id == first_id)
494 return 0;
495 if (first_id == APEI_ERST_INVALID_RECORD_ID)
496 first_id = id;
497 prev_id = id;
498
499 entries = erst_record_id_cache.entries;
500 for (i = 0; i < erst_record_id_cache.len; i++) {
501 if (entries[i] == id)
502 break;
503 }
504 /* record id already in cache, try next */
505 if (i < erst_record_id_cache.len)
506 goto retry;
507 if (erst_record_id_cache.len >= erst_record_id_cache.size) {
508 int new_size;
509 u64 *new_entries;
510
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");
517 return 0;
518 }
519 new_entries = kvmalloc_array(new_size, sizeof(entries[0]),
520 GFP_KERNEL);
521 if (!new_entries)
522 return -ENOMEM;
523 memcpy(new_entries, entries,
524 erst_record_id_cache.len * sizeof(entries[0]));
525 kvfree(entries);
526 erst_record_id_cache.entries = entries = new_entries;
527 erst_record_id_cache.size = new_size;
528 }
529 entries[i] = id;
530 erst_record_id_cache.len++;
531
532 return 1;
533}
534
535/*
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.
539 */
540int erst_get_record_id_next(int *pos, u64 *record_id)
541{
542 int rc = 0;
543 u64 *entries;
544
545 if (erst_disable)
546 return -ENODEV;
547
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);
551
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)
556 break;
557 /* found next record id in cache */
558 if (*pos < erst_record_id_cache.len) {
559 *record_id = entries[*pos];
560 (*pos)++;
561 goto out_unlock;
562 }
563
564 /* Try to add one more record ID to cache */
565 rc = __erst_record_id_cache_add_one();
566 if (rc < 0)
567 goto out_unlock;
568 /* successfully add one new ID */
569 if (rc == 1) {
570 *record_id = erst_record_id_cache.entries[*pos];
571 (*pos)++;
572 rc = 0;
573 } else {
574 *pos = -1;
575 *record_id = APEI_ERST_INVALID_RECORD_ID;
576 }
577out_unlock:
578 mutex_unlock(&erst_record_id_cache.lock);
579
580 return rc;
581}
582EXPORT_SYMBOL_GPL(erst_get_record_id_next);
583
584/* erst_record_id_cache.lock must be held by caller */
585static void __erst_record_id_cache_compact(void)
586{
587 int i, wpos = 0;
588 u64 *entries;
589
590 if (erst_record_id_cache.refcount)
591 return;
592
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)
596 continue;
597 if (wpos != i)
598 entries[wpos] = entries[i];
599 wpos++;
600 }
601 erst_record_id_cache.len = wpos;
602}
603
604void erst_get_record_id_end(void)
605{
606 /*
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.
610 */
611 BUG_ON(erst_disable);
612
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);
618}
619EXPORT_SYMBOL_GPL(erst_get_record_id_end);
620
621static int __erst_write_to_storage(u64 offset)
622{
623 struct apei_exec_context ctx;
624 u64 timeout = FIRMWARE_TIMEOUT;
625 u64 val;
626 int rc;
627
628 erst_exec_ctx_init(&ctx);
629 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_WRITE);
630 if (rc)
631 return rc;
632 apei_exec_ctx_set_input(&ctx, offset);
633 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
634 if (rc)
635 return rc;
636 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
637 if (rc)
638 return rc;
639 for (;;) {
640 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
641 if (rc)
642 return rc;
643 val = apei_exec_ctx_get_output(&ctx);
644 if (!val)
645 break;
646 if (erst_timedout(&timeout, SPIN_UNIT))
647 return -EIO;
648 }
649 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
650 if (rc)
651 return rc;
652 val = apei_exec_ctx_get_output(&ctx);
653 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
654 if (rc)
655 return rc;
656
657 return erst_errno(val);
658}
659
660static int __erst_read_from_storage(u64 record_id, u64 offset)
661{
662 struct apei_exec_context ctx;
663 u64 timeout = FIRMWARE_TIMEOUT;
664 u64 val;
665 int rc;
666
667 erst_exec_ctx_init(&ctx);
668 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_READ);
669 if (rc)
670 return rc;
671 apei_exec_ctx_set_input(&ctx, offset);
672 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
673 if (rc)
674 return rc;
675 apei_exec_ctx_set_input(&ctx, record_id);
676 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
677 if (rc)
678 return rc;
679 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
680 if (rc)
681 return rc;
682 for (;;) {
683 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
684 if (rc)
685 return rc;
686 val = apei_exec_ctx_get_output(&ctx);
687 if (!val)
688 break;
689 if (erst_timedout(&timeout, SPIN_UNIT))
690 return -EIO;
691 }
692 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
693 if (rc)
694 return rc;
695 val = apei_exec_ctx_get_output(&ctx);
696 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
697 if (rc)
698 return rc;
699
700 return erst_errno(val);
701}
702
703static int __erst_clear_from_storage(u64 record_id)
704{
705 struct apei_exec_context ctx;
706 u64 timeout = FIRMWARE_TIMEOUT;
707 u64 val;
708 int rc;
709
710 erst_exec_ctx_init(&ctx);
711 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_CLEAR);
712 if (rc)
713 return rc;
714 apei_exec_ctx_set_input(&ctx, record_id);
715 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
716 if (rc)
717 return rc;
718 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
719 if (rc)
720 return rc;
721 for (;;) {
722 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
723 if (rc)
724 return rc;
725 val = apei_exec_ctx_get_output(&ctx);
726 if (!val)
727 break;
728 if (erst_timedout(&timeout, SPIN_UNIT))
729 return -EIO;
730 }
731 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
732 if (rc)
733 return rc;
734 val = apei_exec_ctx_get_output(&ctx);
735 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
736 if (rc)
737 return rc;
738
739 return erst_errno(val);
740}
741
742/* NVRAM ERST Error Log Address Range is not supported yet */
743static void pr_unimpl_nvram(void)
744{
745 if (printk_ratelimit())
746 pr_warn("NVRAM ERST Log Address Range not implemented yet.\n");
747}
748
749static int __erst_write_to_nvram(const struct cper_record_header *record)
750{
751 /* do not print message, because printk is not safe for NMI */
752 return -ENOSYS;
753}
754
755static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
756{
757 pr_unimpl_nvram();
758 return -ENOSYS;
759}
760
761static int __erst_clear_from_nvram(u64 record_id)
762{
763 pr_unimpl_nvram();
764 return -ENOSYS;
765}
766
767int erst_write(const struct cper_record_header *record)
768{
769 int rc;
770 unsigned long flags;
771 struct cper_record_header *rcd_erange;
772
773 if (erst_disable)
774 return -ENODEV;
775
776 if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
777 return -EINVAL;
778
779 if (erst_erange.attr & ERST_RANGE_NVRAM) {
780 if (!raw_spin_trylock_irqsave(&erst_lock, flags))
781 return -EBUSY;
782 rc = __erst_write_to_nvram(record);
783 raw_spin_unlock_irqrestore(&erst_lock, flags);
784 return rc;
785 }
786
787 if (record->record_length > erst_erange.size)
788 return -EINVAL;
789
790 if (!raw_spin_trylock_irqsave(&erst_lock, flags))
791 return -EBUSY;
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);
796
797 rc = __erst_write_to_storage(0);
798 raw_spin_unlock_irqrestore(&erst_lock, flags);
799
800 return rc;
801}
802EXPORT_SYMBOL_GPL(erst_write);
803
804static int __erst_read_to_erange(u64 record_id, u64 *offset)
805{
806 int rc;
807
808 if (erst_erange.attr & ERST_RANGE_NVRAM)
809 return __erst_read_to_erange_from_nvram(
810 record_id, offset);
811
812 rc = __erst_read_from_storage(record_id, 0);
813 if (rc)
814 return rc;
815 *offset = 0;
816
817 return 0;
818}
819
820static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
821 size_t buflen)
822{
823 int rc;
824 u64 offset, len = 0;
825 struct cper_record_header *rcd_tmp;
826
827 rc = __erst_read_to_erange(record_id, &offset);
828 if (rc)
829 return rc;
830 rcd_tmp = erst_erange.vaddr + offset;
831 len = rcd_tmp->record_length;
832 if (len <= buflen)
833 memcpy(record, rcd_tmp, len);
834
835 return len;
836}
837
838/*
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
842 */
843ssize_t erst_read(u64 record_id, struct cper_record_header *record,
844 size_t buflen)
845{
846 ssize_t len;
847 unsigned long flags;
848
849 if (erst_disable)
850 return -ENODEV;
851
852 raw_spin_lock_irqsave(&erst_lock, flags);
853 len = __erst_read(record_id, record, buflen);
854 raw_spin_unlock_irqrestore(&erst_lock, flags);
855 return len;
856}
857EXPORT_SYMBOL_GPL(erst_read);
858
859static void erst_clear_cache(u64 record_id)
860{
861 int i;
862 u64 *entries;
863
864 mutex_lock(&erst_record_id_cache.lock);
865
866 entries = erst_record_id_cache.entries;
867 for (i = 0; i < erst_record_id_cache.len; i++) {
868 if (entries[i] == record_id)
869 entries[i] = APEI_ERST_INVALID_RECORD_ID;
870 }
871 __erst_record_id_cache_compact();
872
873 mutex_unlock(&erst_record_id_cache.lock);
874}
875
876ssize_t erst_read_record(u64 record_id, struct cper_record_header *record,
877 size_t buflen, size_t recordlen, const guid_t *creatorid)
878{
879 ssize_t len;
880
881 /*
882 * if creatorid is NULL, read any record for erst-dbg module
883 */
884 if (creatorid == NULL) {
885 len = erst_read(record_id, record, buflen);
886 if (len == -ENOENT)
887 erst_clear_cache(record_id);
888
889 return len;
890 }
891
892 len = erst_read(record_id, record, buflen);
893 /*
894 * if erst_read return value is -ENOENT skip to next record_id,
895 * and clear the record_id cache.
896 */
897 if (len == -ENOENT) {
898 erst_clear_cache(record_id);
899 goto out;
900 }
901
902 if (len < 0)
903 goto out;
904
905 /*
906 * if erst_read return value is less than record head length,
907 * consider it as -EIO, and clear the record_id cache.
908 */
909 if (len < recordlen) {
910 len = -EIO;
911 erst_clear_cache(record_id);
912 goto out;
913 }
914
915 /*
916 * if creatorid is not wanted, consider it as not found,
917 * for skipping to next record_id.
918 */
919 if (!guid_equal(&record->creator_id, creatorid))
920 len = -ENOENT;
921
922out:
923 return len;
924}
925EXPORT_SYMBOL_GPL(erst_read_record);
926
927int erst_clear(u64 record_id)
928{
929 int rc, i;
930 unsigned long flags;
931 u64 *entries;
932
933 if (erst_disable)
934 return -ENODEV;
935
936 rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
937 if (rc)
938 return rc;
939 raw_spin_lock_irqsave(&erst_lock, flags);
940 if (erst_erange.attr & ERST_RANGE_NVRAM)
941 rc = __erst_clear_from_nvram(record_id);
942 else
943 rc = __erst_clear_from_storage(record_id);
944 raw_spin_unlock_irqrestore(&erst_lock, flags);
945 if (rc)
946 goto out;
947 entries = erst_record_id_cache.entries;
948 for (i = 0; i < erst_record_id_cache.len; i++) {
949 if (entries[i] == record_id)
950 entries[i] = APEI_ERST_INVALID_RECORD_ID;
951 }
952 __erst_record_id_cache_compact();
953out:
954 mutex_unlock(&erst_record_id_cache.lock);
955 return rc;
956}
957EXPORT_SYMBOL_GPL(erst_clear);
958
959static int __init setup_erst_disable(char *str)
960{
961 erst_disable = 1;
962 return 1;
963}
964
965__setup("erst_disable", setup_erst_disable);
966
967static int erst_check_table(struct acpi_table_erst *erst_tab)
968{
969 if ((erst_tab->header_length !=
970 (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
971 && (erst_tab->header_length != sizeof(struct acpi_table_erst)))
972 return -EINVAL;
973 if (erst_tab->header.length < sizeof(struct acpi_table_erst))
974 return -EINVAL;
975 if (erst_tab->entries !=
976 (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
977 sizeof(struct acpi_erst_entry))
978 return -EINVAL;
979
980 return 0;
981}
982
983static int erst_open_pstore(struct pstore_info *psi);
984static int erst_close_pstore(struct pstore_info *psi);
985static ssize_t erst_reader(struct pstore_record *record);
986static int erst_writer(struct pstore_record *record);
987static int erst_clearer(struct pstore_record *record);
988
989static struct pstore_info erst_info = {
990 .owner = THIS_MODULE,
991 .name = "erst",
992 .flags = PSTORE_FLAGS_DMESG,
993 .open = erst_open_pstore,
994 .close = erst_close_pstore,
995 .read = erst_reader,
996 .write = erst_writer,
997 .erase = erst_clearer
998};
999
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
1013struct cper_pstore_record {
1014 struct cper_record_header hdr;
1015 struct cper_section_descriptor sec_hdr;
1016 char data[];
1017} __packed;
1018
1019static int reader_pos;
1020
1021static int erst_open_pstore(struct pstore_info *psi)
1022{
1023 if (erst_disable)
1024 return -ENODEV;
1025
1026 return erst_get_record_id_begin(&reader_pos);
1027}
1028
1029static int erst_close_pstore(struct pstore_info *psi)
1030{
1031 erst_get_record_id_end();
1032
1033 return 0;
1034}
1035
1036static ssize_t erst_reader(struct pstore_record *record)
1037{
1038 int rc;
1039 ssize_t len = 0;
1040 u64 record_id;
1041 struct cper_pstore_record *rcd;
1042 size_t rcd_len = sizeof(*rcd) + erst_info.bufsize;
1043
1044 if (erst_disable)
1045 return -ENODEV;
1046
1047 rcd = kmalloc(rcd_len, GFP_KERNEL);
1048 if (!rcd) {
1049 rc = -ENOMEM;
1050 goto out;
1051 }
1052skip:
1053 rc = erst_get_record_id_next(&reader_pos, &record_id);
1054 if (rc)
1055 goto out;
1056
1057 /* no more record */
1058 if (record_id == APEI_ERST_INVALID_RECORD_ID) {
1059 rc = -EINVAL;
1060 goto out;
1061 }
1062
1063 len = erst_read_record(record_id, &rcd->hdr, rcd_len, sizeof(*rcd),
1064 &CPER_CREATOR_PSTORE);
1065 /* The record may be cleared by others, try read next record */
1066 if (len == -ENOENT)
1067 goto skip;
1068 else if (len < 0)
1069 goto out;
1070
1071 record->buf = kmalloc(len, GFP_KERNEL);
1072 if (record->buf == NULL) {
1073 rc = -ENOMEM;
1074 goto out;
1075 }
1076 memcpy(record->buf, rcd->data, len - sizeof(*rcd));
1077 record->id = record_id;
1078 record->compressed = false;
1079 record->ecc_notice_size = 0;
1080 if (guid_equal(&rcd->sec_hdr.section_type, &CPER_SECTION_TYPE_DMESG_Z)) {
1081 record->type = PSTORE_TYPE_DMESG;
1082 record->compressed = true;
1083 } else if (guid_equal(&rcd->sec_hdr.section_type, &CPER_SECTION_TYPE_DMESG))
1084 record->type = PSTORE_TYPE_DMESG;
1085 else if (guid_equal(&rcd->sec_hdr.section_type, &CPER_SECTION_TYPE_MCE))
1086 record->type = PSTORE_TYPE_MCE;
1087 else
1088 record->type = PSTORE_TYPE_MAX;
1089
1090 if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
1091 record->time.tv_sec = rcd->hdr.timestamp;
1092 else
1093 record->time.tv_sec = 0;
1094 record->time.tv_nsec = 0;
1095
1096out:
1097 kfree(rcd);
1098 return (rc < 0) ? rc : (len - sizeof(*rcd));
1099}
1100
1101static int erst_writer(struct pstore_record *record)
1102{
1103 struct cper_pstore_record *rcd = (struct cper_pstore_record *)
1104 (erst_info.buf - sizeof(*rcd));
1105 int ret;
1106
1107 memset(rcd, 0, sizeof(*rcd));
1108 memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
1109 rcd->hdr.revision = CPER_RECORD_REV;
1110 rcd->hdr.signature_end = CPER_SIG_END;
1111 rcd->hdr.section_count = 1;
1112 rcd->hdr.error_severity = CPER_SEV_FATAL;
1113 /* timestamp valid. platform_id, partition_id are invalid */
1114 rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
1115 rcd->hdr.timestamp = ktime_get_real_seconds();
1116 rcd->hdr.record_length = sizeof(*rcd) + record->size;
1117 rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
1118 rcd->hdr.notification_type = CPER_NOTIFY_MCE;
1119 rcd->hdr.record_id = cper_next_record_id();
1120 rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
1121
1122 rcd->sec_hdr.section_offset = sizeof(*rcd);
1123 rcd->sec_hdr.section_length = record->size;
1124 rcd->sec_hdr.revision = CPER_SEC_REV;
1125 /* fru_id and fru_text is invalid */
1126 rcd->sec_hdr.validation_bits = 0;
1127 rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
1128 switch (record->type) {
1129 case PSTORE_TYPE_DMESG:
1130 if (record->compressed)
1131 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG_Z;
1132 else
1133 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
1134 break;
1135 case PSTORE_TYPE_MCE:
1136 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
1137 break;
1138 default:
1139 return -EINVAL;
1140 }
1141 rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
1142
1143 ret = erst_write(&rcd->hdr);
1144 record->id = rcd->hdr.record_id;
1145
1146 return ret;
1147}
1148
1149static int erst_clearer(struct pstore_record *record)
1150{
1151 return erst_clear(record->id);
1152}
1153
1154static int __init erst_init(void)
1155{
1156 int rc = 0;
1157 acpi_status status;
1158 struct apei_exec_context ctx;
1159 struct apei_resources erst_resources;
1160 struct resource *r;
1161 char *buf;
1162
1163 if (acpi_disabled)
1164 goto err;
1165
1166 if (erst_disable) {
1167 pr_info(
1168 "Error Record Serialization Table (ERST) support is disabled.\n");
1169 goto err;
1170 }
1171
1172 status = acpi_get_table(ACPI_SIG_ERST, 0,
1173 (struct acpi_table_header **)&erst_tab);
1174 if (status == AE_NOT_FOUND)
1175 goto err;
1176 else if (ACPI_FAILURE(status)) {
1177 const char *msg = acpi_format_exception(status);
1178 pr_err("Failed to get table, %s\n", msg);
1179 rc = -EINVAL;
1180 goto err;
1181 }
1182
1183 rc = erst_check_table(erst_tab);
1184 if (rc) {
1185 pr_err(FW_BUG "ERST table is invalid.\n");
1186 goto err_put_erst_tab;
1187 }
1188
1189 apei_resources_init(&erst_resources);
1190 erst_exec_ctx_init(&ctx);
1191 rc = apei_exec_collect_resources(&ctx, &erst_resources);
1192 if (rc)
1193 goto err_fini;
1194 rc = apei_resources_request(&erst_resources, "APEI ERST");
1195 if (rc)
1196 goto err_fini;
1197 rc = apei_exec_pre_map_gars(&ctx);
1198 if (rc)
1199 goto err_release;
1200 rc = erst_get_erange(&erst_erange);
1201 if (rc) {
1202 if (rc == -ENODEV)
1203 pr_info(
1204 "The corresponding hardware device or firmware implementation "
1205 "is not available.\n");
1206 else
1207 pr_err("Failed to get Error Log Address Range.\n");
1208 goto err_unmap_reg;
1209 }
1210
1211 r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
1212 if (!r) {
1213 pr_err("Can not request [mem %#010llx-%#010llx] for ERST.\n",
1214 (unsigned long long)erst_erange.base,
1215 (unsigned long long)erst_erange.base + erst_erange.size - 1);
1216 rc = -EIO;
1217 goto err_unmap_reg;
1218 }
1219 rc = -ENOMEM;
1220 erst_erange.vaddr = ioremap_cache(erst_erange.base,
1221 erst_erange.size);
1222 if (!erst_erange.vaddr)
1223 goto err_release_erange;
1224
1225 pr_info(
1226 "Error Record Serialization Table (ERST) support is initialized.\n");
1227
1228 buf = kmalloc(erst_erange.size, GFP_KERNEL);
1229 if (buf) {
1230 erst_info.buf = buf + sizeof(struct cper_pstore_record);
1231 erst_info.bufsize = erst_erange.size -
1232 sizeof(struct cper_pstore_record);
1233 rc = pstore_register(&erst_info);
1234 if (rc) {
1235 if (rc != -EPERM)
1236 pr_info(
1237 "Could not register with persistent store.\n");
1238 erst_info.buf = NULL;
1239 erst_info.bufsize = 0;
1240 kfree(buf);
1241 }
1242 } else
1243 pr_err(
1244 "Failed to allocate %lld bytes for persistent store error log.\n",
1245 erst_erange.size);
1246
1247 /* Cleanup ERST Resources */
1248 apei_resources_fini(&erst_resources);
1249
1250 return 0;
1251
1252err_release_erange:
1253 release_mem_region(erst_erange.base, erst_erange.size);
1254err_unmap_reg:
1255 apei_exec_post_unmap_gars(&ctx);
1256err_release:
1257 apei_resources_release(&erst_resources);
1258err_fini:
1259 apei_resources_fini(&erst_resources);
1260err_put_erst_tab:
1261 acpi_put_table((struct acpi_table_header *)erst_tab);
1262err:
1263 erst_disable = 1;
1264 return rc;
1265}
1266
1267device_initcall(erst_init);