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;
46 static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
50 if (ppc_md.nvram_size == NULL)
52 size = ppc_md.nvram_size();
56 offset += file->f_pos;
69 static ssize_t dev_nvram_read(struct file *file, char __user *buf,
70 size_t count, loff_t *ppos)
77 if (!ppc_md.nvram_size)
81 size = ppc_md.nvram_size();
82 if (*ppos >= size || size < 0)
85 count = min_t(size_t, count, size - *ppos);
86 count = min(count, PAGE_SIZE);
89 tmp = kmalloc(count, GFP_KERNEL);
93 ret = ppc_md.nvram_read(tmp, count, ppos);
97 if (copy_to_user(buf, tmp, ret))
106 static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
107 size_t count, loff_t *ppos)
114 if (!ppc_md.nvram_size)
118 size = ppc_md.nvram_size();
119 if (*ppos >= size || size < 0)
122 count = min_t(size_t, count, size - *ppos);
123 count = min(count, PAGE_SIZE);
126 tmp = kmalloc(count, GFP_KERNEL);
131 if (copy_from_user(tmp, buf, count))
134 ret = ppc_md.nvram_write(tmp, count, ppos);
142 static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
146 #ifdef CONFIG_PPC_PMAC
147 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
148 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
149 case IOC_NVRAM_GET_OFFSET: {
152 if (!machine_is(powermac))
154 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
156 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
158 offset = pmac_get_partition(part);
161 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
165 #endif /* CONFIG_PPC_PMAC */
171 const struct file_operations nvram_fops = {
172 .owner = THIS_MODULE,
173 .llseek = dev_nvram_llseek,
174 .read = dev_nvram_read,
175 .write = dev_nvram_write,
176 .unlocked_ioctl = dev_nvram_ioctl,
179 static struct miscdevice nvram_dev = {
187 static void __init nvram_print_partitions(char * label)
189 struct list_head * p;
190 struct nvram_partition * tmp_part;
192 printk(KERN_WARNING "--------%s---------\n", label);
193 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
194 list_for_each(p, &nvram_part->partition) {
195 tmp_part = list_entry(p, struct nvram_partition, partition);
196 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%s\n",
197 tmp_part->index, tmp_part->header.signature,
198 tmp_part->header.checksum, tmp_part->header.length,
199 tmp_part->header.name);
205 static int __init nvram_write_header(struct nvram_partition * part)
210 tmp_index = part->index;
211 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
217 static unsigned char __init nvram_checksum(struct nvram_header *p)
219 unsigned int c_sum, c_sum2;
220 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
221 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
223 /* The sum may have spilled into the 3rd byte. Fold it back. */
224 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
225 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
226 c_sum2 = (c_sum >> 8) + (c_sum << 8);
227 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
231 static int __init nvram_remove_os_partition(void)
235 struct nvram_partition * part;
236 struct nvram_partition * cur_part;
239 list_for_each(i, &nvram_part->partition) {
240 part = list_entry(i, struct nvram_partition, partition);
241 if (part->header.signature != NVRAM_SIG_OS)
244 /* Make os partition a free partition */
245 part->header.signature = NVRAM_SIG_FREE;
246 sprintf(part->header.name, "wwwwwwwwwwww");
247 part->header.checksum = nvram_checksum(&part->header);
249 /* Merge contiguous free partitions backwards */
250 list_for_each_prev(j, &part->partition) {
251 cur_part = list_entry(j, struct nvram_partition, partition);
252 if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
256 part->header.length += cur_part->header.length;
257 part->header.checksum = nvram_checksum(&part->header);
258 part->index = cur_part->index;
260 list_del(&cur_part->partition);
262 j = &part->partition; /* fixup our loop */
265 /* Merge contiguous free partitions forwards */
266 list_for_each(j, &part->partition) {
267 cur_part = list_entry(j, struct nvram_partition, partition);
268 if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
272 part->header.length += cur_part->header.length;
273 part->header.checksum = nvram_checksum(&part->header);
275 list_del(&cur_part->partition);
277 j = &part->partition; /* fixup our loop */
280 rc = nvram_write_header(part);
282 printk(KERN_ERR "nvram_remove_os_partition: nvram_write failed (%d)\n", rc);
291 /* nvram_create_os_partition
293 * Create a OS linux partition to buffer error logs.
294 * Will create a partition starting at the first free
295 * space found if space has enough room.
297 static int __init nvram_create_os_partition(void)
299 struct nvram_partition *part;
300 struct nvram_partition *new_part;
301 struct nvram_partition *free_part = NULL;
302 int seq_init[2] = { 0, 0 };
307 /* Find a free partition that will give us the maximum needed size
308 If can't find one that will give us the minimum size needed */
309 list_for_each_entry(part, &nvram_part->partition, partition) {
310 if (part->header.signature != NVRAM_SIG_FREE)
313 if (part->header.length >= NVRAM_MAX_REQ) {
314 size = NVRAM_MAX_REQ;
318 if (!size && part->header.length >= NVRAM_MIN_REQ) {
319 size = NVRAM_MIN_REQ;
326 /* Create our OS partition */
327 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
329 printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n");
333 new_part->index = free_part->index;
334 new_part->header.signature = NVRAM_SIG_OS;
335 new_part->header.length = size;
336 strcpy(new_part->header.name, "ppc64,linux");
337 new_part->header.checksum = nvram_checksum(&new_part->header);
339 rc = nvram_write_header(new_part);
341 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header \
346 /* make sure and initialize to zero the sequence number and the error
348 tmp_index = new_part->index + NVRAM_HEADER_LEN;
349 rc = ppc_md.nvram_write((char *)&seq_init, sizeof(seq_init), &tmp_index);
351 printk(KERN_ERR "nvram_create_os_partition: nvram_write "
352 "failed (%d)\n", rc);
356 nvram_error_log_index = new_part->index + NVRAM_HEADER_LEN;
357 nvram_error_log_size = ((part->header.length - 1) *
358 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
360 list_add_tail(&new_part->partition, &free_part->partition);
362 if (free_part->header.length <= size) {
363 list_del(&free_part->partition);
368 /* Adjust the partition we stole the space from */
369 free_part->index += size * NVRAM_BLOCK_LEN;
370 free_part->header.length -= size;
371 free_part->header.checksum = nvram_checksum(&free_part->header);
373 rc = nvram_write_header(free_part);
375 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header "
376 "failed (%d)\n", rc);
384 /* nvram_setup_partition
386 * This will setup the partition we need for buffering the
387 * error logs and cleanup partitions if needed.
389 * The general strategy is the following:
390 * 1.) If there is ppc64,linux partition large enough then use it.
391 * 2.) If there is not a ppc64,linux partition large enough, search
392 * for a free partition that is large enough.
393 * 3.) If there is not a free partition large enough remove
394 * _all_ OS partitions and consolidate the space.
395 * 4.) Will first try getting a chunk that will satisfy the maximum
396 * error log size (NVRAM_MAX_REQ).
397 * 5.) If the max chunk cannot be allocated then try finding a chunk
398 * that will satisfy the minum needed (NVRAM_MIN_REQ).
400 static int __init nvram_setup_partition(void)
402 struct list_head * p;
403 struct nvram_partition * part;
406 /* For now, we don't do any of this on pmac, until I
407 * have figured out if it's worth killing some unused stuffs
408 * in our nvram, as Apple defined partitions use pretty much
411 if (machine_is(powermac))
414 /* see if we have an OS partition that meets our needs.
415 will try getting the max we need. If not we'll delete
416 partitions and try again. */
417 list_for_each(p, &nvram_part->partition) {
418 part = list_entry(p, struct nvram_partition, partition);
419 if (part->header.signature != NVRAM_SIG_OS)
422 if (strcmp(part->header.name, "ppc64,linux"))
425 if (part->header.length >= NVRAM_MIN_REQ) {
426 /* found our partition */
427 nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
428 nvram_error_log_size = ((part->header.length - 1) *
429 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
434 /* try creating a partition with the free space we have */
435 rc = nvram_create_os_partition();
440 /* need to free up some space */
441 rc = nvram_remove_os_partition();
446 /* create a partition in this new space */
447 rc = nvram_create_os_partition();
449 printk(KERN_ERR "nvram_create_os_partition: Could not find a "
450 "NVRAM partition large enough\n");
458 static int __init nvram_scan_partitions(void)
460 loff_t cur_index = 0;
461 struct nvram_header phead;
462 struct nvram_partition * tmp_part;
468 if (ppc_md.nvram_size == NULL)
470 total_size = ppc_md.nvram_size();
472 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
474 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
478 while (cur_index < total_size) {
480 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
481 if (err != NVRAM_HEADER_LEN) {
482 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
483 "nvram partitions\n");
487 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
489 memcpy(&phead, header, NVRAM_HEADER_LEN);
492 c_sum = nvram_checksum(&phead);
493 if (c_sum != phead.checksum) {
494 printk(KERN_WARNING "WARNING: nvram partition checksum"
495 " was %02x, should be %02x!\n",
496 phead.checksum, c_sum);
497 printk(KERN_WARNING "Terminating nvram partition scan\n");
501 printk(KERN_WARNING "WARNING: nvram corruption "
502 "detected: 0-length partition\n");
505 tmp_part = (struct nvram_partition *)
506 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
509 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
513 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
514 tmp_part->index = cur_index;
515 list_add_tail(&tmp_part->partition, &nvram_part->partition);
517 cur_index += phead.length * NVRAM_BLOCK_LEN;
526 static int __init nvram_init(void)
531 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
534 rc = misc_register(&nvram_dev);
536 printk(KERN_ERR "nvram_init: failed to register device\n");
540 /* initialize our anchor for the nvram partition list */
541 nvram_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
543 printk(KERN_ERR "nvram_init: Failed kmalloc\n");
546 INIT_LIST_HEAD(&nvram_part->partition);
548 /* Get all the NVRAM partitions */
549 error = nvram_scan_partitions();
551 printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
555 if(nvram_setup_partition())
556 printk(KERN_WARNING "nvram_init: Could not find nvram partition"
557 " for nvram buffered error logging.\n");
560 nvram_print_partitions("NVRAM Partitions");
566 void __exit nvram_cleanup(void)
568 misc_deregister( &nvram_dev );
572 #ifdef CONFIG_PPC_PSERIES
574 /* nvram_write_error_log
576 * We need to buffer the error logs into nvram to ensure that we have
577 * the failure information to decode. If we have a severe error there
578 * is no way to guarantee that the OS or the machine is in a state to
579 * get back to user land and write the error to disk. For example if
580 * the SCSI device driver causes a Machine Check by writing to a bad
581 * IO address, there is no way of guaranteeing that the device driver
582 * is in any state that is would also be able to write the error data
583 * captured to disk, thus we buffer it in NVRAM for analysis on the
586 * In NVRAM the partition containing the error log buffer will looks like:
588 * +-----------+----------+--------+------------+------------------+
589 * | signature | checksum | length | name | data |
590 * |0 |1 |2 3|4 15|16 length-1|
591 * +-----------+----------+--------+------------+------------------+
593 * The 'data' section would look like (in bytes):
594 * +--------------+------------+-----------------------------------+
595 * | event_logged | sequence # | error log |
596 * |0 3|4 7|8 nvram_error_log_size-1|
597 * +--------------+------------+-----------------------------------+
599 * event_logged: 0 if event has not been logged to syslog, 1 if it has
600 * sequence #: The unique sequence # for each event. (until it wraps)
601 * error log: The error log from event_scan
603 int nvram_write_error_log(char * buff, int length,
604 unsigned int err_type, unsigned int error_log_cnt)
608 struct err_log_info info;
610 if (nvram_error_log_index == -1) {
614 if (length > nvram_error_log_size) {
615 length = nvram_error_log_size;
618 info.error_type = err_type;
619 info.seq_num = error_log_cnt;
621 tmp_index = nvram_error_log_index;
623 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
625 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
629 rc = ppc_md.nvram_write(buff, length, &tmp_index);
631 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
638 /* nvram_read_error_log
640 * Reads nvram for error log for at most 'length'
642 int nvram_read_error_log(char * buff, int length,
643 unsigned int * err_type, unsigned int * error_log_cnt)
647 struct err_log_info info;
649 if (nvram_error_log_index == -1)
652 if (length > nvram_error_log_size)
653 length = nvram_error_log_size;
655 tmp_index = nvram_error_log_index;
657 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
659 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
663 rc = ppc_md.nvram_read(buff, length, &tmp_index);
665 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
669 *error_log_cnt = info.seq_num;
670 *err_type = info.error_type;
675 /* This doesn't actually zero anything, but it sets the event_logged
676 * word to tell that this event is safely in syslog.
678 int nvram_clear_error_log(void)
681 int clear_word = ERR_FLAG_ALREADY_LOGGED;
684 if (nvram_error_log_index == -1)
687 tmp_index = nvram_error_log_index;
689 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
691 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
698 #endif /* CONFIG_PPC_PSERIES */
700 module_init(nvram_init);
701 module_exit(nvram_cleanup);
702 MODULE_LICENSE("GPL");