powerpc: Move remaining .c files from arch/ppc64 to arch/powerpc
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / powerpc / kernel / nvram_64.c
blobc0fcd29918cec0b3fea13b26177877807f076e24
1 /*
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
15 * parsing code.
18 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/errno.h>
22 #include <linux/fs.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>
31 #include <asm/rtas.h>
32 #include <asm/prom.h>
33 #include <asm/machdep.h>
35 #undef DEBUG_NVRAM
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
48 * anything */
50 extern volatile int error_log_cnt;
52 struct err_log_info {
53 int error_type;
54 unsigned int seq_num;
57 static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
59 int size;
61 if (ppc_md.nvram_size == NULL)
62 return -ENODEV;
63 size = ppc_md.nvram_size();
65 switch (origin) {
66 case 1:
67 offset += file->f_pos;
68 break;
69 case 2:
70 offset += size;
71 break;
73 if (offset < 0)
74 return -EINVAL;
75 file->f_pos = offset;
76 return file->f_pos;
80 static ssize_t dev_nvram_read(struct file *file, char __user *buf,
81 size_t count, loff_t *ppos)
83 ssize_t len;
84 char *tmp_buffer;
85 int size;
87 if (ppc_md.nvram_size == NULL)
88 return -ENODEV;
89 size = ppc_md.nvram_size();
91 if (!access_ok(VERIFY_WRITE, buf, count))
92 return -EFAULT;
93 if (*ppos >= size)
94 return 0;
95 if (count > size)
96 count = size;
98 tmp_buffer = (char *) kmalloc(count, GFP_KERNEL);
99 if (!tmp_buffer) {
100 printk(KERN_ERR "dev_read_nvram: kmalloc failed\n");
101 return -ENOMEM;
104 len = ppc_md.nvram_read(tmp_buffer, count, ppos);
105 if ((long)len <= 0) {
106 kfree(tmp_buffer);
107 return len;
110 if (copy_to_user(buf, tmp_buffer, len)) {
111 kfree(tmp_buffer);
112 return -EFAULT;
115 kfree(tmp_buffer);
116 return len;
120 static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
121 size_t count, loff_t *ppos)
123 ssize_t len;
124 char * tmp_buffer;
125 int size;
127 if (ppc_md.nvram_size == NULL)
128 return -ENODEV;
129 size = ppc_md.nvram_size();
131 if (!access_ok(VERIFY_READ, buf, count))
132 return -EFAULT;
133 if (*ppos >= size)
134 return 0;
135 if (count > size)
136 count = size;
138 tmp_buffer = (char *) kmalloc(count, GFP_KERNEL);
139 if (!tmp_buffer) {
140 printk(KERN_ERR "dev_nvram_write: kmalloc failed\n");
141 return -ENOMEM;
144 if (copy_from_user(tmp_buffer, buf, count)) {
145 kfree(tmp_buffer);
146 return -EFAULT;
149 len = ppc_md.nvram_write(tmp_buffer, count, ppos);
150 if ((long)len <= 0) {
151 kfree(tmp_buffer);
152 return len;
155 kfree(tmp_buffer);
156 return len;
159 static int dev_nvram_ioctl(struct inode *inode, struct file *file,
160 unsigned int cmd, unsigned long arg)
162 switch(cmd) {
163 #ifdef CONFIG_PPC_PMAC
164 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
165 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
166 case IOC_NVRAM_GET_OFFSET: {
167 int part, offset;
169 if (_machine != PLATFORM_POWERMAC)
170 return -EINVAL;
171 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
172 return -EFAULT;
173 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
174 return -EINVAL;
175 offset = pmac_get_partition(part);
176 if (offset < 0)
177 return offset;
178 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
179 return -EFAULT;
180 return 0;
182 #endif /* CONFIG_PPC_PMAC */
184 return -EINVAL;
187 struct file_operations nvram_fops = {
188 .owner = THIS_MODULE,
189 .llseek = dev_nvram_llseek,
190 .read = dev_nvram_read,
191 .write = dev_nvram_write,
192 .ioctl = dev_nvram_ioctl,
195 static struct miscdevice nvram_dev = {
196 NVRAM_MINOR,
197 "nvram",
198 &nvram_fops
202 #ifdef DEBUG_NVRAM
203 static void nvram_print_partitions(char * label)
205 struct list_head * p;
206 struct nvram_partition * tmp_part;
208 printk(KERN_WARNING "--------%s---------\n", label);
209 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
210 list_for_each(p, &nvram_part->partition) {
211 tmp_part = list_entry(p, struct nvram_partition, partition);
212 printk(KERN_WARNING "%d \t%02x\t%02x\t%d\t%s\n",
213 tmp_part->index, tmp_part->header.signature,
214 tmp_part->header.checksum, tmp_part->header.length,
215 tmp_part->header.name);
218 #endif
221 static int nvram_write_header(struct nvram_partition * part)
223 loff_t tmp_index;
224 int rc;
226 tmp_index = part->index;
227 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
229 return rc;
233 static unsigned char nvram_checksum(struct nvram_header *p)
235 unsigned int c_sum, c_sum2;
236 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
237 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
239 /* The sum may have spilled into the 3rd byte. Fold it back. */
240 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
241 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
242 c_sum2 = (c_sum >> 8) + (c_sum << 8);
243 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
244 return c_sum;
249 * Find an nvram partition, sig can be 0 for any
250 * partition or name can be NULL for any name, else
251 * tries to match both
253 struct nvram_partition *nvram_find_partition(int sig, const char *name)
255 struct nvram_partition * part;
256 struct list_head * p;
258 list_for_each(p, &nvram_part->partition) {
259 part = list_entry(p, struct nvram_partition, partition);
261 if (sig && part->header.signature != sig)
262 continue;
263 if (name && 0 != strncmp(name, part->header.name, 12))
264 continue;
265 return part;
267 return NULL;
269 EXPORT_SYMBOL(nvram_find_partition);
272 static int nvram_remove_os_partition(void)
274 struct list_head *i;
275 struct list_head *j;
276 struct nvram_partition * part;
277 struct nvram_partition * cur_part;
278 int rc;
280 list_for_each(i, &nvram_part->partition) {
281 part = list_entry(i, struct nvram_partition, partition);
282 if (part->header.signature != NVRAM_SIG_OS)
283 continue;
285 /* Make os partition a free partition */
286 part->header.signature = NVRAM_SIG_FREE;
287 sprintf(part->header.name, "wwwwwwwwwwww");
288 part->header.checksum = nvram_checksum(&part->header);
290 /* Merge contiguous free partitions backwards */
291 list_for_each_prev(j, &part->partition) {
292 cur_part = list_entry(j, struct nvram_partition, partition);
293 if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
294 break;
297 part->header.length += cur_part->header.length;
298 part->header.checksum = nvram_checksum(&part->header);
299 part->index = cur_part->index;
301 list_del(&cur_part->partition);
302 kfree(cur_part);
303 j = &part->partition; /* fixup our loop */
306 /* Merge contiguous free partitions forwards */
307 list_for_each(j, &part->partition) {
308 cur_part = list_entry(j, struct nvram_partition, partition);
309 if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
310 break;
313 part->header.length += cur_part->header.length;
314 part->header.checksum = nvram_checksum(&part->header);
316 list_del(&cur_part->partition);
317 kfree(cur_part);
318 j = &part->partition; /* fixup our loop */
321 rc = nvram_write_header(part);
322 if (rc <= 0) {
323 printk(KERN_ERR "nvram_remove_os_partition: nvram_write failed (%d)\n", rc);
324 return rc;
329 return 0;
332 /* nvram_create_os_partition
334 * Create a OS linux partition to buffer error logs.
335 * Will create a partition starting at the first free
336 * space found if space has enough room.
338 static int nvram_create_os_partition(void)
340 struct nvram_partition *part;
341 struct nvram_partition *new_part;
342 struct nvram_partition *free_part = NULL;
343 int seq_init[2] = { 0, 0 };
344 loff_t tmp_index;
345 long size = 0;
346 int rc;
348 /* Find a free partition that will give us the maximum needed size
349 If can't find one that will give us the minimum size needed */
350 list_for_each_entry(part, &nvram_part->partition, partition) {
351 if (part->header.signature != NVRAM_SIG_FREE)
352 continue;
354 if (part->header.length >= NVRAM_MAX_REQ) {
355 size = NVRAM_MAX_REQ;
356 free_part = part;
357 break;
359 if (!size && part->header.length >= NVRAM_MIN_REQ) {
360 size = NVRAM_MIN_REQ;
361 free_part = part;
364 if (!size)
365 return -ENOSPC;
367 /* Create our OS partition */
368 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
369 if (!new_part) {
370 printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n");
371 return -ENOMEM;
374 new_part->index = free_part->index;
375 new_part->header.signature = NVRAM_SIG_OS;
376 new_part->header.length = size;
377 strcpy(new_part->header.name, "ppc64,linux");
378 new_part->header.checksum = nvram_checksum(&new_part->header);
380 rc = nvram_write_header(new_part);
381 if (rc <= 0) {
382 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header \
383 failed (%d)\n", rc);
384 return rc;
387 /* make sure and initialize to zero the sequence number and the error
388 type logged */
389 tmp_index = new_part->index + NVRAM_HEADER_LEN;
390 rc = ppc_md.nvram_write((char *)&seq_init, sizeof(seq_init), &tmp_index);
391 if (rc <= 0) {
392 printk(KERN_ERR "nvram_create_os_partition: nvram_write "
393 "failed (%d)\n", rc);
394 return rc;
397 nvram_error_log_index = new_part->index + NVRAM_HEADER_LEN;
398 nvram_error_log_size = ((part->header.length - 1) *
399 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
401 list_add_tail(&new_part->partition, &free_part->partition);
403 if (free_part->header.length <= size) {
404 list_del(&free_part->partition);
405 kfree(free_part);
406 return 0;
409 /* Adjust the partition we stole the space from */
410 free_part->index += size * NVRAM_BLOCK_LEN;
411 free_part->header.length -= size;
412 free_part->header.checksum = nvram_checksum(&free_part->header);
414 rc = nvram_write_header(free_part);
415 if (rc <= 0) {
416 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header "
417 "failed (%d)\n", rc);
418 return rc;
421 return 0;
425 /* nvram_setup_partition
427 * This will setup the partition we need for buffering the
428 * error logs and cleanup partitions if needed.
430 * The general strategy is the following:
431 * 1.) If there is ppc64,linux partition large enough then use it.
432 * 2.) If there is not a ppc64,linux partition large enough, search
433 * for a free partition that is large enough.
434 * 3.) If there is not a free partition large enough remove
435 * _all_ OS partitions and consolidate the space.
436 * 4.) Will first try getting a chunk that will satisfy the maximum
437 * error log size (NVRAM_MAX_REQ).
438 * 5.) If the max chunk cannot be allocated then try finding a chunk
439 * that will satisfy the minum needed (NVRAM_MIN_REQ).
441 static int nvram_setup_partition(void)
443 struct list_head * p;
444 struct nvram_partition * part;
445 int rc;
447 /* For now, we don't do any of this on pmac, until I
448 * have figured out if it's worth killing some unused stuffs
449 * in our nvram, as Apple defined partitions use pretty much
450 * all of the space
452 if (_machine == PLATFORM_POWERMAC)
453 return -ENOSPC;
455 /* see if we have an OS partition that meets our needs.
456 will try getting the max we need. If not we'll delete
457 partitions and try again. */
458 list_for_each(p, &nvram_part->partition) {
459 part = list_entry(p, struct nvram_partition, partition);
460 if (part->header.signature != NVRAM_SIG_OS)
461 continue;
463 if (strcmp(part->header.name, "ppc64,linux"))
464 continue;
466 if (part->header.length >= NVRAM_MIN_REQ) {
467 /* found our partition */
468 nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
469 nvram_error_log_size = ((part->header.length - 1) *
470 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
471 return 0;
475 /* try creating a partition with the free space we have */
476 rc = nvram_create_os_partition();
477 if (!rc) {
478 return 0;
481 /* need to free up some space */
482 rc = nvram_remove_os_partition();
483 if (rc) {
484 return rc;
487 /* create a partition in this new space */
488 rc = nvram_create_os_partition();
489 if (rc) {
490 printk(KERN_ERR "nvram_create_os_partition: Could not find a "
491 "NVRAM partition large enough\n");
492 return rc;
495 return 0;
499 static int nvram_scan_partitions(void)
501 loff_t cur_index = 0;
502 struct nvram_header phead;
503 struct nvram_partition * tmp_part;
504 unsigned char c_sum;
505 char * header;
506 int total_size;
507 int err;
509 if (ppc_md.nvram_size == NULL)
510 return -ENODEV;
511 total_size = ppc_md.nvram_size();
513 header = (char *) kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
514 if (!header) {
515 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
516 return -ENOMEM;
519 while (cur_index < total_size) {
521 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
522 if (err != NVRAM_HEADER_LEN) {
523 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
524 "nvram partitions\n");
525 goto out;
528 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
530 memcpy(&phead, header, NVRAM_HEADER_LEN);
532 err = 0;
533 c_sum = nvram_checksum(&phead);
534 if (c_sum != phead.checksum) {
535 printk(KERN_WARNING "WARNING: nvram partition checksum"
536 " was %02x, should be %02x!\n",
537 phead.checksum, c_sum);
538 printk(KERN_WARNING "Terminating nvram partition scan\n");
539 goto out;
541 if (!phead.length) {
542 printk(KERN_WARNING "WARNING: nvram corruption "
543 "detected: 0-length partition\n");
544 goto out;
546 tmp_part = (struct nvram_partition *)
547 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
548 err = -ENOMEM;
549 if (!tmp_part) {
550 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
551 goto out;
554 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
555 tmp_part->index = cur_index;
556 list_add_tail(&tmp_part->partition, &nvram_part->partition);
558 cur_index += phead.length * NVRAM_BLOCK_LEN;
560 err = 0;
562 out:
563 kfree(header);
564 return err;
567 static int __init nvram_init(void)
569 int error;
570 int rc;
572 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
573 return -ENODEV;
575 rc = misc_register(&nvram_dev);
576 if (rc != 0) {
577 printk(KERN_ERR "nvram_init: failed to register device\n");
578 return rc;
581 /* initialize our anchor for the nvram partition list */
582 nvram_part = (struct nvram_partition *) kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
583 if (!nvram_part) {
584 printk(KERN_ERR "nvram_init: Failed kmalloc\n");
585 return -ENOMEM;
587 INIT_LIST_HEAD(&nvram_part->partition);
589 /* Get all the NVRAM partitions */
590 error = nvram_scan_partitions();
591 if (error) {
592 printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
593 return error;
596 if(nvram_setup_partition())
597 printk(KERN_WARNING "nvram_init: Could not find nvram partition"
598 " for nvram buffered error logging.\n");
600 #ifdef DEBUG_NVRAM
601 nvram_print_partitions("NVRAM Partitions");
602 #endif
604 return rc;
607 void __exit nvram_cleanup(void)
609 misc_deregister( &nvram_dev );
613 #ifdef CONFIG_PPC_PSERIES
615 /* nvram_write_error_log
617 * We need to buffer the error logs into nvram to ensure that we have
618 * the failure information to decode. If we have a severe error there
619 * is no way to guarantee that the OS or the machine is in a state to
620 * get back to user land and write the error to disk. For example if
621 * the SCSI device driver causes a Machine Check by writing to a bad
622 * IO address, there is no way of guaranteeing that the device driver
623 * is in any state that is would also be able to write the error data
624 * captured to disk, thus we buffer it in NVRAM for analysis on the
625 * next boot.
627 * In NVRAM the partition containing the error log buffer will looks like:
628 * Header (in bytes):
629 * +-----------+----------+--------+------------+------------------+
630 * | signature | checksum | length | name | data |
631 * |0 |1 |2 3|4 15|16 length-1|
632 * +-----------+----------+--------+------------+------------------+
634 * The 'data' section would look like (in bytes):
635 * +--------------+------------+-----------------------------------+
636 * | event_logged | sequence # | error log |
637 * |0 3|4 7|8 nvram_error_log_size-1|
638 * +--------------+------------+-----------------------------------+
640 * event_logged: 0 if event has not been logged to syslog, 1 if it has
641 * sequence #: The unique sequence # for each event. (until it wraps)
642 * error log: The error log from event_scan
644 int nvram_write_error_log(char * buff, int length, unsigned int err_type)
646 int rc;
647 loff_t tmp_index;
648 struct err_log_info info;
650 if (no_logging) {
651 return -EPERM;
654 if (nvram_error_log_index == -1) {
655 return -ESPIPE;
658 if (length > nvram_error_log_size) {
659 length = nvram_error_log_size;
662 info.error_type = err_type;
663 info.seq_num = error_log_cnt;
665 tmp_index = nvram_error_log_index;
667 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
668 if (rc <= 0) {
669 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
670 return rc;
673 rc = ppc_md.nvram_write(buff, length, &tmp_index);
674 if (rc <= 0) {
675 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
676 return rc;
679 return 0;
682 /* nvram_read_error_log
684 * Reads nvram for error log for at most 'length'
686 int nvram_read_error_log(char * buff, int length, unsigned int * err_type)
688 int rc;
689 loff_t tmp_index;
690 struct err_log_info info;
692 if (nvram_error_log_index == -1)
693 return -1;
695 if (length > nvram_error_log_size)
696 length = nvram_error_log_size;
698 tmp_index = nvram_error_log_index;
700 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
701 if (rc <= 0) {
702 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
703 return rc;
706 rc = ppc_md.nvram_read(buff, length, &tmp_index);
707 if (rc <= 0) {
708 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
709 return rc;
712 error_log_cnt = info.seq_num;
713 *err_type = info.error_type;
715 return 0;
718 /* This doesn't actually zero anything, but it sets the event_logged
719 * word to tell that this event is safely in syslog.
721 int nvram_clear_error_log(void)
723 loff_t tmp_index;
724 int clear_word = ERR_FLAG_ALREADY_LOGGED;
725 int rc;
727 tmp_index = nvram_error_log_index;
729 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
730 if (rc <= 0) {
731 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
732 return rc;
735 return 0;
738 #endif /* CONFIG_PPC_PSERIES */
740 module_init(nvram_init);
741 module_exit(nvram_cleanup);
742 MODULE_LICENSE("GPL");