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 61 int erst_disable; 62 EXPORT_SYMBOL_GPL(erst_disable); 63 64 static 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 */ 75 static 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 */ 90 static DEFINE_RAW_SPINLOCK(erst_lock); 91 92 static 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 109 static 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 122 static 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 128 static 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 134 static 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 140 static 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 147 static 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 154 static 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 168 static 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 182 static 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 199 static 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 228 static 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 246 static 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 253 static 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 259 static 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 265 static 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 iounmap(src); 289 return -ENOMEM; 290 } 291 292 memmove(dst, src, ctx->var2); 293 294 iounmap(src); 295 iounmap(dst); 296 297 return 0; 298 } 299 300 static 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 379 static 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 385 static 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 407 static 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 419 ssize_t erst_get_record_count(void) 420 { 421 ssize_t count; 422 unsigned long flags; 423 424 if (erst_disable) 425 return -ENODEV; 426 427 raw_spin_lock_irqsave(&erst_lock, flags); 428 count = __erst_get_record_count(); 429 raw_spin_unlock_irqrestore(&erst_lock, flags); 430 431 return count; 432 } 433 EXPORT_SYMBOL_GPL(erst_get_record_count); 434 435 #define ERST_RECORD_ID_CACHE_SIZE_MIN 16 436 #define ERST_RECORD_ID_CACHE_SIZE_MAX 1024 437 438 struct erst_record_id_cache { 439 struct mutex lock; 440 u64 *entries; 441 int len; 442 int size; 443 int refcount; 444 }; 445 446 static struct erst_record_id_cache erst_record_id_cache = { 447 .lock = __MUTEX_INITIALIZER(erst_record_id_cache.lock), 448 .refcount = 0, 449 }; 450 451 static 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 465 int 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 } 482 EXPORT_SYMBOL_GPL(erst_get_record_id_begin); 483 484 /* erst_record_id_cache.lock must be held by caller */ 485 static 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; 493 retry: 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 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 */ 558 int erst_get_record_id_next(int *pos, u64 *record_id) 559 { 560 int rc = 0; 561 u64 *entries; 562 563 if (erst_disable) 564 return -ENODEV; 565 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 } 595 out_unlock: 596 mutex_unlock(&erst_record_id_cache.lock); 597 598 return rc; 599 } 600 EXPORT_SYMBOL_GPL(erst_get_record_id_next); 601 602 /* erst_record_id_cache.lock must be held by caller */ 603 static 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 622 void 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 } 637 EXPORT_SYMBOL_GPL(erst_get_record_id_end); 638 639 static 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); 647 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_WRITE); 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); 671 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END); 672 if (rc) 673 return rc; 674 675 return erst_errno(val); 676 } 677 678 static 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); 686 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_READ); 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); 714 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END); 715 if (rc) 716 return rc; 717 718 return erst_errno(val); 719 } 720 721 static 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); 729 rc = apei_exec_run_optional(&ctx, ACPI_ERST_BEGIN_CLEAR); 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); 753 rc = apei_exec_run_optional(&ctx, ACPI_ERST_END); 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 */ 761 static 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 768 static 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 774 static int __erst_read_to_erange_from_nvram(u64 record_id, u64 *offset) 775 { 776 pr_unimpl_nvram(); 777 return -ENOSYS; 778 } 779 780 static int __erst_clear_from_nvram(u64 record_id) 781 { 782 pr_unimpl_nvram(); 783 return -ENOSYS; 784 } 785 786 int 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) { 799 if (!raw_spin_trylock_irqsave(&erst_lock, flags)) 800 return -EBUSY; 801 rc = __erst_write_to_nvram(record); 802 raw_spin_unlock_irqrestore(&erst_lock, flags); 803 return rc; 804 } 805 806 if (record->record_length > erst_erange.size) 807 return -EINVAL; 808 809 if (!raw_spin_trylock_irqsave(&erst_lock, flags)) 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); 817 raw_spin_unlock_irqrestore(&erst_lock, flags); 818 819 return rc; 820 } 821 EXPORT_SYMBOL_GPL(erst_write); 822 823 static 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 839 static 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 */ 862 ssize_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 871 raw_spin_lock_irqsave(&erst_lock, flags); 872 len = __erst_read(record_id, record, buflen); 873 raw_spin_unlock_irqrestore(&erst_lock, flags); 874 return len; 875 } 876 EXPORT_SYMBOL_GPL(erst_read); 877 878 int erst_clear(u64 record_id) 879 { 880 int rc, i; 881 unsigned long flags; 882 u64 *entries; 883 884 if (erst_disable) 885 return -ENODEV; 886 887 rc = mutex_lock_interruptible(&erst_record_id_cache.lock); 888 if (rc) 889 return rc; 890 raw_spin_lock_irqsave(&erst_lock, flags); 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); 895 raw_spin_unlock_irqrestore(&erst_lock, flags); 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(); 904 out: 905 mutex_unlock(&erst_record_id_cache.lock); 906 return rc; 907 } 908 EXPORT_SYMBOL_GPL(erst_clear); 909 910 static 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 918 static int erst_check_table(struct acpi_table_erst *erst_tab) 919 { 920 if ((erst_tab->header_length != 921 (sizeof(struct acpi_table_erst) - sizeof(erst_tab->header))) 922 && (erst_tab->header_length != sizeof(struct acpi_table_erst))) 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 934 static int erst_open_pstore(struct pstore_info *psi); 935 static int erst_close_pstore(struct pstore_info *psi); 936 static ssize_t erst_reader(u64 *id, enum pstore_type_id *type, int *count, 937 struct timespec *time, char **buf, 938 bool *compressed, struct pstore_info *psi); 939 static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason, 940 u64 *id, unsigned int part, int count, bool compressed, 941 size_t size, struct pstore_info *psi); 942 static int erst_clearer(enum pstore_type_id type, u64 id, int count, 943 struct timespec time, struct pstore_info *psi); 944 945 static struct pstore_info erst_info = { 946 .owner = THIS_MODULE, 947 .name = "erst", 948 .open = erst_open_pstore, 949 .close = erst_close_pstore, 950 .read = erst_reader, 951 .write = erst_writer, 952 .erase = erst_clearer 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_DMESG_Z \ 962 UUID_LE(0x4f118707, 0x04dd, 0x4055, 0xb5, 0xdd, 0x95, 0x6d, \ 963 0x34, 0xdd, 0xfa, 0xc6) 964 #define CPER_SECTION_TYPE_MCE \ 965 UUID_LE(0xfe08ffbe, 0x95e4, 0x4be7, 0xbc, 0x73, 0x40, 0x96, \ 966 0x04, 0x4a, 0x38, 0xfc) 967 968 struct cper_pstore_record { 969 struct cper_record_header hdr; 970 struct cper_section_descriptor sec_hdr; 971 char data[]; 972 } __packed; 973 974 static int reader_pos; 975 976 static int erst_open_pstore(struct pstore_info *psi) 977 { 978 int rc; 979 980 if (erst_disable) 981 return -ENODEV; 982 983 rc = erst_get_record_id_begin(&reader_pos); 984 985 return rc; 986 } 987 988 static int erst_close_pstore(struct pstore_info *psi) 989 { 990 erst_get_record_id_end(); 991 992 return 0; 993 } 994 995 static ssize_t erst_reader(u64 *id, enum pstore_type_id *type, int *count, 996 struct timespec *time, char **buf, 997 bool *compressed, struct pstore_info *psi) 998 { 999 int rc; 1000 ssize_t len = 0; 1001 u64 record_id; 1002 struct cper_pstore_record *rcd; 1003 size_t rcd_len = sizeof(*rcd) + erst_info.bufsize; 1004 1005 if (erst_disable) 1006 return -ENODEV; 1007 1008 rcd = kmalloc(rcd_len, GFP_KERNEL); 1009 if (!rcd) { 1010 rc = -ENOMEM; 1011 goto out; 1012 } 1013 skip: 1014 rc = erst_get_record_id_next(&reader_pos, &record_id); 1015 if (rc) 1016 goto out; 1017 1018 /* no more record */ 1019 if (record_id == APEI_ERST_INVALID_RECORD_ID) { 1020 rc = -EINVAL; 1021 goto out; 1022 } 1023 1024 len = erst_read(record_id, &rcd->hdr, rcd_len); 1025 /* The record may be cleared by others, try read next record */ 1026 if (len == -ENOENT) 1027 goto skip; 1028 else if (len < sizeof(*rcd)) { 1029 rc = -EIO; 1030 goto out; 1031 } 1032 if (uuid_le_cmp(rcd->hdr.creator_id, CPER_CREATOR_PSTORE) != 0) 1033 goto skip; 1034 1035 *buf = kmalloc(len, GFP_KERNEL); 1036 if (*buf == NULL) { 1037 rc = -ENOMEM; 1038 goto out; 1039 } 1040 memcpy(*buf, rcd->data, len - sizeof(*rcd)); 1041 *id = record_id; 1042 *compressed = false; 1043 if (uuid_le_cmp(rcd->sec_hdr.section_type, 1044 CPER_SECTION_TYPE_DMESG_Z) == 0) { 1045 *type = PSTORE_TYPE_DMESG; 1046 *compressed = true; 1047 } else if (uuid_le_cmp(rcd->sec_hdr.section_type, 1048 CPER_SECTION_TYPE_DMESG) == 0) 1049 *type = PSTORE_TYPE_DMESG; 1050 else if (uuid_le_cmp(rcd->sec_hdr.section_type, 1051 CPER_SECTION_TYPE_MCE) == 0) 1052 *type = PSTORE_TYPE_MCE; 1053 else 1054 *type = PSTORE_TYPE_UNKNOWN; 1055 1056 if (rcd->hdr.validation_bits & CPER_VALID_TIMESTAMP) 1057 time->tv_sec = rcd->hdr.timestamp; 1058 else 1059 time->tv_sec = 0; 1060 time->tv_nsec = 0; 1061 1062 out: 1063 kfree(rcd); 1064 return (rc < 0) ? rc : (len - sizeof(*rcd)); 1065 } 1066 1067 static int erst_writer(enum pstore_type_id type, enum kmsg_dump_reason reason, 1068 u64 *id, unsigned int part, int count, bool compressed, 1069 size_t size, struct pstore_info *psi) 1070 { 1071 struct cper_pstore_record *rcd = (struct cper_pstore_record *) 1072 (erst_info.buf - sizeof(*rcd)); 1073 int ret; 1074 1075 memset(rcd, 0, sizeof(*rcd)); 1076 memcpy(rcd->hdr.signature, CPER_SIG_RECORD, CPER_SIG_SIZE); 1077 rcd->hdr.revision = CPER_RECORD_REV; 1078 rcd->hdr.signature_end = CPER_SIG_END; 1079 rcd->hdr.section_count = 1; 1080 rcd->hdr.error_severity = CPER_SEV_FATAL; 1081 /* timestamp valid. platform_id, partition_id are invalid */ 1082 rcd->hdr.validation_bits = CPER_VALID_TIMESTAMP; 1083 rcd->hdr.timestamp = get_seconds(); 1084 rcd->hdr.record_length = sizeof(*rcd) + size; 1085 rcd->hdr.creator_id = CPER_CREATOR_PSTORE; 1086 rcd->hdr.notification_type = CPER_NOTIFY_MCE; 1087 rcd->hdr.record_id = cper_next_record_id(); 1088 rcd->hdr.flags = CPER_HW_ERROR_FLAGS_PREVERR; 1089 1090 rcd->sec_hdr.section_offset = sizeof(*rcd); 1091 rcd->sec_hdr.section_length = size; 1092 rcd->sec_hdr.revision = CPER_SEC_REV; 1093 /* fru_id and fru_text is invalid */ 1094 rcd->sec_hdr.validation_bits = 0; 1095 rcd->sec_hdr.flags = CPER_SEC_PRIMARY; 1096 switch (type) { 1097 case PSTORE_TYPE_DMESG: 1098 if (compressed) 1099 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG_Z; 1100 else 1101 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_DMESG; 1102 break; 1103 case PSTORE_TYPE_MCE: 1104 rcd->sec_hdr.section_type = CPER_SECTION_TYPE_MCE; 1105 break; 1106 default: 1107 return -EINVAL; 1108 } 1109 rcd->sec_hdr.section_severity = CPER_SEV_FATAL; 1110 1111 ret = erst_write(&rcd->hdr); 1112 *id = rcd->hdr.record_id; 1113 1114 return ret; 1115 } 1116 1117 static int erst_clearer(enum pstore_type_id type, u64 id, int count, 1118 struct timespec time, struct pstore_info *psi) 1119 { 1120 return erst_clear(id); 1121 } 1122 1123 static int __init erst_init(void) 1124 { 1125 int rc = 0; 1126 acpi_status status; 1127 struct apei_exec_context ctx; 1128 struct apei_resources erst_resources; 1129 struct resource *r; 1130 char *buf; 1131 1132 if (acpi_disabled) 1133 goto err; 1134 1135 if (erst_disable) { 1136 pr_info(ERST_PFX 1137 "Error Record Serialization Table (ERST) support is disabled.\n"); 1138 goto err; 1139 } 1140 1141 status = acpi_get_table(ACPI_SIG_ERST, 0, 1142 (struct acpi_table_header **)&erst_tab); 1143 if (status == AE_NOT_FOUND) 1144 goto err; 1145 else if (ACPI_FAILURE(status)) { 1146 const char *msg = acpi_format_exception(status); 1147 pr_err(ERST_PFX "Failed to get table, %s\n", msg); 1148 rc = -EINVAL; 1149 goto err; 1150 } 1151 1152 rc = erst_check_table(erst_tab); 1153 if (rc) { 1154 pr_err(FW_BUG ERST_PFX "ERST table is invalid\n"); 1155 goto err; 1156 } 1157 1158 apei_resources_init(&erst_resources); 1159 erst_exec_ctx_init(&ctx); 1160 rc = apei_exec_collect_resources(&ctx, &erst_resources); 1161 if (rc) 1162 goto err_fini; 1163 rc = apei_resources_request(&erst_resources, "APEI ERST"); 1164 if (rc) 1165 goto err_fini; 1166 rc = apei_exec_pre_map_gars(&ctx); 1167 if (rc) 1168 goto err_release; 1169 rc = erst_get_erange(&erst_erange); 1170 if (rc) { 1171 if (rc == -ENODEV) 1172 pr_info(ERST_PFX 1173 "The corresponding hardware device or firmware implementation " 1174 "is not available.\n"); 1175 else 1176 pr_err(ERST_PFX 1177 "Failed to get Error Log Address Range.\n"); 1178 goto err_unmap_reg; 1179 } 1180 1181 r = request_mem_region(erst_erange.base, erst_erange.size, "APEI ERST"); 1182 if (!r) { 1183 pr_err(ERST_PFX 1184 "Can not request iomem region <0x%16llx-0x%16llx> for ERST.\n", 1185 (unsigned long long)erst_erange.base, 1186 (unsigned long long)erst_erange.base + erst_erange.size); 1187 rc = -EIO; 1188 goto err_unmap_reg; 1189 } 1190 rc = -ENOMEM; 1191 erst_erange.vaddr = ioremap_cache(erst_erange.base, 1192 erst_erange.size); 1193 if (!erst_erange.vaddr) 1194 goto err_release_erange; 1195 1196 pr_info(ERST_PFX 1197 "Error Record Serialization Table (ERST) support is initialized.\n"); 1198 1199 buf = kmalloc(erst_erange.size, GFP_KERNEL); 1200 spin_lock_init(&erst_info.buf_lock); 1201 if (buf) { 1202 erst_info.buf = buf + sizeof(struct cper_pstore_record); 1203 erst_info.bufsize = erst_erange.size - 1204 sizeof(struct cper_pstore_record); 1205 rc = pstore_register(&erst_info); 1206 if (rc) { 1207 if (rc != -EPERM) 1208 pr_info(ERST_PFX 1209 "Could not register with persistent store\n"); 1210 erst_info.buf = NULL; 1211 erst_info.bufsize = 0; 1212 kfree(buf); 1213 } 1214 } else 1215 pr_err(ERST_PFX 1216 "Failed to allocate %lld bytes for persistent store error log\n", 1217 erst_erange.size); 1218 1219 return 0; 1220 1221 err_release_erange: 1222 release_mem_region(erst_erange.base, erst_erange.size); 1223 err_unmap_reg: 1224 apei_exec_post_unmap_gars(&ctx); 1225 err_release: 1226 apei_resources_release(&erst_resources); 1227 err_fini: 1228 apei_resources_fini(&erst_resources); 1229 err: 1230 erst_disable = 1; 1231 return rc; 1232 } 1233 1234 device_initcall(erst_init); 1235