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
13 * TODO: Split the /dev/nvram part (that one can use
14 * drivers/char/generic_nvram.c) from the arch & partition
18 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/errno.h>
23 #include <linux/miscdevice.h>
24 #include <linux/fcntl.h>
25 #include <linux/nvram.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <asm/uaccess.h>
30 #include <asm/nvram.h>
33 #include <asm/machdep.h>
37 static int nvram_scan_partitions(void);
38 static int nvram_setup_partition(void);
39 static int nvram_create_os_partition(void);
40 static int nvram_remove_os_partition(void);
42 static struct nvram_partition * nvram_part;
43 static long nvram_error_log_index = -1;
44 static long nvram_error_log_size = 0;
46 int no_logging = 1; /* Until we initialize everything,
47 * make sure we don't try logging
50 extern volatile int error_log_cnt;
57 static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
61 if (ppc_md.nvram_size == NULL)
63 size = ppc_md.nvram_size();
67 offset += file->f_pos;
80 static ssize_t dev_nvram_read(struct file *file, char __user *buf,
81 size_t count, loff_t *ppos)
87 if (ppc_md.nvram_size == NULL)
89 size = ppc_md.nvram_size();
91 if (!access_ok(VERIFY_WRITE, buf, count))
98 tmp_buffer = (char *) kmalloc(count, GFP_KERNEL);
100 printk(KERN_ERR "dev_read_nvram: kmalloc failed\n");
104 len = ppc_md.nvram_read(tmp_buffer, count, ppos);
105 if ((long)len <= 0) {
110 if (copy_to_user(buf, tmp_buffer, len)) {
120 static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
121 size_t count, loff_t *ppos)
127 if (ppc_md.nvram_size == NULL)
129 size = ppc_md.nvram_size();
131 if (!access_ok(VERIFY_READ, buf, count))
138 tmp_buffer = (char *) kmalloc(count, GFP_KERNEL);
140 printk(KERN_ERR "dev_nvram_write: kmalloc failed\n");
144 if (copy_from_user(tmp_buffer, buf, count)) {
149 len = ppc_md.nvram_write(tmp_buffer, count, ppos);
150 if ((long)len <= 0) {
159 static int dev_nvram_ioctl(struct inode *inode, struct file *file,
160 unsigned int cmd, unsigned long arg)
163 #ifdef CONFIG_PPC_PMAC
164 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
165 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
166 case IOC_NVRAM_GET_OFFSET: {
169 if (_machine != PLATFORM_POWERMAC)
171 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
173 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
175 offset = pmac_get_partition(part);
178 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
182 #endif /* CONFIG_PPC_PMAC */
187 struct file_operations nvram_fops = {
188 .owner = THIS_MODULE,
189 .llseek = dev_nvram_llseek,
190 .read = dev_nvram_read,
191 .write = dev_nvram_write,
192 .ioctl = dev_nvram_ioctl,
195 static struct miscdevice nvram_dev = {
203 static void nvram_print_partitions(char * label)
205 struct list_head * p;
206 struct nvram_partition * tmp_part;
208 printk(KERN_WARNING "--------%s---------\n", label);
209 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
210 list_for_each(p, &nvram_part->partition) {
211 tmp_part = list_entry(p, struct nvram_partition, partition);
212 printk(KERN_WARNING "%d \t%02x\t%02x\t%d\t%s\n",
213 tmp_part->index, tmp_part->header.signature,
214 tmp_part->header.checksum, tmp_part->header.length,
215 tmp_part->header.name);
221 static int nvram_write_header(struct nvram_partition * part)
226 tmp_index = part->index;
227 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
233 static unsigned char nvram_checksum(struct nvram_header *p)
235 unsigned int c_sum, c_sum2;
236 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
237 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
239 /* The sum may have spilled into the 3rd byte. Fold it back. */
240 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
241 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
242 c_sum2 = (c_sum >> 8) + (c_sum << 8);
243 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
249 * Find an nvram partition, sig can be 0 for any
250 * partition or name can be NULL for any name, else
251 * tries to match both
253 struct nvram_partition *nvram_find_partition(int sig, const char *name)
255 struct nvram_partition * part;
256 struct list_head * p;
258 list_for_each(p, &nvram_part->partition) {
259 part = list_entry(p, struct nvram_partition, partition);
261 if (sig && part->header.signature != sig)
263 if (name && 0 != strncmp(name, part->header.name, 12))
269 EXPORT_SYMBOL(nvram_find_partition);
272 static int nvram_remove_os_partition(void)
276 struct nvram_partition * part;
277 struct nvram_partition * cur_part;
280 list_for_each(i, &nvram_part->partition) {
281 part = list_entry(i, struct nvram_partition, partition);
282 if (part->header.signature != NVRAM_SIG_OS)
285 /* Make os partition a free partition */
286 part->header.signature = NVRAM_SIG_FREE;
287 sprintf(part->header.name, "wwwwwwwwwwww");
288 part->header.checksum = nvram_checksum(&part->header);
290 /* Merge contiguous free partitions backwards */
291 list_for_each_prev(j, &part->partition) {
292 cur_part = list_entry(j, struct nvram_partition, partition);
293 if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
297 part->header.length += cur_part->header.length;
298 part->header.checksum = nvram_checksum(&part->header);
299 part->index = cur_part->index;
301 list_del(&cur_part->partition);
303 j = &part->partition; /* fixup our loop */
306 /* Merge contiguous free partitions forwards */
307 list_for_each(j, &part->partition) {
308 cur_part = list_entry(j, struct nvram_partition, partition);
309 if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
313 part->header.length += cur_part->header.length;
314 part->header.checksum = nvram_checksum(&part->header);
316 list_del(&cur_part->partition);
318 j = &part->partition; /* fixup our loop */
321 rc = nvram_write_header(part);
323 printk(KERN_ERR "nvram_remove_os_partition: nvram_write failed (%d)\n", rc);
332 /* nvram_create_os_partition
334 * Create a OS linux partition to buffer error logs.
335 * Will create a partition starting at the first free
336 * space found if space has enough room.
338 static int nvram_create_os_partition(void)
340 struct nvram_partition *part;
341 struct nvram_partition *new_part;
342 struct nvram_partition *free_part = NULL;
343 int seq_init[2] = { 0, 0 };
348 /* Find a free partition that will give us the maximum needed size
349 If can't find one that will give us the minimum size needed */
350 list_for_each_entry(part, &nvram_part->partition, partition) {
351 if (part->header.signature != NVRAM_SIG_FREE)
354 if (part->header.length >= NVRAM_MAX_REQ) {
355 size = NVRAM_MAX_REQ;
359 if (!size && part->header.length >= NVRAM_MIN_REQ) {
360 size = NVRAM_MIN_REQ;
367 /* Create our OS partition */
368 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
370 printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n");
374 new_part->index = free_part->index;
375 new_part->header.signature = NVRAM_SIG_OS;
376 new_part->header.length = size;
377 strcpy(new_part->header.name, "ppc64,linux");
378 new_part->header.checksum = nvram_checksum(&new_part->header);
380 rc = nvram_write_header(new_part);
382 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header \
387 /* make sure and initialize to zero the sequence number and the error
389 tmp_index = new_part->index + NVRAM_HEADER_LEN;
390 rc = ppc_md.nvram_write((char *)&seq_init, sizeof(seq_init), &tmp_index);
392 printk(KERN_ERR "nvram_create_os_partition: nvram_write "
393 "failed (%d)\n", rc);
397 nvram_error_log_index = new_part->index + NVRAM_HEADER_LEN;
398 nvram_error_log_size = ((part->header.length - 1) *
399 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
401 list_add_tail(&new_part->partition, &free_part->partition);
403 if (free_part->header.length <= size) {
404 list_del(&free_part->partition);
409 /* Adjust the partition we stole the space from */
410 free_part->index += size * NVRAM_BLOCK_LEN;
411 free_part->header.length -= size;
412 free_part->header.checksum = nvram_checksum(&free_part->header);
414 rc = nvram_write_header(free_part);
416 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header "
417 "failed (%d)\n", rc);
425 /* nvram_setup_partition
427 * This will setup the partition we need for buffering the
428 * error logs and cleanup partitions if needed.
430 * The general strategy is the following:
431 * 1.) If there is ppc64,linux partition large enough then use it.
432 * 2.) If there is not a ppc64,linux partition large enough, search
433 * for a free partition that is large enough.
434 * 3.) If there is not a free partition large enough remove
435 * _all_ OS partitions and consolidate the space.
436 * 4.) Will first try getting a chunk that will satisfy the maximum
437 * error log size (NVRAM_MAX_REQ).
438 * 5.) If the max chunk cannot be allocated then try finding a chunk
439 * that will satisfy the minum needed (NVRAM_MIN_REQ).
441 static int nvram_setup_partition(void)
443 struct list_head * p;
444 struct nvram_partition * part;
447 /* For now, we don't do any of this on pmac, until I
448 * have figured out if it's worth killing some unused stuffs
449 * in our nvram, as Apple defined partitions use pretty much
452 if (_machine == PLATFORM_POWERMAC)
455 /* see if we have an OS partition that meets our needs.
456 will try getting the max we need. If not we'll delete
457 partitions and try again. */
458 list_for_each(p, &nvram_part->partition) {
459 part = list_entry(p, struct nvram_partition, partition);
460 if (part->header.signature != NVRAM_SIG_OS)
463 if (strcmp(part->header.name, "ppc64,linux"))
466 if (part->header.length >= NVRAM_MIN_REQ) {
467 /* found our partition */
468 nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
469 nvram_error_log_size = ((part->header.length - 1) *
470 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
475 /* try creating a partition with the free space we have */
476 rc = nvram_create_os_partition();
481 /* need to free up some space */
482 rc = nvram_remove_os_partition();
487 /* create a partition in this new space */
488 rc = nvram_create_os_partition();
490 printk(KERN_ERR "nvram_create_os_partition: Could not find a "
491 "NVRAM partition large enough\n");
499 static int nvram_scan_partitions(void)
501 loff_t cur_index = 0;
502 struct nvram_header phead;
503 struct nvram_partition * tmp_part;
509 if (ppc_md.nvram_size == NULL)
511 total_size = ppc_md.nvram_size();
513 header = (char *) kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
515 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
519 while (cur_index < total_size) {
521 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
522 if (err != NVRAM_HEADER_LEN) {
523 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
524 "nvram partitions\n");
528 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
530 memcpy(&phead, header, NVRAM_HEADER_LEN);
533 c_sum = nvram_checksum(&phead);
534 if (c_sum != phead.checksum) {
535 printk(KERN_WARNING "WARNING: nvram partition checksum"
536 " was %02x, should be %02x!\n",
537 phead.checksum, c_sum);
538 printk(KERN_WARNING "Terminating nvram partition scan\n");
542 printk(KERN_WARNING "WARNING: nvram corruption "
543 "detected: 0-length partition\n");
546 tmp_part = (struct nvram_partition *)
547 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
550 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
554 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
555 tmp_part->index = cur_index;
556 list_add_tail(&tmp_part->partition, &nvram_part->partition);
558 cur_index += phead.length * NVRAM_BLOCK_LEN;
567 static int __init nvram_init(void)
572 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
575 rc = misc_register(&nvram_dev);
577 printk(KERN_ERR "nvram_init: failed to register device\n");
581 /* initialize our anchor for the nvram partition list */
582 nvram_part = (struct nvram_partition *) kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
584 printk(KERN_ERR "nvram_init: Failed kmalloc\n");
587 INIT_LIST_HEAD(&nvram_part->partition);
589 /* Get all the NVRAM partitions */
590 error = nvram_scan_partitions();
592 printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
596 if(nvram_setup_partition())
597 printk(KERN_WARNING "nvram_init: Could not find nvram partition"
598 " for nvram buffered error logging.\n");
601 nvram_print_partitions("NVRAM Partitions");
607 void __exit nvram_cleanup(void)
609 misc_deregister( &nvram_dev );
613 #ifdef CONFIG_PPC_PSERIES
615 /* nvram_write_error_log
617 * We need to buffer the error logs into nvram to ensure that we have
618 * the failure information to decode. If we have a severe error there
619 * is no way to guarantee that the OS or the machine is in a state to
620 * get back to user land and write the error to disk. For example if
621 * the SCSI device driver causes a Machine Check by writing to a bad
622 * IO address, there is no way of guaranteeing that the device driver
623 * is in any state that is would also be able to write the error data
624 * captured to disk, thus we buffer it in NVRAM for analysis on the
627 * In NVRAM the partition containing the error log buffer will looks like:
629 * +-----------+----------+--------+------------+------------------+
630 * | signature | checksum | length | name | data |
631 * |0 |1 |2 3|4 15|16 length-1|
632 * +-----------+----------+--------+------------+------------------+
634 * The 'data' section would look like (in bytes):
635 * +--------------+------------+-----------------------------------+
636 * | event_logged | sequence # | error log |
637 * |0 3|4 7|8 nvram_error_log_size-1|
638 * +--------------+------------+-----------------------------------+
640 * event_logged: 0 if event has not been logged to syslog, 1 if it has
641 * sequence #: The unique sequence # for each event. (until it wraps)
642 * error log: The error log from event_scan
644 int nvram_write_error_log(char * buff, int length, unsigned int err_type)
648 struct err_log_info info;
654 if (nvram_error_log_index == -1) {
658 if (length > nvram_error_log_size) {
659 length = nvram_error_log_size;
662 info.error_type = err_type;
663 info.seq_num = error_log_cnt;
665 tmp_index = nvram_error_log_index;
667 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
669 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
673 rc = ppc_md.nvram_write(buff, length, &tmp_index);
675 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
682 /* nvram_read_error_log
684 * Reads nvram for error log for at most 'length'
686 int nvram_read_error_log(char * buff, int length, unsigned int * err_type)
690 struct err_log_info info;
692 if (nvram_error_log_index == -1)
695 if (length > nvram_error_log_size)
696 length = nvram_error_log_size;
698 tmp_index = nvram_error_log_index;
700 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
702 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
706 rc = ppc_md.nvram_read(buff, length, &tmp_index);
708 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
712 error_log_cnt = info.seq_num;
713 *err_type = info.error_type;
718 /* This doesn't actually zero anything, but it sets the event_logged
719 * word to tell that this event is safely in syslog.
721 int nvram_clear_error_log(void)
724 int clear_word = ERR_FLAG_ALREADY_LOGGED;
727 tmp_index = nvram_error_log_index;
729 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
731 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
738 #endif /* CONFIG_PPC_PSERIES */
740 module_init(nvram_init);
741 module_exit(nvram_cleanup);
742 MODULE_LICENSE("GPL");