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 != PLATFORM_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 */
181 struct file_operations nvram_fops = {
182 .owner = THIS_MODULE,
183 .llseek = dev_nvram_llseek,
184 .read = dev_nvram_read,
185 .write = dev_nvram_write,
186 .ioctl = dev_nvram_ioctl,
189 static struct miscdevice nvram_dev = {
197 static void nvram_print_partitions(char * label)
199 struct list_head * p;
200 struct nvram_partition * tmp_part;
202 printk(KERN_WARNING "--------%s---------\n", label);
203 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
204 list_for_each(p, &nvram_part->partition) {
205 tmp_part = list_entry(p, struct nvram_partition, partition);
206 printk(KERN_WARNING "%d \t%02x\t%02x\t%d\t%s\n",
207 tmp_part->index, tmp_part->header.signature,
208 tmp_part->header.checksum, tmp_part->header.length,
209 tmp_part->header.name);
215 static int nvram_write_header(struct nvram_partition * part)
220 tmp_index = part->index;
221 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
227 static unsigned char nvram_checksum(struct nvram_header *p)
229 unsigned int c_sum, c_sum2;
230 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
231 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
233 /* The sum may have spilled into the 3rd byte. Fold it back. */
234 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
235 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
236 c_sum2 = (c_sum >> 8) + (c_sum << 8);
237 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
243 * Find an nvram partition, sig can be 0 for any
244 * partition or name can be NULL for any name, else
245 * tries to match both
247 struct nvram_partition *nvram_find_partition(int sig, const char *name)
249 struct nvram_partition * part;
250 struct list_head * p;
252 list_for_each(p, &nvram_part->partition) {
253 part = list_entry(p, struct nvram_partition, partition);
255 if (sig && part->header.signature != sig)
257 if (name && 0 != strncmp(name, part->header.name, 12))
263 EXPORT_SYMBOL(nvram_find_partition);
266 static int nvram_remove_os_partition(void)
270 struct nvram_partition * part;
271 struct nvram_partition * cur_part;
274 list_for_each(i, &nvram_part->partition) {
275 part = list_entry(i, struct nvram_partition, partition);
276 if (part->header.signature != NVRAM_SIG_OS)
279 /* Make os partition a free partition */
280 part->header.signature = NVRAM_SIG_FREE;
281 sprintf(part->header.name, "wwwwwwwwwwww");
282 part->header.checksum = nvram_checksum(&part->header);
284 /* Merge contiguous free partitions backwards */
285 list_for_each_prev(j, &part->partition) {
286 cur_part = list_entry(j, struct nvram_partition, partition);
287 if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
291 part->header.length += cur_part->header.length;
292 part->header.checksum = nvram_checksum(&part->header);
293 part->index = cur_part->index;
295 list_del(&cur_part->partition);
297 j = &part->partition; /* fixup our loop */
300 /* Merge contiguous free partitions forwards */
301 list_for_each(j, &part->partition) {
302 cur_part = list_entry(j, struct nvram_partition, partition);
303 if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
307 part->header.length += cur_part->header.length;
308 part->header.checksum = nvram_checksum(&part->header);
310 list_del(&cur_part->partition);
312 j = &part->partition; /* fixup our loop */
315 rc = nvram_write_header(part);
317 printk(KERN_ERR "nvram_remove_os_partition: nvram_write failed (%d)\n", rc);
326 /* nvram_create_os_partition
328 * Create a OS linux partition to buffer error logs.
329 * Will create a partition starting at the first free
330 * space found if space has enough room.
332 static int nvram_create_os_partition(void)
334 struct nvram_partition *part;
335 struct nvram_partition *new_part;
336 struct nvram_partition *free_part = NULL;
337 int seq_init[2] = { 0, 0 };
342 /* Find a free partition that will give us the maximum needed size
343 If can't find one that will give us the minimum size needed */
344 list_for_each_entry(part, &nvram_part->partition, partition) {
345 if (part->header.signature != NVRAM_SIG_FREE)
348 if (part->header.length >= NVRAM_MAX_REQ) {
349 size = NVRAM_MAX_REQ;
353 if (!size && part->header.length >= NVRAM_MIN_REQ) {
354 size = NVRAM_MIN_REQ;
361 /* Create our OS partition */
362 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
364 printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n");
368 new_part->index = free_part->index;
369 new_part->header.signature = NVRAM_SIG_OS;
370 new_part->header.length = size;
371 strcpy(new_part->header.name, "ppc64,linux");
372 new_part->header.checksum = nvram_checksum(&new_part->header);
374 rc = nvram_write_header(new_part);
376 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header \
381 /* make sure and initialize to zero the sequence number and the error
383 tmp_index = new_part->index + NVRAM_HEADER_LEN;
384 rc = ppc_md.nvram_write((char *)&seq_init, sizeof(seq_init), &tmp_index);
386 printk(KERN_ERR "nvram_create_os_partition: nvram_write "
387 "failed (%d)\n", rc);
391 nvram_error_log_index = new_part->index + NVRAM_HEADER_LEN;
392 nvram_error_log_size = ((part->header.length - 1) *
393 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
395 list_add_tail(&new_part->partition, &free_part->partition);
397 if (free_part->header.length <= size) {
398 list_del(&free_part->partition);
403 /* Adjust the partition we stole the space from */
404 free_part->index += size * NVRAM_BLOCK_LEN;
405 free_part->header.length -= size;
406 free_part->header.checksum = nvram_checksum(&free_part->header);
408 rc = nvram_write_header(free_part);
410 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header "
411 "failed (%d)\n", rc);
419 /* nvram_setup_partition
421 * This will setup the partition we need for buffering the
422 * error logs and cleanup partitions if needed.
424 * The general strategy is the following:
425 * 1.) If there is ppc64,linux partition large enough then use it.
426 * 2.) If there is not a ppc64,linux partition large enough, search
427 * for a free partition that is large enough.
428 * 3.) If there is not a free partition large enough remove
429 * _all_ OS partitions and consolidate the space.
430 * 4.) Will first try getting a chunk that will satisfy the maximum
431 * error log size (NVRAM_MAX_REQ).
432 * 5.) If the max chunk cannot be allocated then try finding a chunk
433 * that will satisfy the minum needed (NVRAM_MIN_REQ).
435 static int nvram_setup_partition(void)
437 struct list_head * p;
438 struct nvram_partition * part;
441 /* For now, we don't do any of this on pmac, until I
442 * have figured out if it's worth killing some unused stuffs
443 * in our nvram, as Apple defined partitions use pretty much
446 if (_machine == PLATFORM_POWERMAC)
449 /* see if we have an OS partition that meets our needs.
450 will try getting the max we need. If not we'll delete
451 partitions and try again. */
452 list_for_each(p, &nvram_part->partition) {
453 part = list_entry(p, struct nvram_partition, partition);
454 if (part->header.signature != NVRAM_SIG_OS)
457 if (strcmp(part->header.name, "ppc64,linux"))
460 if (part->header.length >= NVRAM_MIN_REQ) {
461 /* found our partition */
462 nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
463 nvram_error_log_size = ((part->header.length - 1) *
464 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
469 /* try creating a partition with the free space we have */
470 rc = nvram_create_os_partition();
475 /* need to free up some space */
476 rc = nvram_remove_os_partition();
481 /* create a partition in this new space */
482 rc = nvram_create_os_partition();
484 printk(KERN_ERR "nvram_create_os_partition: Could not find a "
485 "NVRAM partition large enough\n");
493 static int nvram_scan_partitions(void)
495 loff_t cur_index = 0;
496 struct nvram_header phead;
497 struct nvram_partition * tmp_part;
503 if (ppc_md.nvram_size == NULL)
505 total_size = ppc_md.nvram_size();
507 header = (char *) kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
509 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
513 while (cur_index < total_size) {
515 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
516 if (err != NVRAM_HEADER_LEN) {
517 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
518 "nvram partitions\n");
522 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
524 memcpy(&phead, header, NVRAM_HEADER_LEN);
527 c_sum = nvram_checksum(&phead);
528 if (c_sum != phead.checksum) {
529 printk(KERN_WARNING "WARNING: nvram partition checksum"
530 " was %02x, should be %02x!\n",
531 phead.checksum, c_sum);
532 printk(KERN_WARNING "Terminating nvram partition scan\n");
536 printk(KERN_WARNING "WARNING: nvram corruption "
537 "detected: 0-length partition\n");
540 tmp_part = (struct nvram_partition *)
541 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
544 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
548 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
549 tmp_part->index = cur_index;
550 list_add_tail(&tmp_part->partition, &nvram_part->partition);
552 cur_index += phead.length * NVRAM_BLOCK_LEN;
561 static int __init nvram_init(void)
566 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
569 rc = misc_register(&nvram_dev);
571 printk(KERN_ERR "nvram_init: failed to register device\n");
575 /* initialize our anchor for the nvram partition list */
576 nvram_part = (struct nvram_partition *) kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
578 printk(KERN_ERR "nvram_init: Failed kmalloc\n");
581 INIT_LIST_HEAD(&nvram_part->partition);
583 /* Get all the NVRAM partitions */
584 error = nvram_scan_partitions();
586 printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
590 if(nvram_setup_partition())
591 printk(KERN_WARNING "nvram_init: Could not find nvram partition"
592 " for nvram buffered error logging.\n");
595 nvram_print_partitions("NVRAM Partitions");
601 void __exit nvram_cleanup(void)
603 misc_deregister( &nvram_dev );
607 #ifdef CONFIG_PPC_PSERIES
609 /* nvram_write_error_log
611 * We need to buffer the error logs into nvram to ensure that we have
612 * the failure information to decode. If we have a severe error there
613 * is no way to guarantee that the OS or the machine is in a state to
614 * get back to user land and write the error to disk. For example if
615 * the SCSI device driver causes a Machine Check by writing to a bad
616 * IO address, there is no way of guaranteeing that the device driver
617 * is in any state that is would also be able to write the error data
618 * captured to disk, thus we buffer it in NVRAM for analysis on the
621 * In NVRAM the partition containing the error log buffer will looks like:
623 * +-----------+----------+--------+------------+------------------+
624 * | signature | checksum | length | name | data |
625 * |0 |1 |2 3|4 15|16 length-1|
626 * +-----------+----------+--------+------------+------------------+
628 * The 'data' section would look like (in bytes):
629 * +--------------+------------+-----------------------------------+
630 * | event_logged | sequence # | error log |
631 * |0 3|4 7|8 nvram_error_log_size-1|
632 * +--------------+------------+-----------------------------------+
634 * event_logged: 0 if event has not been logged to syslog, 1 if it has
635 * sequence #: The unique sequence # for each event. (until it wraps)
636 * error log: The error log from event_scan
638 int nvram_write_error_log(char * buff, int length, unsigned int err_type)
642 struct err_log_info info;
648 if (nvram_error_log_index == -1) {
652 if (length > nvram_error_log_size) {
653 length = nvram_error_log_size;
656 info.error_type = err_type;
657 info.seq_num = error_log_cnt;
659 tmp_index = nvram_error_log_index;
661 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
663 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
667 rc = ppc_md.nvram_write(buff, length, &tmp_index);
669 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
676 /* nvram_read_error_log
678 * Reads nvram for error log for at most 'length'
680 int nvram_read_error_log(char * buff, int length, unsigned int * err_type)
684 struct err_log_info info;
686 if (nvram_error_log_index == -1)
689 if (length > nvram_error_log_size)
690 length = nvram_error_log_size;
692 tmp_index = nvram_error_log_index;
694 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
696 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
700 rc = ppc_md.nvram_read(buff, length, &tmp_index);
702 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
706 error_log_cnt = info.seq_num;
707 *err_type = info.error_type;
712 /* This doesn't actually zero anything, but it sets the event_logged
713 * word to tell that this event is safely in syslog.
715 int nvram_clear_error_log(void)
718 int clear_word = ERR_FLAG_ALREADY_LOGGED;
721 tmp_index = nvram_error_log_index;
723 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
725 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
732 #endif /* CONFIG_PPC_PSERIES */
734 module_init(nvram_init);
735 module_exit(nvram_cleanup);
736 MODULE_LICENSE("GPL");