1
2
3
4
5
6
7
8
9
10
11
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
31#define NVRW_CNT 0x20
32
33
34
35
36
37
38#define OOPS_HDR_VERSION 5000
39
40static unsigned int nvram_size;
41static int nvram_fetch, nvram_store;
42static char nvram_buf[NVRW_CNT];
43static DEFINE_SPINLOCK(nvram_lock);
44
45struct err_log_info {
46 __be32 error_type;
47 __be32 seq_num;
48};
49
50struct nvram_os_partition {
51 const char *name;
52 int req_size;
53 int min_size;
54 long size;
55 long index;
56 bool os_partition;
57};
58
59static 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
67static 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
75static const char *pseries_nvram_os_partitions[] = {
76 "ibm,rtas-log",
77 "lnx,oops-log",
78 NULL
79};
80
81struct oops_log_info {
82 __be16 version;
83 __be16 report_length;
84 __be64 timestamp;
85} __attribute__((packed));
86
87static void oops_to_nvram(struct kmsg_dumper *dumper,
88 enum kmsg_dump_reason reason);
89
90static struct kmsg_dumper nvram_kmsg_dumper = {
91 .dump = oops_to_nvram
92};
93
94
95#define NVRAM_RTAS_READ_TIMEOUT 5
96static unsigned long last_unread_rtas_event;
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123static size_t big_oops_buf_sz;
124static char *big_oops_buf, *oops_buf;
125static char *oops_data;
126static size_t oops_data_sz;
127
128
129#define COMPR_LEVEL 6
130#define WINDOW_BITS 12
131#define MEM_LEVEL 4
132static struct z_stream_s stream;
133
134#ifdef CONFIG_PSTORE
135static struct nvram_os_partition of_config_partition = {
136 .name = "of-config",
137 .index = -1,
138 .os_partition = false
139};
140
141static struct nvram_os_partition common_partition = {
142 .name = "common",
143 .index = -1,
144 .os_partition = false
145};
146
147static 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};
154static int read_type;
155static unsigned long last_rtas_event;
156#endif
157
158static 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
202static 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
244static ssize_t pSeries_nvram_get_size(void)
245{
246 return nvram_size ? nvram_size : -ENODEV;
247}
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279int nvram_write_os_partition(struct nvram_os_partition *part, char * buff,
280 int length, unsigned int err_type, unsigned int error_log_cnt)
281{
282 int rc;
283 loff_t tmp_index;
284 struct err_log_info info;
285
286 if (part->index == -1) {
287 return -ESPIPE;
288 }
289
290 if (length > part->size) {
291 length = part->size;
292 }
293
294 info.error_type = cpu_to_be32(err_type);
295 info.seq_num = cpu_to_be32(error_log_cnt);
296
297 tmp_index = part->index;
298
299 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
300 if (rc <= 0) {
301 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
302 return rc;
303 }
304
305 rc = ppc_md.nvram_write(buff, length, &tmp_index);
306 if (rc <= 0) {
307 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
308 return rc;
309 }
310
311 return 0;
312}
313
314int nvram_write_error_log(char * buff, int length,
315 unsigned int err_type, unsigned int error_log_cnt)
316{
317 int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,
318 err_type, error_log_cnt);
319 if (!rc) {
320 last_unread_rtas_event = get_seconds();
321#ifdef CONFIG_PSTORE
322 last_rtas_event = get_seconds();
323#endif
324 }
325
326 return rc;
327}
328
329
330
331
332
333int nvram_read_partition(struct nvram_os_partition *part, char *buff,
334 int length, unsigned int *err_type,
335 unsigned int *error_log_cnt)
336{
337 int rc;
338 loff_t tmp_index;
339 struct err_log_info info;
340
341 if (part->index == -1)
342 return -1;
343
344 if (length > part->size)
345 length = part->size;
346
347 tmp_index = part->index;
348
349 if (part->os_partition) {
350 rc = ppc_md.nvram_read((char *)&info,
351 sizeof(struct err_log_info),
352 &tmp_index);
353 if (rc <= 0) {
354 pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__,
355 rc);
356 return rc;
357 }
358 }
359
360 rc = ppc_md.nvram_read(buff, length, &tmp_index);
361 if (rc <= 0) {
362 pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__, rc);
363 return rc;
364 }
365
366 if (part->os_partition) {
367 *error_log_cnt = be32_to_cpu(info.seq_num);
368 *err_type = be32_to_cpu(info.error_type);
369 }
370
371 return 0;
372}
373
374
375
376
377
378int nvram_read_error_log(char *buff, int length,
379 unsigned int *err_type, unsigned int *error_log_cnt)
380{
381 return nvram_read_partition(&rtas_log_partition, buff, length,
382 err_type, error_log_cnt);
383}
384
385
386
387
388int nvram_clear_error_log(void)
389{
390 loff_t tmp_index;
391 int clear_word = ERR_FLAG_ALREADY_LOGGED;
392 int rc;
393
394 if (rtas_log_partition.index == -1)
395 return -1;
396
397 tmp_index = rtas_log_partition.index;
398
399 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
400 if (rc <= 0) {
401 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
402 return rc;
403 }
404 last_unread_rtas_event = 0;
405
406 return 0;
407}
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426static int __init pseries_nvram_init_os_partition(struct nvram_os_partition
427 *part)
428{
429 loff_t p;
430 int size;
431
432
433 p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size);
434
435
436 if (p && size < part->min_size) {
437 pr_info("nvram: Found too small %s partition,"
438 " removing it...\n", part->name);
439 nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL);
440 p = 0;
441 }
442
443
444 if (!p) {
445 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
446 part->req_size, part->min_size);
447 if (p == -ENOSPC) {
448 pr_info("nvram: No room to create %s partition, "
449 "deleting any obsolete OS partitions...\n",
450 part->name);
451 nvram_remove_partition(NULL, NVRAM_SIG_OS,
452 pseries_nvram_os_partitions);
453 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
454 part->req_size, part->min_size);
455 }
456 }
457
458 if (p <= 0) {
459 pr_err("nvram: Failed to find or create %s"
460 " partition, err %d\n", part->name, (int)p);
461 return -1;
462 }
463
464 part->index = p;
465 part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info);
466
467 return 0;
468}
469
470
471
472
473
474
475
476
477
478static int clobbering_unread_rtas_event(void)
479{
480 return (oops_log_partition.index == rtas_log_partition.index
481 && last_unread_rtas_event
482 && get_seconds() - last_unread_rtas_event <=
483 NVRAM_RTAS_READ_TIMEOUT);
484}
485
486
487static int nvram_compress(const void *in, void *out, size_t inlen,
488 size_t outlen)
489{
490 int err, ret;
491
492 ret = -EIO;
493 err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
494 MEM_LEVEL, Z_DEFAULT_STRATEGY);
495 if (err != Z_OK)
496 goto error;
497
498 stream.next_in = in;
499 stream.avail_in = inlen;
500 stream.total_in = 0;
501 stream.next_out = out;
502 stream.avail_out = outlen;
503 stream.total_out = 0;
504
505 err = zlib_deflate(&stream, Z_FINISH);
506 if (err != Z_STREAM_END)
507 goto error;
508
509 err = zlib_deflateEnd(&stream);
510 if (err != Z_OK)
511 goto error;
512
513 if (stream.total_out >= stream.total_in)
514 goto error;
515
516 ret = stream.total_out;
517error:
518 return ret;
519}
520
521
522static int zip_oops(size_t text_len)
523{
524 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
525 int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len,
526 oops_data_sz);
527 if (zipped_len < 0) {
528 pr_err("nvram: compression failed; returned %d\n", zipped_len);
529 pr_err("nvram: logging uncompressed oops/panic report\n");
530 return -1;
531 }
532 oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
533 oops_hdr->report_length = cpu_to_be16(zipped_len);
534 oops_hdr->timestamp = cpu_to_be64(get_seconds());
535 return 0;
536}
537
538#ifdef CONFIG_PSTORE
539static int nvram_pstore_open(struct pstore_info *psi)
540{
541
542 read_type = -1;
543 return 0;
544}
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562static int nvram_pstore_write(enum pstore_type_id type,
563 enum kmsg_dump_reason reason,
564 u64 *id, unsigned int part, int count,
565 bool compressed, size_t size,
566 struct pstore_info *psi)
567{
568 int rc;
569 unsigned int err_type = ERR_TYPE_KERNEL_PANIC;
570 struct oops_log_info *oops_hdr = (struct oops_log_info *) oops_buf;
571
572
573 if (part > 1 || type != PSTORE_TYPE_DMESG ||
574 clobbering_unread_rtas_event())
575 return -1;
576
577 oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
578 oops_hdr->report_length = cpu_to_be16(size);
579 oops_hdr->timestamp = cpu_to_be64(get_seconds());
580
581 if (compressed)
582 err_type = ERR_TYPE_KERNEL_PANIC_GZ;
583
584 rc = nvram_write_os_partition(&oops_log_partition, oops_buf,
585 (int) (sizeof(*oops_hdr) + size), err_type, count);
586
587 if (rc != 0)
588 return rc;
589
590 *id = part;
591 return 0;
592}
593
594
595
596
597
598
599static ssize_t nvram_pstore_read(u64 *id, enum pstore_type_id *type,
600 int *count, struct timespec *time, char **buf,
601 bool *compressed, struct pstore_info *psi)
602{
603 struct oops_log_info *oops_hdr;
604 unsigned int err_type, id_no, size = 0;
605 struct nvram_os_partition *part = NULL;
606 char *buff = NULL;
607 int sig = 0;
608 loff_t p;
609
610 read_type++;
611
612 switch (nvram_type_ids[read_type]) {
613 case PSTORE_TYPE_DMESG:
614 part = &oops_log_partition;
615 *type = PSTORE_TYPE_DMESG;
616 break;
617 case PSTORE_TYPE_PPC_RTAS:
618 part = &rtas_log_partition;
619 *type = PSTORE_TYPE_PPC_RTAS;
620 time->tv_sec = last_rtas_event;
621 time->tv_nsec = 0;
622 break;
623 case PSTORE_TYPE_PPC_OF:
624 sig = NVRAM_SIG_OF;
625 part = &of_config_partition;
626 *type = PSTORE_TYPE_PPC_OF;
627 *id = PSTORE_TYPE_PPC_OF;
628 time->tv_sec = 0;
629 time->tv_nsec = 0;
630 break;
631 case PSTORE_TYPE_PPC_COMMON:
632 sig = NVRAM_SIG_SYS;
633 part = &common_partition;
634 *type = PSTORE_TYPE_PPC_COMMON;
635 *id = PSTORE_TYPE_PPC_COMMON;
636 time->tv_sec = 0;
637 time->tv_nsec = 0;
638 break;
639 default:
640 return 0;
641 }
642
643 if (!part->os_partition) {
644 p = nvram_find_partition(part->name, sig, &size);
645 if (p <= 0) {
646 pr_err("nvram: Failed to find partition %s, "
647 "err %d\n", part->name, (int)p);
648 return 0;
649 }
650 part->index = p;
651 part->size = size;
652 }
653
654 buff = kmalloc(part->size, GFP_KERNEL);
655
656 if (!buff)
657 return -ENOMEM;
658
659 if (nvram_read_partition(part, buff, part->size, &err_type, &id_no)) {
660 kfree(buff);
661 return 0;
662 }
663
664 *count = 0;
665
666 if (part->os_partition)
667 *id = id_no;
668
669 if (nvram_type_ids[read_type] == PSTORE_TYPE_DMESG) {
670 size_t length, hdr_size;
671
672 oops_hdr = (struct oops_log_info *)buff;
673 if (be16_to_cpu(oops_hdr->version) < OOPS_HDR_VERSION) {
674
675 hdr_size = sizeof(u16);
676 length = be16_to_cpu(oops_hdr->version);
677 time->tv_sec = 0;
678 time->tv_nsec = 0;
679 } else {
680 hdr_size = sizeof(*oops_hdr);
681 length = be16_to_cpu(oops_hdr->report_length);
682 time->tv_sec = be64_to_cpu(oops_hdr->timestamp);
683 time->tv_nsec = 0;
684 }
685 *buf = kmalloc(length, GFP_KERNEL);
686 if (*buf == NULL)
687 return -ENOMEM;
688 memcpy(*buf, buff + hdr_size, length);
689 kfree(buff);
690
691 if (err_type == ERR_TYPE_KERNEL_PANIC_GZ)
692 *compressed = true;
693 else
694 *compressed = false;
695 return length;
696 }
697
698 *buf = buff;
699 return part->size;
700}
701
702static struct pstore_info nvram_pstore_info = {
703 .owner = THIS_MODULE,
704 .name = "nvram",
705 .open = nvram_pstore_open,
706 .read = nvram_pstore_read,
707 .write = nvram_pstore_write,
708};
709
710static int nvram_pstore_init(void)
711{
712 int rc = 0;
713
714 nvram_pstore_info.buf = oops_data;
715 nvram_pstore_info.bufsize = oops_data_sz;
716
717 rc = pstore_register(&nvram_pstore_info);
718 if (rc != 0)
719 pr_err("nvram: pstore_register() failed, defaults to "
720 "kmsg_dump; returned %d\n", rc);
721
722 return rc;
723}
724#else
725static int nvram_pstore_init(void)
726{
727 return -1;
728}
729#endif
730
731static void __init nvram_init_oops_partition(int rtas_partition_exists)
732{
733 int rc;
734
735 rc = pseries_nvram_init_os_partition(&oops_log_partition);
736 if (rc != 0) {
737 if (!rtas_partition_exists)
738 return;
739 pr_notice("nvram: Using %s partition to log both"
740 " RTAS errors and oops/panic reports\n",
741 rtas_log_partition.name);
742 memcpy(&oops_log_partition, &rtas_log_partition,
743 sizeof(rtas_log_partition));
744 }
745 oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
746 if (!oops_buf) {
747 pr_err("nvram: No memory for %s partition\n",
748 oops_log_partition.name);
749 return;
750 }
751 oops_data = oops_buf + sizeof(struct oops_log_info);
752 oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info);
753
754 rc = nvram_pstore_init();
755
756 if (!rc)
757 return;
758
759
760
761
762
763
764 big_oops_buf_sz = (oops_data_sz * 100) / 45;
765 big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
766 if (big_oops_buf) {
767 stream.workspace = kmalloc(zlib_deflate_workspacesize(
768 WINDOW_BITS, MEM_LEVEL), GFP_KERNEL);
769 if (!stream.workspace) {
770 pr_err("nvram: No memory for compression workspace; "
771 "skipping compression of %s partition data\n",
772 oops_log_partition.name);
773 kfree(big_oops_buf);
774 big_oops_buf = NULL;
775 }
776 } else {
777 pr_err("No memory for uncompressed %s data; "
778 "skipping compression\n", oops_log_partition.name);
779 stream.workspace = NULL;
780 }
781
782 rc = kmsg_dump_register(&nvram_kmsg_dumper);
783 if (rc != 0) {
784 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
785 kfree(oops_buf);
786 kfree(big_oops_buf);
787 kfree(stream.workspace);
788 }
789}
790
791static int __init pseries_nvram_init_log_partitions(void)
792{
793 int rc;
794
795
796 nvram_scan_partitions();
797
798 rc = pseries_nvram_init_os_partition(&rtas_log_partition);
799 nvram_init_oops_partition(rc == 0);
800 return 0;
801}
802machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
803
804int __init pSeries_nvram_init(void)
805{
806 struct device_node *nvram;
807 const __be32 *nbytes_p;
808 unsigned int proplen;
809
810 nvram = of_find_node_by_type(NULL, "nvram");
811 if (nvram == NULL)
812 return -ENODEV;
813
814 nbytes_p = of_get_property(nvram, "#bytes", &proplen);
815 if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
816 of_node_put(nvram);
817 return -EIO;
818 }
819
820 nvram_size = be32_to_cpup(nbytes_p);
821
822 nvram_fetch = rtas_token("nvram-fetch");
823 nvram_store = rtas_token("nvram-store");
824 printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
825 of_node_put(nvram);
826
827 ppc_md.nvram_read = pSeries_nvram_read;
828 ppc_md.nvram_write = pSeries_nvram_write;
829 ppc_md.nvram_size = pSeries_nvram_get_size;
830
831 return 0;
832}
833
834
835
836
837
838
839
840
841
842static void oops_to_nvram(struct kmsg_dumper *dumper,
843 enum kmsg_dump_reason reason)
844{
845 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
846 static unsigned int oops_count = 0;
847 static bool panicking = false;
848 static DEFINE_SPINLOCK(lock);
849 unsigned long flags;
850 size_t text_len;
851 unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ;
852 int rc = -1;
853
854 switch (reason) {
855 case KMSG_DUMP_RESTART:
856 case KMSG_DUMP_HALT:
857 case KMSG_DUMP_POWEROFF:
858
859 return;
860 case KMSG_DUMP_OOPS:
861 break;
862 case KMSG_DUMP_PANIC:
863 panicking = true;
864 break;
865 case KMSG_DUMP_EMERG:
866 if (panicking)
867
868 return;
869 break;
870 default:
871 pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
872 __FUNCTION__, (int) reason);
873 return;
874 }
875
876 if (clobbering_unread_rtas_event())
877 return;
878
879 if (!spin_trylock_irqsave(&lock, flags))
880 return;
881
882 if (big_oops_buf) {
883 kmsg_dump_get_buffer(dumper, false,
884 big_oops_buf, big_oops_buf_sz, &text_len);
885 rc = zip_oops(text_len);
886 }
887 if (rc != 0) {
888 kmsg_dump_rewind(dumper);
889 kmsg_dump_get_buffer(dumper, false,
890 oops_data, oops_data_sz, &text_len);
891 err_type = ERR_TYPE_KERNEL_PANIC;
892 oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
893 oops_hdr->report_length = cpu_to_be16(text_len);
894 oops_hdr->timestamp = cpu_to_be64(get_seconds());
895 }
896
897 (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
898 (int) (sizeof(*oops_hdr) + text_len), err_type,
899 ++oops_count);
900
901 spin_unlock_irqrestore(&lock, flags);
902}
903