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 #define NVRAM_HEADER_LEN sizeof(struct nvram_header)
38 #define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN
39 #define NVRAM_MAX_REQ 2079
40 #define NVRAM_MIN_REQ 1055
42 /* If change this size, then change the size of NVNAME_LEN */
44 unsigned char signature;
45 unsigned char checksum;
46 unsigned short length;
50 struct nvram_partition {
51 struct list_head partition;
52 struct nvram_header header;
56 static struct nvram_partition * nvram_part;
57 static long nvram_error_log_index = -1;
58 static long nvram_error_log_size = 0;
65 static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
69 if (ppc_md.nvram_size == NULL)
71 size = ppc_md.nvram_size();
75 offset += file->f_pos;
88 static ssize_t dev_nvram_read(struct file *file, char __user *buf,
89 size_t count, loff_t *ppos)
96 if (!ppc_md.nvram_size)
100 size = ppc_md.nvram_size();
101 if (*ppos >= size || size < 0)
104 count = min_t(size_t, count, size - *ppos);
105 count = min(count, PAGE_SIZE);
108 tmp = kmalloc(count, GFP_KERNEL);
112 ret = ppc_md.nvram_read(tmp, count, ppos);
116 if (copy_to_user(buf, tmp, ret))
125 static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
126 size_t count, loff_t *ppos)
133 if (!ppc_md.nvram_size)
137 size = ppc_md.nvram_size();
138 if (*ppos >= size || size < 0)
141 count = min_t(size_t, count, size - *ppos);
142 count = min(count, PAGE_SIZE);
145 tmp = kmalloc(count, GFP_KERNEL);
150 if (copy_from_user(tmp, buf, count))
153 ret = ppc_md.nvram_write(tmp, count, ppos);
161 static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
165 #ifdef CONFIG_PPC_PMAC
166 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
167 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
168 case IOC_NVRAM_GET_OFFSET: {
171 if (!machine_is(powermac))
173 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
175 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
177 offset = pmac_get_partition(part);
180 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
184 #endif /* CONFIG_PPC_PMAC */
190 const struct file_operations nvram_fops = {
191 .owner = THIS_MODULE,
192 .llseek = dev_nvram_llseek,
193 .read = dev_nvram_read,
194 .write = dev_nvram_write,
195 .unlocked_ioctl = dev_nvram_ioctl,
198 static struct miscdevice nvram_dev = {
206 static void __init nvram_print_partitions(char * label)
208 struct list_head * p;
209 struct nvram_partition * tmp_part;
211 printk(KERN_WARNING "--------%s---------\n", label);
212 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
213 list_for_each(p, &nvram_part->partition) {
214 tmp_part = list_entry(p, struct nvram_partition, partition);
215 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%s\n",
216 tmp_part->index, tmp_part->header.signature,
217 tmp_part->header.checksum, tmp_part->header.length,
218 tmp_part->header.name);
224 static int __init nvram_write_header(struct nvram_partition * part)
229 tmp_index = part->index;
230 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
236 static unsigned char __init nvram_checksum(struct nvram_header *p)
238 unsigned int c_sum, c_sum2;
239 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
240 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
242 /* The sum may have spilled into the 3rd byte. Fold it back. */
243 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
244 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
245 c_sum2 = (c_sum >> 8) + (c_sum << 8);
246 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
251 * nvram_remove_partition - Remove one or more partitions in nvram
252 * @name: name of the partition to remove, or NULL for a
253 * signature only match
254 * @sig: signature of the partition(s) to remove
257 static int __init nvram_remove_partition(const char *name, int sig)
259 struct nvram_partition *part, *prev, *tmp;
262 list_for_each_entry(part, &nvram_part->partition, partition) {
263 if (part->header.signature != sig)
265 if (name && strncmp(name, part->header.name, 12))
268 /* Make partition a free partition */
269 part->header.signature = NVRAM_SIG_FREE;
270 sprintf(part->header.name, "wwwwwwwwwwww");
271 part->header.checksum = nvram_checksum(&part->header);
272 rc = nvram_write_header(part);
274 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
279 /* Merge contiguous ones */
281 list_for_each_entry_safe(part, tmp, &nvram_part->partition, partition) {
282 if (part->header.signature != NVRAM_SIG_FREE) {
287 prev->header.length += part->header.length;
288 prev->header.checksum = nvram_checksum(&part->header);
289 rc = nvram_write_header(part);
291 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
294 list_del(&part->partition);
304 * nvram_create_partition - Create a partition in nvram
305 * @name: name of the partition to create
306 * @sig: signature of the partition to create
307 * @req_size: size of data to allocate in bytes
308 * @min_size: minimum acceptable size (0 means req_size)
310 * Returns a negative error code or a positive nvram index
311 * of the beginning of the data area of the newly created
312 * partition. If you provided a min_size smaller than req_size
313 * you need to query for the actual size yourself after the
314 * call using nvram_partition_get_size().
316 static loff_t __init nvram_create_partition(const char *name, int sig,
317 int req_size, int min_size)
319 struct nvram_partition *part;
320 struct nvram_partition *new_part;
321 struct nvram_partition *free_part = NULL;
322 static char nv_init_vals[16];
327 /* Convert sizes from bytes to blocks */
328 req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
329 min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
331 /* If no minimum size specified, make it the same as the
336 if (min_size > req_size)
339 /* Now add one block to each for the header */
343 /* Find a free partition that will give us the maximum needed size
344 If can't find one that will give us the minimum size needed */
345 list_for_each_entry(part, &nvram_part->partition, partition) {
346 if (part->header.signature != NVRAM_SIG_FREE)
349 if (part->header.length >= req_size) {
354 if (part->header.length > size &&
355 part->header.length >= min_size) {
356 size = part->header.length;
363 /* Create our OS partition */
364 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
366 pr_err("nvram_create_os_partition: kmalloc failed\n");
370 new_part->index = free_part->index;
371 new_part->header.signature = sig;
372 new_part->header.length = size;
373 strncpy(new_part->header.name, name, 12);
374 new_part->header.checksum = nvram_checksum(&new_part->header);
376 rc = nvram_write_header(new_part);
378 pr_err("nvram_create_os_partition: nvram_write_header "
379 "failed (%d)\n", rc);
382 list_add_tail(&new_part->partition, &free_part->partition);
384 /* Adjust or remove the partition we stole the space from */
385 if (free_part->header.length > size) {
386 free_part->index += size * NVRAM_BLOCK_LEN;
387 free_part->header.length -= size;
388 free_part->header.checksum = nvram_checksum(&free_part->header);
389 rc = nvram_write_header(free_part);
391 pr_err("nvram_create_os_partition: nvram_write_header "
392 "failed (%d)\n", rc);
396 list_del(&free_part->partition);
400 /* Clear the new partition */
401 for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
402 tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
403 tmp_index += NVRAM_BLOCK_LEN) {
404 rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
406 pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
411 return new_part->index + NVRAM_HEADER_LEN;
415 * nvram_get_partition_size - Get the data size of an nvram partition
416 * @data_index: This is the offset of the start of the data of
417 * the partition. The same value that is returned by
418 * nvram_create_partition().
420 static int nvram_get_partition_size(loff_t data_index)
422 struct nvram_partition *part;
424 list_for_each_entry(part, &nvram_part->partition, partition) {
425 if (part->index + NVRAM_HEADER_LEN == data_index)
426 return (part->header.length - 1) * NVRAM_BLOCK_LEN;
432 /* nvram_setup_partition
434 * This will setup the partition we need for buffering the
435 * error logs and cleanup partitions if needed.
437 * The general strategy is the following:
438 * 1.) If there is ppc64,linux partition large enough then use it.
439 * 2.) If there is not a ppc64,linux partition large enough, search
440 * for a free partition that is large enough.
441 * 3.) If there is not a free partition large enough remove
442 * _all_ OS partitions and consolidate the space.
443 * 4.) Will first try getting a chunk that will satisfy the maximum
444 * error log size (NVRAM_MAX_REQ).
445 * 5.) If the max chunk cannot be allocated then try finding a chunk
446 * that will satisfy the minum needed (NVRAM_MIN_REQ).
448 static int __init nvram_setup_partition(void)
450 struct list_head * p;
451 struct nvram_partition * part;
454 /* For now, we don't do any of this on pmac, until I
455 * have figured out if it's worth killing some unused stuffs
456 * in our nvram, as Apple defined partitions use pretty much
459 if (machine_is(powermac))
462 /* see if we have an OS partition that meets our needs.
463 will try getting the max we need. If not we'll delete
464 partitions and try again. */
465 list_for_each(p, &nvram_part->partition) {
466 part = list_entry(p, struct nvram_partition, partition);
467 if (part->header.signature != NVRAM_SIG_OS)
470 if (strcmp(part->header.name, "ppc64,linux"))
473 if ((part->header.length - 1) * NVRAM_BLOCK_LEN >= NVRAM_MIN_REQ) {
474 /* found our partition */
475 nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
476 nvram_error_log_size = ((part->header.length - 1) *
477 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
481 /* Found one but it's too small, remove it */
482 nvram_remove_partition("ppc64,linux", NVRAM_SIG_OS);
485 /* try creating a partition with the free space we have */
486 rc = nvram_create_partition("ppc64,linux", NVRAM_SIG_OS,
487 NVRAM_MAX_REQ, NVRAM_MIN_REQ);
489 /* need to free up some space, remove any "OS" partition */
490 nvram_remove_partition(NULL, NVRAM_SIG_OS);
493 rc = nvram_create_partition("ppc64,linux", NVRAM_SIG_OS,
494 NVRAM_MAX_REQ, NVRAM_MIN_REQ);
496 pr_err("nvram_create_partition: Could not find"
497 " enough space in NVRAM for partition\n");
502 nvram_error_log_index = rc;
503 nvram_error_log_size = nvram_get_partition_size(rc) - sizeof(struct err_log_info);
507 static int __init nvram_scan_partitions(void)
509 loff_t cur_index = 0;
510 struct nvram_header phead;
511 struct nvram_partition * tmp_part;
517 if (ppc_md.nvram_size == NULL)
519 total_size = ppc_md.nvram_size();
521 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
523 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
527 while (cur_index < total_size) {
529 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
530 if (err != NVRAM_HEADER_LEN) {
531 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
532 "nvram partitions\n");
536 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
538 memcpy(&phead, header, NVRAM_HEADER_LEN);
541 c_sum = nvram_checksum(&phead);
542 if (c_sum != phead.checksum) {
543 printk(KERN_WARNING "WARNING: nvram partition checksum"
544 " was %02x, should be %02x!\n",
545 phead.checksum, c_sum);
546 printk(KERN_WARNING "Terminating nvram partition scan\n");
550 printk(KERN_WARNING "WARNING: nvram corruption "
551 "detected: 0-length partition\n");
554 tmp_part = (struct nvram_partition *)
555 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
558 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
562 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
563 tmp_part->index = cur_index;
564 list_add_tail(&tmp_part->partition, &nvram_part->partition);
566 cur_index += phead.length * NVRAM_BLOCK_LEN;
575 static int __init nvram_init(void)
580 BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
582 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
585 rc = misc_register(&nvram_dev);
587 printk(KERN_ERR "nvram_init: failed to register device\n");
591 /* initialize our anchor for the nvram partition list */
592 nvram_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
594 printk(KERN_ERR "nvram_init: Failed kmalloc\n");
597 INIT_LIST_HEAD(&nvram_part->partition);
599 /* Get all the NVRAM partitions */
600 error = nvram_scan_partitions();
602 printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
606 if(nvram_setup_partition())
607 printk(KERN_WARNING "nvram_init: Could not find nvram partition"
608 " for nvram buffered error logging.\n");
611 nvram_print_partitions("NVRAM Partitions");
617 void __exit nvram_cleanup(void)
619 misc_deregister( &nvram_dev );
623 #ifdef CONFIG_PPC_PSERIES
625 /* nvram_write_error_log
627 * We need to buffer the error logs into nvram to ensure that we have
628 * the failure information to decode. If we have a severe error there
629 * is no way to guarantee that the OS or the machine is in a state to
630 * get back to user land and write the error to disk. For example if
631 * the SCSI device driver causes a Machine Check by writing to a bad
632 * IO address, there is no way of guaranteeing that the device driver
633 * is in any state that is would also be able to write the error data
634 * captured to disk, thus we buffer it in NVRAM for analysis on the
637 * In NVRAM the partition containing the error log buffer will looks like:
639 * +-----------+----------+--------+------------+------------------+
640 * | signature | checksum | length | name | data |
641 * |0 |1 |2 3|4 15|16 length-1|
642 * +-----------+----------+--------+------------+------------------+
644 * The 'data' section would look like (in bytes):
645 * +--------------+------------+-----------------------------------+
646 * | event_logged | sequence # | error log |
647 * |0 3|4 7|8 nvram_error_log_size-1|
648 * +--------------+------------+-----------------------------------+
650 * event_logged: 0 if event has not been logged to syslog, 1 if it has
651 * sequence #: The unique sequence # for each event. (until it wraps)
652 * error log: The error log from event_scan
654 int nvram_write_error_log(char * buff, int length,
655 unsigned int err_type, unsigned int error_log_cnt)
659 struct err_log_info info;
661 if (nvram_error_log_index == -1) {
665 if (length > nvram_error_log_size) {
666 length = nvram_error_log_size;
669 info.error_type = err_type;
670 info.seq_num = error_log_cnt;
672 tmp_index = nvram_error_log_index;
674 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
676 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
680 rc = ppc_md.nvram_write(buff, length, &tmp_index);
682 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
689 /* nvram_read_error_log
691 * Reads nvram for error log for at most 'length'
693 int nvram_read_error_log(char * buff, int length,
694 unsigned int * err_type, unsigned int * error_log_cnt)
698 struct err_log_info info;
700 if (nvram_error_log_index == -1)
703 if (length > nvram_error_log_size)
704 length = nvram_error_log_size;
706 tmp_index = nvram_error_log_index;
708 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
710 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
714 rc = ppc_md.nvram_read(buff, length, &tmp_index);
716 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
720 *error_log_cnt = info.seq_num;
721 *err_type = info.error_type;
726 /* This doesn't actually zero anything, but it sets the event_logged
727 * word to tell that this event is safely in syslog.
729 int nvram_clear_error_log(void)
732 int clear_word = ERR_FLAG_ALREADY_LOGGED;
735 if (nvram_error_log_index == -1)
738 tmp_index = nvram_error_log_index;
740 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
742 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
749 #endif /* CONFIG_PPC_PSERIES */
751 module_init(nvram_init);
752 module_exit(nvram_cleanup);
753 MODULE_LICENSE("GPL");