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