blob: 742735acd8b6409fb2bd68fcc9ba706a2f97944e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/spinlock.h>
Jim Kenistona5cf4b02011-02-09 13:00:09 +000019#include <linux/slab.h>
20#include <linux/kmsg_dump.h>
Jim Keniston6c493682011-07-25 07:54:50 +000021#include <linux/ctype.h>
22#include <linux/zlib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24#include <asm/nvram.h>
25#include <asm/rtas.h>
26#include <asm/prom.h>
27#include <asm/machdep.h>
28
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +100029/* Max bytes to read/write in one go */
30#define NVRW_CNT 0x20
31
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +053032/*
33 * Set oops header version to distingush between old and new format header.
34 * lnx,oops-log partition max size is 4000, header version > 4000 will
35 * help in identifying new header.
36 */
37#define OOPS_HDR_VERSION 5000
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static unsigned int nvram_size;
40static int nvram_fetch, nvram_store;
41static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */
42static DEFINE_SPINLOCK(nvram_lock);
43
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +100044struct err_log_info {
45 int error_type;
46 unsigned int seq_num;
47};
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +100048
Jim Keniston0f4ac132011-02-09 12:43:13 +000049struct nvram_os_partition {
50 const char *name;
51 int req_size; /* desired size, in bytes */
52 int min_size; /* minimum acceptable size (0 means req_size) */
Jim Kenistona5cf4b02011-02-09 13:00:09 +000053 long size; /* size of data portion (excluding err_log_info) */
Jim Keniston0f4ac132011-02-09 12:43:13 +000054 long index; /* offset of data portion of partition */
55};
56
57static struct nvram_os_partition rtas_log_partition = {
58 .name = "ibm,rtas-log",
59 .req_size = 2079,
60 .min_size = 1055,
61 .index = -1
62};
63
Jim Kenistona5cf4b02011-02-09 13:00:09 +000064static struct nvram_os_partition oops_log_partition = {
65 .name = "lnx,oops-log",
66 .req_size = 4000,
67 .min_size = 2000,
68 .index = -1
69};
70
Jim Keniston0f4ac132011-02-09 12:43:13 +000071static const char *pseries_nvram_os_partitions[] = {
72 "ibm,rtas-log",
Jim Kenistona5cf4b02011-02-09 13:00:09 +000073 "lnx,oops-log",
Jim Keniston0f4ac132011-02-09 12:43:13 +000074 NULL
75};
Benjamin Herrenschmidt9a866b82010-08-02 10:51:25 +100076
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +053077struct oops_log_info {
78 u16 version;
79 u16 report_length;
80 u64 timestamp;
81} __attribute__((packed));
82
Jim Kenistona5cf4b02011-02-09 13:00:09 +000083static void oops_to_nvram(struct kmsg_dumper *dumper,
Kay Sieverse2ae7152012-06-15 14:07:51 +020084 enum kmsg_dump_reason reason);
Jim Kenistona5cf4b02011-02-09 13:00:09 +000085
86static struct kmsg_dumper nvram_kmsg_dumper = {
87 .dump = oops_to_nvram
88};
89
90/* See clobbering_unread_rtas_event() */
91#define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */
92static unsigned long last_unread_rtas_event; /* timestamp */
93
Jim Keniston6c493682011-07-25 07:54:50 +000094/*
95 * For capturing and compressing an oops or panic report...
96
97 * big_oops_buf[] holds the uncompressed text we're capturing.
98 *
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +053099 * oops_buf[] holds the compressed text, preceded by a oops header.
100 * oops header has u16 holding the version of oops header (to differentiate
101 * between old and new format header) followed by u16 holding the length of
102 * the compressed* text (*Or uncompressed, if compression fails.) and u64
103 * holding the timestamp. oops_buf[] gets written to NVRAM.
Jim Keniston6c493682011-07-25 07:54:50 +0000104 *
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530105 * oops_log_info points to the header. oops_data points to the compressed text.
Jim Keniston6c493682011-07-25 07:54:50 +0000106 *
107 * +- oops_buf
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530108 * | +- oops_data
109 * v v
110 * +-----------+-----------+-----------+------------------------+
111 * | version | length | timestamp | text |
112 * | (2 bytes) | (2 bytes) | (8 bytes) | (oops_data_sz bytes) |
113 * +-----------+-----------+-----------+------------------------+
Jim Keniston6c493682011-07-25 07:54:50 +0000114 * ^
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530115 * +- oops_log_info
Jim Keniston6c493682011-07-25 07:54:50 +0000116 *
117 * We preallocate these buffers during init to avoid kmalloc during oops/panic.
118 */
119static size_t big_oops_buf_sz;
120static char *big_oops_buf, *oops_buf;
Jim Keniston6c493682011-07-25 07:54:50 +0000121static char *oops_data;
122static size_t oops_data_sz;
123
124/* Compression parameters */
125#define COMPR_LEVEL 6
126#define WINDOW_BITS 12
127#define MEM_LEVEL 4
128static struct z_stream_s stream;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
131{
132 unsigned int i;
133 unsigned long len;
134 int done;
135 unsigned long flags;
136 char *p = buf;
137
138
139 if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
140 return -ENODEV;
141
142 if (*index >= nvram_size)
143 return 0;
144
145 i = *index;
146 if (i + count > nvram_size)
147 count = nvram_size - i;
148
149 spin_lock_irqsave(&nvram_lock, flags);
150
151 for (; count != 0; count -= len) {
152 len = count;
153 if (len > NVRW_CNT)
154 len = NVRW_CNT;
155
156 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
157 len) != 0) || len != done) {
158 spin_unlock_irqrestore(&nvram_lock, flags);
159 return -EIO;
160 }
161
162 memcpy(p, nvram_buf, len);
163
164 p += len;
165 i += len;
166 }
167
168 spin_unlock_irqrestore(&nvram_lock, flags);
169
170 *index = i;
171 return p - buf;
172}
173
174static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
175{
176 unsigned int i;
177 unsigned long len;
178 int done;
179 unsigned long flags;
180 const char *p = buf;
181
182 if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
183 return -ENODEV;
184
185 if (*index >= nvram_size)
186 return 0;
187
188 i = *index;
189 if (i + count > nvram_size)
190 count = nvram_size - i;
191
192 spin_lock_irqsave(&nvram_lock, flags);
193
194 for (; count != 0; count -= len) {
195 len = count;
196 if (len > NVRW_CNT)
197 len = NVRW_CNT;
198
199 memcpy(nvram_buf, p, len);
200
201 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
202 len) != 0) || len != done) {
203 spin_unlock_irqrestore(&nvram_lock, flags);
204 return -EIO;
205 }
206
207 p += len;
208 i += len;
209 }
210 spin_unlock_irqrestore(&nvram_lock, flags);
211
212 *index = i;
213 return p - buf;
214}
215
216static ssize_t pSeries_nvram_get_size(void)
217{
218 return nvram_size ? nvram_size : -ENODEV;
219}
220
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000221
Jim Keniston0f4ac132011-02-09 12:43:13 +0000222/* nvram_write_os_partition, nvram_write_error_log
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000223 *
224 * We need to buffer the error logs into nvram to ensure that we have
225 * the failure information to decode. If we have a severe error there
226 * is no way to guarantee that the OS or the machine is in a state to
227 * get back to user land and write the error to disk. For example if
228 * the SCSI device driver causes a Machine Check by writing to a bad
229 * IO address, there is no way of guaranteeing that the device driver
230 * is in any state that is would also be able to write the error data
231 * captured to disk, thus we buffer it in NVRAM for analysis on the
232 * next boot.
233 *
234 * In NVRAM the partition containing the error log buffer will looks like:
235 * Header (in bytes):
236 * +-----------+----------+--------+------------+------------------+
237 * | signature | checksum | length | name | data |
238 * |0 |1 |2 3|4 15|16 length-1|
239 * +-----------+----------+--------+------------+------------------+
240 *
241 * The 'data' section would look like (in bytes):
242 * +--------------+------------+-----------------------------------+
243 * | event_logged | sequence # | error log |
Jim Keniston0f4ac132011-02-09 12:43:13 +0000244 * |0 3|4 7|8 error_log_size-1|
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000245 * +--------------+------------+-----------------------------------+
246 *
247 * event_logged: 0 if event has not been logged to syslog, 1 if it has
248 * sequence #: The unique sequence # for each event. (until it wraps)
249 * error log: The error log from event_scan
250 */
Jim Keniston0f4ac132011-02-09 12:43:13 +0000251int nvram_write_os_partition(struct nvram_os_partition *part, char * buff,
252 int length, unsigned int err_type, unsigned int error_log_cnt)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000253{
254 int rc;
255 loff_t tmp_index;
256 struct err_log_info info;
257
Jim Keniston0f4ac132011-02-09 12:43:13 +0000258 if (part->index == -1) {
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000259 return -ESPIPE;
260 }
261
Jim Keniston0f4ac132011-02-09 12:43:13 +0000262 if (length > part->size) {
263 length = part->size;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000264 }
265
266 info.error_type = err_type;
267 info.seq_num = error_log_cnt;
268
Jim Keniston0f4ac132011-02-09 12:43:13 +0000269 tmp_index = part->index;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000270
271 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
272 if (rc <= 0) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000273 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000274 return rc;
275 }
276
277 rc = ppc_md.nvram_write(buff, length, &tmp_index);
278 if (rc <= 0) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000279 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000280 return rc;
281 }
282
283 return 0;
284}
285
Jim Keniston0f4ac132011-02-09 12:43:13 +0000286int nvram_write_error_log(char * buff, int length,
287 unsigned int err_type, unsigned int error_log_cnt)
288{
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000289 int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,
Jim Keniston0f4ac132011-02-09 12:43:13 +0000290 err_type, error_log_cnt);
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000291 if (!rc)
292 last_unread_rtas_event = get_seconds();
293 return rc;
Jim Keniston0f4ac132011-02-09 12:43:13 +0000294}
295
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000296/* nvram_read_error_log
297 *
298 * Reads nvram for error log for at most 'length'
299 */
300int nvram_read_error_log(char * buff, int length,
301 unsigned int * err_type, unsigned int * error_log_cnt)
302{
303 int rc;
304 loff_t tmp_index;
305 struct err_log_info info;
306
Jim Keniston0f4ac132011-02-09 12:43:13 +0000307 if (rtas_log_partition.index == -1)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000308 return -1;
309
Jim Keniston0f4ac132011-02-09 12:43:13 +0000310 if (length > rtas_log_partition.size)
311 length = rtas_log_partition.size;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000312
Jim Keniston0f4ac132011-02-09 12:43:13 +0000313 tmp_index = rtas_log_partition.index;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000314
315 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
316 if (rc <= 0) {
317 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
318 return rc;
319 }
320
321 rc = ppc_md.nvram_read(buff, length, &tmp_index);
322 if (rc <= 0) {
323 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
324 return rc;
325 }
326
327 *error_log_cnt = info.seq_num;
328 *err_type = info.error_type;
329
330 return 0;
331}
332
333/* This doesn't actually zero anything, but it sets the event_logged
334 * word to tell that this event is safely in syslog.
335 */
336int nvram_clear_error_log(void)
337{
338 loff_t tmp_index;
339 int clear_word = ERR_FLAG_ALREADY_LOGGED;
340 int rc;
341
Jim Keniston0f4ac132011-02-09 12:43:13 +0000342 if (rtas_log_partition.index == -1)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000343 return -1;
344
Jim Keniston0f4ac132011-02-09 12:43:13 +0000345 tmp_index = rtas_log_partition.index;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000346
347 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
348 if (rc <= 0) {
349 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
350 return rc;
351 }
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000352 last_unread_rtas_event = 0;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000353
354 return 0;
355}
356
Jim Keniston0f4ac132011-02-09 12:43:13 +0000357/* pseries_nvram_init_os_partition
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000358 *
Jim Keniston0f4ac132011-02-09 12:43:13 +0000359 * This sets up a partition with an "OS" signature.
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000360 *
361 * The general strategy is the following:
Jim Keniston0f4ac132011-02-09 12:43:13 +0000362 * 1.) If a partition with the indicated name already exists...
363 * - If it's large enough, use it.
364 * - Otherwise, recycle it and keep going.
365 * 2.) Search for a free partition that is large enough.
366 * 3.) If there's not a free partition large enough, recycle any obsolete
367 * OS partitions and try again.
368 * 4.) Will first try getting a chunk that will satisfy the requested size.
369 * 5.) If a chunk of the requested size cannot be allocated, then try finding
370 * a chunk that will satisfy the minum needed.
371 *
372 * Returns 0 on success, else -1.
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000373 */
Jim Keniston0f4ac132011-02-09 12:43:13 +0000374static int __init pseries_nvram_init_os_partition(struct nvram_os_partition
375 *part)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000376{
377 loff_t p;
378 int size;
379
380 /* Scan nvram for partitions */
381 nvram_scan_partitions();
382
Jim Keniston0f4ac132011-02-09 12:43:13 +0000383 /* Look for ours */
384 p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000385
386 /* Found one but too small, remove it */
Jim Keniston0f4ac132011-02-09 12:43:13 +0000387 if (p && size < part->min_size) {
388 pr_info("nvram: Found too small %s partition,"
389 " removing it...\n", part->name);
390 nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000391 p = 0;
392 }
393
394 /* Create one if we didn't find */
395 if (!p) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000396 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
397 part->req_size, part->min_size);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000398 if (p == -ENOSPC) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000399 pr_info("nvram: No room to create %s partition, "
400 "deleting any obsolete OS partitions...\n",
401 part->name);
402 nvram_remove_partition(NULL, NVRAM_SIG_OS,
403 pseries_nvram_os_partitions);
404 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
405 part->req_size, part->min_size);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000406 }
407 }
408
409 if (p <= 0) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000410 pr_err("nvram: Failed to find or create %s"
411 " partition, err %d\n", part->name, (int)p);
412 return -1;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000413 }
414
Jim Keniston0f4ac132011-02-09 12:43:13 +0000415 part->index = p;
416 part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000417
418 return 0;
419}
Jim Keniston0f4ac132011-02-09 12:43:13 +0000420
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000421static void __init nvram_init_oops_partition(int rtas_partition_exists)
422{
423 int rc;
424
425 rc = pseries_nvram_init_os_partition(&oops_log_partition);
426 if (rc != 0) {
427 if (!rtas_partition_exists)
428 return;
429 pr_notice("nvram: Using %s partition to log both"
430 " RTAS errors and oops/panic reports\n",
431 rtas_log_partition.name);
432 memcpy(&oops_log_partition, &rtas_log_partition,
433 sizeof(rtas_log_partition));
434 }
435 oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
Jim Keniston6c493682011-07-25 07:54:50 +0000436 if (!oops_buf) {
437 pr_err("nvram: No memory for %s partition\n",
438 oops_log_partition.name);
439 return;
440 }
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530441 oops_data = oops_buf + sizeof(struct oops_log_info);
442 oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info);
Jim Keniston6c493682011-07-25 07:54:50 +0000443
444 /*
445 * Figure compression (preceded by elimination of each line's <n>
446 * severity prefix) will reduce the oops/panic report to at most
447 * 45% of its original size.
448 */
449 big_oops_buf_sz = (oops_data_sz * 100) / 45;
450 big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
451 if (big_oops_buf) {
452 stream.workspace = kmalloc(zlib_deflate_workspacesize(
453 WINDOW_BITS, MEM_LEVEL), GFP_KERNEL);
454 if (!stream.workspace) {
455 pr_err("nvram: No memory for compression workspace; "
456 "skipping compression of %s partition data\n",
457 oops_log_partition.name);
458 kfree(big_oops_buf);
459 big_oops_buf = NULL;
460 }
461 } else {
462 pr_err("No memory for uncompressed %s data; "
463 "skipping compression\n", oops_log_partition.name);
464 stream.workspace = NULL;
465 }
466
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000467 rc = kmsg_dump_register(&nvram_kmsg_dumper);
468 if (rc != 0) {
469 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
470 kfree(oops_buf);
Jim Keniston6c493682011-07-25 07:54:50 +0000471 kfree(big_oops_buf);
472 kfree(stream.workspace);
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000473 }
474}
475
Jim Keniston0f4ac132011-02-09 12:43:13 +0000476static int __init pseries_nvram_init_log_partitions(void)
477{
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000478 int rc;
479
480 rc = pseries_nvram_init_os_partition(&rtas_log_partition);
481 nvram_init_oops_partition(rc == 0);
Jim Keniston0f4ac132011-02-09 12:43:13 +0000482 return 0;
483}
484machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486int __init pSeries_nvram_init(void)
487{
488 struct device_node *nvram;
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000489 const unsigned int *nbytes_p;
490 unsigned int proplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 nvram = of_find_node_by_type(NULL, "nvram");
493 if (nvram == NULL)
494 return -ENODEV;
495
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000496 nbytes_p = of_get_property(nvram, "#bytes", &proplen);
Julia Lawallbad52322008-06-09 22:20:04 +1000497 if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
498 of_node_put(nvram);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return -EIO;
Julia Lawallbad52322008-06-09 22:20:04 +1000500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 nvram_size = *nbytes_p;
503
504 nvram_fetch = rtas_token("nvram-fetch");
505 nvram_store = rtas_token("nvram-store");
506 printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
507 of_node_put(nvram);
508
509 ppc_md.nvram_read = pSeries_nvram_read;
510 ppc_md.nvram_write = pSeries_nvram_write;
511 ppc_md.nvram_size = pSeries_nvram_get_size;
512
513 return 0;
514}
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000515
516/*
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000517 * Are we using the ibm,rtas-log for oops/panic reports? And if so,
518 * would logging this oops/panic overwrite an RTAS event that rtas_errd
519 * hasn't had a chance to read and process? Return 1 if so, else 0.
520 *
521 * We assume that if rtas_errd hasn't read the RTAS event in
522 * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to.
523 */
524static int clobbering_unread_rtas_event(void)
525{
526 return (oops_log_partition.index == rtas_log_partition.index
527 && last_unread_rtas_event
528 && get_seconds() - last_unread_rtas_event <=
529 NVRAM_RTAS_READ_TIMEOUT);
530}
531
Jim Keniston6c493682011-07-25 07:54:50 +0000532/* Derived from logfs_compress() */
533static int nvram_compress(const void *in, void *out, size_t inlen,
534 size_t outlen)
535{
536 int err, ret;
537
538 ret = -EIO;
539 err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
540 MEM_LEVEL, Z_DEFAULT_STRATEGY);
541 if (err != Z_OK)
542 goto error;
543
544 stream.next_in = in;
545 stream.avail_in = inlen;
546 stream.total_in = 0;
547 stream.next_out = out;
548 stream.avail_out = outlen;
549 stream.total_out = 0;
550
551 err = zlib_deflate(&stream, Z_FINISH);
552 if (err != Z_STREAM_END)
553 goto error;
554
555 err = zlib_deflateEnd(&stream);
556 if (err != Z_OK)
557 goto error;
558
559 if (stream.total_out >= stream.total_in)
560 goto error;
561
562 ret = stream.total_out;
563error:
564 return ret;
565}
566
567/* Compress the text from big_oops_buf into oops_buf. */
568static int zip_oops(size_t text_len)
569{
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530570 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
Jim Keniston6c493682011-07-25 07:54:50 +0000571 int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len,
572 oops_data_sz);
573 if (zipped_len < 0) {
574 pr_err("nvram: compression failed; returned %d\n", zipped_len);
575 pr_err("nvram: logging uncompressed oops/panic report\n");
576 return -1;
577 }
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530578 oops_hdr->version = OOPS_HDR_VERSION;
579 oops_hdr->report_length = (u16) zipped_len;
580 oops_hdr->timestamp = get_seconds();
Jim Keniston6c493682011-07-25 07:54:50 +0000581 return 0;
582}
583
584/*
585 * This is our kmsg_dump callback, called after an oops or panic report
586 * has been written to the printk buffer. We want to capture as much
587 * of the printk buffer as possible. First, capture as much as we can
588 * that we think will compress sufficiently to fit in the lnx,oops-log
589 * partition. If that's too much, go back and capture uncompressed text.
590 */
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000591static void oops_to_nvram(struct kmsg_dumper *dumper,
Kay Sieverse2ae7152012-06-15 14:07:51 +0200592 enum kmsg_dump_reason reason)
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000593{
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530594 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000595 static unsigned int oops_count = 0;
Jim Keniston15d260b2011-03-25 12:47:58 +0000596 static bool panicking = false;
Anton Blanchard120a52c2011-11-30 15:46:45 +0000597 static DEFINE_SPINLOCK(lock);
598 unsigned long flags;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000599 size_t text_len;
Jim Keniston6c493682011-07-25 07:54:50 +0000600 unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ;
601 int rc = -1;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000602
Jim Keniston15d260b2011-03-25 12:47:58 +0000603 switch (reason) {
604 case KMSG_DUMP_RESTART:
605 case KMSG_DUMP_HALT:
606 case KMSG_DUMP_POWEROFF:
607 /* These are almost always orderly shutdowns. */
608 return;
609 case KMSG_DUMP_OOPS:
Jim Keniston15d260b2011-03-25 12:47:58 +0000610 break;
611 case KMSG_DUMP_PANIC:
612 panicking = true;
613 break;
614 case KMSG_DUMP_EMERG:
615 if (panicking)
616 /* Panic report already captured. */
617 return;
618 break;
619 default:
620 pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
621 __FUNCTION__, (int) reason);
622 return;
623 }
624
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000625 if (clobbering_unread_rtas_event())
626 return;
627
Anton Blanchard120a52c2011-11-30 15:46:45 +0000628 if (!spin_trylock_irqsave(&lock, flags))
629 return;
630
Jim Keniston6c493682011-07-25 07:54:50 +0000631 if (big_oops_buf) {
Kay Sieverse2ae7152012-06-15 14:07:51 +0200632 kmsg_dump_get_buffer(dumper, false,
633 big_oops_buf, big_oops_buf_sz, &text_len);
Jim Keniston6c493682011-07-25 07:54:50 +0000634 rc = zip_oops(text_len);
635 }
636 if (rc != 0) {
Kay Sieverse2ae7152012-06-15 14:07:51 +0200637 kmsg_dump_rewind(dumper);
Aruna Balakrishnaiah1bf247f2013-06-06 00:20:55 +0530638 kmsg_dump_get_buffer(dumper, false,
Kay Sieverse2ae7152012-06-15 14:07:51 +0200639 oops_data, oops_data_sz, &text_len);
Jim Keniston6c493682011-07-25 07:54:50 +0000640 err_type = ERR_TYPE_KERNEL_PANIC;
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530641 oops_hdr->version = OOPS_HDR_VERSION;
642 oops_hdr->report_length = (u16) text_len;
643 oops_hdr->timestamp = get_seconds();
Jim Keniston6c493682011-07-25 07:54:50 +0000644 }
645
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000646 (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530647 (int) (sizeof(*oops_hdr) + oops_hdr->report_length), err_type,
648 ++oops_count);
Anton Blanchard120a52c2011-11-30 15:46:45 +0000649
650 spin_unlock_irqrestore(&lock, flags);
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000651}