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 struct nvram_partition * nvram_part;
38 static long nvram_error_log_index = -1;
39 static long nvram_error_log_size = 0;
41 extern volatile int error_log_cnt;
48 static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
52 if (ppc_md.nvram_size == NULL)
54 size = ppc_md.nvram_size();
58 offset += file->f_pos;
71 static ssize_t dev_nvram_read(struct file *file, char __user *buf,
72 size_t count, loff_t *ppos)
79 if (!ppc_md.nvram_size)
83 size = ppc_md.nvram_size();
84 if (*ppos >= size || size < 0)
87 count = min_t(size_t, count, size - *ppos);
88 count = min(count, PAGE_SIZE);
91 tmp = kmalloc(count, GFP_KERNEL);
95 ret = ppc_md.nvram_read(tmp, count, ppos);
99 if (copy_to_user(buf, tmp, ret))
108 static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
109 size_t count, loff_t *ppos)
116 if (!ppc_md.nvram_size)
120 size = ppc_md.nvram_size();
121 if (*ppos >= size || size < 0)
124 count = min_t(size_t, count, size - *ppos);
125 count = min(count, PAGE_SIZE);
128 tmp = kmalloc(count, GFP_KERNEL);
133 if (copy_from_user(tmp, buf, count))
136 ret = ppc_md.nvram_write(tmp, count, ppos);
144 static int dev_nvram_ioctl(struct inode *inode, struct file *file,
145 unsigned int cmd, unsigned long arg)
148 #ifdef CONFIG_PPC_PMAC
149 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
150 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
151 case IOC_NVRAM_GET_OFFSET: {
154 if (!machine_is(powermac))
156 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
158 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
160 offset = pmac_get_partition(part);
163 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
167 #endif /* CONFIG_PPC_PMAC */
173 const struct file_operations nvram_fops = {
174 .owner = THIS_MODULE,
175 .llseek = dev_nvram_llseek,
176 .read = dev_nvram_read,
177 .write = dev_nvram_write,
178 .ioctl = dev_nvram_ioctl,
181 static struct miscdevice nvram_dev = {
189 static void nvram_print_partitions(char * label)
191 struct list_head * p;
192 struct nvram_partition * tmp_part;
194 printk(KERN_WARNING "--------%s---------\n", label);
195 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
196 list_for_each(p, &nvram_part->partition) {
197 tmp_part = list_entry(p, struct nvram_partition, partition);
198 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%s\n",
199 tmp_part->index, tmp_part->header.signature,
200 tmp_part->header.checksum, tmp_part->header.length,
201 tmp_part->header.name);
207 static int nvram_write_header(struct nvram_partition * part)
212 tmp_index = part->index;
213 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
219 static unsigned char nvram_checksum(struct nvram_header *p)
221 unsigned int c_sum, c_sum2;
222 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
223 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
225 /* The sum may have spilled into the 3rd byte. Fold it back. */
226 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
227 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
228 c_sum2 = (c_sum >> 8) + (c_sum << 8);
229 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
235 * Find an nvram partition, sig can be 0 for any
236 * partition or name can be NULL for any name, else
237 * tries to match both
239 struct nvram_partition *nvram_find_partition(int sig, const char *name)
241 struct nvram_partition * part;
242 struct list_head * p;
244 list_for_each(p, &nvram_part->partition) {
245 part = list_entry(p, struct nvram_partition, partition);
247 if (sig && part->header.signature != sig)
249 if (name && 0 != strncmp(name, part->header.name, 12))
255 EXPORT_SYMBOL(nvram_find_partition);
258 static int nvram_remove_os_partition(void)
262 struct nvram_partition * part;
263 struct nvram_partition * cur_part;
266 list_for_each(i, &nvram_part->partition) {
267 part = list_entry(i, struct nvram_partition, partition);
268 if (part->header.signature != NVRAM_SIG_OS)
271 /* Make os partition a free partition */
272 part->header.signature = NVRAM_SIG_FREE;
273 sprintf(part->header.name, "wwwwwwwwwwww");
274 part->header.checksum = nvram_checksum(&part->header);
276 /* Merge contiguous free partitions backwards */
277 list_for_each_prev(j, &part->partition) {
278 cur_part = list_entry(j, struct nvram_partition, partition);
279 if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
283 part->header.length += cur_part->header.length;
284 part->header.checksum = nvram_checksum(&part->header);
285 part->index = cur_part->index;
287 list_del(&cur_part->partition);
289 j = &part->partition; /* fixup our loop */
292 /* Merge contiguous free partitions forwards */
293 list_for_each(j, &part->partition) {
294 cur_part = list_entry(j, struct nvram_partition, partition);
295 if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
299 part->header.length += cur_part->header.length;
300 part->header.checksum = nvram_checksum(&part->header);
302 list_del(&cur_part->partition);
304 j = &part->partition; /* fixup our loop */
307 rc = nvram_write_header(part);
309 printk(KERN_ERR "nvram_remove_os_partition: nvram_write failed (%d)\n", rc);
318 /* nvram_create_os_partition
320 * Create a OS linux partition to buffer error logs.
321 * Will create a partition starting at the first free
322 * space found if space has enough room.
324 static int nvram_create_os_partition(void)
326 struct nvram_partition *part;
327 struct nvram_partition *new_part;
328 struct nvram_partition *free_part = NULL;
329 int seq_init[2] = { 0, 0 };
334 /* Find a free partition that will give us the maximum needed size
335 If can't find one that will give us the minimum size needed */
336 list_for_each_entry(part, &nvram_part->partition, partition) {
337 if (part->header.signature != NVRAM_SIG_FREE)
340 if (part->header.length >= NVRAM_MAX_REQ) {
341 size = NVRAM_MAX_REQ;
345 if (!size && part->header.length >= NVRAM_MIN_REQ) {
346 size = NVRAM_MIN_REQ;
353 /* Create our OS partition */
354 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
356 printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n");
360 new_part->index = free_part->index;
361 new_part->header.signature = NVRAM_SIG_OS;
362 new_part->header.length = size;
363 strcpy(new_part->header.name, "ppc64,linux");
364 new_part->header.checksum = nvram_checksum(&new_part->header);
366 rc = nvram_write_header(new_part);
368 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header \
373 /* make sure and initialize to zero the sequence number and the error
375 tmp_index = new_part->index + NVRAM_HEADER_LEN;
376 rc = ppc_md.nvram_write((char *)&seq_init, sizeof(seq_init), &tmp_index);
378 printk(KERN_ERR "nvram_create_os_partition: nvram_write "
379 "failed (%d)\n", rc);
383 nvram_error_log_index = new_part->index + NVRAM_HEADER_LEN;
384 nvram_error_log_size = ((part->header.length - 1) *
385 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
387 list_add_tail(&new_part->partition, &free_part->partition);
389 if (free_part->header.length <= size) {
390 list_del(&free_part->partition);
395 /* Adjust the partition we stole the space from */
396 free_part->index += size * NVRAM_BLOCK_LEN;
397 free_part->header.length -= size;
398 free_part->header.checksum = nvram_checksum(&free_part->header);
400 rc = nvram_write_header(free_part);
402 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header "
403 "failed (%d)\n", rc);
411 /* nvram_setup_partition
413 * This will setup the partition we need for buffering the
414 * error logs and cleanup partitions if needed.
416 * The general strategy is the following:
417 * 1.) If there is ppc64,linux partition large enough then use it.
418 * 2.) If there is not a ppc64,linux partition large enough, search
419 * for a free partition that is large enough.
420 * 3.) If there is not a free partition large enough remove
421 * _all_ OS partitions and consolidate the space.
422 * 4.) Will first try getting a chunk that will satisfy the maximum
423 * error log size (NVRAM_MAX_REQ).
424 * 5.) If the max chunk cannot be allocated then try finding a chunk
425 * that will satisfy the minum needed (NVRAM_MIN_REQ).
427 static int nvram_setup_partition(void)
429 struct list_head * p;
430 struct nvram_partition * part;
433 /* For now, we don't do any of this on pmac, until I
434 * have figured out if it's worth killing some unused stuffs
435 * in our nvram, as Apple defined partitions use pretty much
438 if (machine_is(powermac))
441 /* see if we have an OS partition that meets our needs.
442 will try getting the max we need. If not we'll delete
443 partitions and try again. */
444 list_for_each(p, &nvram_part->partition) {
445 part = list_entry(p, struct nvram_partition, partition);
446 if (part->header.signature != NVRAM_SIG_OS)
449 if (strcmp(part->header.name, "ppc64,linux"))
452 if (part->header.length >= NVRAM_MIN_REQ) {
453 /* found our partition */
454 nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
455 nvram_error_log_size = ((part->header.length - 1) *
456 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
461 /* try creating a partition with the free space we have */
462 rc = nvram_create_os_partition();
467 /* need to free up some space */
468 rc = nvram_remove_os_partition();
473 /* create a partition in this new space */
474 rc = nvram_create_os_partition();
476 printk(KERN_ERR "nvram_create_os_partition: Could not find a "
477 "NVRAM partition large enough\n");
485 static int nvram_scan_partitions(void)
487 loff_t cur_index = 0;
488 struct nvram_header phead;
489 struct nvram_partition * tmp_part;
495 if (ppc_md.nvram_size == NULL)
497 total_size = ppc_md.nvram_size();
499 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
501 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
505 while (cur_index < total_size) {
507 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
508 if (err != NVRAM_HEADER_LEN) {
509 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
510 "nvram partitions\n");
514 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
516 memcpy(&phead, header, NVRAM_HEADER_LEN);
519 c_sum = nvram_checksum(&phead);
520 if (c_sum != phead.checksum) {
521 printk(KERN_WARNING "WARNING: nvram partition checksum"
522 " was %02x, should be %02x!\n",
523 phead.checksum, c_sum);
524 printk(KERN_WARNING "Terminating nvram partition scan\n");
528 printk(KERN_WARNING "WARNING: nvram corruption "
529 "detected: 0-length partition\n");
532 tmp_part = (struct nvram_partition *)
533 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
536 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
540 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
541 tmp_part->index = cur_index;
542 list_add_tail(&tmp_part->partition, &nvram_part->partition);
544 cur_index += phead.length * NVRAM_BLOCK_LEN;
553 static int __init nvram_init(void)
558 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
561 rc = misc_register(&nvram_dev);
563 printk(KERN_ERR "nvram_init: failed to register device\n");
567 /* initialize our anchor for the nvram partition list */
568 nvram_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
570 printk(KERN_ERR "nvram_init: Failed kmalloc\n");
573 INIT_LIST_HEAD(&nvram_part->partition);
575 /* Get all the NVRAM partitions */
576 error = nvram_scan_partitions();
578 printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
582 if(nvram_setup_partition())
583 printk(KERN_WARNING "nvram_init: Could not find nvram partition"
584 " for nvram buffered error logging.\n");
587 nvram_print_partitions("NVRAM Partitions");
593 void __exit nvram_cleanup(void)
595 misc_deregister( &nvram_dev );
599 #ifdef CONFIG_PPC_PSERIES
601 /* nvram_write_error_log
603 * We need to buffer the error logs into nvram to ensure that we have
604 * the failure information to decode. If we have a severe error there
605 * is no way to guarantee that the OS or the machine is in a state to
606 * get back to user land and write the error to disk. For example if
607 * the SCSI device driver causes a Machine Check by writing to a bad
608 * IO address, there is no way of guaranteeing that the device driver
609 * is in any state that is would also be able to write the error data
610 * captured to disk, thus we buffer it in NVRAM for analysis on the
613 * In NVRAM the partition containing the error log buffer will looks like:
615 * +-----------+----------+--------+------------+------------------+
616 * | signature | checksum | length | name | data |
617 * |0 |1 |2 3|4 15|16 length-1|
618 * +-----------+----------+--------+------------+------------------+
620 * The 'data' section would look like (in bytes):
621 * +--------------+------------+-----------------------------------+
622 * | event_logged | sequence # | error log |
623 * |0 3|4 7|8 nvram_error_log_size-1|
624 * +--------------+------------+-----------------------------------+
626 * event_logged: 0 if event has not been logged to syslog, 1 if it has
627 * sequence #: The unique sequence # for each event. (until it wraps)
628 * error log: The error log from event_scan
630 int nvram_write_error_log(char * buff, int length, unsigned int err_type)
634 struct err_log_info info;
636 if (nvram_error_log_index == -1) {
640 if (length > nvram_error_log_size) {
641 length = nvram_error_log_size;
644 info.error_type = err_type;
645 info.seq_num = error_log_cnt;
647 tmp_index = nvram_error_log_index;
649 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
651 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
655 rc = ppc_md.nvram_write(buff, length, &tmp_index);
657 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
664 /* nvram_read_error_log
666 * Reads nvram for error log for at most 'length'
668 int nvram_read_error_log(char * buff, int length, unsigned int * err_type)
672 struct err_log_info info;
674 if (nvram_error_log_index == -1)
677 if (length > nvram_error_log_size)
678 length = nvram_error_log_size;
680 tmp_index = nvram_error_log_index;
682 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
684 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
688 rc = ppc_md.nvram_read(buff, length, &tmp_index);
690 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
694 error_log_cnt = info.seq_num;
695 *err_type = info.error_type;
700 /* This doesn't actually zero anything, but it sets the event_logged
701 * word to tell that this event is safely in syslog.
703 int nvram_clear_error_log(void)
706 int clear_word = ERR_FLAG_ALREADY_LOGGED;
709 tmp_index = nvram_error_log_index;
711 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
713 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
720 #endif /* CONFIG_PPC_PSERIES */
722 module_init(nvram_init);
723 module_exit(nvram_cleanup);
724 MODULE_LICENSE("GPL");