1 /* 2 * c 2001 PPC 64 Team, IBM Corp 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 7 * 2 of the License, or (at your option) any later version. 8 * 9 * /dev/nvram driver for PPC64 10 * 11 * This perhaps should live in drivers/char 12 */ 13 14 15 #include <linux/types.h> 16 #include <linux/errno.h> 17 #include <linux/init.h> 18 #include <linux/spinlock.h> 19 #include <linux/slab.h> 20 #include <linux/kmsg_dump.h> 21 #include <linux/pstore.h> 22 #include <linux/ctype.h> 23 #include <linux/zlib.h> 24 #include <asm/uaccess.h> 25 #include <asm/nvram.h> 26 #include <asm/rtas.h> 27 #include <asm/prom.h> 28 #include <asm/machdep.h> 29 30 /* Max bytes to read/write in one go */ 31 #define NVRW_CNT 0x20 32 33 /* 34 * Set oops header version to distinguish between old and new format header. 35 * lnx,oops-log partition max size is 4000, header version > 4000 will 36 * help in identifying new header. 37 */ 38 #define OOPS_HDR_VERSION 5000 39 40 static unsigned int nvram_size; 41 static int nvram_fetch, nvram_store; 42 static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */ 43 static DEFINE_SPINLOCK(nvram_lock); 44 45 struct err_log_info { 46 __be32 error_type; 47 __be32 seq_num; 48 }; 49 50 struct nvram_os_partition { 51 const char *name; 52 int req_size; /* desired size, in bytes */ 53 int min_size; /* minimum acceptable size (0 means req_size) */ 54 long size; /* size of data portion (excluding err_log_info) */ 55 long index; /* offset of data portion of partition */ 56 bool os_partition; /* partition initialized by OS, not FW */ 57 }; 58 59 static struct nvram_os_partition rtas_log_partition = { 60 .name = "ibm,rtas-log", 61 .req_size = 2079, 62 .min_size = 1055, 63 .index = -1, 64 .os_partition = true 65 }; 66 67 static struct nvram_os_partition oops_log_partition = { 68 .name = "lnx,oops-log", 69 .req_size = 4000, 70 .min_size = 2000, 71 .index = -1, 72 .os_partition = true 73 }; 74 75 static const char *pseries_nvram_os_partitions[] = { 76 "ibm,rtas-log", 77 "lnx,oops-log", 78 NULL 79 }; 80 81 struct oops_log_info { 82 __be16 version; 83 __be16 report_length; 84 __be64 timestamp; 85 } __attribute__((packed)); 86 87 static void oops_to_nvram(struct kmsg_dumper *dumper, 88 enum kmsg_dump_reason reason); 89 90 static struct kmsg_dumper nvram_kmsg_dumper = { 91 .dump = oops_to_nvram 92 }; 93 94 /* See clobbering_unread_rtas_event() */ 95 #define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */ 96 static unsigned long last_unread_rtas_event; /* timestamp */ 97 98 /* 99 * For capturing and compressing an oops or panic report... 100 101 * big_oops_buf[] holds the uncompressed text we're capturing. 102 * 103 * oops_buf[] holds the compressed text, preceded by a oops header. 104 * oops header has u16 holding the version of oops header (to differentiate 105 * between old and new format header) followed by u16 holding the length of 106 * the compressed* text (*Or uncompressed, if compression fails.) and u64 107 * holding the timestamp. oops_buf[] gets written to NVRAM. 108 * 109 * oops_log_info points to the header. oops_data points to the compressed text. 110 * 111 * +- oops_buf 112 * | +- oops_data 113 * v v 114 * +-----------+-----------+-----------+------------------------+ 115 * | version | length | timestamp | text | 116 * | (2 bytes) | (2 bytes) | (8 bytes) | (oops_data_sz bytes) | 117 * +-----------+-----------+-----------+------------------------+ 118 * ^ 119 * +- oops_log_info 120 * 121 * We preallocate these buffers during init to avoid kmalloc during oops/panic. 122 */ 123 static size_t big_oops_buf_sz; 124 static char *big_oops_buf, *oops_buf; 125 static char *oops_data; 126 static size_t oops_data_sz; 127 128 /* Compression parameters */ 129 #define COMPR_LEVEL 6 130 #define WINDOW_BITS 12 131 #define MEM_LEVEL 4 132 static struct z_stream_s stream; 133 134 #ifdef CONFIG_PSTORE 135 static struct nvram_os_partition of_config_partition = { 136 .name = "of-config", 137 .index = -1, 138 .os_partition = false 139 }; 140 141 static struct nvram_os_partition common_partition = { 142 .name = "common", 143 .index = -1, 144 .os_partition = false 145 }; 146 147 static enum pstore_type_id nvram_type_ids[] = { 148 PSTORE_TYPE_DMESG, 149 PSTORE_TYPE_PPC_RTAS, 150 PSTORE_TYPE_PPC_OF, 151 PSTORE_TYPE_PPC_COMMON, 152 -1 153 }; 154 static int read_type; 155 static unsigned long last_rtas_event; 156 #endif 157 158 static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index) 159 { 160 unsigned int i; 161 unsigned long len; 162 int done; 163 unsigned long flags; 164 char *p = buf; 165 166 167 if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE) 168 return -ENODEV; 169 170 if (*index >= nvram_size) 171 return 0; 172 173 i = *index; 174 if (i + count > nvram_size) 175 count = nvram_size - i; 176 177 spin_lock_irqsave(&nvram_lock, flags); 178 179 for (; count != 0; count -= len) { 180 len = count; 181 if (len > NVRW_CNT) 182 len = NVRW_CNT; 183 184 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf), 185 len) != 0) || len != done) { 186 spin_unlock_irqrestore(&nvram_lock, flags); 187 return -EIO; 188 } 189 190 memcpy(p, nvram_buf, len); 191 192 p += len; 193 i += len; 194 } 195 196 spin_unlock_irqrestore(&nvram_lock, flags); 197 198 *index = i; 199 return p - buf; 200 } 201 202 static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index) 203 { 204 unsigned int i; 205 unsigned long len; 206 int done; 207 unsigned long flags; 208 const char *p = buf; 209 210 if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE) 211 return -ENODEV; 212 213 if (*index >= nvram_size) 214 return 0; 215 216 i = *index; 217 if (i + count > nvram_size) 218 count = nvram_size - i; 219 220 spin_lock_irqsave(&nvram_lock, flags); 221 222 for (; count != 0; count -= len) { 223 len = count; 224 if (len > NVRW_CNT) 225 len = NVRW_CNT; 226 227 memcpy(nvram_buf, p, len); 228 229 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf), 230 len) != 0) || len != done) { 231 spin_unlock_irqrestore(&nvram_lock, flags); 232 return -EIO; 233 } 234 235 p += len; 236 i += len; 237 } 238 spin_unlock_irqrestore(&nvram_lock, flags); 239 240 *index = i; 241 return p - buf; 242 } 243 244 static ssize_t pSeries_nvram_get_size(void) 245 { 246 return nvram_size ? nvram_size : -ENODEV; 247 } 248 249 250 /* nvram_write_os_partition, nvram_write_error_log 251 * 252 * We need to buffer the error logs into nvram to ensure that we have 253 * the failure information to decode. If we have a severe error there 254 * is no way to guarantee that the OS or the machine is in a state to 255 * get back to user land and write the error to disk. For example if 256 * the SCSI device driver causes a Machine Check by writing to a bad 257 * IO address, there is no way of guaranteeing that the device driver 258 * is in any state that is would also be able to write the error data 259 * captured to disk, thus we buffer it in NVRAM for analysis on the 260 * next boot. 261 * 262 * In NVRAM the partition containing the error log buffer will looks like: 263 * Header (in bytes): 264 * +-----------+----------+--------+------------+------------------+ 265 * | signature | checksum | length | name | data | 266 * |0 |1 |2 3|4 15|16 length-1| 267 * +-----------+----------+--------+------------+------------------+ 268 * 269 * The 'data' section would look like (in bytes): 270 * +--------------+------------+-----------------------------------+ 271 * | event_logged | sequence # | error log | 272 * |0 3|4 7|8 error_log_size-1| 273 * +--------------+------------+-----------------------------------+ 274 * 275 * event_logged: 0 if event has not been logged to syslog, 1 if it has 276 * sequence #: The unique sequence # for each event. (until it wraps) 277 * error log: The error log from event_scan 278 */ 279 static int nvram_write_os_partition(struct nvram_os_partition *part, 280 char *buff, int length, 281 unsigned int err_type, 282 unsigned int error_log_cnt) 283 { 284 int rc; 285 loff_t tmp_index; 286 struct err_log_info info; 287 288 if (part->index == -1) { 289 return -ESPIPE; 290 } 291 292 if (length > part->size) { 293 length = part->size; 294 } 295 296 info.error_type = cpu_to_be32(err_type); 297 info.seq_num = cpu_to_be32(error_log_cnt); 298 299 tmp_index = part->index; 300 301 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index); 302 if (rc <= 0) { 303 pr_err("%s: Failed nvram_write (%d)\n", __func__, rc); 304 return rc; 305 } 306 307 rc = ppc_md.nvram_write(buff, length, &tmp_index); 308 if (rc <= 0) { 309 pr_err("%s: Failed nvram_write (%d)\n", __func__, rc); 310 return rc; 311 } 312 313 return 0; 314 } 315 316 int nvram_write_error_log(char * buff, int length, 317 unsigned int err_type, unsigned int error_log_cnt) 318 { 319 int rc = nvram_write_os_partition(&rtas_log_partition, buff, length, 320 err_type, error_log_cnt); 321 if (!rc) { 322 last_unread_rtas_event = get_seconds(); 323 #ifdef CONFIG_PSTORE 324 last_rtas_event = get_seconds(); 325 #endif 326 } 327 328 return rc; 329 } 330 331 /* nvram_read_partition 332 * 333 * Reads nvram partition for at most 'length' 334 */ 335 static int nvram_read_partition(struct nvram_os_partition *part, char *buff, 336 int length, unsigned int *err_type, 337 unsigned int *error_log_cnt) 338 { 339 int rc; 340 loff_t tmp_index; 341 struct err_log_info info; 342 343 if (part->index == -1) 344 return -1; 345 346 if (length > part->size) 347 length = part->size; 348 349 tmp_index = part->index; 350 351 if (part->os_partition) { 352 rc = ppc_md.nvram_read((char *)&info, 353 sizeof(struct err_log_info), 354 &tmp_index); 355 if (rc <= 0) { 356 pr_err("%s: Failed nvram_read (%d)\n", __func__, rc); 357 return rc; 358 } 359 } 360 361 rc = ppc_md.nvram_read(buff, length, &tmp_index); 362 if (rc <= 0) { 363 pr_err("%s: Failed nvram_read (%d)\n", __func__, rc); 364 return rc; 365 } 366 367 if (part->os_partition) { 368 *error_log_cnt = be32_to_cpu(info.seq_num); 369 *err_type = be32_to_cpu(info.error_type); 370 } 371 372 return 0; 373 } 374 375 /* nvram_read_error_log 376 * 377 * Reads nvram for error log for at most 'length' 378 */ 379 int nvram_read_error_log(char *buff, int length, 380 unsigned int *err_type, unsigned int *error_log_cnt) 381 { 382 return nvram_read_partition(&rtas_log_partition, buff, length, 383 err_type, error_log_cnt); 384 } 385 386 /* This doesn't actually zero anything, but it sets the event_logged 387 * word to tell that this event is safely in syslog. 388 */ 389 int nvram_clear_error_log(void) 390 { 391 loff_t tmp_index; 392 int clear_word = ERR_FLAG_ALREADY_LOGGED; 393 int rc; 394 395 if (rtas_log_partition.index == -1) 396 return -1; 397 398 tmp_index = rtas_log_partition.index; 399 400 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index); 401 if (rc <= 0) { 402 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc); 403 return rc; 404 } 405 last_unread_rtas_event = 0; 406 407 return 0; 408 } 409 410 /* pseries_nvram_init_os_partition 411 * 412 * This sets up a partition with an "OS" signature. 413 * 414 * The general strategy is the following: 415 * 1.) If a partition with the indicated name already exists... 416 * - If it's large enough, use it. 417 * - Otherwise, recycle it and keep going. 418 * 2.) Search for a free partition that is large enough. 419 * 3.) If there's not a free partition large enough, recycle any obsolete 420 * OS partitions and try again. 421 * 4.) Will first try getting a chunk that will satisfy the requested size. 422 * 5.) If a chunk of the requested size cannot be allocated, then try finding 423 * a chunk that will satisfy the minum needed. 424 * 425 * Returns 0 on success, else -1. 426 */ 427 static int __init pseries_nvram_init_os_partition(struct nvram_os_partition 428 *part) 429 { 430 loff_t p; 431 int size; 432 433 /* Look for ours */ 434 p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size); 435 436 /* Found one but too small, remove it */ 437 if (p && size < part->min_size) { 438 pr_info("nvram: Found too small %s partition," 439 " removing it...\n", part->name); 440 nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL); 441 p = 0; 442 } 443 444 /* Create one if we didn't find */ 445 if (!p) { 446 p = nvram_create_partition(part->name, NVRAM_SIG_OS, 447 part->req_size, part->min_size); 448 if (p == -ENOSPC) { 449 pr_info("nvram: No room to create %s partition, " 450 "deleting any obsolete OS partitions...\n", 451 part->name); 452 nvram_remove_partition(NULL, NVRAM_SIG_OS, 453 pseries_nvram_os_partitions); 454 p = nvram_create_partition(part->name, NVRAM_SIG_OS, 455 part->req_size, part->min_size); 456 } 457 } 458 459 if (p <= 0) { 460 pr_err("nvram: Failed to find or create %s" 461 " partition, err %d\n", part->name, (int)p); 462 return -1; 463 } 464 465 part->index = p; 466 part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info); 467 468 return 0; 469 } 470 471 /* 472 * Are we using the ibm,rtas-log for oops/panic reports? And if so, 473 * would logging this oops/panic overwrite an RTAS event that rtas_errd 474 * hasn't had a chance to read and process? Return 1 if so, else 0. 475 * 476 * We assume that if rtas_errd hasn't read the RTAS event in 477 * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to. 478 */ 479 static int clobbering_unread_rtas_event(void) 480 { 481 return (oops_log_partition.index == rtas_log_partition.index 482 && last_unread_rtas_event 483 && get_seconds() - last_unread_rtas_event <= 484 NVRAM_RTAS_READ_TIMEOUT); 485 } 486 487 /* Derived from logfs_compress() */ 488 static int nvram_compress(const void *in, void *out, size_t inlen, 489 size_t outlen) 490 { 491 int err, ret; 492 493 ret = -EIO; 494 err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS, 495 MEM_LEVEL, Z_DEFAULT_STRATEGY); 496 if (err != Z_OK) 497 goto error; 498 499 stream.next_in = in; 500 stream.avail_in = inlen; 501 stream.total_in = 0; 502 stream.next_out = out; 503 stream.avail_out = outlen; 504 stream.total_out = 0; 505 506 err = zlib_deflate(&stream, Z_FINISH); 507 if (err != Z_STREAM_END) 508 goto error; 509 510 err = zlib_deflateEnd(&stream); 511 if (err != Z_OK) 512 goto error; 513 514 if (stream.total_out >= stream.total_in) 515 goto error; 516 517 ret = stream.total_out; 518 error: 519 return ret; 520 } 521 522 /* Compress the text from big_oops_buf into oops_buf. */ 523 static int zip_oops(size_t text_len) 524 { 525 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf; 526 int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len, 527 oops_data_sz); 528 if (zipped_len < 0) { 529 pr_err("nvram: compression failed; returned %d\n", zipped_len); 530 pr_err("nvram: logging uncompressed oops/panic report\n"); 531 return -1; 532 } 533 oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION); 534 oops_hdr->report_length = cpu_to_be16(zipped_len); 535 oops_hdr->timestamp = cpu_to_be64(get_seconds()); 536 return 0; 537 } 538 539 #ifdef CONFIG_PSTORE 540 static int nvram_pstore_open(struct pstore_info *psi) 541 { 542 /* Reset the iterator to start reading partitions again */ 543 read_type = -1; 544 return 0; 545 } 546 547 /** 548 * nvram_pstore_write - pstore write callback for nvram 549 * @type: Type of message logged 550 * @reason: reason behind dump (oops/panic) 551 * @id: identifier to indicate the write performed 552 * @part: pstore writes data to registered buffer in parts, 553 * part number will indicate the same. 554 * @count: Indicates oops count 555 * @compressed: Flag to indicate the log is compressed 556 * @size: number of bytes written to the registered buffer 557 * @psi: registered pstore_info structure 558 * 559 * Called by pstore_dump() when an oops or panic report is logged in the 560 * printk buffer. 561 * Returns 0 on successful write. 562 */ 563 static int nvram_pstore_write(enum pstore_type_id type, 564 enum kmsg_dump_reason reason, 565 u64 *id, unsigned int part, int count, 566 bool compressed, size_t size, 567 struct pstore_info *psi) 568 { 569 int rc; 570 unsigned int err_type = ERR_TYPE_KERNEL_PANIC; 571 struct oops_log_info *oops_hdr = (struct oops_log_info *) oops_buf; 572 573 /* part 1 has the recent messages from printk buffer */ 574 if (part > 1 || type != PSTORE_TYPE_DMESG || 575 clobbering_unread_rtas_event()) 576 return -1; 577 578 oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION); 579 oops_hdr->report_length = cpu_to_be16(size); 580 oops_hdr->timestamp = cpu_to_be64(get_seconds()); 581 582 if (compressed) 583 err_type = ERR_TYPE_KERNEL_PANIC_GZ; 584 585 rc = nvram_write_os_partition(&oops_log_partition, oops_buf, 586 (int) (sizeof(*oops_hdr) + size), err_type, count); 587 588 if (rc != 0) 589 return rc; 590 591 *id = part; 592 return 0; 593 } 594 595 /* 596 * Reads the oops/panic report, rtas, of-config and common partition. 597 * Returns the length of the data we read from each partition. 598 * Returns 0 if we've been called before. 599 */ 600 static ssize_t nvram_pstore_read(u64 *id, enum pstore_type_id *type, 601 int *count, struct timespec *time, char **buf, 602 bool *compressed, struct pstore_info *psi) 603 { 604 struct oops_log_info *oops_hdr; 605 unsigned int err_type, id_no, size = 0; 606 struct nvram_os_partition *part = NULL; 607 char *buff = NULL; 608 int sig = 0; 609 loff_t p; 610 611 read_type++; 612 613 switch (nvram_type_ids[read_type]) { 614 case PSTORE_TYPE_DMESG: 615 part = &oops_log_partition; 616 *type = PSTORE_TYPE_DMESG; 617 break; 618 case PSTORE_TYPE_PPC_RTAS: 619 part = &rtas_log_partition; 620 *type = PSTORE_TYPE_PPC_RTAS; 621 time->tv_sec = last_rtas_event; 622 time->tv_nsec = 0; 623 break; 624 case PSTORE_TYPE_PPC_OF: 625 sig = NVRAM_SIG_OF; 626 part = &of_config_partition; 627 *type = PSTORE_TYPE_PPC_OF; 628 *id = PSTORE_TYPE_PPC_OF; 629 time->tv_sec = 0; 630 time->tv_nsec = 0; 631 break; 632 case PSTORE_TYPE_PPC_COMMON: 633 sig = NVRAM_SIG_SYS; 634 part = &common_partition; 635 *type = PSTORE_TYPE_PPC_COMMON; 636 *id = PSTORE_TYPE_PPC_COMMON; 637 time->tv_sec = 0; 638 time->tv_nsec = 0; 639 break; 640 default: 641 return 0; 642 } 643 644 if (!part->os_partition) { 645 p = nvram_find_partition(part->name, sig, &size); 646 if (p <= 0) { 647 pr_err("nvram: Failed to find partition %s, " 648 "err %d\n", part->name, (int)p); 649 return 0; 650 } 651 part->index = p; 652 part->size = size; 653 } 654 655 buff = kmalloc(part->size, GFP_KERNEL); 656 657 if (!buff) 658 return -ENOMEM; 659 660 if (nvram_read_partition(part, buff, part->size, &err_type, &id_no)) { 661 kfree(buff); 662 return 0; 663 } 664 665 *count = 0; 666 667 if (part->os_partition) 668 *id = id_no; 669 670 if (nvram_type_ids[read_type] == PSTORE_TYPE_DMESG) { 671 size_t length, hdr_size; 672 673 oops_hdr = (struct oops_log_info *)buff; 674 if (be16_to_cpu(oops_hdr->version) < OOPS_HDR_VERSION) { 675 /* Old format oops header had 2-byte record size */ 676 hdr_size = sizeof(u16); 677 length = be16_to_cpu(oops_hdr->version); 678 time->tv_sec = 0; 679 time->tv_nsec = 0; 680 } else { 681 hdr_size = sizeof(*oops_hdr); 682 length = be16_to_cpu(oops_hdr->report_length); 683 time->tv_sec = be64_to_cpu(oops_hdr->timestamp); 684 time->tv_nsec = 0; 685 } 686 *buf = kmalloc(length, GFP_KERNEL); 687 if (*buf == NULL) 688 return -ENOMEM; 689 memcpy(*buf, buff + hdr_size, length); 690 kfree(buff); 691 692 if (err_type == ERR_TYPE_KERNEL_PANIC_GZ) 693 *compressed = true; 694 else 695 *compressed = false; 696 return length; 697 } 698 699 *buf = buff; 700 return part->size; 701 } 702 703 static struct pstore_info nvram_pstore_info = { 704 .owner = THIS_MODULE, 705 .name = "nvram", 706 .open = nvram_pstore_open, 707 .read = nvram_pstore_read, 708 .write = nvram_pstore_write, 709 }; 710 711 static int nvram_pstore_init(void) 712 { 713 int rc = 0; 714 715 nvram_pstore_info.buf = oops_data; 716 nvram_pstore_info.bufsize = oops_data_sz; 717 718 rc = pstore_register(&nvram_pstore_info); 719 if (rc != 0) 720 pr_err("nvram: pstore_register() failed, defaults to " 721 "kmsg_dump; returned %d\n", rc); 722 723 return rc; 724 } 725 #else 726 static int nvram_pstore_init(void) 727 { 728 return -1; 729 } 730 #endif 731 732 static void __init nvram_init_oops_partition(int rtas_partition_exists) 733 { 734 int rc; 735 736 rc = pseries_nvram_init_os_partition(&oops_log_partition); 737 if (rc != 0) { 738 if (!rtas_partition_exists) 739 return; 740 pr_notice("nvram: Using %s partition to log both" 741 " RTAS errors and oops/panic reports\n", 742 rtas_log_partition.name); 743 memcpy(&oops_log_partition, &rtas_log_partition, 744 sizeof(rtas_log_partition)); 745 } 746 oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL); 747 if (!oops_buf) { 748 pr_err("nvram: No memory for %s partition\n", 749 oops_log_partition.name); 750 return; 751 } 752 oops_data = oops_buf + sizeof(struct oops_log_info); 753 oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info); 754 755 rc = nvram_pstore_init(); 756 757 if (!rc) 758 return; 759 760 /* 761 * Figure compression (preceded by elimination of each line's <n> 762 * severity prefix) will reduce the oops/panic report to at most 763 * 45% of its original size. 764 */ 765 big_oops_buf_sz = (oops_data_sz * 100) / 45; 766 big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); 767 if (big_oops_buf) { 768 stream.workspace = kmalloc(zlib_deflate_workspacesize( 769 WINDOW_BITS, MEM_LEVEL), GFP_KERNEL); 770 if (!stream.workspace) { 771 pr_err("nvram: No memory for compression workspace; " 772 "skipping compression of %s partition data\n", 773 oops_log_partition.name); 774 kfree(big_oops_buf); 775 big_oops_buf = NULL; 776 } 777 } else { 778 pr_err("No memory for uncompressed %s data; " 779 "skipping compression\n", oops_log_partition.name); 780 stream.workspace = NULL; 781 } 782 783 rc = kmsg_dump_register(&nvram_kmsg_dumper); 784 if (rc != 0) { 785 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc); 786 kfree(oops_buf); 787 kfree(big_oops_buf); 788 kfree(stream.workspace); 789 } 790 } 791 792 static int __init pseries_nvram_init_log_partitions(void) 793 { 794 int rc; 795 796 /* Scan nvram for partitions */ 797 nvram_scan_partitions(); 798 799 rc = pseries_nvram_init_os_partition(&rtas_log_partition); 800 nvram_init_oops_partition(rc == 0); 801 return 0; 802 } 803 machine_arch_initcall(pseries, pseries_nvram_init_log_partitions); 804 805 int __init pSeries_nvram_init(void) 806 { 807 struct device_node *nvram; 808 const __be32 *nbytes_p; 809 unsigned int proplen; 810 811 nvram = of_find_node_by_type(NULL, "nvram"); 812 if (nvram == NULL) 813 return -ENODEV; 814 815 nbytes_p = of_get_property(nvram, "#bytes", &proplen); 816 if (nbytes_p == NULL || proplen != sizeof(unsigned int)) { 817 of_node_put(nvram); 818 return -EIO; 819 } 820 821 nvram_size = be32_to_cpup(nbytes_p); 822 823 nvram_fetch = rtas_token("nvram-fetch"); 824 nvram_store = rtas_token("nvram-store"); 825 printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size); 826 of_node_put(nvram); 827 828 ppc_md.nvram_read = pSeries_nvram_read; 829 ppc_md.nvram_write = pSeries_nvram_write; 830 ppc_md.nvram_size = pSeries_nvram_get_size; 831 832 return 0; 833 } 834 835 836 /* 837 * This is our kmsg_dump callback, called after an oops or panic report 838 * has been written to the printk buffer. We want to capture as much 839 * of the printk buffer as possible. First, capture as much as we can 840 * that we think will compress sufficiently to fit in the lnx,oops-log 841 * partition. If that's too much, go back and capture uncompressed text. 842 */ 843 static void oops_to_nvram(struct kmsg_dumper *dumper, 844 enum kmsg_dump_reason reason) 845 { 846 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf; 847 static unsigned int oops_count = 0; 848 static bool panicking = false; 849 static DEFINE_SPINLOCK(lock); 850 unsigned long flags; 851 size_t text_len; 852 unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ; 853 int rc = -1; 854 855 switch (reason) { 856 case KMSG_DUMP_RESTART: 857 case KMSG_DUMP_HALT: 858 case KMSG_DUMP_POWEROFF: 859 /* These are almost always orderly shutdowns. */ 860 return; 861 case KMSG_DUMP_OOPS: 862 break; 863 case KMSG_DUMP_PANIC: 864 panicking = true; 865 break; 866 case KMSG_DUMP_EMERG: 867 if (panicking) 868 /* Panic report already captured. */ 869 return; 870 break; 871 default: 872 pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n", 873 __func__, (int) reason); 874 return; 875 } 876 877 if (clobbering_unread_rtas_event()) 878 return; 879 880 if (!spin_trylock_irqsave(&lock, flags)) 881 return; 882 883 if (big_oops_buf) { 884 kmsg_dump_get_buffer(dumper, false, 885 big_oops_buf, big_oops_buf_sz, &text_len); 886 rc = zip_oops(text_len); 887 } 888 if (rc != 0) { 889 kmsg_dump_rewind(dumper); 890 kmsg_dump_get_buffer(dumper, false, 891 oops_data, oops_data_sz, &text_len); 892 err_type = ERR_TYPE_KERNEL_PANIC; 893 oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION); 894 oops_hdr->report_length = cpu_to_be16(text_len); 895 oops_hdr->timestamp = cpu_to_be64(get_seconds()); 896 } 897 898 (void) nvram_write_os_partition(&oops_log_partition, oops_buf, 899 (int) (sizeof(*oops_hdr) + text_len), err_type, 900 ++oops_count); 901 902 spin_unlock_irqrestore(&lock, flags); 903 } 904