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 #define NVRAM_HEADER_LEN 16 /* sizeof(struct nvram_header) */
38 #define NVRAM_BLOCK_LEN 16
39 #define NVRAM_MAX_REQ (2080/NVRAM_BLOCK_LEN)
40 #define NVRAM_MIN_REQ (1056/NVRAM_BLOCK_LEN)
42 /* If change this size, then change the size of NVNAME_LEN */
44 unsigned char signature;
45 unsigned char checksum;
46 unsigned short length;
50 struct nvram_partition {
51 struct list_head partition;
52 struct nvram_header header;
56 static struct nvram_partition * nvram_part;
57 static long nvram_error_log_index = -1;
58 static long nvram_error_log_size = 0;
65 static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
69 if (ppc_md.nvram_size == NULL)
71 size = ppc_md.nvram_size();
75 offset += file->f_pos;
88 static ssize_t dev_nvram_read(struct file *file, char __user *buf,
89 size_t count, loff_t *ppos)
96 if (!ppc_md.nvram_size)
100 size = ppc_md.nvram_size();
101 if (*ppos >= size || size < 0)
104 count = min_t(size_t, count, size - *ppos);
105 count = min(count, PAGE_SIZE);
108 tmp = kmalloc(count, GFP_KERNEL);
112 ret = ppc_md.nvram_read(tmp, count, ppos);
116 if (copy_to_user(buf, tmp, ret))
125 static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
126 size_t count, loff_t *ppos)
133 if (!ppc_md.nvram_size)
137 size = ppc_md.nvram_size();
138 if (*ppos >= size || size < 0)
141 count = min_t(size_t, count, size - *ppos);
142 count = min(count, PAGE_SIZE);
145 tmp = kmalloc(count, GFP_KERNEL);
150 if (copy_from_user(tmp, buf, count))
153 ret = ppc_md.nvram_write(tmp, count, ppos);
161 static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
165 #ifdef CONFIG_PPC_PMAC
166 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
167 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
168 case IOC_NVRAM_GET_OFFSET: {
171 if (!machine_is(powermac))
173 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
175 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
177 offset = pmac_get_partition(part);
180 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
184 #endif /* CONFIG_PPC_PMAC */
190 const struct file_operations nvram_fops = {
191 .owner = THIS_MODULE,
192 .llseek = dev_nvram_llseek,
193 .read = dev_nvram_read,
194 .write = dev_nvram_write,
195 .unlocked_ioctl = dev_nvram_ioctl,
198 static struct miscdevice nvram_dev = {
206 static void __init nvram_print_partitions(char * label)
208 struct list_head * p;
209 struct nvram_partition * tmp_part;
211 printk(KERN_WARNING "--------%s---------\n", label);
212 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
213 list_for_each(p, &nvram_part->partition) {
214 tmp_part = list_entry(p, struct nvram_partition, partition);
215 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%s\n",
216 tmp_part->index, tmp_part->header.signature,
217 tmp_part->header.checksum, tmp_part->header.length,
218 tmp_part->header.name);
224 static int __init nvram_write_header(struct nvram_partition * part)
229 tmp_index = part->index;
230 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
236 static unsigned char __init nvram_checksum(struct nvram_header *p)
238 unsigned int c_sum, c_sum2;
239 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
240 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
242 /* The sum may have spilled into the 3rd byte. Fold it back. */
243 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
244 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
245 c_sum2 = (c_sum >> 8) + (c_sum << 8);
246 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
250 static int __init nvram_remove_os_partition(void)
254 struct nvram_partition * part;
255 struct nvram_partition * cur_part;
258 list_for_each(i, &nvram_part->partition) {
259 part = list_entry(i, struct nvram_partition, partition);
260 if (part->header.signature != NVRAM_SIG_OS)
263 /* Make os partition a free partition */
264 part->header.signature = NVRAM_SIG_FREE;
265 sprintf(part->header.name, "wwwwwwwwwwww");
266 part->header.checksum = nvram_checksum(&part->header);
268 /* Merge contiguous free partitions backwards */
269 list_for_each_prev(j, &part->partition) {
270 cur_part = list_entry(j, struct nvram_partition, partition);
271 if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
275 part->header.length += cur_part->header.length;
276 part->header.checksum = nvram_checksum(&part->header);
277 part->index = cur_part->index;
279 list_del(&cur_part->partition);
281 j = &part->partition; /* fixup our loop */
284 /* Merge contiguous free partitions forwards */
285 list_for_each(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);
294 list_del(&cur_part->partition);
296 j = &part->partition; /* fixup our loop */
299 rc = nvram_write_header(part);
301 printk(KERN_ERR "nvram_remove_os_partition: nvram_write failed (%d)\n", rc);
310 /* nvram_create_os_partition
312 * Create a OS linux partition to buffer error logs.
313 * Will create a partition starting at the first free
314 * space found if space has enough room.
316 static int __init nvram_create_os_partition(void)
318 struct nvram_partition *part;
319 struct nvram_partition *new_part;
320 struct nvram_partition *free_part = NULL;
321 int seq_init[2] = { 0, 0 };
326 /* Find a free partition that will give us the maximum needed size
327 If can't find one that will give us the minimum size needed */
328 list_for_each_entry(part, &nvram_part->partition, partition) {
329 if (part->header.signature != NVRAM_SIG_FREE)
332 if (part->header.length >= NVRAM_MAX_REQ) {
333 size = NVRAM_MAX_REQ;
337 if (!size && part->header.length >= NVRAM_MIN_REQ) {
338 size = NVRAM_MIN_REQ;
345 /* Create our OS partition */
346 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
348 printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n");
352 new_part->index = free_part->index;
353 new_part->header.signature = NVRAM_SIG_OS;
354 new_part->header.length = size;
355 strcpy(new_part->header.name, "ppc64,linux");
356 new_part->header.checksum = nvram_checksum(&new_part->header);
358 rc = nvram_write_header(new_part);
360 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header "
361 "failed (%d)\n", rc);
365 /* make sure and initialize to zero the sequence number and the error
367 tmp_index = new_part->index + NVRAM_HEADER_LEN;
368 rc = ppc_md.nvram_write((char *)&seq_init, sizeof(seq_init), &tmp_index);
370 printk(KERN_ERR "nvram_create_os_partition: nvram_write "
371 "failed (%d)\n", rc);
375 nvram_error_log_index = new_part->index + NVRAM_HEADER_LEN;
376 nvram_error_log_size = ((part->header.length - 1) *
377 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
379 list_add_tail(&new_part->partition, &free_part->partition);
381 if (free_part->header.length <= size) {
382 list_del(&free_part->partition);
387 /* Adjust the partition we stole the space from */
388 free_part->index += size * NVRAM_BLOCK_LEN;
389 free_part->header.length -= size;
390 free_part->header.checksum = nvram_checksum(&free_part->header);
392 rc = nvram_write_header(free_part);
394 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header "
395 "failed (%d)\n", rc);
403 /* nvram_setup_partition
405 * This will setup the partition we need for buffering the
406 * error logs and cleanup partitions if needed.
408 * The general strategy is the following:
409 * 1.) If there is ppc64,linux partition large enough then use it.
410 * 2.) If there is not a ppc64,linux partition large enough, search
411 * for a free partition that is large enough.
412 * 3.) If there is not a free partition large enough remove
413 * _all_ OS partitions and consolidate the space.
414 * 4.) Will first try getting a chunk that will satisfy the maximum
415 * error log size (NVRAM_MAX_REQ).
416 * 5.) If the max chunk cannot be allocated then try finding a chunk
417 * that will satisfy the minum needed (NVRAM_MIN_REQ).
419 static int __init nvram_setup_partition(void)
421 struct list_head * p;
422 struct nvram_partition * part;
425 /* For now, we don't do any of this on pmac, until I
426 * have figured out if it's worth killing some unused stuffs
427 * in our nvram, as Apple defined partitions use pretty much
430 if (machine_is(powermac))
433 /* see if we have an OS partition that meets our needs.
434 will try getting the max we need. If not we'll delete
435 partitions and try again. */
436 list_for_each(p, &nvram_part->partition) {
437 part = list_entry(p, struct nvram_partition, partition);
438 if (part->header.signature != NVRAM_SIG_OS)
441 if (strcmp(part->header.name, "ppc64,linux"))
444 if (part->header.length >= NVRAM_MIN_REQ) {
445 /* found our partition */
446 nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
447 nvram_error_log_size = ((part->header.length - 1) *
448 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
453 /* try creating a partition with the free space we have */
454 rc = nvram_create_partition("ppc64,linux", );
459 /* need to free up some space */
460 rc = nvram_remove_os_partition();
465 /* create a partition in this new space */
466 rc = nvram_create_os_partition();
468 printk(KERN_ERR "nvram_create_os_partition: Could not find a "
469 "NVRAM partition large enough\n");
477 static int __init nvram_scan_partitions(void)
479 loff_t cur_index = 0;
480 struct nvram_header phead;
481 struct nvram_partition * tmp_part;
487 if (ppc_md.nvram_size == NULL)
489 total_size = ppc_md.nvram_size();
491 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
493 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
497 while (cur_index < total_size) {
499 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
500 if (err != NVRAM_HEADER_LEN) {
501 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
502 "nvram partitions\n");
506 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
508 memcpy(&phead, header, NVRAM_HEADER_LEN);
511 c_sum = nvram_checksum(&phead);
512 if (c_sum != phead.checksum) {
513 printk(KERN_WARNING "WARNING: nvram partition checksum"
514 " was %02x, should be %02x!\n",
515 phead.checksum, c_sum);
516 printk(KERN_WARNING "Terminating nvram partition scan\n");
520 printk(KERN_WARNING "WARNING: nvram corruption "
521 "detected: 0-length partition\n");
524 tmp_part = (struct nvram_partition *)
525 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
528 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
532 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
533 tmp_part->index = cur_index;
534 list_add_tail(&tmp_part->partition, &nvram_part->partition);
536 cur_index += phead.length * NVRAM_BLOCK_LEN;
545 static int __init nvram_init(void)
550 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
553 rc = misc_register(&nvram_dev);
555 printk(KERN_ERR "nvram_init: failed to register device\n");
559 /* initialize our anchor for the nvram partition list */
560 nvram_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
562 printk(KERN_ERR "nvram_init: Failed kmalloc\n");
565 INIT_LIST_HEAD(&nvram_part->partition);
567 /* Get all the NVRAM partitions */
568 error = nvram_scan_partitions();
570 printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
574 if(nvram_setup_partition())
575 printk(KERN_WARNING "nvram_init: Could not find nvram partition"
576 " for nvram buffered error logging.\n");
579 nvram_print_partitions("NVRAM Partitions");
585 void __exit nvram_cleanup(void)
587 misc_deregister( &nvram_dev );
591 #ifdef CONFIG_PPC_PSERIES
593 /* nvram_write_error_log
595 * We need to buffer the error logs into nvram to ensure that we have
596 * the failure information to decode. If we have a severe error there
597 * is no way to guarantee that the OS or the machine is in a state to
598 * get back to user land and write the error to disk. For example if
599 * the SCSI device driver causes a Machine Check by writing to a bad
600 * IO address, there is no way of guaranteeing that the device driver
601 * is in any state that is would also be able to write the error data
602 * captured to disk, thus we buffer it in NVRAM for analysis on the
605 * In NVRAM the partition containing the error log buffer will looks like:
607 * +-----------+----------+--------+------------+------------------+
608 * | signature | checksum | length | name | data |
609 * |0 |1 |2 3|4 15|16 length-1|
610 * +-----------+----------+--------+------------+------------------+
612 * The 'data' section would look like (in bytes):
613 * +--------------+------------+-----------------------------------+
614 * | event_logged | sequence # | error log |
615 * |0 3|4 7|8 nvram_error_log_size-1|
616 * +--------------+------------+-----------------------------------+
618 * event_logged: 0 if event has not been logged to syslog, 1 if it has
619 * sequence #: The unique sequence # for each event. (until it wraps)
620 * error log: The error log from event_scan
622 int nvram_write_error_log(char * buff, int length,
623 unsigned int err_type, unsigned int error_log_cnt)
627 struct err_log_info info;
629 if (nvram_error_log_index == -1) {
633 if (length > nvram_error_log_size) {
634 length = nvram_error_log_size;
637 info.error_type = err_type;
638 info.seq_num = error_log_cnt;
640 tmp_index = nvram_error_log_index;
642 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
644 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
648 rc = ppc_md.nvram_write(buff, length, &tmp_index);
650 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
657 /* nvram_read_error_log
659 * Reads nvram for error log for at most 'length'
661 int nvram_read_error_log(char * buff, int length,
662 unsigned int * err_type, unsigned int * error_log_cnt)
666 struct err_log_info info;
668 if (nvram_error_log_index == -1)
671 if (length > nvram_error_log_size)
672 length = nvram_error_log_size;
674 tmp_index = nvram_error_log_index;
676 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
678 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
682 rc = ppc_md.nvram_read(buff, length, &tmp_index);
684 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
688 *error_log_cnt = info.seq_num;
689 *err_type = info.error_type;
694 /* This doesn't actually zero anything, but it sets the event_logged
695 * word to tell that this event is safely in syslog.
697 int nvram_clear_error_log(void)
700 int clear_word = ERR_FLAG_ALREADY_LOGGED;
703 if (nvram_error_log_index == -1)
706 tmp_index = nvram_error_log_index;
708 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
710 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
717 #endif /* CONFIG_PPC_PSERIES */
719 module_init(nvram_init);
720 module_exit(nvram_cleanup);
721 MODULE_LICENSE("GPL");