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 int no_logging = 1; /* Until we initialize everything,
42 * make sure we don't try logging
45 extern volatile int error_log_cnt;
52 static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
56 if (ppc_md.nvram_size == NULL)
58 size = ppc_md.nvram_size();
62 offset += file->f_pos;
75 static ssize_t dev_nvram_read(struct file *file, char __user *buf,
76 size_t count, loff_t *ppos)
83 if (!ppc_md.nvram_size)
87 size = ppc_md.nvram_size();
88 if (*ppos >= size || size < 0)
91 count = min_t(size_t, count, size - *ppos);
92 count = min(count, PAGE_SIZE);
95 tmp = kmalloc(count, GFP_KERNEL);
99 ret = ppc_md.nvram_read(tmp, count, ppos);
103 if (copy_to_user(buf, tmp, ret))
112 static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
113 size_t count, loff_t *ppos)
120 if (!ppc_md.nvram_size)
124 size = ppc_md.nvram_size();
125 if (*ppos >= size || size < 0)
128 count = min_t(size_t, count, size - *ppos);
129 count = min(count, PAGE_SIZE);
132 tmp = kmalloc(count, GFP_KERNEL);
137 if (copy_from_user(tmp, buf, count))
140 ret = ppc_md.nvram_write(tmp, count, ppos);
148 static int dev_nvram_ioctl(struct inode *inode, struct file *file,
149 unsigned int cmd, unsigned long arg)
152 #ifdef CONFIG_PPC_PMAC
153 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
154 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
155 case IOC_NVRAM_GET_OFFSET: {
158 if (!machine_is(powermac))
160 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
162 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
164 offset = pmac_get_partition(part);
167 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
171 #endif /* CONFIG_PPC_PMAC */
177 const struct file_operations nvram_fops = {
178 .owner = THIS_MODULE,
179 .llseek = dev_nvram_llseek,
180 .read = dev_nvram_read,
181 .write = dev_nvram_write,
182 .ioctl = dev_nvram_ioctl,
185 static struct miscdevice nvram_dev = {
193 static void nvram_print_partitions(char * label)
195 struct list_head * p;
196 struct nvram_partition * tmp_part;
198 printk(KERN_WARNING "--------%s---------\n", label);
199 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
200 list_for_each(p, &nvram_part->partition) {
201 tmp_part = list_entry(p, struct nvram_partition, partition);
202 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%s\n",
203 tmp_part->index, tmp_part->header.signature,
204 tmp_part->header.checksum, tmp_part->header.length,
205 tmp_part->header.name);
211 static int nvram_write_header(struct nvram_partition * part)
216 tmp_index = part->index;
217 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
223 static unsigned char nvram_checksum(struct nvram_header *p)
225 unsigned int c_sum, c_sum2;
226 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
227 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
229 /* The sum may have spilled into the 3rd byte. Fold it back. */
230 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
231 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
232 c_sum2 = (c_sum >> 8) + (c_sum << 8);
233 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
239 * Find an nvram partition, sig can be 0 for any
240 * partition or name can be NULL for any name, else
241 * tries to match both
243 struct nvram_partition *nvram_find_partition(int sig, const char *name)
245 struct nvram_partition * part;
246 struct list_head * p;
248 list_for_each(p, &nvram_part->partition) {
249 part = list_entry(p, struct nvram_partition, partition);
251 if (sig && part->header.signature != sig)
253 if (name && 0 != strncmp(name, part->header.name, 12))
259 EXPORT_SYMBOL(nvram_find_partition);
262 static int nvram_remove_os_partition(void)
266 struct nvram_partition * part;
267 struct nvram_partition * cur_part;
270 list_for_each(i, &nvram_part->partition) {
271 part = list_entry(i, struct nvram_partition, partition);
272 if (part->header.signature != NVRAM_SIG_OS)
275 /* Make os partition a free partition */
276 part->header.signature = NVRAM_SIG_FREE;
277 sprintf(part->header.name, "wwwwwwwwwwww");
278 part->header.checksum = nvram_checksum(&part->header);
280 /* Merge contiguous free partitions backwards */
281 list_for_each_prev(j, &part->partition) {
282 cur_part = list_entry(j, struct nvram_partition, partition);
283 if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
287 part->header.length += cur_part->header.length;
288 part->header.checksum = nvram_checksum(&part->header);
289 part->index = cur_part->index;
291 list_del(&cur_part->partition);
293 j = &part->partition; /* fixup our loop */
296 /* Merge contiguous free partitions forwards */
297 list_for_each(j, &part->partition) {
298 cur_part = list_entry(j, struct nvram_partition, partition);
299 if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
303 part->header.length += cur_part->header.length;
304 part->header.checksum = nvram_checksum(&part->header);
306 list_del(&cur_part->partition);
308 j = &part->partition; /* fixup our loop */
311 rc = nvram_write_header(part);
313 printk(KERN_ERR "nvram_remove_os_partition: nvram_write failed (%d)\n", rc);
322 /* nvram_create_os_partition
324 * Create a OS linux partition to buffer error logs.
325 * Will create a partition starting at the first free
326 * space found if space has enough room.
328 static int nvram_create_os_partition(void)
330 struct nvram_partition *part;
331 struct nvram_partition *new_part;
332 struct nvram_partition *free_part = NULL;
333 int seq_init[2] = { 0, 0 };
338 /* Find a free partition that will give us the maximum needed size
339 If can't find one that will give us the minimum size needed */
340 list_for_each_entry(part, &nvram_part->partition, partition) {
341 if (part->header.signature != NVRAM_SIG_FREE)
344 if (part->header.length >= NVRAM_MAX_REQ) {
345 size = NVRAM_MAX_REQ;
349 if (!size && part->header.length >= NVRAM_MIN_REQ) {
350 size = NVRAM_MIN_REQ;
357 /* Create our OS partition */
358 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
360 printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n");
364 new_part->index = free_part->index;
365 new_part->header.signature = NVRAM_SIG_OS;
366 new_part->header.length = size;
367 strcpy(new_part->header.name, "ppc64,linux");
368 new_part->header.checksum = nvram_checksum(&new_part->header);
370 rc = nvram_write_header(new_part);
372 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header \
377 /* make sure and initialize to zero the sequence number and the error
379 tmp_index = new_part->index + NVRAM_HEADER_LEN;
380 rc = ppc_md.nvram_write((char *)&seq_init, sizeof(seq_init), &tmp_index);
382 printk(KERN_ERR "nvram_create_os_partition: nvram_write "
383 "failed (%d)\n", rc);
387 nvram_error_log_index = new_part->index + NVRAM_HEADER_LEN;
388 nvram_error_log_size = ((part->header.length - 1) *
389 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
391 list_add_tail(&new_part->partition, &free_part->partition);
393 if (free_part->header.length <= size) {
394 list_del(&free_part->partition);
399 /* Adjust the partition we stole the space from */
400 free_part->index += size * NVRAM_BLOCK_LEN;
401 free_part->header.length -= size;
402 free_part->header.checksum = nvram_checksum(&free_part->header);
404 rc = nvram_write_header(free_part);
406 printk(KERN_ERR "nvram_create_os_partition: nvram_write_header "
407 "failed (%d)\n", rc);
415 /* nvram_setup_partition
417 * This will setup the partition we need for buffering the
418 * error logs and cleanup partitions if needed.
420 * The general strategy is the following:
421 * 1.) If there is ppc64,linux partition large enough then use it.
422 * 2.) If there is not a ppc64,linux partition large enough, search
423 * for a free partition that is large enough.
424 * 3.) If there is not a free partition large enough remove
425 * _all_ OS partitions and consolidate the space.
426 * 4.) Will first try getting a chunk that will satisfy the maximum
427 * error log size (NVRAM_MAX_REQ).
428 * 5.) If the max chunk cannot be allocated then try finding a chunk
429 * that will satisfy the minum needed (NVRAM_MIN_REQ).
431 static int nvram_setup_partition(void)
433 struct list_head * p;
434 struct nvram_partition * part;
437 /* For now, we don't do any of this on pmac, until I
438 * have figured out if it's worth killing some unused stuffs
439 * in our nvram, as Apple defined partitions use pretty much
442 if (machine_is(powermac))
445 /* see if we have an OS partition that meets our needs.
446 will try getting the max we need. If not we'll delete
447 partitions and try again. */
448 list_for_each(p, &nvram_part->partition) {
449 part = list_entry(p, struct nvram_partition, partition);
450 if (part->header.signature != NVRAM_SIG_OS)
453 if (strcmp(part->header.name, "ppc64,linux"))
456 if (part->header.length >= NVRAM_MIN_REQ) {
457 /* found our partition */
458 nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
459 nvram_error_log_size = ((part->header.length - 1) *
460 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
465 /* try creating a partition with the free space we have */
466 rc = nvram_create_os_partition();
471 /* need to free up some space */
472 rc = nvram_remove_os_partition();
477 /* create a partition in this new space */
478 rc = nvram_create_os_partition();
480 printk(KERN_ERR "nvram_create_os_partition: Could not find a "
481 "NVRAM partition large enough\n");
489 static int nvram_scan_partitions(void)
491 loff_t cur_index = 0;
492 struct nvram_header phead;
493 struct nvram_partition * tmp_part;
499 if (ppc_md.nvram_size == NULL)
501 total_size = ppc_md.nvram_size();
503 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
505 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
509 while (cur_index < total_size) {
511 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
512 if (err != NVRAM_HEADER_LEN) {
513 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
514 "nvram partitions\n");
518 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
520 memcpy(&phead, header, NVRAM_HEADER_LEN);
523 c_sum = nvram_checksum(&phead);
524 if (c_sum != phead.checksum) {
525 printk(KERN_WARNING "WARNING: nvram partition checksum"
526 " was %02x, should be %02x!\n",
527 phead.checksum, c_sum);
528 printk(KERN_WARNING "Terminating nvram partition scan\n");
532 printk(KERN_WARNING "WARNING: nvram corruption "
533 "detected: 0-length partition\n");
536 tmp_part = (struct nvram_partition *)
537 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
540 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
544 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
545 tmp_part->index = cur_index;
546 list_add_tail(&tmp_part->partition, &nvram_part->partition);
548 cur_index += phead.length * NVRAM_BLOCK_LEN;
557 static int __init nvram_init(void)
562 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
565 rc = misc_register(&nvram_dev);
567 printk(KERN_ERR "nvram_init: failed to register device\n");
571 /* initialize our anchor for the nvram partition list */
572 nvram_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
574 printk(KERN_ERR "nvram_init: Failed kmalloc\n");
577 INIT_LIST_HEAD(&nvram_part->partition);
579 /* Get all the NVRAM partitions */
580 error = nvram_scan_partitions();
582 printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
586 if(nvram_setup_partition())
587 printk(KERN_WARNING "nvram_init: Could not find nvram partition"
588 " for nvram buffered error logging.\n");
591 nvram_print_partitions("NVRAM Partitions");
597 void __exit nvram_cleanup(void)
599 misc_deregister( &nvram_dev );
603 #ifdef CONFIG_PPC_PSERIES
605 /* nvram_write_error_log
607 * We need to buffer the error logs into nvram to ensure that we have
608 * the failure information to decode. If we have a severe error there
609 * is no way to guarantee that the OS or the machine is in a state to
610 * get back to user land and write the error to disk. For example if
611 * the SCSI device driver causes a Machine Check by writing to a bad
612 * IO address, there is no way of guaranteeing that the device driver
613 * is in any state that is would also be able to write the error data
614 * captured to disk, thus we buffer it in NVRAM for analysis on the
617 * In NVRAM the partition containing the error log buffer will looks like:
619 * +-----------+----------+--------+------------+------------------+
620 * | signature | checksum | length | name | data |
621 * |0 |1 |2 3|4 15|16 length-1|
622 * +-----------+----------+--------+------------+------------------+
624 * The 'data' section would look like (in bytes):
625 * +--------------+------------+-----------------------------------+
626 * | event_logged | sequence # | error log |
627 * |0 3|4 7|8 nvram_error_log_size-1|
628 * +--------------+------------+-----------------------------------+
630 * event_logged: 0 if event has not been logged to syslog, 1 if it has
631 * sequence #: The unique sequence # for each event. (until it wraps)
632 * error log: The error log from event_scan
634 int nvram_write_error_log(char * buff, int length, unsigned int err_type)
638 struct err_log_info info;
644 if (nvram_error_log_index == -1) {
648 if (length > nvram_error_log_size) {
649 length = nvram_error_log_size;
652 info.error_type = err_type;
653 info.seq_num = error_log_cnt;
655 tmp_index = nvram_error_log_index;
657 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
659 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
663 rc = ppc_md.nvram_write(buff, length, &tmp_index);
665 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
672 /* nvram_read_error_log
674 * Reads nvram for error log for at most 'length'
676 int nvram_read_error_log(char * buff, int length, unsigned int * err_type)
680 struct err_log_info info;
682 if (nvram_error_log_index == -1)
685 if (length > nvram_error_log_size)
686 length = nvram_error_log_size;
688 tmp_index = nvram_error_log_index;
690 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
692 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
696 rc = ppc_md.nvram_read(buff, length, &tmp_index);
698 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
702 error_log_cnt = info.seq_num;
703 *err_type = info.error_type;
708 /* This doesn't actually zero anything, but it sets the event_logged
709 * word to tell that this event is safely in syslog.
711 int nvram_clear_error_log(void)
714 int clear_word = ERR_FLAG_ALREADY_LOGGED;
717 tmp_index = nvram_error_log_index;
719 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
721 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
728 #endif /* CONFIG_PPC_PSERIES */
730 module_init(nvram_init);
731 module_exit(nvram_cleanup);
732 MODULE_LICENSE("GPL");