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
)
88 if (!ppc_md
.nvram_size
)
92 size
= ppc_md
.nvram_size();
93 if (*ppos
>= size
|| size
< 0)
96 count
= min_t(size_t, count
, size
- *ppos
);
97 count
= min(count
, PAGE_SIZE
);
100 tmp
= kmalloc(count
, GFP_KERNEL
);
104 ret
= ppc_md
.nvram_read(tmp
, count
, ppos
);
108 if (copy_to_user(buf
, tmp
, ret
))
117 static ssize_t
dev_nvram_write(struct file
*file
, const char __user
*buf
,
118 size_t count
, loff_t
*ppos
)
125 if (!ppc_md
.nvram_size
)
129 size
= ppc_md
.nvram_size();
130 if (*ppos
>= size
|| size
< 0)
133 count
= min_t(size_t, count
, size
- *ppos
);
134 count
= min(count
, PAGE_SIZE
);
137 tmp
= kmalloc(count
, GFP_KERNEL
);
142 if (copy_from_user(tmp
, buf
, count
))
145 ret
= ppc_md
.nvram_write(tmp
, count
, ppos
);
153 static int dev_nvram_ioctl(struct inode
*inode
, struct file
*file
,
154 unsigned int cmd
, unsigned long arg
)
157 #ifdef CONFIG_PPC_PMAC
158 case OBSOLETE_PMAC_NVRAM_GET_OFFSET
:
159 printk(KERN_WARNING
"nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
160 case IOC_NVRAM_GET_OFFSET
: {
163 if (!machine_is(powermac
))
165 if (copy_from_user(&part
, (void __user
*)arg
, sizeof(part
)) != 0)
167 if (part
< pmac_nvram_OF
|| part
> pmac_nvram_NR
)
169 offset
= pmac_get_partition(part
);
172 if (copy_to_user((void __user
*)arg
, &offset
, sizeof(offset
)) != 0)
176 #endif /* CONFIG_PPC_PMAC */
182 struct file_operations nvram_fops
= {
183 .owner
= THIS_MODULE
,
184 .llseek
= dev_nvram_llseek
,
185 .read
= dev_nvram_read
,
186 .write
= dev_nvram_write
,
187 .ioctl
= dev_nvram_ioctl
,
190 static struct miscdevice nvram_dev
= {
198 static void nvram_print_partitions(char * label
)
200 struct list_head
* p
;
201 struct nvram_partition
* tmp_part
;
203 printk(KERN_WARNING
"--------%s---------\n", label
);
204 printk(KERN_WARNING
"indx\t\tsig\tchks\tlen\tname\n");
205 list_for_each(p
, &nvram_part
->partition
) {
206 tmp_part
= list_entry(p
, struct nvram_partition
, partition
);
207 printk(KERN_WARNING
"%4d \t%02x\t%02x\t%d\t%s\n",
208 tmp_part
->index
, tmp_part
->header
.signature
,
209 tmp_part
->header
.checksum
, tmp_part
->header
.length
,
210 tmp_part
->header
.name
);
216 static int nvram_write_header(struct nvram_partition
* part
)
221 tmp_index
= part
->index
;
222 rc
= ppc_md
.nvram_write((char *)&part
->header
, NVRAM_HEADER_LEN
, &tmp_index
);
228 static unsigned char nvram_checksum(struct nvram_header
*p
)
230 unsigned int c_sum
, c_sum2
;
231 unsigned short *sp
= (unsigned short *)p
->name
; /* assume 6 shorts */
232 c_sum
= p
->signature
+ p
->length
+ sp
[0] + sp
[1] + sp
[2] + sp
[3] + sp
[4] + sp
[5];
234 /* The sum may have spilled into the 3rd byte. Fold it back. */
235 c_sum
= ((c_sum
& 0xffff) + (c_sum
>> 16)) & 0xffff;
236 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
237 c_sum2
= (c_sum
>> 8) + (c_sum
<< 8);
238 c_sum
= ((c_sum
+ c_sum2
) >> 8) & 0xff;
244 * Find an nvram partition, sig can be 0 for any
245 * partition or name can be NULL for any name, else
246 * tries to match both
248 struct nvram_partition
*nvram_find_partition(int sig
, const char *name
)
250 struct nvram_partition
* part
;
251 struct list_head
* p
;
253 list_for_each(p
, &nvram_part
->partition
) {
254 part
= list_entry(p
, struct nvram_partition
, partition
);
256 if (sig
&& part
->header
.signature
!= sig
)
258 if (name
&& 0 != strncmp(name
, part
->header
.name
, 12))
264 EXPORT_SYMBOL(nvram_find_partition
);
267 static int nvram_remove_os_partition(void)
271 struct nvram_partition
* part
;
272 struct nvram_partition
* cur_part
;
275 list_for_each(i
, &nvram_part
->partition
) {
276 part
= list_entry(i
, struct nvram_partition
, partition
);
277 if (part
->header
.signature
!= NVRAM_SIG_OS
)
280 /* Make os partition a free partition */
281 part
->header
.signature
= NVRAM_SIG_FREE
;
282 sprintf(part
->header
.name
, "wwwwwwwwwwww");
283 part
->header
.checksum
= nvram_checksum(&part
->header
);
285 /* Merge contiguous free partitions backwards */
286 list_for_each_prev(j
, &part
->partition
) {
287 cur_part
= list_entry(j
, struct nvram_partition
, partition
);
288 if (cur_part
== nvram_part
|| cur_part
->header
.signature
!= NVRAM_SIG_FREE
) {
292 part
->header
.length
+= cur_part
->header
.length
;
293 part
->header
.checksum
= nvram_checksum(&part
->header
);
294 part
->index
= cur_part
->index
;
296 list_del(&cur_part
->partition
);
298 j
= &part
->partition
; /* fixup our loop */
301 /* Merge contiguous free partitions forwards */
302 list_for_each(j
, &part
->partition
) {
303 cur_part
= list_entry(j
, struct nvram_partition
, partition
);
304 if (cur_part
== nvram_part
|| cur_part
->header
.signature
!= NVRAM_SIG_FREE
) {
308 part
->header
.length
+= cur_part
->header
.length
;
309 part
->header
.checksum
= nvram_checksum(&part
->header
);
311 list_del(&cur_part
->partition
);
313 j
= &part
->partition
; /* fixup our loop */
316 rc
= nvram_write_header(part
);
318 printk(KERN_ERR
"nvram_remove_os_partition: nvram_write failed (%d)\n", rc
);
327 /* nvram_create_os_partition
329 * Create a OS linux partition to buffer error logs.
330 * Will create a partition starting at the first free
331 * space found if space has enough room.
333 static int nvram_create_os_partition(void)
335 struct nvram_partition
*part
;
336 struct nvram_partition
*new_part
;
337 struct nvram_partition
*free_part
= NULL
;
338 int seq_init
[2] = { 0, 0 };
343 /* Find a free partition that will give us the maximum needed size
344 If can't find one that will give us the minimum size needed */
345 list_for_each_entry(part
, &nvram_part
->partition
, partition
) {
346 if (part
->header
.signature
!= NVRAM_SIG_FREE
)
349 if (part
->header
.length
>= NVRAM_MAX_REQ
) {
350 size
= NVRAM_MAX_REQ
;
354 if (!size
&& part
->header
.length
>= NVRAM_MIN_REQ
) {
355 size
= NVRAM_MIN_REQ
;
362 /* Create our OS partition */
363 new_part
= kmalloc(sizeof(*new_part
), GFP_KERNEL
);
365 printk(KERN_ERR
"nvram_create_os_partition: kmalloc failed\n");
369 new_part
->index
= free_part
->index
;
370 new_part
->header
.signature
= NVRAM_SIG_OS
;
371 new_part
->header
.length
= size
;
372 strcpy(new_part
->header
.name
, "ppc64,linux");
373 new_part
->header
.checksum
= nvram_checksum(&new_part
->header
);
375 rc
= nvram_write_header(new_part
);
377 printk(KERN_ERR
"nvram_create_os_partition: nvram_write_header \
382 /* make sure and initialize to zero the sequence number and the error
384 tmp_index
= new_part
->index
+ NVRAM_HEADER_LEN
;
385 rc
= ppc_md
.nvram_write((char *)&seq_init
, sizeof(seq_init
), &tmp_index
);
387 printk(KERN_ERR
"nvram_create_os_partition: nvram_write "
388 "failed (%d)\n", rc
);
392 nvram_error_log_index
= new_part
->index
+ NVRAM_HEADER_LEN
;
393 nvram_error_log_size
= ((part
->header
.length
- 1) *
394 NVRAM_BLOCK_LEN
) - sizeof(struct err_log_info
);
396 list_add_tail(&new_part
->partition
, &free_part
->partition
);
398 if (free_part
->header
.length
<= size
) {
399 list_del(&free_part
->partition
);
404 /* Adjust the partition we stole the space from */
405 free_part
->index
+= size
* NVRAM_BLOCK_LEN
;
406 free_part
->header
.length
-= size
;
407 free_part
->header
.checksum
= nvram_checksum(&free_part
->header
);
409 rc
= nvram_write_header(free_part
);
411 printk(KERN_ERR
"nvram_create_os_partition: nvram_write_header "
412 "failed (%d)\n", rc
);
420 /* nvram_setup_partition
422 * This will setup the partition we need for buffering the
423 * error logs and cleanup partitions if needed.
425 * The general strategy is the following:
426 * 1.) If there is ppc64,linux partition large enough then use it.
427 * 2.) If there is not a ppc64,linux partition large enough, search
428 * for a free partition that is large enough.
429 * 3.) If there is not a free partition large enough remove
430 * _all_ OS partitions and consolidate the space.
431 * 4.) Will first try getting a chunk that will satisfy the maximum
432 * error log size (NVRAM_MAX_REQ).
433 * 5.) If the max chunk cannot be allocated then try finding a chunk
434 * that will satisfy the minum needed (NVRAM_MIN_REQ).
436 static int nvram_setup_partition(void)
438 struct list_head
* p
;
439 struct nvram_partition
* part
;
442 /* For now, we don't do any of this on pmac, until I
443 * have figured out if it's worth killing some unused stuffs
444 * in our nvram, as Apple defined partitions use pretty much
447 if (machine_is(powermac
))
450 /* see if we have an OS partition that meets our needs.
451 will try getting the max we need. If not we'll delete
452 partitions and try again. */
453 list_for_each(p
, &nvram_part
->partition
) {
454 part
= list_entry(p
, struct nvram_partition
, partition
);
455 if (part
->header
.signature
!= NVRAM_SIG_OS
)
458 if (strcmp(part
->header
.name
, "ppc64,linux"))
461 if (part
->header
.length
>= NVRAM_MIN_REQ
) {
462 /* found our partition */
463 nvram_error_log_index
= part
->index
+ NVRAM_HEADER_LEN
;
464 nvram_error_log_size
= ((part
->header
.length
- 1) *
465 NVRAM_BLOCK_LEN
) - sizeof(struct err_log_info
);
470 /* try creating a partition with the free space we have */
471 rc
= nvram_create_os_partition();
476 /* need to free up some space */
477 rc
= nvram_remove_os_partition();
482 /* create a partition in this new space */
483 rc
= nvram_create_os_partition();
485 printk(KERN_ERR
"nvram_create_os_partition: Could not find a "
486 "NVRAM partition large enough\n");
494 static int nvram_scan_partitions(void)
496 loff_t cur_index
= 0;
497 struct nvram_header phead
;
498 struct nvram_partition
* tmp_part
;
504 if (ppc_md
.nvram_size
== NULL
)
506 total_size
= ppc_md
.nvram_size();
508 header
= (char *) kmalloc(NVRAM_HEADER_LEN
, GFP_KERNEL
);
510 printk(KERN_ERR
"nvram_scan_partitions: Failed kmalloc\n");
514 while (cur_index
< total_size
) {
516 err
= ppc_md
.nvram_read(header
, NVRAM_HEADER_LEN
, &cur_index
);
517 if (err
!= NVRAM_HEADER_LEN
) {
518 printk(KERN_ERR
"nvram_scan_partitions: Error parsing "
519 "nvram partitions\n");
523 cur_index
-= NVRAM_HEADER_LEN
; /* nvram_read will advance us */
525 memcpy(&phead
, header
, NVRAM_HEADER_LEN
);
528 c_sum
= nvram_checksum(&phead
);
529 if (c_sum
!= phead
.checksum
) {
530 printk(KERN_WARNING
"WARNING: nvram partition checksum"
531 " was %02x, should be %02x!\n",
532 phead
.checksum
, c_sum
);
533 printk(KERN_WARNING
"Terminating nvram partition scan\n");
537 printk(KERN_WARNING
"WARNING: nvram corruption "
538 "detected: 0-length partition\n");
541 tmp_part
= (struct nvram_partition
*)
542 kmalloc(sizeof(struct nvram_partition
), GFP_KERNEL
);
545 printk(KERN_ERR
"nvram_scan_partitions: kmalloc failed\n");
549 memcpy(&tmp_part
->header
, &phead
, NVRAM_HEADER_LEN
);
550 tmp_part
->index
= cur_index
;
551 list_add_tail(&tmp_part
->partition
, &nvram_part
->partition
);
553 cur_index
+= phead
.length
* NVRAM_BLOCK_LEN
;
562 static int __init
nvram_init(void)
567 if (ppc_md
.nvram_size
== NULL
|| ppc_md
.nvram_size() <= 0)
570 rc
= misc_register(&nvram_dev
);
572 printk(KERN_ERR
"nvram_init: failed to register device\n");
576 /* initialize our anchor for the nvram partition list */
577 nvram_part
= (struct nvram_partition
*) kmalloc(sizeof(struct nvram_partition
), GFP_KERNEL
);
579 printk(KERN_ERR
"nvram_init: Failed kmalloc\n");
582 INIT_LIST_HEAD(&nvram_part
->partition
);
584 /* Get all the NVRAM partitions */
585 error
= nvram_scan_partitions();
587 printk(KERN_ERR
"nvram_init: Failed nvram_scan_partitions\n");
591 if(nvram_setup_partition())
592 printk(KERN_WARNING
"nvram_init: Could not find nvram partition"
593 " for nvram buffered error logging.\n");
596 nvram_print_partitions("NVRAM Partitions");
602 void __exit
nvram_cleanup(void)
604 misc_deregister( &nvram_dev
);
608 #ifdef CONFIG_PPC_PSERIES
610 /* nvram_write_error_log
612 * We need to buffer the error logs into nvram to ensure that we have
613 * the failure information to decode. If we have a severe error there
614 * is no way to guarantee that the OS or the machine is in a state to
615 * get back to user land and write the error to disk. For example if
616 * the SCSI device driver causes a Machine Check by writing to a bad
617 * IO address, there is no way of guaranteeing that the device driver
618 * is in any state that is would also be able to write the error data
619 * captured to disk, thus we buffer it in NVRAM for analysis on the
622 * In NVRAM the partition containing the error log buffer will looks like:
624 * +-----------+----------+--------+------------+------------------+
625 * | signature | checksum | length | name | data |
626 * |0 |1 |2 3|4 15|16 length-1|
627 * +-----------+----------+--------+------------+------------------+
629 * The 'data' section would look like (in bytes):
630 * +--------------+------------+-----------------------------------+
631 * | event_logged | sequence # | error log |
632 * |0 3|4 7|8 nvram_error_log_size-1|
633 * +--------------+------------+-----------------------------------+
635 * event_logged: 0 if event has not been logged to syslog, 1 if it has
636 * sequence #: The unique sequence # for each event. (until it wraps)
637 * error log: The error log from event_scan
639 int nvram_write_error_log(char * buff
, int length
, unsigned int err_type
)
643 struct err_log_info info
;
649 if (nvram_error_log_index
== -1) {
653 if (length
> nvram_error_log_size
) {
654 length
= nvram_error_log_size
;
657 info
.error_type
= err_type
;
658 info
.seq_num
= error_log_cnt
;
660 tmp_index
= nvram_error_log_index
;
662 rc
= ppc_md
.nvram_write((char *)&info
, sizeof(struct err_log_info
), &tmp_index
);
664 printk(KERN_ERR
"nvram_write_error_log: Failed nvram_write (%d)\n", rc
);
668 rc
= ppc_md
.nvram_write(buff
, length
, &tmp_index
);
670 printk(KERN_ERR
"nvram_write_error_log: Failed nvram_write (%d)\n", rc
);
677 /* nvram_read_error_log
679 * Reads nvram for error log for at most 'length'
681 int nvram_read_error_log(char * buff
, int length
, unsigned int * err_type
)
685 struct err_log_info info
;
687 if (nvram_error_log_index
== -1)
690 if (length
> nvram_error_log_size
)
691 length
= nvram_error_log_size
;
693 tmp_index
= nvram_error_log_index
;
695 rc
= ppc_md
.nvram_read((char *)&info
, sizeof(struct err_log_info
), &tmp_index
);
697 printk(KERN_ERR
"nvram_read_error_log: Failed nvram_read (%d)\n", rc
);
701 rc
= ppc_md
.nvram_read(buff
, length
, &tmp_index
);
703 printk(KERN_ERR
"nvram_read_error_log: Failed nvram_read (%d)\n", rc
);
707 error_log_cnt
= info
.seq_num
;
708 *err_type
= info
.error_type
;
713 /* This doesn't actually zero anything, but it sets the event_logged
714 * word to tell that this event is safely in syslog.
716 int nvram_clear_error_log(void)
719 int clear_word
= ERR_FLAG_ALREADY_LOGGED
;
722 tmp_index
= nvram_error_log_index
;
724 rc
= ppc_md
.nvram_write((char *)&clear_word
, sizeof(int), &tmp_index
);
726 printk(KERN_ERR
"nvram_clear_error_log: Failed nvram_write (%d)\n", rc
);
733 #endif /* CONFIG_PPC_PSERIES */
735 module_init(nvram_init
);
736 module_exit(nvram_cleanup
);
737 MODULE_LICENSE("GPL");