1 /*
2 * APEI Error Record Serialization Table support
3 *
4 * ERST is a way provided by APEI to save and retrieve hardware error
5 * infomation 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 * This feature is ported from linux acpi tree
11 * Copyright 2010 Intel Corp.
12 * Author: Huang Ying <ying.huang@intel.com>
13 * Ported by: Liu, Jinsong <jinsong.liu@intel.com>
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License version
17 * 2 as published by the Free Software Foundation.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; If not, see <http://www.gnu.org/licenses/>.
26 */
27
28 #include <xen/kernel.h>
29 #include <xen/errno.h>
30 #include <xen/delay.h>
31 #include <xen/string.h>
32 #include <xen/types.h>
33 #include <xen/spinlock.h>
34 #include <xen/cper.h>
35 #include <asm/fixmap.h>
36 #include <asm/io.h>
37 #include <acpi/acpi.h>
38 #include <acpi/apei.h>
39
40 #include "apei-internal.h"
41
42 /* ERST command status */
43 #define ERST_STATUS_SUCCESS 0x0
44 #define ERST_STATUS_NOT_ENOUGH_SPACE 0x1
45 #define ERST_STATUS_HARDWARE_NOT_AVAILABLE 0x2
46 #define ERST_STATUS_FAILED 0x3
47 #define ERST_STATUS_RECORD_STORE_EMPTY 0x4
48 #define ERST_STATUS_RECORD_NOT_FOUND 0x5
49
50 #define ERST_TAB_ENTRY(tab) \
51 ((struct acpi_whea_header *)((char *)(tab) + \
52 sizeof(struct acpi_table_erst)))
53
54 #define SPIN_UNIT 1 /* 1us */
55 /* Firmware should respond within 1 miliseconds */
56 #define FIRMWARE_TIMEOUT (1 * 1000)
57 #define FIRMWARE_MAX_STALL 50 /* 50us */
58
59 static struct acpi_table_erst *__read_mostly erst_tab;
60 static bool_t __read_mostly erst_enabled;
61
62 /* ERST Error Log Address Range atrributes */
63 #define ERST_RANGE_RESERVED 0x0001
64 #define ERST_RANGE_NVRAM 0x0002
65 #define ERST_RANGE_SLOW 0x0004
66
67 /*
68 * ERST Error Log Address Range, used as buffer for reading/writing
69 * error records.
70 */
71 static struct erst_erange {
72 u64 base;
73 u64 size;
74 void __iomem *vaddr;
75 u32 attr;
76 } erst_erange;
77
78 /*
79 * Prevent ERST interpreter to run simultaneously, because the
80 * corresponding firmware implementation may not work properly when
81 * invoked simultaneously.
82 *
83 * It is used to provide exclusive accessing for ERST Error Log
84 * Address Range too.
85 */
86 static DEFINE_SPINLOCK(erst_lock);
87
erst_errno(int command_status)88 static inline int erst_errno(int command_status)
89 {
90 switch (command_status) {
91 case ERST_STATUS_SUCCESS:
92 return 0;
93 case ERST_STATUS_HARDWARE_NOT_AVAILABLE:
94 return -ENODEV;
95 case ERST_STATUS_NOT_ENOUGH_SPACE:
96 return -ENOSPC;
97 case ERST_STATUS_RECORD_STORE_EMPTY:
98 case ERST_STATUS_RECORD_NOT_FOUND:
99 return -ENOENT;
100 default:
101 return -EINVAL;
102 }
103 }
104
erst_timedout(u64 * t,u64 spin_unit)105 static int erst_timedout(u64 *t, u64 spin_unit)
106 {
107 if ((s64)*t < spin_unit) {
108 printk(XENLOG_WARNING "Firmware does not respond in time\n");
109 return 1;
110 }
111 *t -= spin_unit;
112 udelay(spin_unit);
113 return 0;
114 }
115
erst_exec_load_var1(struct apei_exec_context * ctx,struct acpi_whea_header * entry)116 static int erst_exec_load_var1(struct apei_exec_context *ctx,
117 struct acpi_whea_header *entry)
118 {
119 return __apei_exec_read_register(entry, &ctx->var1);
120 }
121
erst_exec_load_var2(struct apei_exec_context * ctx,struct acpi_whea_header * entry)122 static int erst_exec_load_var2(struct apei_exec_context *ctx,
123 struct acpi_whea_header *entry)
124 {
125 return __apei_exec_read_register(entry, &ctx->var2);
126 }
127
erst_exec_store_var1(struct apei_exec_context * ctx,struct acpi_whea_header * entry)128 static int erst_exec_store_var1(struct apei_exec_context *ctx,
129 struct acpi_whea_header *entry)
130 {
131 return __apei_exec_write_register(entry, ctx->var1);
132 }
133
erst_exec_add(struct apei_exec_context * ctx,struct acpi_whea_header * entry)134 static int erst_exec_add(struct apei_exec_context *ctx,
135 struct acpi_whea_header *entry)
136 {
137 ctx->var1 += ctx->var2;
138 return 0;
139 }
140
erst_exec_subtract(struct apei_exec_context * ctx,struct acpi_whea_header * entry)141 static int erst_exec_subtract(struct apei_exec_context *ctx,
142 struct acpi_whea_header *entry)
143 {
144 ctx->var1 -= ctx->var2;
145 return 0;
146 }
147
erst_exec_add_value(struct apei_exec_context * ctx,struct acpi_whea_header * entry)148 static int erst_exec_add_value(struct apei_exec_context *ctx,
149 struct acpi_whea_header *entry)
150 {
151 int rc;
152 u64 val;
153
154 rc = __apei_exec_read_register(entry, &val);
155 if (rc)
156 return rc;
157 val += ctx->value;
158 rc = __apei_exec_write_register(entry, val);
159 return rc;
160 }
161
erst_exec_subtract_value(struct apei_exec_context * ctx,struct acpi_whea_header * entry)162 static int erst_exec_subtract_value(struct apei_exec_context *ctx,
163 struct acpi_whea_header *entry)
164 {
165 int rc;
166 u64 val;
167
168 rc = __apei_exec_read_register(entry, &val);
169 if (rc)
170 return rc;
171 val -= ctx->value;
172 rc = __apei_exec_write_register(entry, val);
173 return rc;
174 }
175
erst_exec_stall(struct apei_exec_context * ctx,struct acpi_whea_header * entry)176 static int erst_exec_stall(struct apei_exec_context *ctx,
177 struct acpi_whea_header *entry)
178 {
179 udelay((ctx->var1 > FIRMWARE_MAX_STALL) ?
180 FIRMWARE_MAX_STALL :
181 ctx->var1);
182 return 0;
183 }
184
erst_exec_stall_while_true(struct apei_exec_context * ctx,struct acpi_whea_header * entry)185 static int erst_exec_stall_while_true(struct apei_exec_context *ctx,
186 struct acpi_whea_header *entry)
187 {
188 int rc;
189 u64 val;
190 u64 timeout = FIRMWARE_TIMEOUT;
191 u64 stall_time = (ctx->var1 > FIRMWARE_MAX_STALL) ?
192 FIRMWARE_MAX_STALL :
193 ctx->var1;
194
195 for (;;) {
196 rc = __apei_exec_read_register(entry, &val);
197 if (rc)
198 return rc;
199 if (val != ctx->value)
200 break;
201 if (erst_timedout(&timeout, stall_time))
202 return -EIO;
203 }
204 return 0;
205 }
206
erst_exec_skip_next_instruction_if_true(struct apei_exec_context * ctx,struct acpi_whea_header * entry)207 static int erst_exec_skip_next_instruction_if_true(
208 struct apei_exec_context *ctx,
209 struct acpi_whea_header *entry)
210 {
211 int rc;
212 u64 val;
213
214 rc = __apei_exec_read_register(entry, &val);
215 if (rc)
216 return rc;
217 if (val == ctx->value) {
218 ctx->ip += 2;
219 return APEI_EXEC_SET_IP;
220 }
221
222 return 0;
223 }
224
erst_exec_goto(struct apei_exec_context * ctx,struct acpi_whea_header * entry)225 static int erst_exec_goto(struct apei_exec_context *ctx,
226 struct acpi_whea_header *entry)
227 {
228 ctx->ip = ctx->value;
229 return APEI_EXEC_SET_IP;
230 }
231
erst_exec_set_src_address_base(struct apei_exec_context * ctx,struct acpi_whea_header * entry)232 static int erst_exec_set_src_address_base(struct apei_exec_context *ctx,
233 struct acpi_whea_header *entry)
234 {
235 return __apei_exec_read_register(entry, &ctx->src_base);
236 }
237
erst_exec_set_dst_address_base(struct apei_exec_context * ctx,struct acpi_whea_header * entry)238 static int erst_exec_set_dst_address_base(struct apei_exec_context *ctx,
239 struct acpi_whea_header *entry)
240 {
241 return __apei_exec_read_register(entry, &ctx->dst_base);
242 }
243
erst_exec_move_data(struct apei_exec_context * ctx,struct acpi_whea_header * entry)244 static int erst_exec_move_data(struct apei_exec_context *ctx,
245 struct acpi_whea_header *entry)
246 {
247 int rc;
248 u64 offset;
249 void *src, *dst;
250
251 /* ioremap does not work in interrupt context */
252 if (in_irq()) {
253 printk(KERN_WARNING
254 "MOVE_DATA cannot be used in interrupt context\n");
255 return -EBUSY;
256 }
257
258 rc = __apei_exec_read_register(entry, &offset);
259 if (rc)
260 return rc;
261
262 src = ioremap(ctx->src_base + offset, ctx->var2);
263 if (!src)
264 return -ENOMEM;
265
266 dst = ioremap(ctx->dst_base + offset, ctx->var2);
267 if (dst) {
268 memmove(dst, src, ctx->var2);
269 iounmap(dst);
270 } else
271 rc = -ENOMEM;
272
273 iounmap(src);
274
275 return rc;
276 }
277
278 static struct apei_exec_ins_type erst_ins_type[] = {
279 [ACPI_ERST_READ_REGISTER] = {
280 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
281 .run = apei_exec_read_register,
282 },
283 [ACPI_ERST_READ_REGISTER_VALUE] = {
284 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
285 .run = apei_exec_read_register_value,
286 },
287 [ACPI_ERST_WRITE_REGISTER] = {
288 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
289 .run = apei_exec_write_register,
290 },
291 [ACPI_ERST_WRITE_REGISTER_VALUE] = {
292 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
293 .run = apei_exec_write_register_value,
294 },
295 [ACPI_ERST_NOOP] = {
296 .flags = 0,
297 .run = apei_exec_noop,
298 },
299 [ACPI_ERST_LOAD_VAR1] = {
300 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
301 .run = erst_exec_load_var1,
302 },
303 [ACPI_ERST_LOAD_VAR2] = {
304 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
305 .run = erst_exec_load_var2,
306 },
307 [ACPI_ERST_STORE_VAR1] = {
308 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
309 .run = erst_exec_store_var1,
310 },
311 [ACPI_ERST_ADD] = {
312 .flags = 0,
313 .run = erst_exec_add,
314 },
315 [ACPI_ERST_SUBTRACT] = {
316 .flags = 0,
317 .run = erst_exec_subtract,
318 },
319 [ACPI_ERST_ADD_VALUE] = {
320 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
321 .run = erst_exec_add_value,
322 },
323 [ACPI_ERST_SUBTRACT_VALUE] = {
324 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
325 .run = erst_exec_subtract_value,
326 },
327 [ACPI_ERST_STALL] = {
328 .flags = 0,
329 .run = erst_exec_stall,
330 },
331 [ACPI_ERST_STALL_WHILE_TRUE] = {
332 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
333 .run = erst_exec_stall_while_true,
334 },
335 [ACPI_ERST_SKIP_NEXT_IF_TRUE] = {
336 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
337 .run = erst_exec_skip_next_instruction_if_true,
338 },
339 [ACPI_ERST_GOTO] = {
340 .flags = 0,
341 .run = erst_exec_goto,
342 },
343 [ACPI_ERST_SET_SRC_ADDRESS_BASE] = {
344 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
345 .run = erst_exec_set_src_address_base,
346 },
347 [ACPI_ERST_SET_DST_ADDRESS_BASE] = {
348 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
349 .run = erst_exec_set_dst_address_base,
350 },
351 [ACPI_ERST_MOVE_DATA] = {
352 .flags = APEI_EXEC_INS_ACCESS_REGISTER,
353 .run = erst_exec_move_data,
354 },
355 };
356
erst_exec_ctx_init(struct apei_exec_context * ctx)357 static inline void erst_exec_ctx_init(struct apei_exec_context *ctx)
358 {
359 apei_exec_ctx_init(ctx, erst_ins_type, ARRAY_SIZE(erst_ins_type),
360 ERST_TAB_ENTRY(erst_tab), erst_tab->entries);
361 }
362
erst_get_erange(struct erst_erange * range)363 static int erst_get_erange(struct erst_erange *range)
364 {
365 struct apei_exec_context ctx;
366 int rc;
367
368 erst_exec_ctx_init(&ctx);
369 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_RANGE);
370 if (rc)
371 return rc;
372 range->base = apei_exec_ctx_get_output(&ctx);
373 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_LENGTH);
374 if (rc)
375 return rc;
376 range->size = apei_exec_ctx_get_output(&ctx);
377 rc = apei_exec_run(&ctx, ACPI_ERST_GET_ERROR_ATTRIBUTES);
378 if (rc)
379 return rc;
380 range->attr = apei_exec_ctx_get_output(&ctx);
381
382 return 0;
383 }
384
385 #ifndef NDEBUG /* currently dead code */
386
__erst_get_record_count(void)387 static ssize_t __erst_get_record_count(void)
388 {
389 struct apei_exec_context ctx;
390 int rc;
391 u64 output;
392 ssize_t count;
393
394 erst_exec_ctx_init(&ctx);
395 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_COUNT);
396 if (rc)
397 return rc;
398 count = output = apei_exec_ctx_get_output(&ctx);
399 return count >= 0 && count == output ? count : -ERANGE;
400 }
401
erst_get_record_count(void)402 ssize_t erst_get_record_count(void)
403 {
404 ssize_t count;
405 unsigned long flags;
406
407 if (!erst_enabled)
408 return -ENODEV;
409
410 spin_lock_irqsave(&erst_lock, flags);
411 count = __erst_get_record_count();
412 spin_unlock_irqrestore(&erst_lock, flags);
413
414 return count;
415 }
416
__erst_get_next_record_id(u64 * record_id)417 static int __erst_get_next_record_id(u64 *record_id)
418 {
419 struct apei_exec_context ctx;
420 int rc;
421
422 erst_exec_ctx_init(&ctx);
423 rc = apei_exec_run(&ctx, ACPI_ERST_GET_RECORD_ID);
424 if (rc)
425 return rc;
426 *record_id = apei_exec_ctx_get_output(&ctx);
427
428 return 0;
429 }
430
431 /*
432 * Get the record ID of an existing error record on the persistent
433 * storage. If there is no error record on the persistent storage, the
434 * returned record_id is APEI_ERST_INVALID_RECORD_ID.
435 */
erst_get_next_record_id(u64 * record_id)436 int erst_get_next_record_id(u64 *record_id)
437 {
438 int rc;
439 unsigned long flags;
440
441 if (!erst_enabled)
442 return -ENODEV;
443
444 spin_lock_irqsave(&erst_lock, flags);
445 rc = __erst_get_next_record_id(record_id);
446 spin_unlock_irqrestore(&erst_lock, flags);
447
448 return rc;
449 }
450
451 #endif /* currently dead code */
452
__erst_write_to_storage(u64 offset)453 static int __erst_write_to_storage(u64 offset)
454 {
455 struct apei_exec_context ctx;
456 u64 timeout = FIRMWARE_TIMEOUT;
457 u64 val;
458 int rc;
459
460 erst_exec_ctx_init(&ctx);
461 rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_WRITE);
462 if (rc)
463 return rc;
464 apei_exec_ctx_set_input(&ctx, offset);
465 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
466 if (rc)
467 return rc;
468 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
469 if (rc)
470 return rc;
471 for (;;) {
472 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
473 if (rc)
474 return rc;
475 val = apei_exec_ctx_get_output(&ctx);
476 if (!val)
477 break;
478 if (erst_timedout(&timeout, SPIN_UNIT))
479 return -EIO;
480 }
481 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
482 if (rc)
483 return rc;
484 val = apei_exec_ctx_get_output(&ctx);
485 rc = apei_exec_run(&ctx, ACPI_ERST_END);
486 if (rc)
487 return rc;
488
489 return erst_errno(val);
490 }
491
492 #ifndef NDEBUG /* currently dead code */
493
__erst_read_from_storage(u64 record_id,u64 offset)494 static int __erst_read_from_storage(u64 record_id, u64 offset)
495 {
496 struct apei_exec_context ctx;
497 u64 timeout = FIRMWARE_TIMEOUT;
498 u64 val;
499 int rc;
500
501 erst_exec_ctx_init(&ctx);
502 rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_READ);
503 if (rc)
504 return rc;
505 apei_exec_ctx_set_input(&ctx, offset);
506 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_OFFSET);
507 if (rc)
508 return rc;
509 apei_exec_ctx_set_input(&ctx, record_id);
510 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
511 if (rc)
512 return rc;
513 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
514 if (rc)
515 return rc;
516 for (;;) {
517 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
518 if (rc)
519 return rc;
520 val = apei_exec_ctx_get_output(&ctx);
521 if (!val)
522 break;
523 if (erst_timedout(&timeout, SPIN_UNIT))
524 return -EIO;
525 };
526 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
527 if (rc)
528 return rc;
529 val = apei_exec_ctx_get_output(&ctx);
530 rc = apei_exec_run(&ctx, ACPI_ERST_END);
531 if (rc)
532 return rc;
533
534 return erst_errno(val);
535 }
536
__erst_clear_from_storage(u64 record_id)537 static int __erst_clear_from_storage(u64 record_id)
538 {
539 struct apei_exec_context ctx;
540 u64 timeout = FIRMWARE_TIMEOUT;
541 u64 val;
542 int rc;
543
544 erst_exec_ctx_init(&ctx);
545 rc = apei_exec_run(&ctx, ACPI_ERST_BEGIN_CLEAR);
546 if (rc)
547 return rc;
548 apei_exec_ctx_set_input(&ctx, record_id);
549 rc = apei_exec_run(&ctx, ACPI_ERST_SET_RECORD_ID);
550 if (rc)
551 return rc;
552 rc = apei_exec_run(&ctx, ACPI_ERST_EXECUTE_OPERATION);
553 if (rc)
554 return rc;
555 for (;;) {
556 rc = apei_exec_run(&ctx, ACPI_ERST_CHECK_BUSY_STATUS);
557 if (rc)
558 return rc;
559 val = apei_exec_ctx_get_output(&ctx);
560 if (!val)
561 break;
562 if (erst_timedout(&timeout, SPIN_UNIT))
563 return -EIO;
564 }
565 rc = apei_exec_run(&ctx, ACPI_ERST_GET_COMMAND_STATUS);
566 if (rc)
567 return rc;
568 val = apei_exec_ctx_get_output(&ctx);
569 rc = apei_exec_run(&ctx, ACPI_ERST_END);
570 if (rc)
571 return rc;
572
573 return erst_errno(val);
574 }
575
576 #endif /* currently dead code */
577
578 /* NVRAM ERST Error Log Address Range is not supported yet */
__erst_write_to_nvram(const struct cper_record_header * record)579 static int __erst_write_to_nvram(const struct cper_record_header *record)
580 {
581 /* do not print message, because printk is not safe for NMI */
582 return -ENOSYS;
583 }
584
585 #ifndef NDEBUG /* currently dead code */
586
__erst_read_to_erange_from_nvram(u64 record_id,u64 * offset)587 static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset)
588 {
589 printk(KERN_WARNING
590 "NVRAM ERST Log Address Range is not implemented yet\n");
591 return -ENOSYS;
592 }
593
__erst_clear_from_nvram(u64 record_id)594 static int __erst_clear_from_nvram(u64 record_id)
595 {
596 printk(KERN_WARNING
597 "NVRAM ERST Log Address Range is not implemented yet\n");
598 return -ENOSYS;
599 }
600
601 #endif /* currently dead code */
602
erst_write(const struct cper_record_header * record)603 int erst_write(const struct cper_record_header *record)
604 {
605 int rc;
606 unsigned long flags;
607 struct cper_record_header *rcd_erange;
608
609 if (!record)
610 return -EINVAL;
611
612 if (!erst_enabled)
613 return -ENODEV;
614
615 if (memcmp(record->signature, CPER_SIG_RECORD, CPER_SIG_SIZE))
616 return -EINVAL;
617
618 if (erst_erange.attr & ERST_RANGE_NVRAM) {
619 if (!spin_trylock_irqsave(&erst_lock, flags))
620 return -EBUSY;
621 rc = __erst_write_to_nvram(record);
622 spin_unlock_irqrestore(&erst_lock, flags);
623 return rc;
624 }
625
626 if (record->record_length > erst_erange.size)
627 return -EINVAL;
628
629 if (!spin_trylock_irqsave(&erst_lock, flags))
630 return -EBUSY;
631 memcpy(erst_erange.vaddr, record, record->record_length);
632 rcd_erange = erst_erange.vaddr;
633 /* signature for serialization system */
634 memcpy(&rcd_erange->persistence_information, "ER", 2);
635
636 rc = __erst_write_to_storage(0);
637 spin_unlock_irqrestore(&erst_lock, flags);
638
639 return rc;
640 }
641
642 #ifndef NDEBUG /* currently dead code */
643
__erst_read_to_erange(u64 record_id,u64 * offset)644 static int __erst_read_to_erange(u64 record_id, u64 *offset)
645 {
646 int rc;
647
648 if (erst_erange.attr & ERST_RANGE_NVRAM)
649 return __erst_read_to_erange_from_nvram(
650 record_id, offset);
651
652 rc = __erst_read_from_storage(record_id, 0);
653 if (rc)
654 return rc;
655 *offset = 0;
656
657 return 0;
658 }
659
__erst_read(u64 record_id,struct cper_record_header * record,size_t buflen)660 static ssize_t __erst_read(u64 record_id, struct cper_record_header *record,
661 size_t buflen)
662 {
663 int rc;
664 u64 offset;
665 ssize_t len;
666 struct cper_record_header *rcd_tmp;
667
668 rc = __erst_read_to_erange(record_id, &offset);
669 if (rc)
670 return rc;
671 rcd_tmp = erst_erange.vaddr + offset;
672 if (rcd_tmp->record_length > buflen)
673 return -ENOBUFS;
674 len = rcd_tmp->record_length;
675 if (len < 0)
676 return -ERANGE;
677 memcpy(record, rcd_tmp, len);
678
679 return len;
680 }
681
682 /*
683 * If return value > buflen, the buffer size is not big enough,
684 * else if return value < 0, something goes wrong,
685 * else everything is OK, and return value is record length
686 */
erst_read(u64 record_id,struct cper_record_header * record,size_t buflen)687 ssize_t erst_read(u64 record_id, struct cper_record_header *record,
688 size_t buflen)
689 {
690 ssize_t len;
691 unsigned long flags;
692
693 if (!erst_enabled)
694 return -ENODEV;
695
696 spin_lock_irqsave(&erst_lock, flags);
697 len = __erst_read(record_id, record, buflen);
698 spin_unlock_irqrestore(&erst_lock, flags);
699 return len;
700 }
701
702 /*
703 * If return value > buflen, the buffer size is not big enough,
704 * else if return value = 0, there is no more record to read,
705 * else if return value < 0, something goes wrong,
706 * else everything is OK, and return value is record length
707 */
erst_read_next(struct cper_record_header * record,size_t buflen)708 ssize_t erst_read_next(struct cper_record_header *record, size_t buflen)
709 {
710 int rc;
711 ssize_t len;
712 unsigned long flags;
713 u64 record_id;
714
715 if (!erst_enabled)
716 return -ENODEV;
717
718 spin_lock_irqsave(&erst_lock, flags);
719 rc = __erst_get_next_record_id(&record_id);
720 if (rc) {
721 spin_unlock_irqrestore(&erst_lock, flags);
722 return rc;
723 }
724 /* no more record */
725 if (record_id == APEI_ERST_INVALID_RECORD_ID) {
726 spin_unlock_irqrestore(&erst_lock, flags);
727 return 0;
728 }
729
730 len = __erst_read(record_id, record, buflen);
731 spin_unlock_irqrestore(&erst_lock, flags);
732
733 return len;
734 }
735
erst_clear(u64 record_id)736 int erst_clear(u64 record_id)
737 {
738 int rc;
739 unsigned long flags;
740
741 if (!erst_enabled)
742 return -ENODEV;
743
744 spin_lock_irqsave(&erst_lock, flags);
745 if (erst_erange.attr & ERST_RANGE_NVRAM)
746 rc = __erst_clear_from_nvram(record_id);
747 else
748 rc = __erst_clear_from_storage(record_id);
749 spin_unlock_irqrestore(&erst_lock, flags);
750
751 return rc;
752 }
753
754 #endif /* currently dead code */
755
erst_check_table(struct acpi_table_erst * erst_tab)756 static int __init erst_check_table(struct acpi_table_erst *erst_tab)
757 {
758 if (erst_tab->header.length < sizeof(*erst_tab))
759 return -EINVAL;
760
761 switch (erst_tab->header_length) {
762 case sizeof(*erst_tab) - sizeof(erst_tab->header):
763 /*
764 * While invalid per specification, there are (early?) systems
765 * indicating the full header size here, so accept that value too.
766 */
767 case sizeof(*erst_tab):
768 break;
769 default:
770 return -EINVAL;
771 }
772
773 if (erst_tab->entries !=
774 (erst_tab->header.length - sizeof(*erst_tab)) /
775 sizeof(struct acpi_erst_entry))
776 return -EINVAL;
777
778 return 0;
779 }
780
erst_init(void)781 int __init erst_init(void)
782 {
783 int rc = 0;
784 acpi_status status;
785 acpi_physical_address erst_addr;
786 acpi_native_uint erst_len;
787 struct apei_exec_context ctx;
788
789 if (acpi_disabled)
790 return -ENODEV;
791
792 status = acpi_get_table_phys(ACPI_SIG_ERST, 0, &erst_addr, &erst_len);
793 if (status == AE_NOT_FOUND) {
794 printk(KERN_INFO "ERST table was not found\n");
795 return -ENODEV;
796 }
797 if (ACPI_FAILURE(status)) {
798 const char *msg = acpi_format_exception(status);
799 printk(KERN_WARNING "Failed to get ERST table: %s\n", msg);
800 return -EINVAL;
801 }
802 map_pages_to_xen((unsigned long)__va(erst_addr), PFN_DOWN(erst_addr),
803 PFN_UP(erst_addr + erst_len) - PFN_DOWN(erst_addr),
804 PAGE_HYPERVISOR);
805 erst_tab = __va(erst_addr);
806
807 rc = erst_check_table(erst_tab);
808 if (rc) {
809 printk(KERN_ERR "ERST table is invalid\n");
810 return rc;
811 }
812
813 erst_exec_ctx_init(&ctx);
814 rc = apei_exec_pre_map_gars(&ctx);
815 if (rc)
816 return rc;
817
818 rc = erst_get_erange(&erst_erange);
819 if (rc) {
820 if (rc == -ENODEV)
821 printk(KERN_INFO
822 "The corresponding hardware device or firmware "
823 "implementation is not available.\n");
824 else
825 printk(KERN_ERR
826 "Failed to get Error Log Address Range.\n");
827 goto err_unmap_reg;
828 }
829
830 erst_erange.vaddr = apei_pre_map(erst_erange.base, erst_erange.size);
831 if (!erst_erange.vaddr) {
832 rc = -ENOMEM;
833 goto err_unmap_reg;
834 }
835
836 printk(KERN_INFO "Xen ERST support is initialized.\n");
837 erst_enabled = 1;
838
839 return 0;
840
841 err_unmap_reg:
842 apei_exec_post_unmap_gars(&ctx);
843 return rc;
844 }
845