blob: 9edec8ee02ef941b83bf9233edf54778e6087abb [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>
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +053021#include <linux/pstore.h>
Jim Keniston6c493682011-07-25 07:54:50 +000022#include <linux/ctype.h>
23#include <linux/zlib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +100030/* Max bytes to read/write in one go */
31#define NVRW_CNT 0x20
32
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +053033/*
34 * Set oops header version to distingush 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static unsigned int nvram_size;
41static int nvram_fetch, nvram_store;
42static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */
43static DEFINE_SPINLOCK(nvram_lock);
44
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +100045struct err_log_info {
46 int error_type;
47 unsigned int seq_num;
48};
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +100049
Jim Keniston0f4ac132011-02-09 12:43:13 +000050struct 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) */
Jim Kenistona5cf4b02011-02-09 13:00:09 +000054 long size; /* size of data portion (excluding err_log_info) */
Jim Keniston0f4ac132011-02-09 12:43:13 +000055 long index; /* offset of data portion of partition */
56};
57
58static struct nvram_os_partition rtas_log_partition = {
59 .name = "ibm,rtas-log",
60 .req_size = 2079,
61 .min_size = 1055,
62 .index = -1
63};
64
Jim Kenistona5cf4b02011-02-09 13:00:09 +000065static struct nvram_os_partition oops_log_partition = {
66 .name = "lnx,oops-log",
67 .req_size = 4000,
68 .min_size = 2000,
69 .index = -1
70};
71
Jim Keniston0f4ac132011-02-09 12:43:13 +000072static const char *pseries_nvram_os_partitions[] = {
73 "ibm,rtas-log",
Jim Kenistona5cf4b02011-02-09 13:00:09 +000074 "lnx,oops-log",
Jim Keniston0f4ac132011-02-09 12:43:13 +000075 NULL
76};
Benjamin Herrenschmidt9a866b82010-08-02 10:51:25 +100077
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +053078struct oops_log_info {
79 u16 version;
80 u16 report_length;
81 u64 timestamp;
82} __attribute__((packed));
83
Jim Kenistona5cf4b02011-02-09 13:00:09 +000084static void oops_to_nvram(struct kmsg_dumper *dumper,
Kay Sieverse2ae7152012-06-15 14:07:51 +020085 enum kmsg_dump_reason reason);
Jim Kenistona5cf4b02011-02-09 13:00:09 +000086
87static struct kmsg_dumper nvram_kmsg_dumper = {
88 .dump = oops_to_nvram
89};
90
91/* See clobbering_unread_rtas_event() */
92#define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */
93static unsigned long last_unread_rtas_event; /* timestamp */
94
Jim Keniston6c493682011-07-25 07:54:50 +000095/*
96 * For capturing and compressing an oops or panic report...
97
98 * big_oops_buf[] holds the uncompressed text we're capturing.
99 *
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530100 * oops_buf[] holds the compressed text, preceded by a oops header.
101 * oops header has u16 holding the version of oops header (to differentiate
102 * between old and new format header) followed by u16 holding the length of
103 * the compressed* text (*Or uncompressed, if compression fails.) and u64
104 * holding the timestamp. oops_buf[] gets written to NVRAM.
Jim Keniston6c493682011-07-25 07:54:50 +0000105 *
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530106 * oops_log_info points to the header. oops_data points to the compressed text.
Jim Keniston6c493682011-07-25 07:54:50 +0000107 *
108 * +- oops_buf
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530109 * | +- oops_data
110 * v v
111 * +-----------+-----------+-----------+------------------------+
112 * | version | length | timestamp | text |
113 * | (2 bytes) | (2 bytes) | (8 bytes) | (oops_data_sz bytes) |
114 * +-----------+-----------+-----------+------------------------+
Jim Keniston6c493682011-07-25 07:54:50 +0000115 * ^
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530116 * +- oops_log_info
Jim Keniston6c493682011-07-25 07:54:50 +0000117 *
118 * We preallocate these buffers during init to avoid kmalloc during oops/panic.
119 */
120static size_t big_oops_buf_sz;
121static char *big_oops_buf, *oops_buf;
Jim Keniston6c493682011-07-25 07:54:50 +0000122static char *oops_data;
123static size_t oops_data_sz;
124
125/* Compression parameters */
126#define COMPR_LEVEL 6
127#define WINDOW_BITS 12
128#define MEM_LEVEL 4
129static struct z_stream_s stream;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000130
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530131#ifdef CONFIG_PSTORE
132static enum pstore_type_id nvram_type_ids[] = {
133 PSTORE_TYPE_DMESG,
134 -1
135};
136static int read_type;
137#endif
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
140{
141 unsigned int i;
142 unsigned long len;
143 int done;
144 unsigned long flags;
145 char *p = buf;
146
147
148 if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
149 return -ENODEV;
150
151 if (*index >= nvram_size)
152 return 0;
153
154 i = *index;
155 if (i + count > nvram_size)
156 count = nvram_size - i;
157
158 spin_lock_irqsave(&nvram_lock, flags);
159
160 for (; count != 0; count -= len) {
161 len = count;
162 if (len > NVRW_CNT)
163 len = NVRW_CNT;
164
165 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
166 len) != 0) || len != done) {
167 spin_unlock_irqrestore(&nvram_lock, flags);
168 return -EIO;
169 }
170
171 memcpy(p, nvram_buf, len);
172
173 p += len;
174 i += len;
175 }
176
177 spin_unlock_irqrestore(&nvram_lock, flags);
178
179 *index = i;
180 return p - buf;
181}
182
183static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
184{
185 unsigned int i;
186 unsigned long len;
187 int done;
188 unsigned long flags;
189 const char *p = buf;
190
191 if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
192 return -ENODEV;
193
194 if (*index >= nvram_size)
195 return 0;
196
197 i = *index;
198 if (i + count > nvram_size)
199 count = nvram_size - i;
200
201 spin_lock_irqsave(&nvram_lock, flags);
202
203 for (; count != 0; count -= len) {
204 len = count;
205 if (len > NVRW_CNT)
206 len = NVRW_CNT;
207
208 memcpy(nvram_buf, p, len);
209
210 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
211 len) != 0) || len != done) {
212 spin_unlock_irqrestore(&nvram_lock, flags);
213 return -EIO;
214 }
215
216 p += len;
217 i += len;
218 }
219 spin_unlock_irqrestore(&nvram_lock, flags);
220
221 *index = i;
222 return p - buf;
223}
224
225static ssize_t pSeries_nvram_get_size(void)
226{
227 return nvram_size ? nvram_size : -ENODEV;
228}
229
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000230
Jim Keniston0f4ac132011-02-09 12:43:13 +0000231/* nvram_write_os_partition, nvram_write_error_log
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000232 *
233 * We need to buffer the error logs into nvram to ensure that we have
234 * the failure information to decode. If we have a severe error there
235 * is no way to guarantee that the OS or the machine is in a state to
236 * get back to user land and write the error to disk. For example if
237 * the SCSI device driver causes a Machine Check by writing to a bad
238 * IO address, there is no way of guaranteeing that the device driver
239 * is in any state that is would also be able to write the error data
240 * captured to disk, thus we buffer it in NVRAM for analysis on the
241 * next boot.
242 *
243 * In NVRAM the partition containing the error log buffer will looks like:
244 * Header (in bytes):
245 * +-----------+----------+--------+------------+------------------+
246 * | signature | checksum | length | name | data |
247 * |0 |1 |2 3|4 15|16 length-1|
248 * +-----------+----------+--------+------------+------------------+
249 *
250 * The 'data' section would look like (in bytes):
251 * +--------------+------------+-----------------------------------+
252 * | event_logged | sequence # | error log |
Jim Keniston0f4ac132011-02-09 12:43:13 +0000253 * |0 3|4 7|8 error_log_size-1|
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000254 * +--------------+------------+-----------------------------------+
255 *
256 * event_logged: 0 if event has not been logged to syslog, 1 if it has
257 * sequence #: The unique sequence # for each event. (until it wraps)
258 * error log: The error log from event_scan
259 */
Jim Keniston0f4ac132011-02-09 12:43:13 +0000260int nvram_write_os_partition(struct nvram_os_partition *part, char * buff,
261 int length, unsigned int err_type, unsigned int error_log_cnt)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000262{
263 int rc;
264 loff_t tmp_index;
265 struct err_log_info info;
266
Jim Keniston0f4ac132011-02-09 12:43:13 +0000267 if (part->index == -1) {
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000268 return -ESPIPE;
269 }
270
Jim Keniston0f4ac132011-02-09 12:43:13 +0000271 if (length > part->size) {
272 length = part->size;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000273 }
274
275 info.error_type = err_type;
276 info.seq_num = error_log_cnt;
277
Jim Keniston0f4ac132011-02-09 12:43:13 +0000278 tmp_index = part->index;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000279
280 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
281 if (rc <= 0) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000282 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000283 return rc;
284 }
285
286 rc = ppc_md.nvram_write(buff, length, &tmp_index);
287 if (rc <= 0) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000288 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000289 return rc;
290 }
291
292 return 0;
293}
294
Jim Keniston0f4ac132011-02-09 12:43:13 +0000295int nvram_write_error_log(char * buff, int length,
296 unsigned int err_type, unsigned int error_log_cnt)
297{
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000298 int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,
Jim Keniston0f4ac132011-02-09 12:43:13 +0000299 err_type, error_log_cnt);
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000300 if (!rc)
301 last_unread_rtas_event = get_seconds();
302 return rc;
Jim Keniston0f4ac132011-02-09 12:43:13 +0000303}
304
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530305/* nvram_read_partition
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000306 *
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530307 * Reads nvram partition for at most 'length'
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000308 */
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530309int nvram_read_partition(struct nvram_os_partition *part, char *buff,
310 int length, unsigned int *err_type,
311 unsigned int *error_log_cnt)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000312{
313 int rc;
314 loff_t tmp_index;
315 struct err_log_info info;
316
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530317 if (part->index == -1)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000318 return -1;
319
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530320 if (length > part->size)
321 length = part->size;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000322
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530323 tmp_index = part->index;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000324
325 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
326 if (rc <= 0) {
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530327 pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000328 return rc;
329 }
330
331 rc = ppc_md.nvram_read(buff, length, &tmp_index);
332 if (rc <= 0) {
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530333 pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000334 return rc;
335 }
336
337 *error_log_cnt = info.seq_num;
338 *err_type = info.error_type;
339
340 return 0;
341}
342
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530343/* nvram_read_error_log
344 *
345 * Reads nvram for error log for at most 'length'
346 */
347int nvram_read_error_log(char *buff, int length,
348 unsigned int *err_type, unsigned int *error_log_cnt)
349{
350 return nvram_read_partition(&rtas_log_partition, buff, length,
351 err_type, error_log_cnt);
352}
353
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000354/* This doesn't actually zero anything, but it sets the event_logged
355 * word to tell that this event is safely in syslog.
356 */
357int nvram_clear_error_log(void)
358{
359 loff_t tmp_index;
360 int clear_word = ERR_FLAG_ALREADY_LOGGED;
361 int rc;
362
Jim Keniston0f4ac132011-02-09 12:43:13 +0000363 if (rtas_log_partition.index == -1)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000364 return -1;
365
Jim Keniston0f4ac132011-02-09 12:43:13 +0000366 tmp_index = rtas_log_partition.index;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000367
368 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
369 if (rc <= 0) {
370 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
371 return rc;
372 }
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000373 last_unread_rtas_event = 0;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000374
375 return 0;
376}
377
Jim Keniston0f4ac132011-02-09 12:43:13 +0000378/* pseries_nvram_init_os_partition
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000379 *
Jim Keniston0f4ac132011-02-09 12:43:13 +0000380 * This sets up a partition with an "OS" signature.
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000381 *
382 * The general strategy is the following:
Jim Keniston0f4ac132011-02-09 12:43:13 +0000383 * 1.) If a partition with the indicated name already exists...
384 * - If it's large enough, use it.
385 * - Otherwise, recycle it and keep going.
386 * 2.) Search for a free partition that is large enough.
387 * 3.) If there's not a free partition large enough, recycle any obsolete
388 * OS partitions and try again.
389 * 4.) Will first try getting a chunk that will satisfy the requested size.
390 * 5.) If a chunk of the requested size cannot be allocated, then try finding
391 * a chunk that will satisfy the minum needed.
392 *
393 * Returns 0 on success, else -1.
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000394 */
Jim Keniston0f4ac132011-02-09 12:43:13 +0000395static int __init pseries_nvram_init_os_partition(struct nvram_os_partition
396 *part)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000397{
398 loff_t p;
399 int size;
400
401 /* Scan nvram for partitions */
402 nvram_scan_partitions();
403
Jim Keniston0f4ac132011-02-09 12:43:13 +0000404 /* Look for ours */
405 p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000406
407 /* Found one but too small, remove it */
Jim Keniston0f4ac132011-02-09 12:43:13 +0000408 if (p && size < part->min_size) {
409 pr_info("nvram: Found too small %s partition,"
410 " removing it...\n", part->name);
411 nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000412 p = 0;
413 }
414
415 /* Create one if we didn't find */
416 if (!p) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000417 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
418 part->req_size, part->min_size);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000419 if (p == -ENOSPC) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000420 pr_info("nvram: No room to create %s partition, "
421 "deleting any obsolete OS partitions...\n",
422 part->name);
423 nvram_remove_partition(NULL, NVRAM_SIG_OS,
424 pseries_nvram_os_partitions);
425 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
426 part->req_size, part->min_size);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000427 }
428 }
429
430 if (p <= 0) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000431 pr_err("nvram: Failed to find or create %s"
432 " partition, err %d\n", part->name, (int)p);
433 return -1;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000434 }
435
Jim Keniston0f4ac132011-02-09 12:43:13 +0000436 part->index = p;
437 part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000438
439 return 0;
440}
Jim Keniston0f4ac132011-02-09 12:43:13 +0000441
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530442/*
443 * Are we using the ibm,rtas-log for oops/panic reports? And if so,
444 * would logging this oops/panic overwrite an RTAS event that rtas_errd
445 * hasn't had a chance to read and process? Return 1 if so, else 0.
446 *
447 * We assume that if rtas_errd hasn't read the RTAS event in
448 * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to.
449 */
450static int clobbering_unread_rtas_event(void)
451{
452 return (oops_log_partition.index == rtas_log_partition.index
453 && last_unread_rtas_event
454 && get_seconds() - last_unread_rtas_event <=
455 NVRAM_RTAS_READ_TIMEOUT);
456}
457
458#ifdef CONFIG_PSTORE
459static int nvram_pstore_open(struct pstore_info *psi)
460{
461 /* Reset the iterator to start reading partitions again */
462 read_type = -1;
463 return 0;
464}
465
466/**
467 * nvram_pstore_write - pstore write callback for nvram
468 * @type: Type of message logged
469 * @reason: reason behind dump (oops/panic)
470 * @id: identifier to indicate the write performed
471 * @part: pstore writes data to registered buffer in parts,
472 * part number will indicate the same.
473 * @count: Indicates oops count
474 * @size: number of bytes written to the registered buffer
475 * @psi: registered pstore_info structure
476 *
477 * Called by pstore_dump() when an oops or panic report is logged in the
478 * printk buffer.
479 * Returns 0 on successful write.
480 */
481static int nvram_pstore_write(enum pstore_type_id type,
482 enum kmsg_dump_reason reason,
483 u64 *id, unsigned int part, int count,
484 size_t size, struct pstore_info *psi)
485{
486 int rc;
487 struct oops_log_info *oops_hdr = (struct oops_log_info *) oops_buf;
488
489 /* part 1 has the recent messages from printk buffer */
490 if (part > 1 || type != PSTORE_TYPE_DMESG ||
491 clobbering_unread_rtas_event())
492 return -1;
493
494 oops_hdr->version = OOPS_HDR_VERSION;
495 oops_hdr->report_length = (u16) size;
496 oops_hdr->timestamp = get_seconds();
497 rc = nvram_write_os_partition(&oops_log_partition, oops_buf,
498 (int) (sizeof(*oops_hdr) + size), ERR_TYPE_KERNEL_PANIC,
499 count);
500
501 if (rc != 0)
502 return rc;
503
504 *id = part;
505 return 0;
506}
507
508/*
509 * Reads the oops/panic report.
510 * Returns the length of the data we read from each partition.
511 * Returns 0 if we've been called before.
512 */
513static ssize_t nvram_pstore_read(u64 *id, enum pstore_type_id *type,
514 int *count, struct timespec *time, char **buf,
515 struct pstore_info *psi)
516{
517 struct oops_log_info *oops_hdr;
518 unsigned int err_type, id_no;
519 struct nvram_os_partition *part = NULL;
520 char *buff = NULL;
521
522 read_type++;
523
524 switch (nvram_type_ids[read_type]) {
525 case PSTORE_TYPE_DMESG:
526 part = &oops_log_partition;
527 *type = PSTORE_TYPE_DMESG;
528 break;
529 default:
530 return 0;
531 }
532
533 buff = kmalloc(part->size, GFP_KERNEL);
534
535 if (!buff)
536 return -ENOMEM;
537
538 if (nvram_read_partition(part, buff, part->size, &err_type, &id_no)) {
539 kfree(buff);
540 return 0;
541 }
542
543 *count = 0;
544 *id = id_no;
545 oops_hdr = (struct oops_log_info *)buff;
546 *buf = buff + sizeof(*oops_hdr);
547 time->tv_sec = oops_hdr->timestamp;
548 time->tv_nsec = 0;
549 return oops_hdr->report_length;
550}
551
552static struct pstore_info nvram_pstore_info = {
553 .owner = THIS_MODULE,
554 .name = "nvram",
555 .open = nvram_pstore_open,
556 .read = nvram_pstore_read,
557 .write = nvram_pstore_write,
558};
559
560static int nvram_pstore_init(void)
561{
562 int rc = 0;
563
564 nvram_pstore_info.buf = oops_data;
565 nvram_pstore_info.bufsize = oops_data_sz;
566
567 rc = pstore_register(&nvram_pstore_info);
568 if (rc != 0)
569 pr_err("nvram: pstore_register() failed, defaults to "
570 "kmsg_dump; returned %d\n", rc);
571 else
572 /*TODO: Support compression when pstore is configured */
573 pr_info("nvram: Compression of oops text supported only when "
574 "pstore is not configured");
575
576 return rc;
577}
578#else
579static int nvram_pstore_init(void)
580{
581 return -1;
582}
583#endif
584
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000585static void __init nvram_init_oops_partition(int rtas_partition_exists)
586{
587 int rc;
588
589 rc = pseries_nvram_init_os_partition(&oops_log_partition);
590 if (rc != 0) {
591 if (!rtas_partition_exists)
592 return;
593 pr_notice("nvram: Using %s partition to log both"
594 " RTAS errors and oops/panic reports\n",
595 rtas_log_partition.name);
596 memcpy(&oops_log_partition, &rtas_log_partition,
597 sizeof(rtas_log_partition));
598 }
599 oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
Jim Keniston6c493682011-07-25 07:54:50 +0000600 if (!oops_buf) {
601 pr_err("nvram: No memory for %s partition\n",
602 oops_log_partition.name);
603 return;
604 }
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530605 oops_data = oops_buf + sizeof(struct oops_log_info);
606 oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info);
Jim Keniston6c493682011-07-25 07:54:50 +0000607
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530608 rc = nvram_pstore_init();
609
610 if (!rc)
611 return;
612
Jim Keniston6c493682011-07-25 07:54:50 +0000613 /*
614 * Figure compression (preceded by elimination of each line's <n>
615 * severity prefix) will reduce the oops/panic report to at most
616 * 45% of its original size.
617 */
618 big_oops_buf_sz = (oops_data_sz * 100) / 45;
619 big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
620 if (big_oops_buf) {
621 stream.workspace = kmalloc(zlib_deflate_workspacesize(
622 WINDOW_BITS, MEM_LEVEL), GFP_KERNEL);
623 if (!stream.workspace) {
624 pr_err("nvram: No memory for compression workspace; "
625 "skipping compression of %s partition data\n",
626 oops_log_partition.name);
627 kfree(big_oops_buf);
628 big_oops_buf = NULL;
629 }
630 } else {
631 pr_err("No memory for uncompressed %s data; "
632 "skipping compression\n", oops_log_partition.name);
633 stream.workspace = NULL;
634 }
635
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000636 rc = kmsg_dump_register(&nvram_kmsg_dumper);
637 if (rc != 0) {
638 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
639 kfree(oops_buf);
Jim Keniston6c493682011-07-25 07:54:50 +0000640 kfree(big_oops_buf);
641 kfree(stream.workspace);
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000642 }
643}
644
Jim Keniston0f4ac132011-02-09 12:43:13 +0000645static int __init pseries_nvram_init_log_partitions(void)
646{
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000647 int rc;
648
649 rc = pseries_nvram_init_os_partition(&rtas_log_partition);
650 nvram_init_oops_partition(rc == 0);
Jim Keniston0f4ac132011-02-09 12:43:13 +0000651 return 0;
652}
653machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655int __init pSeries_nvram_init(void)
656{
657 struct device_node *nvram;
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000658 const unsigned int *nbytes_p;
659 unsigned int proplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 nvram = of_find_node_by_type(NULL, "nvram");
662 if (nvram == NULL)
663 return -ENODEV;
664
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000665 nbytes_p = of_get_property(nvram, "#bytes", &proplen);
Julia Lawallbad52322008-06-09 22:20:04 +1000666 if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
667 of_node_put(nvram);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -EIO;
Julia Lawallbad52322008-06-09 22:20:04 +1000669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 nvram_size = *nbytes_p;
672
673 nvram_fetch = rtas_token("nvram-fetch");
674 nvram_store = rtas_token("nvram-store");
675 printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
676 of_node_put(nvram);
677
678 ppc_md.nvram_read = pSeries_nvram_read;
679 ppc_md.nvram_write = pSeries_nvram_write;
680 ppc_md.nvram_size = pSeries_nvram_get_size;
681
682 return 0;
683}
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000684
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000685
Jim Keniston6c493682011-07-25 07:54:50 +0000686/* Derived from logfs_compress() */
687static int nvram_compress(const void *in, void *out, size_t inlen,
688 size_t outlen)
689{
690 int err, ret;
691
692 ret = -EIO;
693 err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
694 MEM_LEVEL, Z_DEFAULT_STRATEGY);
695 if (err != Z_OK)
696 goto error;
697
698 stream.next_in = in;
699 stream.avail_in = inlen;
700 stream.total_in = 0;
701 stream.next_out = out;
702 stream.avail_out = outlen;
703 stream.total_out = 0;
704
705 err = zlib_deflate(&stream, Z_FINISH);
706 if (err != Z_STREAM_END)
707 goto error;
708
709 err = zlib_deflateEnd(&stream);
710 if (err != Z_OK)
711 goto error;
712
713 if (stream.total_out >= stream.total_in)
714 goto error;
715
716 ret = stream.total_out;
717error:
718 return ret;
719}
720
721/* Compress the text from big_oops_buf into oops_buf. */
722static int zip_oops(size_t text_len)
723{
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530724 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
Jim Keniston6c493682011-07-25 07:54:50 +0000725 int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len,
726 oops_data_sz);
727 if (zipped_len < 0) {
728 pr_err("nvram: compression failed; returned %d\n", zipped_len);
729 pr_err("nvram: logging uncompressed oops/panic report\n");
730 return -1;
731 }
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530732 oops_hdr->version = OOPS_HDR_VERSION;
733 oops_hdr->report_length = (u16) zipped_len;
734 oops_hdr->timestamp = get_seconds();
Jim Keniston6c493682011-07-25 07:54:50 +0000735 return 0;
736}
737
738/*
739 * This is our kmsg_dump callback, called after an oops or panic report
740 * has been written to the printk buffer. We want to capture as much
741 * of the printk buffer as possible. First, capture as much as we can
742 * that we think will compress sufficiently to fit in the lnx,oops-log
743 * partition. If that's too much, go back and capture uncompressed text.
744 */
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000745static void oops_to_nvram(struct kmsg_dumper *dumper,
Kay Sieverse2ae7152012-06-15 14:07:51 +0200746 enum kmsg_dump_reason reason)
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000747{
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530748 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000749 static unsigned int oops_count = 0;
Jim Keniston15d260b2011-03-25 12:47:58 +0000750 static bool panicking = false;
Anton Blanchard120a52c2011-11-30 15:46:45 +0000751 static DEFINE_SPINLOCK(lock);
752 unsigned long flags;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000753 size_t text_len;
Jim Keniston6c493682011-07-25 07:54:50 +0000754 unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ;
755 int rc = -1;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000756
Jim Keniston15d260b2011-03-25 12:47:58 +0000757 switch (reason) {
758 case KMSG_DUMP_RESTART:
759 case KMSG_DUMP_HALT:
760 case KMSG_DUMP_POWEROFF:
761 /* These are almost always orderly shutdowns. */
762 return;
763 case KMSG_DUMP_OOPS:
Jim Keniston15d260b2011-03-25 12:47:58 +0000764 break;
765 case KMSG_DUMP_PANIC:
766 panicking = true;
767 break;
768 case KMSG_DUMP_EMERG:
769 if (panicking)
770 /* Panic report already captured. */
771 return;
772 break;
773 default:
774 pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
775 __FUNCTION__, (int) reason);
776 return;
777 }
778
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000779 if (clobbering_unread_rtas_event())
780 return;
781
Anton Blanchard120a52c2011-11-30 15:46:45 +0000782 if (!spin_trylock_irqsave(&lock, flags))
783 return;
784
Jim Keniston6c493682011-07-25 07:54:50 +0000785 if (big_oops_buf) {
Kay Sieverse2ae7152012-06-15 14:07:51 +0200786 kmsg_dump_get_buffer(dumper, false,
787 big_oops_buf, big_oops_buf_sz, &text_len);
Jim Keniston6c493682011-07-25 07:54:50 +0000788 rc = zip_oops(text_len);
789 }
790 if (rc != 0) {
Kay Sieverse2ae7152012-06-15 14:07:51 +0200791 kmsg_dump_rewind(dumper);
Aruna Balakrishnaiah1bf247f2013-06-06 00:20:55 +0530792 kmsg_dump_get_buffer(dumper, false,
Kay Sieverse2ae7152012-06-15 14:07:51 +0200793 oops_data, oops_data_sz, &text_len);
Jim Keniston6c493682011-07-25 07:54:50 +0000794 err_type = ERR_TYPE_KERNEL_PANIC;
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530795 oops_hdr->version = OOPS_HDR_VERSION;
796 oops_hdr->report_length = (u16) text_len;
797 oops_hdr->timestamp = get_seconds();
Jim Keniston6c493682011-07-25 07:54:50 +0000798 }
799
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000800 (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530801 (int) (sizeof(*oops_hdr) + oops_hdr->report_length), err_type,
802 ++oops_count);
Anton Blanchard120a52c2011-11-30 15:46:45 +0000803
804 spin_unlock_irqrestore(&lock, flags);
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000805}