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 volatile int no_more_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 *buf,
81 size_t count, loff_t *ppos)
87 if (ppc_md.nvram_size == NULL)
89 size = ppc_md.nvram_size();
91 if (verify_area(VERIFY_WRITE, buf, count))
98 tmp_buffer = (char *) kmalloc(count, GFP_KERNEL);
100 printk(KERN_ERR "dev_read_nvram: kmalloc failed\n");
104 len = ppc_md.nvram_read(tmp_buffer, count, ppos);
105 if ((long)len <= 0) {
110 if (copy_to_user(buf, tmp_buffer, len)) {
120 static ssize_t dev_nvram_write(struct file *file, const char *buf,
121 size_t count, loff_t *ppos)
127 if (ppc_md.nvram_size == NULL)
129 size = ppc_md.nvram_size();
131 if (verify_area(VERIFY_READ, buf, count))
138 tmp_buffer = (char *) kmalloc(count, GFP_KERNEL);
140 printk(KERN_ERR "dev_nvram_write: kmalloc failed\n");
144 if (copy_from_user(tmp_buffer, buf, count)) {
149 len = ppc_md.nvram_write(tmp_buffer, count, ppos);
150 if ((long)len <= 0) {
159 static int dev_nvram_ioctl(struct inode *inode, struct file *file,
160 unsigned int cmd, unsigned long arg)
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: {
169 if (systemcfg->platform != PLATFORM_POWERMAC)
171 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
173 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
175 offset = pmac_get_partition(part);
178 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
182 #endif /* CONFIG_PPC_PMAC */
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 = {
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);
221 static int nvram_write_header(struct nvram_partition * part)
226 tmp_index = part->index;
227 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
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;
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)
263 if (name && 0 != strncmp(name, part->header.name, 12))
269 EXPORT_SYMBOL(nvram_find_partition);
272 static int nvram_remove_os_partition(void)
276 struct nvram_partition * part;
277 struct nvram_partition * cur_part;
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)
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) {
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);
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) {
313 part->header.length += cur_part->header.length;
314 part->header.checksum = nvram_checksum(&part->header);
316 list_del(&cur_part->partition);
318 j = &part->partition; /* fixup our loop */
321 rc = nvram_write_header(part);
323 printk(KERN_ERR "nvram_remove_os_partition: nvram_write failed (%d)\n", rc);
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 list_head * p;
341 struct nvram_partition * part;
342 struct nvram_partition * new_part = NULL;
343 struct nvram_partition * free_part = NULL;
344 int seq_init[2] = { 0, 0 };
349 /* Find a free partition that will give us the maximum needed size
350 If can't find one that will give us the minimum size needed */
351 list_for_each(p, &nvram_part->partition) {
352 part = list_entry(p, struct nvram_partition, partition);
353 if (part->header.signature != NVRAM_SIG_FREE)
356 if (part->header.length >= NVRAM_MAX_REQ) {
357 size = NVRAM_MAX_REQ;
361 if (!size && part->header.length >= NVRAM_MIN_REQ) {
362 size = NVRAM_MIN_REQ;
370 /* Create our OS partition */
371 new_part = (struct nvram_partition *)
372 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
374 printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n");
378 new_part->index = free_part->index;
379 new_part->header.signature = NVRAM_SIG_OS;
380 new_part->header.length = size;
381 sprintf(new_part->header.name, "ppc64,linux");
382 new_part->header.checksum = nvram_checksum(&new_part->header);
384 rc = nvram_write_header(new_part);
386 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header \
391 /* make sure and initialize to zero the sequence number and the error
393 tmp_index = new_part->index + NVRAM_HEADER_LEN;
394 rc = ppc_md.nvram_write((char *)&seq_init, sizeof(seq_init), &tmp_index);
396 printk(KERN_ERR "nvram_create_os_partition: nvram_write failed (%d)\n", rc);
400 nvram_error_log_index = new_part->index + NVRAM_HEADER_LEN;
401 nvram_error_log_size = ((part->header.length - 1) *
402 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
404 list_add_tail(&new_part->partition, &free_part->partition);
406 if (free_part->header.length <= size) {
407 list_del(&free_part->partition);
412 /* Adjust the partition we stole the space from */
413 free_part->index += size * NVRAM_BLOCK_LEN;
414 free_part->header.length -= size;
415 free_part->header.checksum = nvram_checksum(&free_part->header);
417 rc = nvram_write_header(free_part);
419 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header "
420 "failed (%d)\n", rc);
428 /* nvram_setup_partition
430 * This will setup the partition we need for buffering the
431 * error logs and cleanup partitions if needed.
433 * The general strategy is the following:
434 * 1.) If there is ppc64,linux partition large enough then use it.
435 * 2.) If there is not a ppc64,linux partition large enough, search
436 * for a free partition that is large enough.
437 * 3.) If there is not a free partition large enough remove
438 * _all_ OS partitions and consolidate the space.
439 * 4.) Will first try getting a chunk that will satisfy the maximum
440 * error log size (NVRAM_MAX_REQ).
441 * 5.) If the max chunk cannot be allocated then try finding a chunk
442 * that will satisfy the minum needed (NVRAM_MIN_REQ).
444 static int nvram_setup_partition(void)
446 struct list_head * p;
447 struct nvram_partition * part;
450 /* For now, we don't do any of this on pmac, until I
451 * have figured out if it's worth killing some unused stuffs
452 * in our nvram, as Apple defined partitions use pretty much
455 if (systemcfg->platform == PLATFORM_POWERMAC)
458 /* see if we have an OS partition that meets our needs.
459 will try getting the max we need. If not we'll delete
460 partitions and try again. */
461 list_for_each(p, &nvram_part->partition) {
462 part = list_entry(p, struct nvram_partition, partition);
463 if (part->header.signature != NVRAM_SIG_OS)
466 if (strcmp(part->header.name, "ppc64,linux"))
469 if (part->header.length >= NVRAM_MIN_REQ) {
470 /* found our partition */
471 nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
472 nvram_error_log_size = ((part->header.length - 1) *
473 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
478 /* try creating a partition with the free space we have */
479 rc = nvram_create_os_partition();
484 /* need to free up some space */
485 rc = nvram_remove_os_partition();
490 /* create a partition in this new space */
491 rc = nvram_create_os_partition();
493 printk(KERN_ERR "nvram_create_os_partition: Could not find a "
494 "NVRAM partition large enough\n");
502 static int nvram_scan_partitions(void)
504 loff_t cur_index = 0;
505 struct nvram_header phead;
506 struct nvram_partition * tmp_part;
512 if (ppc_md.nvram_size == NULL)
514 total_size = ppc_md.nvram_size();
516 header = (char *) kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
518 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
522 while (cur_index < total_size) {
524 size = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
525 if (size != NVRAM_HEADER_LEN) {
526 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
527 "nvram partitions\n");
532 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
534 memcpy(&phead, header, NVRAM_HEADER_LEN);
536 c_sum = nvram_checksum(&phead);
537 if (c_sum != phead.checksum)
538 printk(KERN_WARNING "WARNING: nvram partition checksum "
539 "was %02x, should be %02x!\n", phead.checksum, c_sum);
541 tmp_part = (struct nvram_partition *)
542 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
544 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;
560 static int __init nvram_init(void)
565 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
568 rc = misc_register(&nvram_dev);
570 printk(KERN_ERR "nvram_init: failed to register device\n");
574 /* initialize our anchor for the nvram partition list */
575 nvram_part = (struct nvram_partition *) kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
577 printk(KERN_ERR "nvram_init: Failed kmalloc\n");
580 INIT_LIST_HEAD(&nvram_part->partition);
582 /* Get all the NVRAM partitions */
583 error = nvram_scan_partitions();
585 printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
589 if(nvram_setup_partition())
590 printk(KERN_WARNING "nvram_init: Could not find nvram partition"
591 " for nvram buffered error logging.\n");
594 nvram_print_partitions("NVRAM Partitions");
600 void __exit nvram_cleanup(void)
602 misc_deregister( &nvram_dev );
606 #ifdef CONFIG_PPC_PSERIES
608 /* nvram_write_error_log
610 * We need to buffer the error logs into nvram to ensure that we have
611 * the failure information to decode. If we have a severe error there
612 * is no way to guarantee that the OS or the machine is in a state to
613 * get back to user land and write the error to disk. For example if
614 * the SCSI device driver causes a Machine Check by writing to a bad
615 * IO address, there is no way of guaranteeing that the device driver
616 * is in any state that is would also be able to write the error data
617 * captured to disk, thus we buffer it in NVRAM for analysis on the
620 * In NVRAM the partition containing the error log buffer will looks like:
622 * +-----------+----------+--------+------------+------------------+
623 * | signature | checksum | length | name | data |
624 * |0 |1 |2 3|4 15|16 length-1|
625 * +-----------+----------+--------+------------+------------------+
627 * The 'data' section would look like (in bytes):
628 * +--------------+------------+-----------------------------------+
629 * | event_logged | sequence # | error log |
630 * |0 3|4 7|8 nvram_error_log_size-1|
631 * +--------------+------------+-----------------------------------+
633 * event_logged: 0 if event has not been logged to syslog, 1 if it has
634 * sequence #: The unique sequence # for each event. (until it wraps)
635 * error log: The error log from event_scan
637 int nvram_write_error_log(char * buff, int length, unsigned int err_type)
641 struct err_log_info info;
643 if (no_more_logging) {
647 if (nvram_error_log_index == -1) {
651 if (length > nvram_error_log_size) {
652 length = nvram_error_log_size;
655 info.error_type = err_type;
656 info.seq_num = error_log_cnt;
658 tmp_index = nvram_error_log_index;
660 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
662 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
666 rc = ppc_md.nvram_write(buff, length, &tmp_index);
668 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
675 /* nvram_read_error_log
677 * Reads nvram for error log for at most 'length'
679 int nvram_read_error_log(char * buff, int length, unsigned int * err_type)
683 struct err_log_info info;
685 if (nvram_error_log_index == -1)
688 if (length > nvram_error_log_size)
689 length = nvram_error_log_size;
691 tmp_index = nvram_error_log_index;
693 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
695 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
699 rc = ppc_md.nvram_read(buff, length, &tmp_index);
701 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
705 error_log_cnt = info.seq_num;
706 *err_type = info.error_type;
711 /* This doesn't actually zero anything, but it sets the event_logged
712 * word to tell that this event is safely in syslog.
714 int nvram_clear_error_log()
717 int clear_word = ERR_FLAG_ALREADY_LOGGED;
720 tmp_index = nvram_error_log_index;
722 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
724 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
731 #endif /* CONFIG_PPC_PSERIES */
733 module_init(nvram_init);
734 module_exit(nvram_cleanup);
735 MODULE_LICENSE("GPL");