2 * c 2001 PPC 64 Team, IBM Corp
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.
9 * /dev/nvram driver for PPC64
11 * This perhaps should live in drivers/char
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
19 #include <linux/slab.h>
20 #include <linux/kmsg_dump.h>
21 #include <asm/uaccess.h>
22 #include <asm/nvram.h>
25 #include <asm/machdep.h>
27 /* Max bytes to read/write in one go */
30 static unsigned int nvram_size;
31 static int nvram_fetch, nvram_store;
32 static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */
33 static DEFINE_SPINLOCK(nvram_lock);
40 struct nvram_os_partition {
42 int req_size; /* desired size, in bytes */
43 int min_size; /* minimum acceptable size (0 means req_size) */
44 long size; /* size of data portion (excluding err_log_info) */
45 long index; /* offset of data portion of partition */
48 static struct nvram_os_partition rtas_log_partition = {
49 .name = "ibm,rtas-log",
55 static struct nvram_os_partition oops_log_partition = {
56 .name = "lnx,oops-log",
62 static const char *pseries_nvram_os_partitions[] = {
68 static void oops_to_nvram(struct kmsg_dumper *dumper,
69 enum kmsg_dump_reason reason,
70 const char *old_msgs, unsigned long old_len,
71 const char *new_msgs, unsigned long new_len);
73 static struct kmsg_dumper nvram_kmsg_dumper = {
77 /* See clobbering_unread_rtas_event() */
78 #define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */
79 static unsigned long last_unread_rtas_event; /* timestamp */
81 /* We preallocate oops_buf during init to avoid kmalloc during oops/panic. */
82 static char *oops_buf;
84 static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
93 if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
96 if (*index >= nvram_size)
100 if (i + count > nvram_size)
101 count = nvram_size - i;
103 spin_lock_irqsave(&nvram_lock, flags);
105 for (; count != 0; count -= len) {
110 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
111 len) != 0) || len != done) {
112 spin_unlock_irqrestore(&nvram_lock, flags);
116 memcpy(p, nvram_buf, len);
122 spin_unlock_irqrestore(&nvram_lock, flags);
128 static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
136 if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
139 if (*index >= nvram_size)
143 if (i + count > nvram_size)
144 count = nvram_size - i;
146 spin_lock_irqsave(&nvram_lock, flags);
148 for (; count != 0; count -= len) {
153 memcpy(nvram_buf, p, len);
155 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
156 len) != 0) || len != done) {
157 spin_unlock_irqrestore(&nvram_lock, flags);
164 spin_unlock_irqrestore(&nvram_lock, flags);
170 static ssize_t pSeries_nvram_get_size(void)
172 return nvram_size ? nvram_size : -ENODEV;
176 /* nvram_write_os_partition, nvram_write_error_log
178 * We need to buffer the error logs into nvram to ensure that we have
179 * the failure information to decode. If we have a severe error there
180 * is no way to guarantee that the OS or the machine is in a state to
181 * get back to user land and write the error to disk. For example if
182 * the SCSI device driver causes a Machine Check by writing to a bad
183 * IO address, there is no way of guaranteeing that the device driver
184 * is in any state that is would also be able to write the error data
185 * captured to disk, thus we buffer it in NVRAM for analysis on the
188 * In NVRAM the partition containing the error log buffer will looks like:
190 * +-----------+----------+--------+------------+------------------+
191 * | signature | checksum | length | name | data |
192 * |0 |1 |2 3|4 15|16 length-1|
193 * +-----------+----------+--------+------------+------------------+
195 * The 'data' section would look like (in bytes):
196 * +--------------+------------+-----------------------------------+
197 * | event_logged | sequence # | error log |
198 * |0 3|4 7|8 error_log_size-1|
199 * +--------------+------------+-----------------------------------+
201 * event_logged: 0 if event has not been logged to syslog, 1 if it has
202 * sequence #: The unique sequence # for each event. (until it wraps)
203 * error log: The error log from event_scan
205 int nvram_write_os_partition(struct nvram_os_partition *part, char * buff,
206 int length, unsigned int err_type, unsigned int error_log_cnt)
210 struct err_log_info info;
212 if (part->index == -1) {
216 if (length > part->size) {
220 info.error_type = err_type;
221 info.seq_num = error_log_cnt;
223 tmp_index = part->index;
225 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
227 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
231 rc = ppc_md.nvram_write(buff, length, &tmp_index);
233 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
240 int nvram_write_error_log(char * buff, int length,
241 unsigned int err_type, unsigned int error_log_cnt)
243 int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,
244 err_type, error_log_cnt);
246 last_unread_rtas_event = get_seconds();
250 /* nvram_read_error_log
252 * Reads nvram for error log for at most 'length'
254 int nvram_read_error_log(char * buff, int length,
255 unsigned int * err_type, unsigned int * error_log_cnt)
259 struct err_log_info info;
261 if (rtas_log_partition.index == -1)
264 if (length > rtas_log_partition.size)
265 length = rtas_log_partition.size;
267 tmp_index = rtas_log_partition.index;
269 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
271 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
275 rc = ppc_md.nvram_read(buff, length, &tmp_index);
277 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
281 *error_log_cnt = info.seq_num;
282 *err_type = info.error_type;
287 /* This doesn't actually zero anything, but it sets the event_logged
288 * word to tell that this event is safely in syslog.
290 int nvram_clear_error_log(void)
293 int clear_word = ERR_FLAG_ALREADY_LOGGED;
296 if (rtas_log_partition.index == -1)
299 tmp_index = rtas_log_partition.index;
301 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
303 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
306 last_unread_rtas_event = 0;
311 /* pseries_nvram_init_os_partition
313 * This sets up a partition with an "OS" signature.
315 * The general strategy is the following:
316 * 1.) If a partition with the indicated name already exists...
317 * - If it's large enough, use it.
318 * - Otherwise, recycle it and keep going.
319 * 2.) Search for a free partition that is large enough.
320 * 3.) If there's not a free partition large enough, recycle any obsolete
321 * OS partitions and try again.
322 * 4.) Will first try getting a chunk that will satisfy the requested size.
323 * 5.) If a chunk of the requested size cannot be allocated, then try finding
324 * a chunk that will satisfy the minum needed.
326 * Returns 0 on success, else -1.
328 static int __init pseries_nvram_init_os_partition(struct nvram_os_partition
334 /* Scan nvram for partitions */
335 nvram_scan_partitions();
338 p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size);
340 /* Found one but too small, remove it */
341 if (p && size < part->min_size) {
342 pr_info("nvram: Found too small %s partition,"
343 " removing it...\n", part->name);
344 nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL);
348 /* Create one if we didn't find */
350 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
351 part->req_size, part->min_size);
353 pr_info("nvram: No room to create %s partition, "
354 "deleting any obsolete OS partitions...\n",
356 nvram_remove_partition(NULL, NVRAM_SIG_OS,
357 pseries_nvram_os_partitions);
358 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
359 part->req_size, part->min_size);
364 pr_err("nvram: Failed to find or create %s"
365 " partition, err %d\n", part->name, (int)p);
370 part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info);
375 static void __init nvram_init_oops_partition(int rtas_partition_exists)
379 rc = pseries_nvram_init_os_partition(&oops_log_partition);
381 if (!rtas_partition_exists)
383 pr_notice("nvram: Using %s partition to log both"
384 " RTAS errors and oops/panic reports\n",
385 rtas_log_partition.name);
386 memcpy(&oops_log_partition, &rtas_log_partition,
387 sizeof(rtas_log_partition));
389 oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
390 rc = kmsg_dump_register(&nvram_kmsg_dumper);
392 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
398 static int __init pseries_nvram_init_log_partitions(void)
402 rc = pseries_nvram_init_os_partition(&rtas_log_partition);
403 nvram_init_oops_partition(rc == 0);
406 machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
408 int __init pSeries_nvram_init(void)
410 struct device_node *nvram;
411 const unsigned int *nbytes_p;
412 unsigned int proplen;
414 nvram = of_find_node_by_type(NULL, "nvram");
418 nbytes_p = of_get_property(nvram, "#bytes", &proplen);
419 if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
424 nvram_size = *nbytes_p;
426 nvram_fetch = rtas_token("nvram-fetch");
427 nvram_store = rtas_token("nvram-store");
428 printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
431 ppc_md.nvram_read = pSeries_nvram_read;
432 ppc_md.nvram_write = pSeries_nvram_write;
433 ppc_md.nvram_size = pSeries_nvram_get_size;
439 * Try to capture the last capture_len bytes of the printk buffer. Return
440 * the amount actually captured.
442 static size_t capture_last_msgs(const char *old_msgs, size_t old_len,
443 const char *new_msgs, size_t new_len,
444 char *captured, size_t capture_len)
446 if (new_len >= capture_len) {
447 memcpy(captured, new_msgs + (new_len - capture_len),
451 /* Grab the end of old_msgs. */
452 size_t old_tail_len = min(old_len, capture_len - new_len);
453 memcpy(captured, old_msgs + (old_len - old_tail_len),
455 memcpy(captured + old_tail_len, new_msgs, new_len);
456 return old_tail_len + new_len;
461 * Are we using the ibm,rtas-log for oops/panic reports? And if so,
462 * would logging this oops/panic overwrite an RTAS event that rtas_errd
463 * hasn't had a chance to read and process? Return 1 if so, else 0.
465 * We assume that if rtas_errd hasn't read the RTAS event in
466 * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to.
468 static int clobbering_unread_rtas_event(void)
470 return (oops_log_partition.index == rtas_log_partition.index
471 && last_unread_rtas_event
472 && get_seconds() - last_unread_rtas_event <=
473 NVRAM_RTAS_READ_TIMEOUT);
476 /* our kmsg_dump callback */
477 static void oops_to_nvram(struct kmsg_dumper *dumper,
478 enum kmsg_dump_reason reason,
479 const char *old_msgs, unsigned long old_len,
480 const char *new_msgs, unsigned long new_len)
482 static unsigned int oops_count = 0;
483 static bool panicking = false;
487 case KMSG_DUMP_RESTART:
489 case KMSG_DUMP_POWEROFF:
490 /* These are almost always orderly shutdowns. */
493 case KMSG_DUMP_KEXEC:
495 case KMSG_DUMP_PANIC:
498 case KMSG_DUMP_EMERG:
500 /* Panic report already captured. */
504 pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
505 __FUNCTION__, (int) reason);
509 if (clobbering_unread_rtas_event())
512 text_len = capture_last_msgs(old_msgs, old_len, new_msgs, new_len,
513 oops_buf, oops_log_partition.size);
514 (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
515 (int) text_len, ERR_TYPE_KERNEL_PANIC, ++oops_count);