nvram_64.c revision f9ce299fc629d5c899a2e56b00e21f5da05cf590
11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* 21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * c 2001 PPC 64 Team, IBM Corp 31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is free software; you can redistribute it and/or 51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * modify it under the terms of the GNU General Public License 61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * as published by the Free Software Foundation; either version 71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 2 of the License, or (at your option) any later version. 81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * /dev/nvram driver for PPC64 101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This perhaps should live in drivers/char 121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * TODO: Split the /dev/nvram part (that one can use 141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * drivers/char/generic_nvram.c) from the arch & partition 151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * parsing code. 161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h> 191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/types.h> 211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/errno.h> 221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/fs.h> 231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/miscdevice.h> 241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/fcntl.h> 251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/nvram.h> 261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h> 271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/slab.h> 281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/spinlock.h> 291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/uaccess.h> 301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/nvram.h> 311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/rtas.h> 321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/prom.h> 331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/machdep.h> 341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef DEBUG_NVRAM 361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_scan_partitions(void); 381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_setup_partition(void); 391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_create_os_partition(void); 401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_remove_os_partition(void); 411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct nvram_partition * nvram_part; 431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic long nvram_error_log_index = -1; 441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic long nvram_error_log_size = 0; 451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint no_logging = 1; /* Until we initialize everything, 471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * make sure we don't try logging 481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * anything */ 491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsextern volatile int error_log_cnt; 511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct err_log_info { 531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int error_type; 541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds unsigned int seq_num; 551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}; 561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin) 581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int size; 601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (ppc_md.nvram_size == NULL) 621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -ENODEV; 631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds size = ppc_md.nvram_size(); 641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds switch (origin) { 661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds case 1: 671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds offset += file->f_pos; 681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds break; 691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds case 2: 701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds offset += size; 711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds break; 721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (offset < 0) 741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -EINVAL; 751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds file->f_pos = offset; 761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return file->f_pos; 771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t dev_nvram_read(struct file *file, char __user *buf, 811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds size_t count, loff_t *ppos) 821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 83f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ssize_t ret; 84f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann char *tmp = NULL; 85f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ssize_t size; 861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 87f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ret = -ENODEV; 88f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann if (!ppc_md.nvram_size) 89f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann goto out; 90f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann 91f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ret = 0; 921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds size = ppc_md.nvram_size(); 93f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann if (*ppos >= size || size < 0) 94f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann goto out; 951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 96f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann count = min_t(size_t, count, size - *ppos); 97f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann count = min(count, PAGE_SIZE); 981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 99f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ret = -ENOMEM; 100f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann tmp = kmalloc(count, GFP_KERNEL); 101f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann if (!tmp) 102f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann goto out; 1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 104f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ret = ppc_md.nvram_read(tmp, count, ppos); 105f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann if (ret <= 0) 106f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann goto out; 107f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann 108f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann if (copy_to_user(buf, tmp, ret)) 109f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ret = -EFAULT; 1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 111f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmannout: 112f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann kfree(tmp); 113f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann return ret; 1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t dev_nvram_write(struct file *file, const char __user *buf, 118f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann size_t count, loff_t *ppos) 1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 120f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ssize_t ret; 121f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann char *tmp = NULL; 122f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ssize_t size; 1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 124f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ret = -ENODEV; 125f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann if (!ppc_md.nvram_size) 126f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann goto out; 127f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann 128f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ret = 0; 1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds size = ppc_md.nvram_size(); 130f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann if (*ppos >= size || size < 0) 131f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann goto out; 1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 133f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann count = min_t(size_t, count, size - *ppos); 134f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann count = min(count, PAGE_SIZE); 1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 136f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ret = -ENOMEM; 137f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann tmp = kmalloc(count, GFP_KERNEL); 138f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann if (!tmp) 139f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann goto out; 140f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann 141f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ret = -EFAULT; 142f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann if (copy_from_user(tmp, buf, count)) 143f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann goto out; 144f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann 145f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann ret = ppc_md.nvram_write(tmp, count, ppos); 146f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann 147f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmannout: 148f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann kfree(tmp); 149f9ce299fc629d5c899a2e56b00e21f5da05cf590Arnd Bergmann return ret; 1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int dev_nvram_ioctl(struct inode *inode, struct file *file, 1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds unsigned int cmd, unsigned long arg) 1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds switch(cmd) { 1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef CONFIG_PPC_PMAC 1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds case OBSOLETE_PMAC_NVRAM_GET_OFFSET: 1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n"); 1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds case IOC_NVRAM_GET_OFFSET: { 1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int part, offset; 1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 163799d6046d3fb557006e6d7c9767fdb96479b0e0aPaul Mackerras if (_machine != PLATFORM_POWERMAC) 1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -EINVAL; 1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0) 1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -EFAULT; 1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (part < pmac_nvram_OF || part > pmac_nvram_NR) 1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -EINVAL; 1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds offset = pmac_get_partition(part); 1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (offset < 0) 1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return offset; 1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0) 1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -EFAULT; 1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif /* CONFIG_PPC_PMAC */ 1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -EINVAL; 1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct file_operations nvram_fops = { 1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .owner = THIS_MODULE, 1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .llseek = dev_nvram_llseek, 1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .read = dev_nvram_read, 1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .write = dev_nvram_write, 1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .ioctl = dev_nvram_ioctl, 1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}; 1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct miscdevice nvram_dev = { 1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds NVRAM_MINOR, 1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "nvram", 1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds &nvram_fops 1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}; 1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef DEBUG_NVRAM 1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void nvram_print_partitions(char * label) 1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct list_head * p; 2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct nvram_partition * tmp_part; 2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_WARNING "--------%s---------\n", label); 2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n"); 2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds list_for_each(p, &nvram_part->partition) { 2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds tmp_part = list_entry(p, struct nvram_partition, partition); 2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_WARNING "%d \t%02x\t%02x\t%d\t%s\n", 2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds tmp_part->index, tmp_part->header.signature, 2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds tmp_part->header.checksum, tmp_part->header.length, 2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds tmp_part->header.name); 2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif 2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_write_header(struct nvram_partition * part) 2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds loff_t tmp_index; 2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int rc; 2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds tmp_index = part->index; 2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index); 2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic unsigned char nvram_checksum(struct nvram_header *p) 2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds unsigned int c_sum, c_sum2; 2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */ 2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5]; 2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* The sum may have spilled into the 3rd byte. Fold it back. */ 2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff; 2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* The sum cannot exceed 2 bytes. Fold it into a checksum */ 2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds c_sum2 = (c_sum >> 8) + (c_sum << 8); 2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds c_sum = ((c_sum + c_sum2) >> 8) & 0xff; 2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return c_sum; 2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* 2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Find an nvram partition, sig can be 0 for any 2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * partition or name can be NULL for any name, else 2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * tries to match both 2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct nvram_partition *nvram_find_partition(int sig, const char *name) 2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct nvram_partition * part; 2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct list_head * p; 2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds list_for_each(p, &nvram_part->partition) { 2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds part = list_entry(p, struct nvram_partition, partition); 2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (sig && part->header.signature != sig) 2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds continue; 2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (name && 0 != strncmp(name, part->header.name, 12)) 2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds continue; 2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return part; 2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return NULL; 2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(nvram_find_partition); 2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_remove_os_partition(void) 2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct list_head *i; 2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct list_head *j; 2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct nvram_partition * part; 2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct nvram_partition * cur_part; 2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int rc; 2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds list_for_each(i, &nvram_part->partition) { 2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds part = list_entry(i, struct nvram_partition, partition); 2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (part->header.signature != NVRAM_SIG_OS) 2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds continue; 2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Make os partition a free partition */ 2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds part->header.signature = NVRAM_SIG_FREE; 2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sprintf(part->header.name, "wwwwwwwwwwww"); 2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds part->header.checksum = nvram_checksum(&part->header); 2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Merge contiguous free partitions backwards */ 2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds list_for_each_prev(j, &part->partition) { 2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cur_part = list_entry(j, struct nvram_partition, partition); 2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) { 2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds break; 2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds part->header.length += cur_part->header.length; 2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds part->header.checksum = nvram_checksum(&part->header); 2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds part->index = cur_part->index; 2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds list_del(&cur_part->partition); 2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds kfree(cur_part); 2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds j = &part->partition; /* fixup our loop */ 2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Merge contiguous free partitions forwards */ 3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds list_for_each(j, &part->partition) { 3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cur_part = list_entry(j, struct nvram_partition, partition); 3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) { 3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds break; 3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds part->header.length += cur_part->header.length; 3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds part->header.checksum = nvram_checksum(&part->header); 3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds list_del(&cur_part->partition); 3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds kfree(cur_part); 3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds j = &part->partition; /* fixup our loop */ 3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = nvram_write_header(part); 3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (rc <= 0) { 3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_remove_os_partition: nvram_write failed (%d)\n", rc); 3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* nvram_create_os_partition 3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Create a OS linux partition to buffer error logs. 3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Will create a partition starting at the first free 3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * space found if space has enough room. 3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_create_os_partition(void) 3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 334a341ad97245d01c923995cfe7deacd0c8aee6e16Arnd Bergmann struct nvram_partition *part; 335a341ad97245d01c923995cfe7deacd0c8aee6e16Arnd Bergmann struct nvram_partition *new_part; 3360339ad77c4a06fa8529db17c91f790058e18b65bAndrew Morton struct nvram_partition *free_part = NULL; 3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int seq_init[2] = { 0, 0 }; 3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds loff_t tmp_index; 3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds long size = 0; 3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int rc; 3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Find a free partition that will give us the maximum needed size 3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds If can't find one that will give us the minimum size needed */ 344a341ad97245d01c923995cfe7deacd0c8aee6e16Arnd Bergmann list_for_each_entry(part, &nvram_part->partition, partition) { 3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (part->header.signature != NVRAM_SIG_FREE) 3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds continue; 3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (part->header.length >= NVRAM_MAX_REQ) { 3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds size = NVRAM_MAX_REQ; 3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds free_part = part; 3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds break; 3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!size && part->header.length >= NVRAM_MIN_REQ) { 3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds size = NVRAM_MIN_REQ; 3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds free_part = part; 3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3580339ad77c4a06fa8529db17c91f790058e18b65bAndrew Morton if (!size) 3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -ENOSPC; 3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Create our OS partition */ 3620339ad77c4a06fa8529db17c91f790058e18b65bAndrew Morton new_part = kmalloc(sizeof(*new_part), GFP_KERNEL); 3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!new_part) { 3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n"); 3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -ENOMEM; 3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds new_part->index = free_part->index; 3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds new_part->header.signature = NVRAM_SIG_OS; 3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds new_part->header.length = size; 3710339ad77c4a06fa8529db17c91f790058e18b65bAndrew Morton strcpy(new_part->header.name, "ppc64,linux"); 3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds new_part->header.checksum = nvram_checksum(&new_part->header); 3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = nvram_write_header(new_part); 3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (rc <= 0) { 3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_create_os_partition: nvram_write_header \ 3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds failed (%d)\n", rc); 3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* make sure and initialize to zero the sequence number and the error 3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds type logged */ 3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds tmp_index = new_part->index + NVRAM_HEADER_LEN; 3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = ppc_md.nvram_write((char *)&seq_init, sizeof(seq_init), &tmp_index); 3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (rc <= 0) { 3860339ad77c4a06fa8529db17c91f790058e18b65bAndrew Morton printk(KERN_ERR "nvram_create_os_partition: nvram_write " 3870339ad77c4a06fa8529db17c91f790058e18b65bAndrew Morton "failed (%d)\n", rc); 3881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds nvram_error_log_index = new_part->index + NVRAM_HEADER_LEN; 3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds nvram_error_log_size = ((part->header.length - 1) * 3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds NVRAM_BLOCK_LEN) - sizeof(struct err_log_info); 3941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds list_add_tail(&new_part->partition, &free_part->partition); 3961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (free_part->header.length <= size) { 3981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds list_del(&free_part->partition); 3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds kfree(free_part); 4001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 4011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Adjust the partition we stole the space from */ 4041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds free_part->index += size * NVRAM_BLOCK_LEN; 4051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds free_part->header.length -= size; 4061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds free_part->header.checksum = nvram_checksum(&free_part->header); 4071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = nvram_write_header(free_part); 4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (rc <= 0) { 4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_create_os_partition: nvram_write_header " 4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "failed (%d)\n", rc); 4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* nvram_setup_partition 4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This will setup the partition we need for buffering the 4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * error logs and cleanup partitions if needed. 4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The general strategy is the following: 4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 1.) If there is ppc64,linux partition large enough then use it. 4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 2.) If there is not a ppc64,linux partition large enough, search 4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * for a free partition that is large enough. 4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 3.) If there is not a free partition large enough remove 4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * _all_ OS partitions and consolidate the space. 4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 4.) Will first try getting a chunk that will satisfy the maximum 4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * error log size (NVRAM_MAX_REQ). 4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 5.) If the max chunk cannot be allocated then try finding a chunk 4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * that will satisfy the minum needed (NVRAM_MIN_REQ). 4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_setup_partition(void) 4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct list_head * p; 4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct nvram_partition * part; 4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int rc; 4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* For now, we don't do any of this on pmac, until I 4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * have figured out if it's worth killing some unused stuffs 4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * in our nvram, as Apple defined partitions use pretty much 4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * all of the space 4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 446799d6046d3fb557006e6d7c9767fdb96479b0e0aPaul Mackerras if (_machine == PLATFORM_POWERMAC) 4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -ENOSPC; 4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* see if we have an OS partition that meets our needs. 4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds will try getting the max we need. If not we'll delete 4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds partitions and try again. */ 4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds list_for_each(p, &nvram_part->partition) { 4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds part = list_entry(p, struct nvram_partition, partition); 4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (part->header.signature != NVRAM_SIG_OS) 4551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds continue; 4561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (strcmp(part->header.name, "ppc64,linux")) 4581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds continue; 4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (part->header.length >= NVRAM_MIN_REQ) { 4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* found our partition */ 4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds nvram_error_log_index = part->index + NVRAM_HEADER_LEN; 4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds nvram_error_log_size = ((part->header.length - 1) * 4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds NVRAM_BLOCK_LEN) - sizeof(struct err_log_info); 4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* try creating a partition with the free space we have */ 4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = nvram_create_os_partition(); 4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!rc) { 4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* need to free up some space */ 4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = nvram_remove_os_partition(); 4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (rc) { 4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* create a partition in this new space */ 4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = nvram_create_os_partition(); 4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (rc) { 4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_create_os_partition: Could not find a " 4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "NVRAM partition large enough\n"); 4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_scan_partitions(void) 4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds loff_t cur_index = 0; 4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct nvram_header phead; 4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct nvram_partition * tmp_part; 4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds unsigned char c_sum; 4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds char * header; 5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int total_size; 5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int err; 5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (ppc_md.nvram_size == NULL) 5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -ENODEV; 5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds total_size = ppc_md.nvram_size(); 5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds header = (char *) kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL); 5081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!header) { 5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n"); 5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -ENOMEM; 5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 5121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds while (cur_index < total_size) { 5141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index); 5161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (err != NVRAM_HEADER_LEN) { 5171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_scan_partitions: Error parsing " 5181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "nvram partitions\n"); 5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds goto out; 5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 5211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */ 5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds memcpy(&phead, header, NVRAM_HEADER_LEN); 5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds err = 0; 5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds c_sum = nvram_checksum(&phead); 5281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (c_sum != phead.checksum) { 5291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_WARNING "WARNING: nvram partition checksum" 5301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds " was %02x, should be %02x!\n", 5311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds phead.checksum, c_sum); 5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_WARNING "Terminating nvram partition scan\n"); 5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds goto out; 5341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 5351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!phead.length) { 5361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_WARNING "WARNING: nvram corruption " 5371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "detected: 0-length partition\n"); 5381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds goto out; 5391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 5401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds tmp_part = (struct nvram_partition *) 5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds kmalloc(sizeof(struct nvram_partition), GFP_KERNEL); 5421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds err = -ENOMEM; 5431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!tmp_part) { 5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n"); 5451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds goto out; 5461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 5471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN); 5491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds tmp_part->index = cur_index; 5501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds list_add_tail(&tmp_part->partition, &nvram_part->partition); 5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cur_index += phead.length * NVRAM_BLOCK_LEN; 5531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 5541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds err = 0; 5551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds out: 5571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds kfree(header); 5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return err; 5591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 5601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int __init nvram_init(void) 5621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int error; 5641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int rc; 5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0) 5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -ENODEV; 5681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = misc_register(&nvram_dev); 5701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (rc != 0) { 5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_init: failed to register device\n"); 5721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 5731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 5741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* initialize our anchor for the nvram partition list */ 5761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds nvram_part = (struct nvram_partition *) kmalloc(sizeof(struct nvram_partition), GFP_KERNEL); 5771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!nvram_part) { 5781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_init: Failed kmalloc\n"); 5791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -ENOMEM; 5801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 5811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds INIT_LIST_HEAD(&nvram_part->partition); 5821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Get all the NVRAM partitions */ 5841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds error = nvram_scan_partitions(); 5851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (error) { 5861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n"); 5871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return error; 5881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 5891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if(nvram_setup_partition()) 5911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_WARNING "nvram_init: Could not find nvram partition" 5921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds " for nvram buffered error logging.\n"); 5931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef DEBUG_NVRAM 5951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds nvram_print_partitions("NVRAM Partitions"); 5961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif 5971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 5991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 6001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid __exit nvram_cleanup(void) 6021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 6031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds misc_deregister( &nvram_dev ); 6041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 6051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef CONFIG_PPC_PSERIES 6081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* nvram_write_error_log 6101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 6111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * We need to buffer the error logs into nvram to ensure that we have 6121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the failure information to decode. If we have a severe error there 6131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * is no way to guarantee that the OS or the machine is in a state to 6141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * get back to user land and write the error to disk. For example if 6151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the SCSI device driver causes a Machine Check by writing to a bad 6161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * IO address, there is no way of guaranteeing that the device driver 6171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * is in any state that is would also be able to write the error data 6181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * captured to disk, thus we buffer it in NVRAM for analysis on the 6191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * next boot. 6201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 6211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * In NVRAM the partition containing the error log buffer will looks like: 6221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Header (in bytes): 6231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * +-----------+----------+--------+------------+------------------+ 6241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * | signature | checksum | length | name | data | 6251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * |0 |1 |2 3|4 15|16 length-1| 6261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * +-----------+----------+--------+------------+------------------+ 6271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 6281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The 'data' section would look like (in bytes): 6291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * +--------------+------------+-----------------------------------+ 6301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * | event_logged | sequence # | error log | 6311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * |0 3|4 7|8 nvram_error_log_size-1| 6321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * +--------------+------------+-----------------------------------+ 6331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 6341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * event_logged: 0 if event has not been logged to syslog, 1 if it has 6351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * sequence #: The unique sequence # for each event. (until it wraps) 6361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * error log: The error log from event_scan 6371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 6381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint nvram_write_error_log(char * buff, int length, unsigned int err_type) 6391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 6401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int rc; 6411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds loff_t tmp_index; 6421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct err_log_info info; 6431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (no_logging) { 6451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -EPERM; 6461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 6471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (nvram_error_log_index == -1) { 6491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -ESPIPE; 6501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 6511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (length > nvram_error_log_size) { 6531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds length = nvram_error_log_size; 6541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 6551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds info.error_type = err_type; 6571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds info.seq_num = error_log_cnt; 6581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds tmp_index = nvram_error_log_index; 6601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index); 6621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (rc <= 0) { 6631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc); 6641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 6651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 6661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = ppc_md.nvram_write(buff, length, &tmp_index); 6681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (rc <= 0) { 6691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc); 6701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 6711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 6721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 6741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 6751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* nvram_read_error_log 6771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 6781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Reads nvram for error log for at most 'length' 6791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 6801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint nvram_read_error_log(char * buff, int length, unsigned int * err_type) 6811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 6821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int rc; 6831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds loff_t tmp_index; 6841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct err_log_info info; 6851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (nvram_error_log_index == -1) 6871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return -1; 6881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (length > nvram_error_log_size) 6901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds length = nvram_error_log_size; 6911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds tmp_index = nvram_error_log_index; 6931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index); 6951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (rc <= 0) { 6961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc); 6971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 6981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 6991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = ppc_md.nvram_read(buff, length, &tmp_index); 7011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (rc <= 0) { 7021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc); 7031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 7041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 7051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds error_log_cnt = info.seq_num; 7071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *err_type = info.error_type; 7081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 7101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 7111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This doesn't actually zero anything, but it sets the event_logged 7131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * word to tell that this event is safely in syslog. 7141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 7151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint nvram_clear_error_log(void) 7161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 7171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds loff_t tmp_index; 7181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int clear_word = ERR_FLAG_ALREADY_LOGGED; 7191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int rc; 7201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds tmp_index = nvram_error_log_index; 7221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index); 7241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (rc <= 0) { 7251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc); 7261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 7271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 7281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 7301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 7311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif /* CONFIG_PPC_PSERIES */ 7331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_init(nvram_init); 7351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_exit(nvram_cleanup); 7361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL"); 737