nvram_64.c revision e8222502ee6157e2713da9e0792c21f4ad458d50
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
163e8222502ee6157e2713da9e0792c21f4ad458d50Benjamin Herrenschmidt		if (!machine_is(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 */
177af308377e204e25f1f58627d05fe0f483703b514Stephen Rothwell	default:
178af308377e204e25f1f58627d05fe0f483703b514Stephen Rothwell		return -EINVAL;
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct file_operations nvram_fops = {
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.owner =	THIS_MODULE,
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.llseek =	dev_nvram_llseek,
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.read =		dev_nvram_read,
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.write =	dev_nvram_write,
1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.ioctl =	dev_nvram_ioctl,
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct miscdevice nvram_dev = {
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	NVRAM_MINOR,
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	"nvram",
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	&nvram_fops
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef DEBUG_NVRAM
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void nvram_print_partitions(char * label)
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct list_head * p;
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct nvram_partition * tmp_part;
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	printk(KERN_WARNING "--------%s---------\n", label);
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each(p, &nvram_part->partition) {
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		tmp_part = list_entry(p, struct nvram_partition, partition);
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_WARNING "%d    \t%02x\t%02x\t%d\t%s\n",
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       tmp_part->index, tmp_part->header.signature,
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       tmp_part->header.checksum, tmp_part->header.length,
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       tmp_part->header.name);
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_write_header(struct nvram_partition * part)
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	loff_t tmp_index;
2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int rc;
2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tmp_index = part->index;
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return rc;
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic unsigned char nvram_checksum(struct nvram_header *p)
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int c_sum, c_sum2;
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* The sum may have spilled into the 3rd byte.  Fold it back. */
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* The sum cannot exceed 2 bytes.  Fold it into a checksum */
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	c_sum2 = (c_sum >> 8) + (c_sum << 8);
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return c_sum;
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Find an nvram partition, sig can be 0 for any
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * partition or name can be NULL for any name, else
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * tries to match both
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct nvram_partition *nvram_find_partition(int sig, const char *name)
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct nvram_partition * part;
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct list_head * p;
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each(p, &nvram_part->partition) {
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		part = list_entry(p, struct nvram_partition, partition);
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (sig && part->header.signature != sig)
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (name && 0 != strncmp(name, part->header.name, 12))
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return part;
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return NULL;
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(nvram_find_partition);
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_remove_os_partition(void)
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct list_head *i;
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct list_head *j;
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct nvram_partition * part;
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct nvram_partition * cur_part;
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int rc;
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each(i, &nvram_part->partition) {
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		part = list_entry(i, struct nvram_partition, partition);
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (part->header.signature != NVRAM_SIG_OS)
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Make os partition a free partition */
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		part->header.signature = NVRAM_SIG_FREE;
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sprintf(part->header.name, "wwwwwwwwwwww");
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		part->header.checksum = nvram_checksum(&part->header);
2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Merge contiguous free partitions backwards */
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		list_for_each_prev(j, &part->partition) {
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cur_part = list_entry(j, struct nvram_partition, partition);
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			part->header.length += cur_part->header.length;
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			part->header.checksum = nvram_checksum(&part->header);
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			part->index = cur_part->index;
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			list_del(&cur_part->partition);
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			kfree(cur_part);
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			j = &part->partition; /* fixup our loop */
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Merge contiguous free partitions forwards */
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		list_for_each(j, &part->partition) {
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cur_part = list_entry(j, struct nvram_partition, partition);
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			part->header.length += cur_part->header.length;
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			part->header.checksum = nvram_checksum(&part->header);
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			list_del(&cur_part->partition);
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			kfree(cur_part);
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			j = &part->partition; /* fixup our loop */
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rc = nvram_write_header(part);
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (rc <= 0) {
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			printk(KERN_ERR "nvram_remove_os_partition: nvram_write failed (%d)\n", rc);
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return rc;
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* nvram_create_os_partition
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Create a OS linux partition to buffer error logs.
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Will create a partition starting at the first free
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * space found if space has enough room.
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_create_os_partition(void)
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
335a341ad97245d01c923995cfe7deacd0c8aee6e16Arnd Bergmann	struct nvram_partition *part;
336a341ad97245d01c923995cfe7deacd0c8aee6e16Arnd Bergmann	struct nvram_partition *new_part;
3370339ad77c4a06fa8529db17c91f790058e18b65bAndrew Morton	struct nvram_partition *free_part = NULL;
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int seq_init[2] = { 0, 0 };
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	loff_t tmp_index;
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	long size = 0;
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int rc;
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Find a free partition that will give us the maximum needed size
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   If can't find one that will give us the minimum size needed */
345a341ad97245d01c923995cfe7deacd0c8aee6e16Arnd Bergmann	list_for_each_entry(part, &nvram_part->partition, partition) {
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (part->header.signature != NVRAM_SIG_FREE)
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (part->header.length >= NVRAM_MAX_REQ) {
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			size = NVRAM_MAX_REQ;
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			free_part = part;
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (!size && part->header.length >= NVRAM_MIN_REQ) {
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			size = NVRAM_MIN_REQ;
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			free_part = part;
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3590339ad77c4a06fa8529db17c91f790058e18b65bAndrew Morton	if (!size)
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOSPC;
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Create our OS partition */
3630339ad77c4a06fa8529db17c91f790058e18b65bAndrew Morton	new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!new_part) {
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n");
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOMEM;
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_part->index = free_part->index;
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_part->header.signature = NVRAM_SIG_OS;
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_part->header.length = size;
3720339ad77c4a06fa8529db17c91f790058e18b65bAndrew Morton	strcpy(new_part->header.name, "ppc64,linux");
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_part->header.checksum = nvram_checksum(&new_part->header);
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rc = nvram_write_header(new_part);
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rc <= 0) {
3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "nvram_create_os_partition: nvram_write_header \
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				failed (%d)\n", rc);
3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return rc;
3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* make sure and initialize to zero the sequence number and the error
3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   type logged */
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tmp_index = new_part->index + NVRAM_HEADER_LEN;
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rc = ppc_md.nvram_write((char *)&seq_init, sizeof(seq_init), &tmp_index);
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rc <= 0) {
3870339ad77c4a06fa8529db17c91f790058e18b65bAndrew Morton		printk(KERN_ERR "nvram_create_os_partition: nvram_write "
3880339ad77c4a06fa8529db17c91f790058e18b65bAndrew Morton				"failed (%d)\n", rc);
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return rc;
3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	nvram_error_log_index = new_part->index + NVRAM_HEADER_LEN;
3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	nvram_error_log_size = ((part->header.length - 1) *
3941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
3951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_add_tail(&new_part->partition, &free_part->partition);
3971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (free_part->header.length <= size) {
3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		list_del(&free_part->partition);
4001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		kfree(free_part);
4011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
4021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Adjust the partition we stole the space from */
4051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	free_part->index += size * NVRAM_BLOCK_LEN;
4061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	free_part->header.length -= size;
4071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	free_part->header.checksum = nvram_checksum(&free_part->header);
4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rc = nvram_write_header(free_part);
4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rc <= 0) {
4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "nvram_create_os_partition: nvram_write_header "
4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       "failed (%d)\n", rc);
4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return rc;
4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* nvram_setup_partition
4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This will setup the partition we need for buffering the
4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * error logs and cleanup partitions if needed.
4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The general strategy is the following:
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 1.) If there is ppc64,linux partition large enough then use it.
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 2.) If there is not a ppc64,linux partition large enough, search
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * for a free partition that is large enough.
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 3.) If there is not a free partition large enough remove
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * _all_ OS partitions and consolidate the space.
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 4.) Will first try getting a chunk that will satisfy the maximum
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * error log size (NVRAM_MAX_REQ).
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 5.) If the max chunk cannot be allocated then try finding a chunk
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * that will satisfy the minum needed (NVRAM_MIN_REQ).
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_setup_partition(void)
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct list_head * p;
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct nvram_partition * part;
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int rc;
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* For now, we don't do any of this on pmac, until I
4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * have figured out if it's worth killing some unused stuffs
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * in our nvram, as Apple defined partitions use pretty much
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * all of the space
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
447e8222502ee6157e2713da9e0792c21f4ad458d50Benjamin Herrenschmidt	if (machine_is(powermac))
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOSPC;
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* see if we have an OS partition that meets our needs.
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   will try getting the max we need.  If not we'll delete
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   partitions and try again. */
4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each(p, &nvram_part->partition) {
4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		part = list_entry(p, struct nvram_partition, partition);
4551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (part->header.signature != NVRAM_SIG_OS)
4561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (strcmp(part->header.name, "ppc64,linux"))
4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
4601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (part->header.length >= NVRAM_MIN_REQ) {
4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* found our partition */
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			nvram_error_log_size = ((part->header.length - 1) *
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds						NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return 0;
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* try creating a partition with the free space we have */
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rc = nvram_create_os_partition();
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!rc) {
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* need to free up some space */
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rc = nvram_remove_os_partition();
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rc) {
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return rc;
4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* create a partition in this new space */
4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rc = nvram_create_os_partition();
4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rc) {
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "nvram_create_os_partition: Could not find a "
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       "NVRAM partition large enough\n");
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return rc;
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int nvram_scan_partitions(void)
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	loff_t cur_index = 0;
4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct nvram_header phead;
4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct nvram_partition * tmp_part;
4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned char c_sum;
5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char * header;
5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int total_size;
5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int err;
5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ppc_md.nvram_size == NULL)
5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENODEV;
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	total_size = ppc_md.nvram_size();
5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	header = (char *) kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!header) {
5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOMEM;
5121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	while (cur_index < total_size) {
5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
5171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (err != NVRAM_HEADER_LEN) {
5181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			printk(KERN_ERR "nvram_scan_partitions: Error parsing "
5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "nvram partitions\n");
5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto out;
5211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
5221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
5241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		memcpy(&phead, header, NVRAM_HEADER_LEN);
5261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		err = 0;
5281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		c_sum = nvram_checksum(&phead);
5291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (c_sum != phead.checksum) {
5301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			printk(KERN_WARNING "WARNING: nvram partition checksum"
5311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       " was %02x, should be %02x!\n",
5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       phead.checksum, c_sum);
5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			printk(KERN_WARNING "Terminating nvram partition scan\n");
5341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto out;
5351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
5361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (!phead.length) {
5371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			printk(KERN_WARNING "WARNING: nvram corruption "
5381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       "detected: 0-length partition\n");
5391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto out;
5401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		tmp_part = (struct nvram_partition *)
5421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
5431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		err = -ENOMEM;
5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (!tmp_part) {
5451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
5461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto out;
5471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
5481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
5501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		tmp_part->index = cur_index;
5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		list_add_tail(&tmp_part->partition, &nvram_part->partition);
5521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cur_index += phead.length * NVRAM_BLOCK_LEN;
5541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = 0;
5561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds out:
5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(header);
5591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return err;
5601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int __init nvram_init(void)
5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int error;
5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int rc;
5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
5681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return  -ENODEV;
5691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  	rc = misc_register(&nvram_dev);
5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rc != 0) {
5721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "nvram_init: failed to register device\n");
5731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return rc;
5741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  	/* initialize our anchor for the nvram partition list */
5771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  	nvram_part = (struct nvram_partition *) kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
5781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  	if (!nvram_part) {
5791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  		printk(KERN_ERR "nvram_init: Failed kmalloc\n");
5801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  		return -ENOMEM;
5811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  	}
5821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  	INIT_LIST_HEAD(&nvram_part->partition);
5831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  	/* Get all the NVRAM partitions */
5851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  	error = nvram_scan_partitions();
5861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  	if (error) {
5871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  		printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
5881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  		return error;
5891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  	}
5901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  	if(nvram_setup_partition())
5921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  		printk(KERN_WARNING "nvram_init: Could not find nvram partition"
5931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  		       " for nvram buffered error logging.\n");
5941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef DEBUG_NVRAM
5961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	nvram_print_partitions("NVRAM Partitions");
5971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif
5981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  	return rc;
6001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid __exit nvram_cleanup(void)
6031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds        misc_deregister( &nvram_dev );
6051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef CONFIG_PPC_PSERIES
6091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* nvram_write_error_log
6111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
6121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * We need to buffer the error logs into nvram to ensure that we have
6131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the failure information to decode.  If we have a severe error there
6141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * is no way to guarantee that the OS or the machine is in a state to
6151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * get back to user land and write the error to disk.  For example if
6161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the SCSI device driver causes a Machine Check by writing to a bad
6171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * IO address, there is no way of guaranteeing that the device driver
6181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * is in any state that is would also be able to write the error data
6191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * captured to disk, thus we buffer it in NVRAM for analysis on the
6201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * next boot.
6211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
6221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * In NVRAM the partition containing the error log buffer will looks like:
6231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Header (in bytes):
6241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * +-----------+----------+--------+------------+------------------+
6251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * | signature | checksum | length | name       | data             |
6261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * |0          |1         |2      3|4         15|16        length-1|
6271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * +-----------+----------+--------+------------+------------------+
6281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
6291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The 'data' section would look like (in bytes):
6301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * +--------------+------------+-----------------------------------+
6311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * | event_logged | sequence # | error log                         |
6321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * |0            3|4          7|8            nvram_error_log_size-1|
6331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * +--------------+------------+-----------------------------------+
6341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
6351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * event_logged: 0 if event has not been logged to syslog, 1 if it has
6361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * sequence #: The unique sequence # for each event. (until it wraps)
6371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * error log: The error log from event_scan
6381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
6391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint nvram_write_error_log(char * buff, int length, unsigned int err_type)
6401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int rc;
6421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	loff_t tmp_index;
6431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct err_log_info info;
6441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (no_logging) {
6461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EPERM;
6471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
6481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (nvram_error_log_index == -1) {
6501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ESPIPE;
6511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
6521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (length > nvram_error_log_size) {
6541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		length = nvram_error_log_size;
6551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
6561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	info.error_type = err_type;
6581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	info.seq_num = error_log_cnt;
6591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tmp_index = nvram_error_log_index;
6611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
6631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rc <= 0) {
6641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
6651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return rc;
6661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
6671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rc = ppc_md.nvram_write(buff, length, &tmp_index);
6691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rc <= 0) {
6701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
6711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return rc;
6721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
6731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
6751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* nvram_read_error_log
6781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
6791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Reads nvram for error log for at most 'length'
6801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
6811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint nvram_read_error_log(char * buff, int length, unsigned int * err_type)
6821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int rc;
6841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	loff_t tmp_index;
6851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct err_log_info info;
6861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (nvram_error_log_index == -1)
6881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -1;
6891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (length > nvram_error_log_size)
6911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		length = nvram_error_log_size;
6921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tmp_index = nvram_error_log_index;
6941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
6961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rc <= 0) {
6971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
6981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return rc;
6991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
7001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rc = ppc_md.nvram_read(buff, length, &tmp_index);
7021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rc <= 0) {
7031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
7041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return rc;
7051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
7061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	error_log_cnt = info.seq_num;
7081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*err_type = info.error_type;
7091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
7111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This doesn't actually zero anything, but it sets the event_logged
7141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * word to tell that this event is safely in syslog.
7151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
7161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint nvram_clear_error_log(void)
7171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
7181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	loff_t tmp_index;
7191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int clear_word = ERR_FLAG_ALREADY_LOGGED;
7201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int rc;
7211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tmp_index = nvram_error_log_index;
7231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
7251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rc <= 0) {
7261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
7271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return rc;
7281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
7291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
7311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif /* CONFIG_PPC_PSERIES */
7341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_init(nvram_init);
7361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_exit(nvram_cleanup);
7371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
738