blob: 78d72f0175adbcdcf8e353473c5f6e59292bc55c [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,
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530134 PSTORE_TYPE_PPC_RTAS,
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530135 -1
136};
137static int read_type;
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530138static unsigned long last_rtas_event;
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530139#endif
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
142{
143 unsigned int i;
144 unsigned long len;
145 int done;
146 unsigned long flags;
147 char *p = buf;
148
149
150 if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
151 return -ENODEV;
152
153 if (*index >= nvram_size)
154 return 0;
155
156 i = *index;
157 if (i + count > nvram_size)
158 count = nvram_size - i;
159
160 spin_lock_irqsave(&nvram_lock, flags);
161
162 for (; count != 0; count -= len) {
163 len = count;
164 if (len > NVRW_CNT)
165 len = NVRW_CNT;
166
167 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
168 len) != 0) || len != done) {
169 spin_unlock_irqrestore(&nvram_lock, flags);
170 return -EIO;
171 }
172
173 memcpy(p, nvram_buf, len);
174
175 p += len;
176 i += len;
177 }
178
179 spin_unlock_irqrestore(&nvram_lock, flags);
180
181 *index = i;
182 return p - buf;
183}
184
185static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
186{
187 unsigned int i;
188 unsigned long len;
189 int done;
190 unsigned long flags;
191 const char *p = buf;
192
193 if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
194 return -ENODEV;
195
196 if (*index >= nvram_size)
197 return 0;
198
199 i = *index;
200 if (i + count > nvram_size)
201 count = nvram_size - i;
202
203 spin_lock_irqsave(&nvram_lock, flags);
204
205 for (; count != 0; count -= len) {
206 len = count;
207 if (len > NVRW_CNT)
208 len = NVRW_CNT;
209
210 memcpy(nvram_buf, p, len);
211
212 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
213 len) != 0) || len != done) {
214 spin_unlock_irqrestore(&nvram_lock, flags);
215 return -EIO;
216 }
217
218 p += len;
219 i += len;
220 }
221 spin_unlock_irqrestore(&nvram_lock, flags);
222
223 *index = i;
224 return p - buf;
225}
226
227static ssize_t pSeries_nvram_get_size(void)
228{
229 return nvram_size ? nvram_size : -ENODEV;
230}
231
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000232
Jim Keniston0f4ac132011-02-09 12:43:13 +0000233/* nvram_write_os_partition, nvram_write_error_log
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000234 *
235 * We need to buffer the error logs into nvram to ensure that we have
236 * the failure information to decode. If we have a severe error there
237 * is no way to guarantee that the OS or the machine is in a state to
238 * get back to user land and write the error to disk. For example if
239 * the SCSI device driver causes a Machine Check by writing to a bad
240 * IO address, there is no way of guaranteeing that the device driver
241 * is in any state that is would also be able to write the error data
242 * captured to disk, thus we buffer it in NVRAM for analysis on the
243 * next boot.
244 *
245 * In NVRAM the partition containing the error log buffer will looks like:
246 * Header (in bytes):
247 * +-----------+----------+--------+------------+------------------+
248 * | signature | checksum | length | name | data |
249 * |0 |1 |2 3|4 15|16 length-1|
250 * +-----------+----------+--------+------------+------------------+
251 *
252 * The 'data' section would look like (in bytes):
253 * +--------------+------------+-----------------------------------+
254 * | event_logged | sequence # | error log |
Jim Keniston0f4ac132011-02-09 12:43:13 +0000255 * |0 3|4 7|8 error_log_size-1|
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000256 * +--------------+------------+-----------------------------------+
257 *
258 * event_logged: 0 if event has not been logged to syslog, 1 if it has
259 * sequence #: The unique sequence # for each event. (until it wraps)
260 * error log: The error log from event_scan
261 */
Jim Keniston0f4ac132011-02-09 12:43:13 +0000262int nvram_write_os_partition(struct nvram_os_partition *part, char * buff,
263 int length, unsigned int err_type, unsigned int error_log_cnt)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000264{
265 int rc;
266 loff_t tmp_index;
267 struct err_log_info info;
268
Jim Keniston0f4ac132011-02-09 12:43:13 +0000269 if (part->index == -1) {
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000270 return -ESPIPE;
271 }
272
Jim Keniston0f4ac132011-02-09 12:43:13 +0000273 if (length > part->size) {
274 length = part->size;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000275 }
276
277 info.error_type = err_type;
278 info.seq_num = error_log_cnt;
279
Jim Keniston0f4ac132011-02-09 12:43:13 +0000280 tmp_index = part->index;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000281
282 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
283 if (rc <= 0) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000284 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000285 return rc;
286 }
287
288 rc = ppc_md.nvram_write(buff, length, &tmp_index);
289 if (rc <= 0) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000290 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000291 return rc;
292 }
293
294 return 0;
295}
296
Jim Keniston0f4ac132011-02-09 12:43:13 +0000297int nvram_write_error_log(char * buff, int length,
298 unsigned int err_type, unsigned int error_log_cnt)
299{
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000300 int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,
Jim Keniston0f4ac132011-02-09 12:43:13 +0000301 err_type, error_log_cnt);
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530302 if (!rc) {
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000303 last_unread_rtas_event = get_seconds();
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530304#ifdef CONFIG_PSTORE
305 last_rtas_event = get_seconds();
306#endif
307 }
308
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000309 return rc;
Jim Keniston0f4ac132011-02-09 12:43:13 +0000310}
311
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530312/* nvram_read_partition
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000313 *
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530314 * Reads nvram partition for at most 'length'
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000315 */
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530316int nvram_read_partition(struct nvram_os_partition *part, char *buff,
317 int length, unsigned int *err_type,
318 unsigned int *error_log_cnt)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000319{
320 int rc;
321 loff_t tmp_index;
322 struct err_log_info info;
323
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530324 if (part->index == -1)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000325 return -1;
326
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530327 if (length > part->size)
328 length = part->size;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000329
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530330 tmp_index = part->index;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000331
332 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
333 if (rc <= 0) {
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530334 pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000335 return rc;
336 }
337
338 rc = ppc_md.nvram_read(buff, length, &tmp_index);
339 if (rc <= 0) {
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530340 pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000341 return rc;
342 }
343
344 *error_log_cnt = info.seq_num;
345 *err_type = info.error_type;
346
347 return 0;
348}
349
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530350/* nvram_read_error_log
351 *
352 * Reads nvram for error log for at most 'length'
353 */
354int nvram_read_error_log(char *buff, int length,
355 unsigned int *err_type, unsigned int *error_log_cnt)
356{
357 return nvram_read_partition(&rtas_log_partition, buff, length,
358 err_type, error_log_cnt);
359}
360
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000361/* This doesn't actually zero anything, but it sets the event_logged
362 * word to tell that this event is safely in syslog.
363 */
364int nvram_clear_error_log(void)
365{
366 loff_t tmp_index;
367 int clear_word = ERR_FLAG_ALREADY_LOGGED;
368 int rc;
369
Jim Keniston0f4ac132011-02-09 12:43:13 +0000370 if (rtas_log_partition.index == -1)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000371 return -1;
372
Jim Keniston0f4ac132011-02-09 12:43:13 +0000373 tmp_index = rtas_log_partition.index;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000374
375 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
376 if (rc <= 0) {
377 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
378 return rc;
379 }
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000380 last_unread_rtas_event = 0;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000381
382 return 0;
383}
384
Jim Keniston0f4ac132011-02-09 12:43:13 +0000385/* pseries_nvram_init_os_partition
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000386 *
Jim Keniston0f4ac132011-02-09 12:43:13 +0000387 * This sets up a partition with an "OS" signature.
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000388 *
389 * The general strategy is the following:
Jim Keniston0f4ac132011-02-09 12:43:13 +0000390 * 1.) If a partition with the indicated name already exists...
391 * - If it's large enough, use it.
392 * - Otherwise, recycle it and keep going.
393 * 2.) Search for a free partition that is large enough.
394 * 3.) If there's not a free partition large enough, recycle any obsolete
395 * OS partitions and try again.
396 * 4.) Will first try getting a chunk that will satisfy the requested size.
397 * 5.) If a chunk of the requested size cannot be allocated, then try finding
398 * a chunk that will satisfy the minum needed.
399 *
400 * Returns 0 on success, else -1.
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000401 */
Jim Keniston0f4ac132011-02-09 12:43:13 +0000402static int __init pseries_nvram_init_os_partition(struct nvram_os_partition
403 *part)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000404{
405 loff_t p;
406 int size;
407
408 /* Scan nvram for partitions */
409 nvram_scan_partitions();
410
Jim Keniston0f4ac132011-02-09 12:43:13 +0000411 /* Look for ours */
412 p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000413
414 /* Found one but too small, remove it */
Jim Keniston0f4ac132011-02-09 12:43:13 +0000415 if (p && size < part->min_size) {
416 pr_info("nvram: Found too small %s partition,"
417 " removing it...\n", part->name);
418 nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000419 p = 0;
420 }
421
422 /* Create one if we didn't find */
423 if (!p) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000424 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
425 part->req_size, part->min_size);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000426 if (p == -ENOSPC) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000427 pr_info("nvram: No room to create %s partition, "
428 "deleting any obsolete OS partitions...\n",
429 part->name);
430 nvram_remove_partition(NULL, NVRAM_SIG_OS,
431 pseries_nvram_os_partitions);
432 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
433 part->req_size, part->min_size);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000434 }
435 }
436
437 if (p <= 0) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000438 pr_err("nvram: Failed to find or create %s"
439 " partition, err %d\n", part->name, (int)p);
440 return -1;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000441 }
442
Jim Keniston0f4ac132011-02-09 12:43:13 +0000443 part->index = p;
444 part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000445
446 return 0;
447}
Jim Keniston0f4ac132011-02-09 12:43:13 +0000448
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530449/*
450 * Are we using the ibm,rtas-log for oops/panic reports? And if so,
451 * would logging this oops/panic overwrite an RTAS event that rtas_errd
452 * hasn't had a chance to read and process? Return 1 if so, else 0.
453 *
454 * We assume that if rtas_errd hasn't read the RTAS event in
455 * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to.
456 */
457static int clobbering_unread_rtas_event(void)
458{
459 return (oops_log_partition.index == rtas_log_partition.index
460 && last_unread_rtas_event
461 && get_seconds() - last_unread_rtas_event <=
462 NVRAM_RTAS_READ_TIMEOUT);
463}
464
465#ifdef CONFIG_PSTORE
466static int nvram_pstore_open(struct pstore_info *psi)
467{
468 /* Reset the iterator to start reading partitions again */
469 read_type = -1;
470 return 0;
471}
472
473/**
474 * nvram_pstore_write - pstore write callback for nvram
475 * @type: Type of message logged
476 * @reason: reason behind dump (oops/panic)
477 * @id: identifier to indicate the write performed
478 * @part: pstore writes data to registered buffer in parts,
479 * part number will indicate the same.
480 * @count: Indicates oops count
481 * @size: number of bytes written to the registered buffer
482 * @psi: registered pstore_info structure
483 *
484 * Called by pstore_dump() when an oops or panic report is logged in the
485 * printk buffer.
486 * Returns 0 on successful write.
487 */
488static int nvram_pstore_write(enum pstore_type_id type,
489 enum kmsg_dump_reason reason,
490 u64 *id, unsigned int part, int count,
491 size_t size, struct pstore_info *psi)
492{
493 int rc;
494 struct oops_log_info *oops_hdr = (struct oops_log_info *) oops_buf;
495
496 /* part 1 has the recent messages from printk buffer */
497 if (part > 1 || type != PSTORE_TYPE_DMESG ||
498 clobbering_unread_rtas_event())
499 return -1;
500
501 oops_hdr->version = OOPS_HDR_VERSION;
502 oops_hdr->report_length = (u16) size;
503 oops_hdr->timestamp = get_seconds();
504 rc = nvram_write_os_partition(&oops_log_partition, oops_buf,
505 (int) (sizeof(*oops_hdr) + size), ERR_TYPE_KERNEL_PANIC,
506 count);
507
508 if (rc != 0)
509 return rc;
510
511 *id = part;
512 return 0;
513}
514
515/*
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530516 * Reads the oops/panic report and ibm,rtas-log partition.
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530517 * Returns the length of the data we read from each partition.
518 * Returns 0 if we've been called before.
519 */
520static ssize_t nvram_pstore_read(u64 *id, enum pstore_type_id *type,
521 int *count, struct timespec *time, char **buf,
522 struct pstore_info *psi)
523{
524 struct oops_log_info *oops_hdr;
525 unsigned int err_type, id_no;
526 struct nvram_os_partition *part = NULL;
527 char *buff = NULL;
528
529 read_type++;
530
531 switch (nvram_type_ids[read_type]) {
532 case PSTORE_TYPE_DMESG:
533 part = &oops_log_partition;
534 *type = PSTORE_TYPE_DMESG;
535 break;
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530536 case PSTORE_TYPE_PPC_RTAS:
537 part = &rtas_log_partition;
538 *type = PSTORE_TYPE_PPC_RTAS;
539 time->tv_sec = last_rtas_event;
540 time->tv_nsec = 0;
541 break;
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530542 default:
543 return 0;
544 }
545
546 buff = kmalloc(part->size, GFP_KERNEL);
547
548 if (!buff)
549 return -ENOMEM;
550
551 if (nvram_read_partition(part, buff, part->size, &err_type, &id_no)) {
552 kfree(buff);
553 return 0;
554 }
555
556 *count = 0;
557 *id = id_no;
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530558
559 if (nvram_type_ids[read_type] == PSTORE_TYPE_DMESG) {
560 oops_hdr = (struct oops_log_info *)buff;
561 *buf = buff + sizeof(*oops_hdr);
562 time->tv_sec = oops_hdr->timestamp;
563 time->tv_nsec = 0;
564 return oops_hdr->report_length;
565 }
566
567 *buf = buff;
568 return part->size;
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530569}
570
571static struct pstore_info nvram_pstore_info = {
572 .owner = THIS_MODULE,
573 .name = "nvram",
574 .open = nvram_pstore_open,
575 .read = nvram_pstore_read,
576 .write = nvram_pstore_write,
577};
578
579static int nvram_pstore_init(void)
580{
581 int rc = 0;
582
583 nvram_pstore_info.buf = oops_data;
584 nvram_pstore_info.bufsize = oops_data_sz;
585
586 rc = pstore_register(&nvram_pstore_info);
587 if (rc != 0)
588 pr_err("nvram: pstore_register() failed, defaults to "
589 "kmsg_dump; returned %d\n", rc);
590 else
591 /*TODO: Support compression when pstore is configured */
592 pr_info("nvram: Compression of oops text supported only when "
593 "pstore is not configured");
594
595 return rc;
596}
597#else
598static int nvram_pstore_init(void)
599{
600 return -1;
601}
602#endif
603
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000604static void __init nvram_init_oops_partition(int rtas_partition_exists)
605{
606 int rc;
607
608 rc = pseries_nvram_init_os_partition(&oops_log_partition);
609 if (rc != 0) {
610 if (!rtas_partition_exists)
611 return;
612 pr_notice("nvram: Using %s partition to log both"
613 " RTAS errors and oops/panic reports\n",
614 rtas_log_partition.name);
615 memcpy(&oops_log_partition, &rtas_log_partition,
616 sizeof(rtas_log_partition));
617 }
618 oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
Jim Keniston6c493682011-07-25 07:54:50 +0000619 if (!oops_buf) {
620 pr_err("nvram: No memory for %s partition\n",
621 oops_log_partition.name);
622 return;
623 }
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530624 oops_data = oops_buf + sizeof(struct oops_log_info);
625 oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info);
Jim Keniston6c493682011-07-25 07:54:50 +0000626
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530627 rc = nvram_pstore_init();
628
629 if (!rc)
630 return;
631
Jim Keniston6c493682011-07-25 07:54:50 +0000632 /*
633 * Figure compression (preceded by elimination of each line's <n>
634 * severity prefix) will reduce the oops/panic report to at most
635 * 45% of its original size.
636 */
637 big_oops_buf_sz = (oops_data_sz * 100) / 45;
638 big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
639 if (big_oops_buf) {
640 stream.workspace = kmalloc(zlib_deflate_workspacesize(
641 WINDOW_BITS, MEM_LEVEL), GFP_KERNEL);
642 if (!stream.workspace) {
643 pr_err("nvram: No memory for compression workspace; "
644 "skipping compression of %s partition data\n",
645 oops_log_partition.name);
646 kfree(big_oops_buf);
647 big_oops_buf = NULL;
648 }
649 } else {
650 pr_err("No memory for uncompressed %s data; "
651 "skipping compression\n", oops_log_partition.name);
652 stream.workspace = NULL;
653 }
654
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000655 rc = kmsg_dump_register(&nvram_kmsg_dumper);
656 if (rc != 0) {
657 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
658 kfree(oops_buf);
Jim Keniston6c493682011-07-25 07:54:50 +0000659 kfree(big_oops_buf);
660 kfree(stream.workspace);
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000661 }
662}
663
Jim Keniston0f4ac132011-02-09 12:43:13 +0000664static int __init pseries_nvram_init_log_partitions(void)
665{
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000666 int rc;
667
668 rc = pseries_nvram_init_os_partition(&rtas_log_partition);
669 nvram_init_oops_partition(rc == 0);
Jim Keniston0f4ac132011-02-09 12:43:13 +0000670 return 0;
671}
672machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674int __init pSeries_nvram_init(void)
675{
676 struct device_node *nvram;
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000677 const unsigned int *nbytes_p;
678 unsigned int proplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 nvram = of_find_node_by_type(NULL, "nvram");
681 if (nvram == NULL)
682 return -ENODEV;
683
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000684 nbytes_p = of_get_property(nvram, "#bytes", &proplen);
Julia Lawallbad52322008-06-09 22:20:04 +1000685 if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
686 of_node_put(nvram);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -EIO;
Julia Lawallbad52322008-06-09 22:20:04 +1000688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 nvram_size = *nbytes_p;
691
692 nvram_fetch = rtas_token("nvram-fetch");
693 nvram_store = rtas_token("nvram-store");
694 printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
695 of_node_put(nvram);
696
697 ppc_md.nvram_read = pSeries_nvram_read;
698 ppc_md.nvram_write = pSeries_nvram_write;
699 ppc_md.nvram_size = pSeries_nvram_get_size;
700
701 return 0;
702}
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000703
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000704
Jim Keniston6c493682011-07-25 07:54:50 +0000705/* Derived from logfs_compress() */
706static int nvram_compress(const void *in, void *out, size_t inlen,
707 size_t outlen)
708{
709 int err, ret;
710
711 ret = -EIO;
712 err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
713 MEM_LEVEL, Z_DEFAULT_STRATEGY);
714 if (err != Z_OK)
715 goto error;
716
717 stream.next_in = in;
718 stream.avail_in = inlen;
719 stream.total_in = 0;
720 stream.next_out = out;
721 stream.avail_out = outlen;
722 stream.total_out = 0;
723
724 err = zlib_deflate(&stream, Z_FINISH);
725 if (err != Z_STREAM_END)
726 goto error;
727
728 err = zlib_deflateEnd(&stream);
729 if (err != Z_OK)
730 goto error;
731
732 if (stream.total_out >= stream.total_in)
733 goto error;
734
735 ret = stream.total_out;
736error:
737 return ret;
738}
739
740/* Compress the text from big_oops_buf into oops_buf. */
741static int zip_oops(size_t text_len)
742{
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530743 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
Jim Keniston6c493682011-07-25 07:54:50 +0000744 int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len,
745 oops_data_sz);
746 if (zipped_len < 0) {
747 pr_err("nvram: compression failed; returned %d\n", zipped_len);
748 pr_err("nvram: logging uncompressed oops/panic report\n");
749 return -1;
750 }
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530751 oops_hdr->version = OOPS_HDR_VERSION;
752 oops_hdr->report_length = (u16) zipped_len;
753 oops_hdr->timestamp = get_seconds();
Jim Keniston6c493682011-07-25 07:54:50 +0000754 return 0;
755}
756
757/*
758 * This is our kmsg_dump callback, called after an oops or panic report
759 * has been written to the printk buffer. We want to capture as much
760 * of the printk buffer as possible. First, capture as much as we can
761 * that we think will compress sufficiently to fit in the lnx,oops-log
762 * partition. If that's too much, go back and capture uncompressed text.
763 */
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000764static void oops_to_nvram(struct kmsg_dumper *dumper,
Kay Sieverse2ae7152012-06-15 14:07:51 +0200765 enum kmsg_dump_reason reason)
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000766{
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530767 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000768 static unsigned int oops_count = 0;
Jim Keniston15d260b2011-03-25 12:47:58 +0000769 static bool panicking = false;
Anton Blanchard120a52c2011-11-30 15:46:45 +0000770 static DEFINE_SPINLOCK(lock);
771 unsigned long flags;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000772 size_t text_len;
Jim Keniston6c493682011-07-25 07:54:50 +0000773 unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ;
774 int rc = -1;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000775
Jim Keniston15d260b2011-03-25 12:47:58 +0000776 switch (reason) {
777 case KMSG_DUMP_RESTART:
778 case KMSG_DUMP_HALT:
779 case KMSG_DUMP_POWEROFF:
780 /* These are almost always orderly shutdowns. */
781 return;
782 case KMSG_DUMP_OOPS:
Jim Keniston15d260b2011-03-25 12:47:58 +0000783 break;
784 case KMSG_DUMP_PANIC:
785 panicking = true;
786 break;
787 case KMSG_DUMP_EMERG:
788 if (panicking)
789 /* Panic report already captured. */
790 return;
791 break;
792 default:
793 pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
794 __FUNCTION__, (int) reason);
795 return;
796 }
797
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000798 if (clobbering_unread_rtas_event())
799 return;
800
Anton Blanchard120a52c2011-11-30 15:46:45 +0000801 if (!spin_trylock_irqsave(&lock, flags))
802 return;
803
Jim Keniston6c493682011-07-25 07:54:50 +0000804 if (big_oops_buf) {
Kay Sieverse2ae7152012-06-15 14:07:51 +0200805 kmsg_dump_get_buffer(dumper, false,
806 big_oops_buf, big_oops_buf_sz, &text_len);
Jim Keniston6c493682011-07-25 07:54:50 +0000807 rc = zip_oops(text_len);
808 }
809 if (rc != 0) {
Kay Sieverse2ae7152012-06-15 14:07:51 +0200810 kmsg_dump_rewind(dumper);
Aruna Balakrishnaiah1bf247f2013-06-06 00:20:55 +0530811 kmsg_dump_get_buffer(dumper, false,
Kay Sieverse2ae7152012-06-15 14:07:51 +0200812 oops_data, oops_data_sz, &text_len);
Jim Keniston6c493682011-07-25 07:54:50 +0000813 err_type = ERR_TYPE_KERNEL_PANIC;
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530814 oops_hdr->version = OOPS_HDR_VERSION;
815 oops_hdr->report_length = (u16) text_len;
816 oops_hdr->timestamp = get_seconds();
Jim Keniston6c493682011-07-25 07:54:50 +0000817 }
818
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000819 (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530820 (int) (sizeof(*oops_hdr) + oops_hdr->report_length), err_type,
821 ++oops_count);
Anton Blanchard120a52c2011-11-30 15:46:45 +0000822
823 spin_unlock_irqrestore(&lock, flags);
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000824}