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>
34 #include <asm/systemcfg.h>
38 static int nvram_scan_partitions(void);
39 static int nvram_setup_partition(void);
40 static int nvram_create_os_partition(void);
41 static int nvram_remove_os_partition(void);
43 static struct nvram_partition
* nvram_part
;
44 static long nvram_error_log_index
= -1;
45 static long nvram_error_log_size
= 0;
47 int no_logging
= 1; /* Until we initialize everything,
48 * make sure we don't try logging
51 extern volatile int error_log_cnt
;
58 static loff_t
dev_nvram_llseek(struct file
*file
, loff_t offset
, int origin
)
62 if (ppc_md
.nvram_size
== NULL
)
64 size
= ppc_md
.nvram_size();
68 offset
+= file
->f_pos
;
81 static ssize_t
dev_nvram_read(struct file
*file
, char __user
*buf
,
82 size_t count
, loff_t
*ppos
)
88 if (ppc_md
.nvram_size
== NULL
)
90 size
= ppc_md
.nvram_size();
92 if (!access_ok(VERIFY_WRITE
, buf
, count
))
99 tmp_buffer
= (char *) kmalloc(count
, GFP_KERNEL
);
101 printk(KERN_ERR
"dev_read_nvram: kmalloc failed\n");
105 len
= ppc_md
.nvram_read(tmp_buffer
, count
, ppos
);
106 if ((long)len
<= 0) {
111 if (copy_to_user(buf
, tmp_buffer
, len
)) {
121 static ssize_t
dev_nvram_write(struct file
*file
, const char __user
*buf
,
122 size_t count
, loff_t
*ppos
)
128 if (ppc_md
.nvram_size
== NULL
)
130 size
= ppc_md
.nvram_size();
132 if (!access_ok(VERIFY_READ
, buf
, count
))
139 tmp_buffer
= (char *) kmalloc(count
, GFP_KERNEL
);
141 printk(KERN_ERR
"dev_nvram_write: kmalloc failed\n");
145 if (copy_from_user(tmp_buffer
, buf
, count
)) {
150 len
= ppc_md
.nvram_write(tmp_buffer
, count
, ppos
);
151 if ((long)len
<= 0) {
160 static int dev_nvram_ioctl(struct inode
*inode
, struct file
*file
,
161 unsigned int cmd
, unsigned long arg
)
164 #ifdef CONFIG_PPC_PMAC
165 case OBSOLETE_PMAC_NVRAM_GET_OFFSET
:
166 printk(KERN_WARNING
"nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
167 case IOC_NVRAM_GET_OFFSET
: {
170 if (systemcfg
->platform
!= PLATFORM_POWERMAC
)
172 if (copy_from_user(&part
, (void __user
*)arg
, sizeof(part
)) != 0)
174 if (part
< pmac_nvram_OF
|| part
> pmac_nvram_NR
)
176 offset
= pmac_get_partition(part
);
179 if (copy_to_user((void __user
*)arg
, &offset
, sizeof(offset
)) != 0)
183 #endif /* CONFIG_PPC_PMAC */
188 struct file_operations nvram_fops
= {
189 .owner
= THIS_MODULE
,
190 .llseek
= dev_nvram_llseek
,
191 .read
= dev_nvram_read
,
192 .write
= dev_nvram_write
,
193 .ioctl
= dev_nvram_ioctl
,
196 static struct miscdevice nvram_dev
= {
204 static void nvram_print_partitions(char * label
)
206 struct list_head
* p
;
207 struct nvram_partition
* tmp_part
;
209 printk(KERN_WARNING
"--------%s---------\n", label
);
210 printk(KERN_WARNING
"indx\t\tsig\tchks\tlen\tname\n");
211 list_for_each(p
, &nvram_part
->partition
) {
212 tmp_part
= list_entry(p
, struct nvram_partition
, partition
);
213 printk(KERN_WARNING
"%d \t%02x\t%02x\t%d\t%s\n",
214 tmp_part
->index
, tmp_part
->header
.signature
,
215 tmp_part
->header
.checksum
, tmp_part
->header
.length
,
216 tmp_part
->header
.name
);
222 static int nvram_write_header(struct nvram_partition
* part
)
227 tmp_index
= part
->index
;
228 rc
= ppc_md
.nvram_write((char *)&part
->header
, NVRAM_HEADER_LEN
, &tmp_index
);
234 static unsigned char nvram_checksum(struct nvram_header
*p
)
236 unsigned int c_sum
, c_sum2
;
237 unsigned short *sp
= (unsigned short *)p
->name
; /* assume 6 shorts */
238 c_sum
= p
->signature
+ p
->length
+ sp
[0] + sp
[1] + sp
[2] + sp
[3] + sp
[4] + sp
[5];
240 /* The sum may have spilled into the 3rd byte. Fold it back. */
241 c_sum
= ((c_sum
& 0xffff) + (c_sum
>> 16)) & 0xffff;
242 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
243 c_sum2
= (c_sum
>> 8) + (c_sum
<< 8);
244 c_sum
= ((c_sum
+ c_sum2
) >> 8) & 0xff;
250 * Find an nvram partition, sig can be 0 for any
251 * partition or name can be NULL for any name, else
252 * tries to match both
254 struct nvram_partition
*nvram_find_partition(int sig
, const char *name
)
256 struct nvram_partition
* part
;
257 struct list_head
* p
;
259 list_for_each(p
, &nvram_part
->partition
) {
260 part
= list_entry(p
, struct nvram_partition
, partition
);
262 if (sig
&& part
->header
.signature
!= sig
)
264 if (name
&& 0 != strncmp(name
, part
->header
.name
, 12))
270 EXPORT_SYMBOL(nvram_find_partition
);
273 static int nvram_remove_os_partition(void)
277 struct nvram_partition
* part
;
278 struct nvram_partition
* cur_part
;
281 list_for_each(i
, &nvram_part
->partition
) {
282 part
= list_entry(i
, struct nvram_partition
, partition
);
283 if (part
->header
.signature
!= NVRAM_SIG_OS
)
286 /* Make os partition a free partition */
287 part
->header
.signature
= NVRAM_SIG_FREE
;
288 sprintf(part
->header
.name
, "wwwwwwwwwwww");
289 part
->header
.checksum
= nvram_checksum(&part
->header
);
291 /* Merge contiguous free partitions backwards */
292 list_for_each_prev(j
, &part
->partition
) {
293 cur_part
= list_entry(j
, struct nvram_partition
, partition
);
294 if (cur_part
== nvram_part
|| cur_part
->header
.signature
!= NVRAM_SIG_FREE
) {
298 part
->header
.length
+= cur_part
->header
.length
;
299 part
->header
.checksum
= nvram_checksum(&part
->header
);
300 part
->index
= cur_part
->index
;
302 list_del(&cur_part
->partition
);
304 j
= &part
->partition
; /* fixup our loop */
307 /* Merge contiguous free partitions forwards */
308 list_for_each(j
, &part
->partition
) {
309 cur_part
= list_entry(j
, struct nvram_partition
, partition
);
310 if (cur_part
== nvram_part
|| cur_part
->header
.signature
!= NVRAM_SIG_FREE
) {
314 part
->header
.length
+= cur_part
->header
.length
;
315 part
->header
.checksum
= nvram_checksum(&part
->header
);
317 list_del(&cur_part
->partition
);
319 j
= &part
->partition
; /* fixup our loop */
322 rc
= nvram_write_header(part
);
324 printk(KERN_ERR
"nvram_remove_os_partition: nvram_write failed (%d)\n", rc
);
333 /* nvram_create_os_partition
335 * Create a OS linux partition to buffer error logs.
336 * Will create a partition starting at the first free
337 * space found if space has enough room.
339 static int nvram_create_os_partition(void)
341 struct list_head
* p
;
342 struct nvram_partition
* part
;
343 struct nvram_partition
* new_part
= NULL
;
344 struct nvram_partition
* free_part
= NULL
;
345 int seq_init
[2] = { 0, 0 };
350 /* Find a free partition that will give us the maximum needed size
351 If can't find one that will give us the minimum size needed */
352 list_for_each(p
, &nvram_part
->partition
) {
353 part
= list_entry(p
, struct nvram_partition
, partition
);
354 if (part
->header
.signature
!= NVRAM_SIG_FREE
)
357 if (part
->header
.length
>= NVRAM_MAX_REQ
) {
358 size
= NVRAM_MAX_REQ
;
362 if (!size
&& part
->header
.length
>= NVRAM_MIN_REQ
) {
363 size
= NVRAM_MIN_REQ
;
371 /* Create our OS partition */
372 new_part
= (struct nvram_partition
*)
373 kmalloc(sizeof(struct nvram_partition
), GFP_KERNEL
);
375 printk(KERN_ERR
"nvram_create_os_partition: kmalloc failed\n");
379 new_part
->index
= free_part
->index
;
380 new_part
->header
.signature
= NVRAM_SIG_OS
;
381 new_part
->header
.length
= size
;
382 sprintf(new_part
->header
.name
, "ppc64,linux");
383 new_part
->header
.checksum
= nvram_checksum(&new_part
->header
);
385 rc
= nvram_write_header(new_part
);
387 printk(KERN_ERR
"nvram_create_os_partition: nvram_write_header \
392 /* make sure and initialize to zero the sequence number and the error
394 tmp_index
= new_part
->index
+ NVRAM_HEADER_LEN
;
395 rc
= ppc_md
.nvram_write((char *)&seq_init
, sizeof(seq_init
), &tmp_index
);
397 printk(KERN_ERR
"nvram_create_os_partition: nvram_write failed (%d)\n", rc
);
401 nvram_error_log_index
= new_part
->index
+ NVRAM_HEADER_LEN
;
402 nvram_error_log_size
= ((part
->header
.length
- 1) *
403 NVRAM_BLOCK_LEN
) - sizeof(struct err_log_info
);
405 list_add_tail(&new_part
->partition
, &free_part
->partition
);
407 if (free_part
->header
.length
<= size
) {
408 list_del(&free_part
->partition
);
413 /* Adjust the partition we stole the space from */
414 free_part
->index
+= size
* NVRAM_BLOCK_LEN
;
415 free_part
->header
.length
-= size
;
416 free_part
->header
.checksum
= nvram_checksum(&free_part
->header
);
418 rc
= nvram_write_header(free_part
);
420 printk(KERN_ERR
"nvram_create_os_partition: nvram_write_header "
421 "failed (%d)\n", rc
);
429 /* nvram_setup_partition
431 * This will setup the partition we need for buffering the
432 * error logs and cleanup partitions if needed.
434 * The general strategy is the following:
435 * 1.) If there is ppc64,linux partition large enough then use it.
436 * 2.) If there is not a ppc64,linux partition large enough, search
437 * for a free partition that is large enough.
438 * 3.) If there is not a free partition large enough remove
439 * _all_ OS partitions and consolidate the space.
440 * 4.) Will first try getting a chunk that will satisfy the maximum
441 * error log size (NVRAM_MAX_REQ).
442 * 5.) If the max chunk cannot be allocated then try finding a chunk
443 * that will satisfy the minum needed (NVRAM_MIN_REQ).
445 static int nvram_setup_partition(void)
447 struct list_head
* p
;
448 struct nvram_partition
* part
;
451 /* For now, we don't do any of this on pmac, until I
452 * have figured out if it's worth killing some unused stuffs
453 * in our nvram, as Apple defined partitions use pretty much
456 if (systemcfg
->platform
== PLATFORM_POWERMAC
)
459 /* see if we have an OS partition that meets our needs.
460 will try getting the max we need. If not we'll delete
461 partitions and try again. */
462 list_for_each(p
, &nvram_part
->partition
) {
463 part
= list_entry(p
, struct nvram_partition
, partition
);
464 if (part
->header
.signature
!= NVRAM_SIG_OS
)
467 if (strcmp(part
->header
.name
, "ppc64,linux"))
470 if (part
->header
.length
>= NVRAM_MIN_REQ
) {
471 /* found our partition */
472 nvram_error_log_index
= part
->index
+ NVRAM_HEADER_LEN
;
473 nvram_error_log_size
= ((part
->header
.length
- 1) *
474 NVRAM_BLOCK_LEN
) - sizeof(struct err_log_info
);
479 /* try creating a partition with the free space we have */
480 rc
= nvram_create_os_partition();
485 /* need to free up some space */
486 rc
= nvram_remove_os_partition();
491 /* create a partition in this new space */
492 rc
= nvram_create_os_partition();
494 printk(KERN_ERR
"nvram_create_os_partition: Could not find a "
495 "NVRAM partition large enough\n");
503 static int nvram_scan_partitions(void)
505 loff_t cur_index
= 0;
506 struct nvram_header phead
;
507 struct nvram_partition
* tmp_part
;
513 if (ppc_md
.nvram_size
== NULL
)
515 total_size
= ppc_md
.nvram_size();
517 header
= (char *) kmalloc(NVRAM_HEADER_LEN
, GFP_KERNEL
);
519 printk(KERN_ERR
"nvram_scan_partitions: Failed kmalloc\n");
523 while (cur_index
< total_size
) {
525 err
= ppc_md
.nvram_read(header
, NVRAM_HEADER_LEN
, &cur_index
);
526 if (err
!= NVRAM_HEADER_LEN
) {
527 printk(KERN_ERR
"nvram_scan_partitions: Error parsing "
528 "nvram partitions\n");
532 cur_index
-= NVRAM_HEADER_LEN
; /* nvram_read will advance us */
534 memcpy(&phead
, header
, NVRAM_HEADER_LEN
);
537 c_sum
= nvram_checksum(&phead
);
538 if (c_sum
!= phead
.checksum
) {
539 printk(KERN_WARNING
"WARNING: nvram partition checksum"
540 " was %02x, should be %02x!\n",
541 phead
.checksum
, c_sum
);
542 printk(KERN_WARNING
"Terminating nvram partition scan\n");
546 printk(KERN_WARNING
"WARNING: nvram corruption "
547 "detected: 0-length partition\n");
550 tmp_part
= (struct nvram_partition
*)
551 kmalloc(sizeof(struct nvram_partition
), GFP_KERNEL
);
554 printk(KERN_ERR
"nvram_scan_partitions: kmalloc failed\n");
558 memcpy(&tmp_part
->header
, &phead
, NVRAM_HEADER_LEN
);
559 tmp_part
->index
= cur_index
;
560 list_add_tail(&tmp_part
->partition
, &nvram_part
->partition
);
562 cur_index
+= phead
.length
* NVRAM_BLOCK_LEN
;
571 static int __init
nvram_init(void)
576 if (ppc_md
.nvram_size
== NULL
|| ppc_md
.nvram_size() <= 0)
579 rc
= misc_register(&nvram_dev
);
581 printk(KERN_ERR
"nvram_init: failed to register device\n");
585 /* initialize our anchor for the nvram partition list */
586 nvram_part
= (struct nvram_partition
*) kmalloc(sizeof(struct nvram_partition
), GFP_KERNEL
);
588 printk(KERN_ERR
"nvram_init: Failed kmalloc\n");
591 INIT_LIST_HEAD(&nvram_part
->partition
);
593 /* Get all the NVRAM partitions */
594 error
= nvram_scan_partitions();
596 printk(KERN_ERR
"nvram_init: Failed nvram_scan_partitions\n");
600 if(nvram_setup_partition())
601 printk(KERN_WARNING
"nvram_init: Could not find nvram partition"
602 " for nvram buffered error logging.\n");
605 nvram_print_partitions("NVRAM Partitions");
611 void __exit
nvram_cleanup(void)
613 misc_deregister( &nvram_dev
);
617 #ifdef CONFIG_PPC_PSERIES
619 /* nvram_write_error_log
621 * We need to buffer the error logs into nvram to ensure that we have
622 * the failure information to decode. If we have a severe error there
623 * is no way to guarantee that the OS or the machine is in a state to
624 * get back to user land and write the error to disk. For example if
625 * the SCSI device driver causes a Machine Check by writing to a bad
626 * IO address, there is no way of guaranteeing that the device driver
627 * is in any state that is would also be able to write the error data
628 * captured to disk, thus we buffer it in NVRAM for analysis on the
631 * In NVRAM the partition containing the error log buffer will looks like:
633 * +-----------+----------+--------+------------+------------------+
634 * | signature | checksum | length | name | data |
635 * |0 |1 |2 3|4 15|16 length-1|
636 * +-----------+----------+--------+------------+------------------+
638 * The 'data' section would look like (in bytes):
639 * +--------------+------------+-----------------------------------+
640 * | event_logged | sequence # | error log |
641 * |0 3|4 7|8 nvram_error_log_size-1|
642 * +--------------+------------+-----------------------------------+
644 * event_logged: 0 if event has not been logged to syslog, 1 if it has
645 * sequence #: The unique sequence # for each event. (until it wraps)
646 * error log: The error log from event_scan
648 int nvram_write_error_log(char * buff
, int length
, unsigned int err_type
)
652 struct err_log_info info
;
658 if (nvram_error_log_index
== -1) {
662 if (length
> nvram_error_log_size
) {
663 length
= nvram_error_log_size
;
666 info
.error_type
= err_type
;
667 info
.seq_num
= error_log_cnt
;
669 tmp_index
= nvram_error_log_index
;
671 rc
= ppc_md
.nvram_write((char *)&info
, sizeof(struct err_log_info
), &tmp_index
);
673 printk(KERN_ERR
"nvram_write_error_log: Failed nvram_write (%d)\n", rc
);
677 rc
= ppc_md
.nvram_write(buff
, length
, &tmp_index
);
679 printk(KERN_ERR
"nvram_write_error_log: Failed nvram_write (%d)\n", rc
);
686 /* nvram_read_error_log
688 * Reads nvram for error log for at most 'length'
690 int nvram_read_error_log(char * buff
, int length
, unsigned int * err_type
)
694 struct err_log_info info
;
696 if (nvram_error_log_index
== -1)
699 if (length
> nvram_error_log_size
)
700 length
= nvram_error_log_size
;
702 tmp_index
= nvram_error_log_index
;
704 rc
= ppc_md
.nvram_read((char *)&info
, sizeof(struct err_log_info
), &tmp_index
);
706 printk(KERN_ERR
"nvram_read_error_log: Failed nvram_read (%d)\n", rc
);
710 rc
= ppc_md
.nvram_read(buff
, length
, &tmp_index
);
712 printk(KERN_ERR
"nvram_read_error_log: Failed nvram_read (%d)\n", rc
);
716 error_log_cnt
= info
.seq_num
;
717 *err_type
= info
.error_type
;
722 /* This doesn't actually zero anything, but it sets the event_logged
723 * word to tell that this event is safely in syslog.
725 int nvram_clear_error_log(void)
728 int clear_word
= ERR_FLAG_ALREADY_LOGGED
;
731 tmp_index
= nvram_error_log_index
;
733 rc
= ppc_md
.nvram_write((char *)&clear_word
, sizeof(int), &tmp_index
);
735 printk(KERN_ERR
"nvram_clear_error_log: Failed nvram_write (%d)\n", rc
);
742 #endif /* CONFIG_PPC_PSERIES */
744 module_init(nvram_init
);
745 module_exit(nvram_cleanup
);
746 MODULE_LICENSE("GPL");