powerpc/pseries: Remove (de)compression in nvram with pstore enabled
[deliverable/linux.git] / drivers / acpi / apei / erst.c
CommitLineData
a08f82d0
HY
1/*
2 * APEI Error Record Serialization Table support
3 *
4 * ERST is a way provided by APEI to save and retrieve hardware error
58f87ed0 5 * information to and from a persistent store.
a08f82d0
HY
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>
0a7992c9 36#include <linux/hardirq.h>
0bb77c46 37#include <linux/pstore.h>
a08f82d0
HY
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 */
e8a8b252 57/* Firmware should respond within 1 milliseconds */
a08f82d0
HY
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 */
3b38bb5f 90static DEFINE_RAW_SPINLOCK(erst_lock);
a08f82d0
HY
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;
0bbba38a
HY
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 }
a08f82d0
HY
278
279 rc = __apei_exec_read_register(entry, &offset);
280 if (rc)
281 return rc;
0bbba38a
HY
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);
08b326d0
WY
287 if (!dst) {
288 iounmap(src);
0bbba38a 289 return -ENOMEM;
08b326d0 290 }
0bbba38a
HY
291
292 memmove(dst, src, ctx->var2);
293
294 iounmap(src);
295 iounmap(dst);
a08f82d0
HY
296
297 return 0;
298}
299
300static struct apei_exec_ins_type erst_ins_type[] = {
301 [ACPI_ERST_READ_REGISTER] = {
302 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
303 .run = apei_exec_read_register,
304 },
305 [ACPI_ERST_READ_REGISTER_VALUE] = {
306 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
307 .run = apei_exec_read_register_value,
308 },
309 [ACPI_ERST_WRITE_REGISTER] = {
310 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
311 .run = apei_exec_write_register,
312 },
313 [ACPI_ERST_WRITE_REGISTER_VALUE] = {
314 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
315 .run = apei_exec_write_register_value,
316 },
317 [ACPI_ERST_NOOP] = {
318 .flags = 0,
319 .run = apei_exec_noop,
320 },
321 [ACPI_ERST_LOAD_VAR1] = {
322 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
323 .run = erst_exec_load_var1,
324 },
325 [ACPI_ERST_LOAD_VAR2] = {
326 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
327 .run = erst_exec_load_var2,
328 },
329 [ACPI_ERST_STORE_VAR1] = {
330 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
331 .run = erst_exec_store_var1,
332 },
333 [ACPI_ERST_ADD] = {
334 .flags = 0,
335 .run = erst_exec_add,
336 },
337 [ACPI_ERST_SUBTRACT] = {
338 .flags = 0,
339 .run = erst_exec_subtract,
340 },
341 [ACPI_ERST_ADD_VALUE] = {
342 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
343 .run = erst_exec_add_value,
344 },
345 [ACPI_ERST_SUBTRACT_VALUE] = {
346 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
347 .run = erst_exec_subtract_value,
348 },
349 [ACPI_ERST_STALL] = {
350 .flags = 0,
351 .run = erst_exec_stall,
352 },
353 [ACPI_ERST_STALL_WHILE_TRUE] = {
354 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
355 .run = erst_exec_stall_while_true,
356 },
357 [ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
358 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
359 .run = erst_exec_skip_next_instruction_if_true,
360 },
361 [ACPI_ERST_GOTO] = {
362 .flags = 0,
363 .run = erst_exec_goto,
364 },
365 [ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
366 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
367 .run = erst_exec_set_src_address_base,
368 },
369 [ACPI_ERST_SET_DST_ADDRESS_BASE] = {
370 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
371 .run = erst_exec_set_dst_address_base,
372 },
373 [ACPI_ERST_MOVE_DATA] = {
374 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
375 .run = erst_exec_move_data,
376 },
377};
378
379static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
380{
381 apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
382 ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
383}
384
385static int erst_get_erange(struct erst_erange *range)
386{
387 struct apei_exec_context ctx;
388 int rc;
389
390 erst_exec_ctx_init(&ctx);
391 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
392 if (rc)
393 return rc;
394 range->base = apei_exec_ctx_get_output(&ctx);
395 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
396 if (rc)
397 return rc;
398 range->size = apei_exec_ctx_get_output(&ctx);
399 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
400 if (rc)
401 return rc;
402 range->attr = apei_exec_ctx_get_output(&ctx);
403
404 return 0;
405}
406
407static ssize_t __erst_get_record_count(void)
408{
409 struct apei_exec_context ctx;
410 int rc;
411
412 erst_exec_ctx_init(&ctx);
413 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
414 if (rc)
415 return rc;
416 return apei_exec_ctx_get_output(&ctx);
417}
418
419ssize_t erst_get_record_count(void)
420{
421 ssize_t count;
422 unsigned long flags;
423
424 if (erst_disable)
425 return -ENODEV;
426
3b38bb5f 427 raw_spin_lock_irqsave(&erst_lock, flags);
a08f82d0 428 count = __erst_get_record_count();
3b38bb5f 429 raw_spin_unlock_irqrestore(&erst_lock, flags);
a08f82d0
HY
430
431 return count;
432}
433EXPORT_SYMBOL_GPL(erst_get_record_count);
434
885b976f
HY
435#define ERST_RECORD_ID_CACHE_SIZE_MIN 16
436#define ERST_RECORD_ID_CACHE_SIZE_MAX 1024
437
438struct erst_record_id_cache {
439 struct mutex lock;
440 u64 *entries;
441 int len;
442 int size;
443 int refcount;
444};
445
446static struct erst_record_id_cache erst_record_id_cache = {
447 .lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock),
448 .refcount = 0,
449};
450
a08f82d0
HY
451static int __erst_get_next_record_id(u64 *record_id)
452{
453 struct apei_exec_context ctx;
454 int rc;
455
456 erst_exec_ctx_init(&ctx);
457 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
458 if (rc)
459 return rc;
460 *record_id = apei_exec_ctx_get_output(&ctx);
461
462 return 0;
463}
464
885b976f
HY
465int erst_get_record_id_begin(int *pos)
466{
467 int rc;
468
469 if (erst_disable)
470 return -ENODEV;
471
472 rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
473 if (rc)
474 return rc;
475 erst_record_id_cache.refcount++;
476 mutex_unlock(&erst_record_id_cache.lock);
477
478 *pos = 0;
479
480 return 0;
481}
482EXPORT_SYMBOL_GPL(erst_get_record_id_begin);
483
484/* erst_record_id_cache.lock must be held by caller */
485static int __erst_record_id_cache_add_one(void)
486{
487 u64 id, prev_id, first_id;
488 int i, rc;
489 u64 *entries;
490 unsigned long flags;
491
492 id = prev_id = first_id = APEI_ERST_INVALID_RECORD_ID;
493retry:
494 raw_spin_lock_irqsave(&erst_lock, flags);
495 rc = __erst_get_next_record_id(&id);
496 raw_spin_unlock_irqrestore(&erst_lock, flags);
497 if (rc == -ENOENT)
498 return 0;
499 if (rc)
500 return rc;
501 if (id == APEI_ERST_INVALID_RECORD_ID)
502 return 0;
503 /* can not skip current ID, or loop back to first ID */
504 if (id == prev_id || id == first_id)
505 return 0;
506 if (first_id == APEI_ERST_INVALID_RECORD_ID)
507 first_id = id;
508 prev_id = id;
509
510 entries = erst_record_id_cache.entries;
511 for (i = 0; i < erst_record_id_cache.len; i++) {
512 if (entries[i] == id)
513 break;
514 }
515 /* record id already in cache, try next */
516 if (i < erst_record_id_cache.len)
517 goto retry;
518 if (erst_record_id_cache.len >= erst_record_id_cache.size) {
519 int new_size, alloc_size;
520 u64 *new_entries;
521
522 new_size = erst_record_id_cache.size * 2;
523 new_size = clamp_val(new_size, ERST_RECORD_ID_CACHE_SIZE_MIN,
524 ERST_RECORD_ID_CACHE_SIZE_MAX);
525 if (new_size <= erst_record_id_cache.size) {
526 if (printk_ratelimit())
527 pr_warning(FW_WARN ERST_PFX
528 "too many record ID!\n");
529 return 0;
530 }
531 alloc_size = new_size * sizeof(entries[0]);
532 if (alloc_size < PAGE_SIZE)
533 new_entries = kmalloc(alloc_size, GFP_KERNEL);
534 else
535 new_entries = vmalloc(alloc_size);
536 if (!new_entries)
537 return -ENOMEM;
538 memcpy(new_entries, entries,
539 erst_record_id_cache.len * sizeof(entries[0]));
540 if (erst_record_id_cache.size < PAGE_SIZE)
541 kfree(entries);
542 else
543 vfree(entries);
544 erst_record_id_cache.entries = entries = new_entries;
545 erst_record_id_cache.size = new_size;
546 }
547 entries[i] = id;
548 erst_record_id_cache.len++;
549
550 return 1;
551}
552
a08f82d0
HY
553/*
554 * Get the record ID of an existing error record on the persistent
555 * storage. If there is no error record on the persistent storage, the
556 * returned record_id is APEI_ERST_INVALID_RECORD_ID.
557 */
885b976f 558int erst_get_record_id_next(int *pos, u64 *record_id)
a08f82d0 559{
885b976f
HY
560 int rc = 0;
561 u64 *entries;
a08f82d0
HY
562
563 if (erst_disable)
564 return -ENODEV;
565
885b976f
HY
566 /* must be enclosed by erst_get_record_id_begin/end */
567 BUG_ON(!erst_record_id_cache.refcount);
568 BUG_ON(*pos < 0 || *pos > erst_record_id_cache.len);
569
570 mutex_lock(&erst_record_id_cache.lock);
571 entries = erst_record_id_cache.entries;
572 for (; *pos < erst_record_id_cache.len; (*pos)++)
573 if (entries[*pos] != APEI_ERST_INVALID_RECORD_ID)
574 break;
575 /* found next record id in cache */
576 if (*pos < erst_record_id_cache.len) {
577 *record_id = entries[*pos];
578 (*pos)++;
579 goto out_unlock;
580 }
581
582 /* Try to add one more record ID to cache */
583 rc = __erst_record_id_cache_add_one();
584 if (rc < 0)
585 goto out_unlock;
586 /* successfully add one new ID */
587 if (rc == 1) {
588 *record_id = erst_record_id_cache.entries[*pos];
589 (*pos)++;
590 rc = 0;
591 } else {
592 *pos = -1;
593 *record_id = APEI_ERST_INVALID_RECORD_ID;
594 }
595out_unlock:
596 mutex_unlock(&erst_record_id_cache.lock);
a08f82d0
HY
597
598 return rc;
599}
885b976f
HY
600EXPORT_SYMBOL_GPL(erst_get_record_id_next);
601
602/* erst_record_id_cache.lock must be held by caller */
603static void __erst_record_id_cache_compact(void)
604{
605 int i, wpos = 0;
606 u64 *entries;
607
608 if (erst_record_id_cache.refcount)
609 return;
610
611 entries = erst_record_id_cache.entries;
612 for (i = 0; i < erst_record_id_cache.len; i++) {
613 if (entries[i] == APEI_ERST_INVALID_RECORD_ID)
614 continue;
615 if (wpos != i)
616 memcpy(&entries[wpos], &entries[i], sizeof(entries[i]));
617 wpos++;
618 }
619 erst_record_id_cache.len = wpos;
620}
621
622void erst_get_record_id_end(void)
623{
624 /*
625 * erst_disable != 0 should be detected by invoker via the
626 * return value of erst_get_record_id_begin/next, so this
627 * function should not be called for erst_disable != 0.
628 */
629 BUG_ON(erst_disable);
630
631 mutex_lock(&erst_record_id_cache.lock);
632 erst_record_id_cache.refcount--;
633 BUG_ON(erst_record_id_cache.refcount < 0);
634 __erst_record_id_cache_compact();
635 mutex_unlock(&erst_record_id_cache.lock);
636}
637EXPORT_SYMBOL_GPL(erst_get_record_id_end);
a08f82d0
HY
638
639static int __erst_write_to_storage(u64 offset)
640{
641 struct apei_exec_context ctx;
642 u64 timeout = FIRMWARE_TIMEOUT;
643 u64 val;
644 int rc;
645
646 erst_exec_ctx_init(&ctx);
392913de 647 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_WRITE);
a08f82d0
HY
648 if (rc)
649 return rc;
650 apei_exec_ctx_set_input(&ctx, offset);
651 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
652 if (rc)
653 return rc;
654 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
655 if (rc)
656 return rc;
657 for (;;) {
658 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
659 if (rc)
660 return rc;
661 val = apei_exec_ctx_get_output(&ctx);
662 if (!val)
663 break;
664 if (erst_timedout(&timeout, SPIN_UNIT))
665 return -EIO;
666 }
667 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
668 if (rc)
669 return rc;
670 val = apei_exec_ctx_get_output(&ctx);
392913de 671 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
a08f82d0
HY
672 if (rc)
673 return rc;
674
675 return erst_errno(val);
676}
677
678static int __erst_read_from_storage(u64 record_id, u64 offset)
679{
680 struct apei_exec_context ctx;
681 u64 timeout = FIRMWARE_TIMEOUT;
682 u64 val;
683 int rc;
684
685 erst_exec_ctx_init(&ctx);
392913de 686 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_READ);
a08f82d0
HY
687 if (rc)
688 return rc;
689 apei_exec_ctx_set_input(&ctx, offset);
690 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
691 if (rc)
692 return rc;
693 apei_exec_ctx_set_input(&ctx, record_id);
694 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
695 if (rc)
696 return rc;
697 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
698 if (rc)
699 return rc;
700 for (;;) {
701 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
702 if (rc)
703 return rc;
704 val = apei_exec_ctx_get_output(&ctx);
705 if (!val)
706 break;
707 if (erst_timedout(&timeout, SPIN_UNIT))
708 return -EIO;
709 };
710 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
711 if (rc)
712 return rc;
713 val = apei_exec_ctx_get_output(&ctx);
392913de 714 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
a08f82d0
HY
715 if (rc)
716 return rc;
717
718 return erst_errno(val);
719}
720
721static int __erst_clear_from_storage(u64 record_id)
722{
723 struct apei_exec_context ctx;
724 u64 timeout = FIRMWARE_TIMEOUT;
725 u64 val;
726 int rc;
727
728 erst_exec_ctx_init(&ctx);
392913de 729 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_CLEAR);
a08f82d0
HY
730 if (rc)
731 return rc;
732 apei_exec_ctx_set_input(&ctx, record_id);
733 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
734 if (rc)
735 return rc;
736 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
737 if (rc)
738 return rc;
739 for (;;) {
740 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
741 if (rc)
742 return rc;
743 val = apei_exec_ctx_get_output(&ctx);
744 if (!val)
745 break;
746 if (erst_timedout(&timeout, SPIN_UNIT))
747 return -EIO;
748 }
749 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
750 if (rc)
751 return rc;
752 val = apei_exec_ctx_get_output(&ctx);
392913de 753 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END);
a08f82d0
HY
754 if (rc)
755 return rc;
756
757 return erst_errno(val);
758}
759
760/* NVRAM ERST Error Log Address Range is not supported yet */
761static void pr_unimpl_nvram(void)
762{
763 if (printk_ratelimit())
764 pr_warning(ERST_PFX
765 "NVRAM ERST Log Address Range is not implemented yet\n");
766}
767
768static int __erst_write_to_nvram(const struct cper_record_header *record)
769{
770 /* do not print message, because printk is not safe for NMI */
771 return -ENOSYS;
772}
773
774static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
775{
776 pr_unimpl_nvram();
777 return -ENOSYS;
778}
779
780static int __erst_clear_from_nvram(u64 record_id)
781{
782 pr_unimpl_nvram();
783 return -ENOSYS;
784}
785
786int erst_write(const struct cper_record_header *record)
787{
788 int rc;
789 unsigned long flags;
790 struct cper_record_header *rcd_erange;
791
792 if (erst_disable)
793 return -ENODEV;
794
795 if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
796 return -EINVAL;
797
798 if (erst_erange.attr & ERST_RANGE_NVRAM) {
3b38bb5f 799 if (!raw_spin_trylock_irqsave(&erst_lock, flags))
a08f82d0
HY
800 return -EBUSY;
801 rc = __erst_write_to_nvram(record);
3b38bb5f 802 raw_spin_unlock_irqrestore(&erst_lock, flags);
a08f82d0
HY
803 return rc;
804 }
805
806 if (record->record_length > erst_erange.size)
807 return -EINVAL;
808
3b38bb5f 809 if (!raw_spin_trylock_irqsave(&erst_lock, flags))
a08f82d0
HY
810 return -EBUSY;
811 memcpy(erst_erange.vaddr, record, record->record_length);
812 rcd_erange = erst_erange.vaddr;
813 /* signature for serialization system */
814 memcpy(&rcd_erange->persistence_information, "ER", 2);
815
816 rc = __erst_write_to_storage(0);
3b38bb5f 817 raw_spin_unlock_irqrestore(&erst_lock, flags);
a08f82d0
HY
818
819 return rc;
820}
821EXPORT_SYMBOL_GPL(erst_write);
822
823static int __erst_read_to_erange(u64 record_id, u64 *offset)
824{
825 int rc;
826
827 if (erst_erange.attr & ERST_RANGE_NVRAM)
828 return __erst_read_to_erange_from_nvram(
829 record_id, offset);
830
831 rc = __erst_read_from_storage(record_id, 0);
832 if (rc)
833 return rc;
834 *offset = 0;
835
836 return 0;
837}
838
839static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
840 size_t buflen)
841{
842 int rc;
843 u64 offset, len = 0;
844 struct cper_record_header *rcd_tmp;
845
846 rc = __erst_read_to_erange(record_id, &offset);
847 if (rc)
848 return rc;
849 rcd_tmp = erst_erange.vaddr + offset;
850 len = rcd_tmp->record_length;
851 if (len <= buflen)
852 memcpy(record, rcd_tmp, len);
853
854 return len;
855}
856
857/*
858 * If return value > buflen, the buffer size is not big enough,
859 * else if return value < 0, something goes wrong,
860 * else everything is OK, and return value is record length
861 */
862ssize_t erst_read(u64 record_id, struct cper_record_header *record,
863 size_t buflen)
864{
865 ssize_t len;
866 unsigned long flags;
867
868 if (erst_disable)
869 return -ENODEV;
870
3b38bb5f 871 raw_spin_lock_irqsave(&erst_lock, flags);
a08f82d0 872 len = __erst_read(record_id, record, buflen);
3b38bb5f 873 raw_spin_unlock_irqrestore(&erst_lock, flags);
a08f82d0
HY
874 return len;
875}
876EXPORT_SYMBOL_GPL(erst_read);
877
a08f82d0
HY
878int erst_clear(u64 record_id)
879{
885b976f 880 int rc, i;
a08f82d0 881 unsigned long flags;
885b976f 882 u64 *entries;
a08f82d0
HY
883
884 if (erst_disable)
885 return -ENODEV;
886
885b976f
HY
887 rc = mutex_lock_interruptible(&erst_record_id_cache.lock);
888 if (rc)
889 return rc;
3b38bb5f 890 raw_spin_lock_irqsave(&erst_lock, flags);
a08f82d0
HY
891 if (erst_erange.attr & ERST_RANGE_NVRAM)
892 rc = __erst_clear_from_nvram(record_id);
893 else
894 rc = __erst_clear_from_storage(record_id);
3b38bb5f 895 raw_spin_unlock_irqrestore(&erst_lock, flags);
885b976f
HY
896 if (rc)
897 goto out;
898 entries = erst_record_id_cache.entries;
899 for (i = 0; i < erst_record_id_cache.len; i++) {
900 if (entries[i] == record_id)
901 entries[i] = APEI_ERST_INVALID_RECORD_ID;
902 }
903 __erst_record_id_cache_compact();
904out:
905 mutex_unlock(&erst_record_id_cache.lock);
a08f82d0
HY
906 return rc;
907}
908EXPORT_SYMBOL_GPL(erst_clear);
909
910static int __init setup_erst_disable(char *str)
911{
912 erst_disable = 1;
913 return 0;
914}
915
916__setup("erst_disable", setup_erst_disable);
917
918static int erst_check_table(struct acpi_table_erst *erst_tab)
919{
3a78f965
HY
920 if ((erst_tab->header_length !=
921 (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header)))
7ed28f2e 922 && (erst_tab->header_length != sizeof(struct acpi_table_erst)))
a08f82d0
HY
923 return -EINVAL;
924 if (erst_tab->header.length < sizeof(struct acpi_table_erst))
925 return -EINVAL;
926 if (erst_tab->entries !=
927 (erst_tab->header.length - sizeof(struct acpi_table_erst)) /
928 sizeof(struct acpi_erst_entry))
929 return -EINVAL;
930
931 return 0;
932}
933
06cf91b4
CG
934static int erst_open_pstore(struct pstore_info *psi);
935static int erst_close_pstore(struct pstore_info *psi);
755d4fe4 936static ssize_t erst_reader(u64 *id, enum pstore_type_id *type, int *count,
f6f82851
KC
937 struct timespec *time, char **buf,
938 struct pstore_info *psi);
3d6d8d20 939static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason,
6bbbca73 940 u64 *id, unsigned int part, int count, size_t hsize,
b94fdd07 941 size_t size, struct pstore_info *psi);
755d4fe4 942static int erst_clearer(enum pstore_type_id type, u64 id, int count,
a9efd39c 943 struct timespec time, struct pstore_info *psi);
0bb77c46
TL
944
945static struct pstore_info erst_info = {
946 .owner = THIS_MODULE,
947 .name = "erst",
06cf91b4
CG
948 .open = erst_open_pstore,
949 .close = erst_close_pstore,
0bb77c46
TL
950 .read = erst_reader,
951 .write = erst_writer,
638c1fd3 952 .erase = erst_clearer
0bb77c46
TL
953};
954
955#define CPER_CREATOR_PSTORE \
956 UUID_LE(0x75a574e3, 0x5052, 0x4b29, 0x8a, 0x8e, 0xbe, 0x2c, \
957 0x64, 0x90, 0xb8, 0x9d)
958#define CPER_SECTION_TYPE_DMESG \
959 UUID_LE(0xc197e04e, 0xd545, 0x4a70, 0x9c, 0x17, 0xa5, 0x54, \
960 0x94, 0x19, 0xeb, 0x12)
961#define CPER_SECTION_TYPE_MCE \
962 UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \
963 0x04, 0x4a, 0x38, 0xfc)
964
965struct cper_pstore_record {
966 struct cper_record_header hdr;
967 struct cper_section_descriptor sec_hdr;
968 char data[];
969} __packed;
970
06cf91b4
CG
971static int reader_pos;
972
973static int erst_open_pstore(struct pstore_info *psi)
974{
975 int rc;
976
977 if (erst_disable)
978 return -ENODEV;
979
980 rc = erst_get_record_id_begin(&reader_pos);
981
982 return rc;
983}
984
985static int erst_close_pstore(struct pstore_info *psi)
986{
987 erst_get_record_id_end();
988
989 return 0;
990}
991
755d4fe4 992static ssize_t erst_reader(u64 *id, enum pstore_type_id *type, int *count,
f6f82851
KC
993 struct timespec *time, char **buf,
994 struct pstore_info *psi)
0bb77c46
TL
995{
996 int rc;
06cf91b4 997 ssize_t len = 0;
0bb77c46 998 u64 record_id;
f6f82851
KC
999 struct cper_pstore_record *rcd;
1000 size_t rcd_len = sizeof(*rcd) + erst_info.bufsize;
0bb77c46
TL
1001
1002 if (erst_disable)
1003 return -ENODEV;
1004
f6f82851
KC
1005 rcd = kmalloc(rcd_len, GFP_KERNEL);
1006 if (!rcd) {
1007 rc = -ENOMEM;
1008 goto out;
1009 }
0bb77c46 1010skip:
06cf91b4
CG
1011 rc = erst_get_record_id_next(&reader_pos, &record_id);
1012 if (rc)
1013 goto out;
1014
0bb77c46
TL
1015 /* no more record */
1016 if (record_id == APEI_ERST_INVALID_RECORD_ID) {
f6f82851 1017 rc = -EINVAL;
06cf91b4 1018 goto out;
0bb77c46
TL
1019 }
1020
f6f82851 1021 len = erst_read(record_id, &rcd->hdr, rcd_len);
f5ec25de
CG
1022 /* The record may be cleared by others, try read next record */
1023 if (len == -ENOENT)
1024 goto skip;
f6f82851
KC
1025 else if (len < sizeof(*rcd)) {
1026 rc = -EIO;
f5ec25de
CG
1027 goto out;
1028 }
0bb77c46
TL
1029 if (uuid_le_cmp(rcd->hdr.creator_id, CPER_CREATOR_PSTORE) != 0)
1030 goto skip;
0bb77c46 1031
f6f82851
KC
1032 *buf = kmalloc(len, GFP_KERNEL);
1033 if (*buf == NULL) {
1034 rc = -ENOMEM;
1035 goto out;
1036 }
1037 memcpy(*buf, rcd->data, len - sizeof(*rcd));
0bb77c46
TL
1038 *id = record_id;
1039 if (uuid_le_cmp(rcd->sec_hdr.section_type,
1040 CPER_SECTION_TYPE_DMESG) == 0)
1041 *type = PSTORE_TYPE_DMESG;
1042 else if (uuid_le_cmp(rcd->sec_hdr.section_type,
1043 CPER_SECTION_TYPE_MCE) == 0)
1044 *type = PSTORE_TYPE_MCE;
1045 else
1046 *type = PSTORE_TYPE_UNKNOWN;
1047
1048 if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP)
1049 time->tv_sec = rcd->hdr.timestamp;
1050 else
1051 time->tv_sec = 0;
1052 time->tv_nsec = 0;
1053
06cf91b4 1054out:
f6f82851 1055 kfree(rcd);
06cf91b4 1056 return (rc < 0) ? rc : (len - sizeof(*rcd));
0bb77c46
TL
1057}
1058
3d6d8d20 1059static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason,
6bbbca73 1060 u64 *id, unsigned int part, int count, size_t hsize,
b94fdd07 1061 size_t size, struct pstore_info *psi)
0bb77c46
TL
1062{
1063 struct cper_pstore_record *rcd = (struct cper_pstore_record *)
1064 (erst_info.buf - sizeof(*rcd));
b238b8fa 1065 int ret;
0bb77c46
TL
1066
1067 memset(rcd, 0, sizeof(*rcd));
1068 memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE);
1069 rcd->hdr.revision = CPER_RECORD_REV;
1070 rcd->hdr.signature_end = CPER_SIG_END;
1071 rcd->hdr.section_count = 1;
1072 rcd->hdr.error_severity = CPER_SEV_FATAL;
1073 /* timestamp valid. platform_id, partition_id are invalid */
1074 rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP;
1075 rcd->hdr.timestamp = get_seconds();
1076 rcd->hdr.record_length = sizeof(*rcd) + size;
1077 rcd->hdr.creator_id = CPER_CREATOR_PSTORE;
1078 rcd->hdr.notification_type = CPER_NOTIFY_MCE;
1079 rcd->hdr.record_id = cper_next_record_id();
1080 rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR;
1081
1082 rcd->sec_hdr.section_offset = sizeof(*rcd);
1083 rcd->sec_hdr.section_length = size;
1084 rcd->sec_hdr.revision = CPER_SEC_REV;
1085 /* fru_id and fru_text is invalid */
1086 rcd->sec_hdr.validation_bits = 0;
1087 rcd->sec_hdr.flags = CPER_SEC_PRIMARY;
1088 switch (type) {
1089 case PSTORE_TYPE_DMESG:
1090 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG;
1091 break;
1092 case PSTORE_TYPE_MCE:
1093 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE;
1094 break;
1095 default:
1096 return -EINVAL;
1097 }
1098 rcd->sec_hdr.section_severity = CPER_SEV_FATAL;
1099
b238b8fa
CG
1100 ret = erst_write(&rcd->hdr);
1101 *id = rcd->hdr.record_id;
0bb77c46 1102
b238b8fa 1103 return ret;
0bb77c46
TL
1104}
1105
755d4fe4 1106static int erst_clearer(enum pstore_type_id type, u64 id, int count,
a9efd39c 1107 struct timespec time, struct pstore_info *psi)
638c1fd3
MG
1108{
1109 return erst_clear(id);
1110}
1111
a08f82d0
HY
1112static int __init erst_init(void)
1113{
1114 int rc = 0;
1115 acpi_status status;
1116 struct apei_exec_context ctx;
1117 struct apei_resources erst_resources;
1118 struct resource *r;
0bb77c46 1119 char *buf;
a08f82d0
HY
1120
1121 if (acpi_disabled)
1122 goto err;
1123
1124 if (erst_disable) {
1125 pr_info(ERST_PFX
1126 "Error Record Serialization Table (ERST) support is disabled.\n");
1127 goto err;
1128 }
1129
1130 status = acpi_get_table(ACPI_SIG_ERST, 0,
1131 (struct acpi_table_header **)&erst_tab);
ad686154 1132 if (status == AE_NOT_FOUND)
a08f82d0 1133 goto err;
ad686154 1134 else if (ACPI_FAILURE(status)) {
a08f82d0
HY
1135 const char *msg = acpi_format_exception(status);
1136 pr_err(ERST_PFX "Failed to get table, %s\n", msg);
1137 rc = -EINVAL;
1138 goto err;
1139 }
1140
1141 rc = erst_check_table(erst_tab);
1142 if (rc) {
1143 pr_err(FW_BUG ERST_PFX "ERST table is invalid\n");
1144 goto err;
1145 }
1146
1147 apei_resources_init(&erst_resources);
1148 erst_exec_ctx_init(&ctx);
1149 rc = apei_exec_collect_resources(&ctx, &erst_resources);
1150 if (rc)
1151 goto err_fini;
1152 rc = apei_resources_request(&erst_resources, "APEI ERST");
1153 if (rc)
1154 goto err_fini;
1155 rc = apei_exec_pre_map_gars(&ctx);
1156 if (rc)
1157 goto err_release;
1158 rc = erst_get_erange(&erst_erange);
1159 if (rc) {
1160 if (rc == -ENODEV)
1161 pr_info(ERST_PFX
1162 "The corresponding hardware device or firmware implementation "
1163 "is not available.\n");
1164 else
1165 pr_err(ERST_PFX
1166 "Failed to get Error Log Address Range.\n");
1167 goto err_unmap_reg;
1168 }
1169
1170 r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST");
1171 if (!r) {
1172 pr_err(ERST_PFX
1173 "Can not request iomem region <0x%16llx-0x%16llx> for ERST.\n",
1174 (unsigned long long)erst_erange.base,
1175 (unsigned long long)erst_erange.base + erst_erange.size);
1176 rc = -EIO;
1177 goto err_unmap_reg;
1178 }
1179 rc = -ENOMEM;
1180 erst_erange.vaddr = ioremap_cache(erst_erange.base,
1181 erst_erange.size);
1182 if (!erst_erange.vaddr)
1183 goto err_release_erange;
1184
74fd6c6f
LS
1185 pr_info(ERST_PFX
1186 "Error Record Serialization Table (ERST) support is initialized.\n");
1187
0bb77c46 1188 buf = kmalloc(erst_erange.size, GFP_KERNEL);
abd4d558 1189 spin_lock_init(&erst_info.buf_lock);
0bb77c46
TL
1190 if (buf) {
1191 erst_info.buf = buf + sizeof(struct cper_pstore_record);
1192 erst_info.bufsize = erst_erange.size -
1193 sizeof(struct cper_pstore_record);
74fd6c6f
LS
1194 rc = pstore_register(&erst_info);
1195 if (rc) {
1196 if (rc != -EPERM)
1197 pr_info(ERST_PFX
1198 "Could not register with persistent store\n");
1199 erst_info.buf = NULL;
1200 erst_info.bufsize = 0;
0bb77c46
TL
1201 kfree(buf);
1202 }
74fd6c6f
LS
1203 } else
1204 pr_err(ERST_PFX
1205 "Failed to allocate %lld bytes for persistent store error log\n",
1206 erst_erange.size);
a08f82d0
HY
1207
1208 return 0;
1209
1210err_release_erange:
1211 release_mem_region(erst_erange.base, erst_erange.size);
1212err_unmap_reg:
1213 apei_exec_post_unmap_gars(&ctx);
1214err_release:
1215 apei_resources_release(&erst_resources);
1216err_fini:
1217 apei_resources_fini(&erst_resources);
1218err:
1219 erst_disable = 1;
1220 return rc;
1221}
1222
1223device_initcall(erst_init);
This page took 0.279573 seconds and 5 git commands to generate.