blob: 714ed8ac7d59a79064a59cd5ff47d8ff4912ed71 [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 */
Aruna Balakrishnaiahedf38462013-06-06 00:21:59 +053056 bool os_partition; /* partition initialized by OS, not FW */
Jim Keniston0f4ac132011-02-09 12:43:13 +000057};
58
59static struct nvram_os_partition rtas_log_partition = {
60 .name = "ibm,rtas-log",
61 .req_size = 2079,
62 .min_size = 1055,
Aruna Balakrishnaiahedf38462013-06-06 00:21:59 +053063 .index = -1,
64 .os_partition = true
Jim Keniston0f4ac132011-02-09 12:43:13 +000065};
66
Jim Kenistona5cf4b02011-02-09 13:00:09 +000067static struct nvram_os_partition oops_log_partition = {
68 .name = "lnx,oops-log",
69 .req_size = 4000,
70 .min_size = 2000,
Aruna Balakrishnaiahedf38462013-06-06 00:21:59 +053071 .index = -1,
72 .os_partition = true
Jim Kenistona5cf4b02011-02-09 13:00:09 +000073};
74
Jim Keniston0f4ac132011-02-09 12:43:13 +000075static const char *pseries_nvram_os_partitions[] = {
76 "ibm,rtas-log",
Jim Kenistona5cf4b02011-02-09 13:00:09 +000077 "lnx,oops-log",
Jim Keniston0f4ac132011-02-09 12:43:13 +000078 NULL
79};
Benjamin Herrenschmidt9a866b82010-08-02 10:51:25 +100080
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +053081struct oops_log_info {
82 u16 version;
83 u16 report_length;
84 u64 timestamp;
85} __attribute__((packed));
86
Jim Kenistona5cf4b02011-02-09 13:00:09 +000087static void oops_to_nvram(struct kmsg_dumper *dumper,
Kay Sieverse2ae7152012-06-15 14:07:51 +020088 enum kmsg_dump_reason reason);
Jim Kenistona5cf4b02011-02-09 13:00:09 +000089
90static struct kmsg_dumper nvram_kmsg_dumper = {
91 .dump = oops_to_nvram
92};
93
94/* See clobbering_unread_rtas_event() */
95#define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */
96static unsigned long last_unread_rtas_event; /* timestamp */
97
Jim Keniston6c493682011-07-25 07:54:50 +000098/*
99 * For capturing and compressing an oops or panic report...
100
101 * big_oops_buf[] holds the uncompressed text we're capturing.
102 *
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530103 * 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.
Jim Keniston6c493682011-07-25 07:54:50 +0000108 *
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530109 * oops_log_info points to the header. oops_data points to the compressed text.
Jim Keniston6c493682011-07-25 07:54:50 +0000110 *
111 * +- oops_buf
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530112 * | +- oops_data
113 * v v
114 * +-----------+-----------+-----------+------------------------+
115 * | version | length | timestamp | text |
116 * | (2 bytes) | (2 bytes) | (8 bytes) | (oops_data_sz bytes) |
117 * +-----------+-----------+-----------+------------------------+
Jim Keniston6c493682011-07-25 07:54:50 +0000118 * ^
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530119 * +- oops_log_info
Jim Keniston6c493682011-07-25 07:54:50 +0000120 *
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;
Jim Keniston6c493682011-07-25 07:54:50 +0000125static 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;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000133
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530134#ifdef CONFIG_PSTORE
135static enum pstore_type_id nvram_type_ids[] = {
136 PSTORE_TYPE_DMESG,
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530137 PSTORE_TYPE_PPC_RTAS,
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530138 -1
139};
140static int read_type;
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530141static unsigned long last_rtas_event;
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
145{
146 unsigned int i;
147 unsigned long len;
148 int done;
149 unsigned long flags;
150 char *p = buf;
151
152
153 if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
154 return -ENODEV;
155
156 if (*index >= nvram_size)
157 return 0;
158
159 i = *index;
160 if (i + count > nvram_size)
161 count = nvram_size - i;
162
163 spin_lock_irqsave(&nvram_lock, flags);
164
165 for (; count != 0; count -= len) {
166 len = count;
167 if (len > NVRW_CNT)
168 len = NVRW_CNT;
169
170 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
171 len) != 0) || len != done) {
172 spin_unlock_irqrestore(&nvram_lock, flags);
173 return -EIO;
174 }
175
176 memcpy(p, nvram_buf, len);
177
178 p += len;
179 i += len;
180 }
181
182 spin_unlock_irqrestore(&nvram_lock, flags);
183
184 *index = i;
185 return p - buf;
186}
187
188static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
189{
190 unsigned int i;
191 unsigned long len;
192 int done;
193 unsigned long flags;
194 const char *p = buf;
195
196 if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
197 return -ENODEV;
198
199 if (*index >= nvram_size)
200 return 0;
201
202 i = *index;
203 if (i + count > nvram_size)
204 count = nvram_size - i;
205
206 spin_lock_irqsave(&nvram_lock, flags);
207
208 for (; count != 0; count -= len) {
209 len = count;
210 if (len > NVRW_CNT)
211 len = NVRW_CNT;
212
213 memcpy(nvram_buf, p, len);
214
215 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
216 len) != 0) || len != done) {
217 spin_unlock_irqrestore(&nvram_lock, flags);
218 return -EIO;
219 }
220
221 p += len;
222 i += len;
223 }
224 spin_unlock_irqrestore(&nvram_lock, flags);
225
226 *index = i;
227 return p - buf;
228}
229
230static ssize_t pSeries_nvram_get_size(void)
231{
232 return nvram_size ? nvram_size : -ENODEV;
233}
234
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000235
Jim Keniston0f4ac132011-02-09 12:43:13 +0000236/* nvram_write_os_partition, nvram_write_error_log
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000237 *
238 * We need to buffer the error logs into nvram to ensure that we have
239 * the failure information to decode. If we have a severe error there
240 * is no way to guarantee that the OS or the machine is in a state to
241 * get back to user land and write the error to disk. For example if
242 * the SCSI device driver causes a Machine Check by writing to a bad
243 * IO address, there is no way of guaranteeing that the device driver
244 * is in any state that is would also be able to write the error data
245 * captured to disk, thus we buffer it in NVRAM for analysis on the
246 * next boot.
247 *
248 * In NVRAM the partition containing the error log buffer will looks like:
249 * Header (in bytes):
250 * +-----------+----------+--------+------------+------------------+
251 * | signature | checksum | length | name | data |
252 * |0 |1 |2 3|4 15|16 length-1|
253 * +-----------+----------+--------+------------+------------------+
254 *
255 * The 'data' section would look like (in bytes):
256 * +--------------+------------+-----------------------------------+
257 * | event_logged | sequence # | error log |
Jim Keniston0f4ac132011-02-09 12:43:13 +0000258 * |0 3|4 7|8 error_log_size-1|
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000259 * +--------------+------------+-----------------------------------+
260 *
261 * event_logged: 0 if event has not been logged to syslog, 1 if it has
262 * sequence #: The unique sequence # for each event. (until it wraps)
263 * error log: The error log from event_scan
264 */
Jim Keniston0f4ac132011-02-09 12:43:13 +0000265int nvram_write_os_partition(struct nvram_os_partition *part, char * buff,
266 int length, unsigned int err_type, unsigned int error_log_cnt)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000267{
268 int rc;
269 loff_t tmp_index;
270 struct err_log_info info;
271
Jim Keniston0f4ac132011-02-09 12:43:13 +0000272 if (part->index == -1) {
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000273 return -ESPIPE;
274 }
275
Jim Keniston0f4ac132011-02-09 12:43:13 +0000276 if (length > part->size) {
277 length = part->size;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000278 }
279
280 info.error_type = err_type;
281 info.seq_num = error_log_cnt;
282
Jim Keniston0f4ac132011-02-09 12:43:13 +0000283 tmp_index = part->index;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000284
285 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
286 if (rc <= 0) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000287 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000288 return rc;
289 }
290
291 rc = ppc_md.nvram_write(buff, length, &tmp_index);
292 if (rc <= 0) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000293 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000294 return rc;
295 }
296
297 return 0;
298}
299
Jim Keniston0f4ac132011-02-09 12:43:13 +0000300int nvram_write_error_log(char * buff, int length,
301 unsigned int err_type, unsigned int error_log_cnt)
302{
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000303 int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,
Jim Keniston0f4ac132011-02-09 12:43:13 +0000304 err_type, error_log_cnt);
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530305 if (!rc) {
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000306 last_unread_rtas_event = get_seconds();
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530307#ifdef CONFIG_PSTORE
308 last_rtas_event = get_seconds();
309#endif
310 }
311
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000312 return rc;
Jim Keniston0f4ac132011-02-09 12:43:13 +0000313}
314
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530315/* nvram_read_partition
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000316 *
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530317 * Reads nvram partition for at most 'length'
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000318 */
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530319int nvram_read_partition(struct nvram_os_partition *part, char *buff,
320 int length, unsigned int *err_type,
321 unsigned int *error_log_cnt)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000322{
323 int rc;
324 loff_t tmp_index;
325 struct err_log_info info;
326
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530327 if (part->index == -1)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000328 return -1;
329
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530330 if (length > part->size)
331 length = part->size;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000332
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530333 tmp_index = part->index;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000334
335 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
336 if (rc <= 0) {
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530337 pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000338 return rc;
339 }
340
341 rc = ppc_md.nvram_read(buff, length, &tmp_index);
342 if (rc <= 0) {
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530343 pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__, rc);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000344 return rc;
345 }
346
347 *error_log_cnt = info.seq_num;
348 *err_type = info.error_type;
349
350 return 0;
351}
352
Aruna Balakrishnaiah12674612013-06-06 00:21:16 +0530353/* nvram_read_error_log
354 *
355 * Reads nvram for error log for at most 'length'
356 */
357int nvram_read_error_log(char *buff, int length,
358 unsigned int *err_type, unsigned int *error_log_cnt)
359{
360 return nvram_read_partition(&rtas_log_partition, buff, length,
361 err_type, error_log_cnt);
362}
363
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000364/* This doesn't actually zero anything, but it sets the event_logged
365 * word to tell that this event is safely in syslog.
366 */
367int nvram_clear_error_log(void)
368{
369 loff_t tmp_index;
370 int clear_word = ERR_FLAG_ALREADY_LOGGED;
371 int rc;
372
Jim Keniston0f4ac132011-02-09 12:43:13 +0000373 if (rtas_log_partition.index == -1)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000374 return -1;
375
Jim Keniston0f4ac132011-02-09 12:43:13 +0000376 tmp_index = rtas_log_partition.index;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000377
378 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
379 if (rc <= 0) {
380 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
381 return rc;
382 }
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000383 last_unread_rtas_event = 0;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000384
385 return 0;
386}
387
Jim Keniston0f4ac132011-02-09 12:43:13 +0000388/* pseries_nvram_init_os_partition
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000389 *
Jim Keniston0f4ac132011-02-09 12:43:13 +0000390 * This sets up a partition with an "OS" signature.
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000391 *
392 * The general strategy is the following:
Jim Keniston0f4ac132011-02-09 12:43:13 +0000393 * 1.) If a partition with the indicated name already exists...
394 * - If it's large enough, use it.
395 * - Otherwise, recycle it and keep going.
396 * 2.) Search for a free partition that is large enough.
397 * 3.) If there's not a free partition large enough, recycle any obsolete
398 * OS partitions and try again.
399 * 4.) Will first try getting a chunk that will satisfy the requested size.
400 * 5.) If a chunk of the requested size cannot be allocated, then try finding
401 * a chunk that will satisfy the minum needed.
402 *
403 * Returns 0 on success, else -1.
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000404 */
Jim Keniston0f4ac132011-02-09 12:43:13 +0000405static int __init pseries_nvram_init_os_partition(struct nvram_os_partition
406 *part)
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000407{
408 loff_t p;
409 int size;
410
411 /* Scan nvram for partitions */
412 nvram_scan_partitions();
413
Jim Keniston0f4ac132011-02-09 12:43:13 +0000414 /* Look for ours */
415 p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000416
417 /* Found one but too small, remove it */
Jim Keniston0f4ac132011-02-09 12:43:13 +0000418 if (p && size < part->min_size) {
419 pr_info("nvram: Found too small %s partition,"
420 " removing it...\n", part->name);
421 nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000422 p = 0;
423 }
424
425 /* Create one if we didn't find */
426 if (!p) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000427 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
428 part->req_size, part->min_size);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000429 if (p == -ENOSPC) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000430 pr_info("nvram: No room to create %s partition, "
431 "deleting any obsolete OS partitions...\n",
432 part->name);
433 nvram_remove_partition(NULL, NVRAM_SIG_OS,
434 pseries_nvram_os_partitions);
435 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
436 part->req_size, part->min_size);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000437 }
438 }
439
440 if (p <= 0) {
Jim Keniston0f4ac132011-02-09 12:43:13 +0000441 pr_err("nvram: Failed to find or create %s"
442 " partition, err %d\n", part->name, (int)p);
443 return -1;
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000444 }
445
Jim Keniston0f4ac132011-02-09 12:43:13 +0000446 part->index = p;
447 part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000448
449 return 0;
450}
Jim Keniston0f4ac132011-02-09 12:43:13 +0000451
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530452/*
453 * Are we using the ibm,rtas-log for oops/panic reports? And if so,
454 * would logging this oops/panic overwrite an RTAS event that rtas_errd
455 * hasn't had a chance to read and process? Return 1 if so, else 0.
456 *
457 * We assume that if rtas_errd hasn't read the RTAS event in
458 * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to.
459 */
460static int clobbering_unread_rtas_event(void)
461{
462 return (oops_log_partition.index == rtas_log_partition.index
463 && last_unread_rtas_event
464 && get_seconds() - last_unread_rtas_event <=
465 NVRAM_RTAS_READ_TIMEOUT);
466}
467
468#ifdef CONFIG_PSTORE
469static int nvram_pstore_open(struct pstore_info *psi)
470{
471 /* Reset the iterator to start reading partitions again */
472 read_type = -1;
473 return 0;
474}
475
476/**
477 * nvram_pstore_write - pstore write callback for nvram
478 * @type: Type of message logged
479 * @reason: reason behind dump (oops/panic)
480 * @id: identifier to indicate the write performed
481 * @part: pstore writes data to registered buffer in parts,
482 * part number will indicate the same.
483 * @count: Indicates oops count
484 * @size: number of bytes written to the registered buffer
485 * @psi: registered pstore_info structure
486 *
487 * Called by pstore_dump() when an oops or panic report is logged in the
488 * printk buffer.
489 * Returns 0 on successful write.
490 */
491static int nvram_pstore_write(enum pstore_type_id type,
492 enum kmsg_dump_reason reason,
493 u64 *id, unsigned int part, int count,
494 size_t size, struct pstore_info *psi)
495{
496 int rc;
497 struct oops_log_info *oops_hdr = (struct oops_log_info *) oops_buf;
498
499 /* part 1 has the recent messages from printk buffer */
500 if (part > 1 || type != PSTORE_TYPE_DMESG ||
501 clobbering_unread_rtas_event())
502 return -1;
503
504 oops_hdr->version = OOPS_HDR_VERSION;
505 oops_hdr->report_length = (u16) size;
506 oops_hdr->timestamp = get_seconds();
507 rc = nvram_write_os_partition(&oops_log_partition, oops_buf,
508 (int) (sizeof(*oops_hdr) + size), ERR_TYPE_KERNEL_PANIC,
509 count);
510
511 if (rc != 0)
512 return rc;
513
514 *id = part;
515 return 0;
516}
517
518/*
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530519 * Reads the oops/panic report and ibm,rtas-log partition.
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530520 * Returns the length of the data we read from each partition.
521 * Returns 0 if we've been called before.
522 */
523static ssize_t nvram_pstore_read(u64 *id, enum pstore_type_id *type,
524 int *count, struct timespec *time, char **buf,
525 struct pstore_info *psi)
526{
527 struct oops_log_info *oops_hdr;
528 unsigned int err_type, id_no;
529 struct nvram_os_partition *part = NULL;
530 char *buff = NULL;
531
532 read_type++;
533
534 switch (nvram_type_ids[read_type]) {
535 case PSTORE_TYPE_DMESG:
536 part = &oops_log_partition;
537 *type = PSTORE_TYPE_DMESG;
538 break;
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530539 case PSTORE_TYPE_PPC_RTAS:
540 part = &rtas_log_partition;
541 *type = PSTORE_TYPE_PPC_RTAS;
542 time->tv_sec = last_rtas_event;
543 time->tv_nsec = 0;
544 break;
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530545 default:
546 return 0;
547 }
548
549 buff = kmalloc(part->size, GFP_KERNEL);
550
551 if (!buff)
552 return -ENOMEM;
553
554 if (nvram_read_partition(part, buff, part->size, &err_type, &id_no)) {
555 kfree(buff);
556 return 0;
557 }
558
559 *count = 0;
560 *id = id_no;
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530561
562 if (nvram_type_ids[read_type] == PSTORE_TYPE_DMESG) {
563 oops_hdr = (struct oops_log_info *)buff;
564 *buf = buff + sizeof(*oops_hdr);
565 time->tv_sec = oops_hdr->timestamp;
566 time->tv_nsec = 0;
567 return oops_hdr->report_length;
568 }
569
570 *buf = buff;
571 return part->size;
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530572}
573
574static struct pstore_info nvram_pstore_info = {
575 .owner = THIS_MODULE,
576 .name = "nvram",
577 .open = nvram_pstore_open,
578 .read = nvram_pstore_read,
579 .write = nvram_pstore_write,
580};
581
582static int nvram_pstore_init(void)
583{
584 int rc = 0;
585
586 nvram_pstore_info.buf = oops_data;
587 nvram_pstore_info.bufsize = oops_data_sz;
588
589 rc = pstore_register(&nvram_pstore_info);
590 if (rc != 0)
591 pr_err("nvram: pstore_register() failed, defaults to "
592 "kmsg_dump; returned %d\n", rc);
593 else
594 /*TODO: Support compression when pstore is configured */
595 pr_info("nvram: Compression of oops text supported only when "
596 "pstore is not configured");
597
598 return rc;
599}
600#else
601static int nvram_pstore_init(void)
602{
603 return -1;
604}
605#endif
606
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000607static void __init nvram_init_oops_partition(int rtas_partition_exists)
608{
609 int rc;
610
611 rc = pseries_nvram_init_os_partition(&oops_log_partition);
612 if (rc != 0) {
613 if (!rtas_partition_exists)
614 return;
615 pr_notice("nvram: Using %s partition to log both"
616 " RTAS errors and oops/panic reports\n",
617 rtas_log_partition.name);
618 memcpy(&oops_log_partition, &rtas_log_partition,
619 sizeof(rtas_log_partition));
620 }
621 oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
Jim Keniston6c493682011-07-25 07:54:50 +0000622 if (!oops_buf) {
623 pr_err("nvram: No memory for %s partition\n",
624 oops_log_partition.name);
625 return;
626 }
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530627 oops_data = oops_buf + sizeof(struct oops_log_info);
628 oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info);
Jim Keniston6c493682011-07-25 07:54:50 +0000629
Aruna Balakrishnaiahd7563c92013-06-06 00:21:32 +0530630 rc = nvram_pstore_init();
631
632 if (!rc)
633 return;
634
Jim Keniston6c493682011-07-25 07:54:50 +0000635 /*
636 * Figure compression (preceded by elimination of each line's <n>
637 * severity prefix) will reduce the oops/panic report to at most
638 * 45% of its original size.
639 */
640 big_oops_buf_sz = (oops_data_sz * 100) / 45;
641 big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
642 if (big_oops_buf) {
643 stream.workspace = kmalloc(zlib_deflate_workspacesize(
644 WINDOW_BITS, MEM_LEVEL), GFP_KERNEL);
645 if (!stream.workspace) {
646 pr_err("nvram: No memory for compression workspace; "
647 "skipping compression of %s partition data\n",
648 oops_log_partition.name);
649 kfree(big_oops_buf);
650 big_oops_buf = NULL;
651 }
652 } else {
653 pr_err("No memory for uncompressed %s data; "
654 "skipping compression\n", oops_log_partition.name);
655 stream.workspace = NULL;
656 }
657
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000658 rc = kmsg_dump_register(&nvram_kmsg_dumper);
659 if (rc != 0) {
660 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
661 kfree(oops_buf);
Jim Keniston6c493682011-07-25 07:54:50 +0000662 kfree(big_oops_buf);
663 kfree(stream.workspace);
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000664 }
665}
666
Jim Keniston0f4ac132011-02-09 12:43:13 +0000667static int __init pseries_nvram_init_log_partitions(void)
668{
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000669 int rc;
670
671 rc = pseries_nvram_init_os_partition(&rtas_log_partition);
672 nvram_init_oops_partition(rc == 0);
Jim Keniston0f4ac132011-02-09 12:43:13 +0000673 return 0;
674}
675machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
Benjamin Herrenschmidtedc79a22010-08-02 11:18:09 +1000676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677int __init pSeries_nvram_init(void)
678{
679 struct device_node *nvram;
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000680 const unsigned int *nbytes_p;
681 unsigned int proplen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 nvram = of_find_node_by_type(NULL, "nvram");
684 if (nvram == NULL)
685 return -ENODEV;
686
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000687 nbytes_p = of_get_property(nvram, "#bytes", &proplen);
Julia Lawallbad52322008-06-09 22:20:04 +1000688 if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
689 of_node_put(nvram);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -EIO;
Julia Lawallbad52322008-06-09 22:20:04 +1000691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 nvram_size = *nbytes_p;
694
695 nvram_fetch = rtas_token("nvram-fetch");
696 nvram_store = rtas_token("nvram-store");
697 printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
698 of_node_put(nvram);
699
700 ppc_md.nvram_read = pSeries_nvram_read;
701 ppc_md.nvram_write = pSeries_nvram_write;
702 ppc_md.nvram_size = pSeries_nvram_get_size;
703
704 return 0;
705}
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000706
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000707
Jim Keniston6c493682011-07-25 07:54:50 +0000708/* Derived from logfs_compress() */
709static int nvram_compress(const void *in, void *out, size_t inlen,
710 size_t outlen)
711{
712 int err, ret;
713
714 ret = -EIO;
715 err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
716 MEM_LEVEL, Z_DEFAULT_STRATEGY);
717 if (err != Z_OK)
718 goto error;
719
720 stream.next_in = in;
721 stream.avail_in = inlen;
722 stream.total_in = 0;
723 stream.next_out = out;
724 stream.avail_out = outlen;
725 stream.total_out = 0;
726
727 err = zlib_deflate(&stream, Z_FINISH);
728 if (err != Z_STREAM_END)
729 goto error;
730
731 err = zlib_deflateEnd(&stream);
732 if (err != Z_OK)
733 goto error;
734
735 if (stream.total_out >= stream.total_in)
736 goto error;
737
738 ret = stream.total_out;
739error:
740 return ret;
741}
742
743/* Compress the text from big_oops_buf into oops_buf. */
744static int zip_oops(size_t text_len)
745{
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530746 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
Jim Keniston6c493682011-07-25 07:54:50 +0000747 int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len,
748 oops_data_sz);
749 if (zipped_len < 0) {
750 pr_err("nvram: compression failed; returned %d\n", zipped_len);
751 pr_err("nvram: logging uncompressed oops/panic report\n");
752 return -1;
753 }
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530754 oops_hdr->version = OOPS_HDR_VERSION;
755 oops_hdr->report_length = (u16) zipped_len;
756 oops_hdr->timestamp = get_seconds();
Jim Keniston6c493682011-07-25 07:54:50 +0000757 return 0;
758}
759
760/*
761 * This is our kmsg_dump callback, called after an oops or panic report
762 * has been written to the printk buffer. We want to capture as much
763 * of the printk buffer as possible. First, capture as much as we can
764 * that we think will compress sufficiently to fit in the lnx,oops-log
765 * partition. If that's too much, go back and capture uncompressed text.
766 */
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000767static void oops_to_nvram(struct kmsg_dumper *dumper,
Kay Sieverse2ae7152012-06-15 14:07:51 +0200768 enum kmsg_dump_reason reason)
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000769{
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530770 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000771 static unsigned int oops_count = 0;
Jim Keniston15d260b2011-03-25 12:47:58 +0000772 static bool panicking = false;
Anton Blanchard120a52c2011-11-30 15:46:45 +0000773 static DEFINE_SPINLOCK(lock);
774 unsigned long flags;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000775 size_t text_len;
Jim Keniston6c493682011-07-25 07:54:50 +0000776 unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ;
777 int rc = -1;
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000778
Jim Keniston15d260b2011-03-25 12:47:58 +0000779 switch (reason) {
780 case KMSG_DUMP_RESTART:
781 case KMSG_DUMP_HALT:
782 case KMSG_DUMP_POWEROFF:
783 /* These are almost always orderly shutdowns. */
784 return;
785 case KMSG_DUMP_OOPS:
Jim Keniston15d260b2011-03-25 12:47:58 +0000786 break;
787 case KMSG_DUMP_PANIC:
788 panicking = true;
789 break;
790 case KMSG_DUMP_EMERG:
791 if (panicking)
792 /* Panic report already captured. */
793 return;
794 break;
795 default:
796 pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
797 __FUNCTION__, (int) reason);
798 return;
799 }
800
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000801 if (clobbering_unread_rtas_event())
802 return;
803
Anton Blanchard120a52c2011-11-30 15:46:45 +0000804 if (!spin_trylock_irqsave(&lock, flags))
805 return;
806
Jim Keniston6c493682011-07-25 07:54:50 +0000807 if (big_oops_buf) {
Kay Sieverse2ae7152012-06-15 14:07:51 +0200808 kmsg_dump_get_buffer(dumper, false,
809 big_oops_buf, big_oops_buf_sz, &text_len);
Jim Keniston6c493682011-07-25 07:54:50 +0000810 rc = zip_oops(text_len);
811 }
812 if (rc != 0) {
Kay Sieverse2ae7152012-06-15 14:07:51 +0200813 kmsg_dump_rewind(dumper);
Aruna Balakrishnaiah1bf247f2013-06-06 00:20:55 +0530814 kmsg_dump_get_buffer(dumper, false,
Kay Sieverse2ae7152012-06-15 14:07:51 +0200815 oops_data, oops_data_sz, &text_len);
Jim Keniston6c493682011-07-25 07:54:50 +0000816 err_type = ERR_TYPE_KERNEL_PANIC;
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530817 oops_hdr->version = OOPS_HDR_VERSION;
818 oops_hdr->report_length = (u16) text_len;
819 oops_hdr->timestamp = get_seconds();
Jim Keniston6c493682011-07-25 07:54:50 +0000820 }
821
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000822 (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
Aruna Balakrishnaiahb1f70e12013-06-06 00:21:05 +0530823 (int) (sizeof(*oops_hdr) + oops_hdr->report_length), err_type,
824 ++oops_count);
Anton Blanchard120a52c2011-11-30 15:46:45 +0000825
826 spin_unlock_irqrestore(&lock, flags);
Jim Kenistona5cf4b02011-02-09 13:00:09 +0000827}