blob: 6dd2700852f040ce5affc2c6463eed62a175811d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * c 2001 PPC 64 Team, IBM Corp
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * /dev/nvram driver for PPC64
10 *
11 * This perhaps should live in drivers/char
12 *
13 * TODO: Split the /dev/nvram part (that one can use
14 * drivers/char/generic_nvram.c) from the arch & partition
15 * parsing code.
16 */
17
18#include <linux/module.h>
19
20#include <linux/types.h>
21#include <linux/errno.h>
22#include <linux/fs.h>
23#include <linux/miscdevice.h>
24#include <linux/fcntl.h>
25#include <linux/nvram.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/spinlock.h>
29#include <asm/uaccess.h>
30#include <asm/nvram.h>
31#include <asm/rtas.h>
32#include <asm/prom.h>
33#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#undef DEBUG_NVRAM
36
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +100037#define NVRAM_HEADER_LEN sizeof(struct nvram_header)
38#define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN
39#define NVRAM_MAX_REQ 2079
40#define NVRAM_MIN_REQ 1055
Benjamin Herrenschmidt74d51d02010-07-29 14:45:24 +100041
42/* If change this size, then change the size of NVNAME_LEN */
43struct nvram_header {
44 unsigned char signature;
45 unsigned char checksum;
46 unsigned short length;
47 char name[12];
48};
49
50struct nvram_partition {
51 struct list_head partition;
52 struct nvram_header header;
53 unsigned int index;
54};
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static struct nvram_partition * nvram_part;
57static long nvram_error_log_index = -1;
58static long nvram_error_log_size = 0;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060struct err_log_info {
61 int error_type;
62 unsigned int seq_num;
63};
64
65static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
66{
67 int size;
68
69 if (ppc_md.nvram_size == NULL)
70 return -ENODEV;
71 size = ppc_md.nvram_size();
72
73 switch (origin) {
74 case 1:
75 offset += file->f_pos;
76 break;
77 case 2:
78 offset += size;
79 break;
80 }
81 if (offset < 0)
82 return -EINVAL;
83 file->f_pos = offset;
84 return file->f_pos;
85}
86
87
88static ssize_t dev_nvram_read(struct file *file, char __user *buf,
89 size_t count, loff_t *ppos)
90{
Arnd Bergmannf9ce2992005-12-09 19:21:44 +010091 ssize_t ret;
92 char *tmp = NULL;
93 ssize_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Arnd Bergmannf9ce2992005-12-09 19:21:44 +010095 ret = -ENODEV;
96 if (!ppc_md.nvram_size)
97 goto out;
98
99 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 size = ppc_md.nvram_size();
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100101 if (*ppos >= size || size < 0)
102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100104 count = min_t(size_t, count, size - *ppos);
105 count = min(count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100107 ret = -ENOMEM;
108 tmp = kmalloc(count, GFP_KERNEL);
109 if (!tmp)
110 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100112 ret = ppc_md.nvram_read(tmp, count, ppos);
113 if (ret <= 0)
114 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100116 if (copy_to_user(buf, tmp, ret))
117 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100119out:
120 kfree(tmp);
121 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123}
124
125static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100126 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100128 ssize_t ret;
129 char *tmp = NULL;
130 ssize_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100132 ret = -ENODEV;
133 if (!ppc_md.nvram_size)
134 goto out;
135
136 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 size = ppc_md.nvram_size();
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100138 if (*ppos >= size || size < 0)
139 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100141 count = min_t(size_t, count, size - *ppos);
142 count = min(count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100144 ret = -ENOMEM;
145 tmp = kmalloc(count, GFP_KERNEL);
146 if (!tmp)
147 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100149 ret = -EFAULT;
150 if (copy_from_user(tmp, buf, count))
151 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Arnd Bergmannf9ce2992005-12-09 19:21:44 +0100153 ret = ppc_md.nvram_write(tmp, count, ppos);
154
155out:
156 kfree(tmp);
157 return ret;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Thomas Gleixner3b03fec2009-10-14 22:42:28 +0000161static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
162 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 switch(cmd) {
165#ifdef CONFIG_PPC_PMAC
166 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
167 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
168 case IOC_NVRAM_GET_OFFSET: {
169 int part, offset;
170
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100171 if (!machine_is(powermac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -EINVAL;
173 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
174 return -EFAULT;
175 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
176 return -EINVAL;
177 offset = pmac_get_partition(part);
178 if (offset < 0)
179 return offset;
180 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
181 return -EFAULT;
182 return 0;
183 }
184#endif /* CONFIG_PPC_PMAC */
Stephen Rothwellaf308372006-03-23 17:38:10 +1100185 default:
186 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800190const struct file_operations nvram_fops = {
Thomas Gleixner3b03fec2009-10-14 22:42:28 +0000191 .owner = THIS_MODULE,
192 .llseek = dev_nvram_llseek,
193 .read = dev_nvram_read,
194 .write = dev_nvram_write,
195 .unlocked_ioctl = dev_nvram_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
198static struct miscdevice nvram_dev = {
199 NVRAM_MINOR,
200 "nvram",
201 &nvram_fops
202};
203
204
205#ifdef DEBUG_NVRAM
Thomas Gleixner32c105c2009-10-14 22:54:46 +0000206static void __init nvram_print_partitions(char * label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct list_head * p;
209 struct nvram_partition * tmp_part;
210
211 printk(KERN_WARNING "--------%s---------\n", label);
212 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
213 list_for_each(p, &nvram_part->partition) {
214 tmp_part = list_entry(p, struct nvram_partition, partition);
Will Schmidt5a43ee62006-04-26 11:09:46 -0500215 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 tmp_part->index, tmp_part->header.signature,
217 tmp_part->header.checksum, tmp_part->header.length,
218 tmp_part->header.name);
219 }
220}
221#endif
222
223
Thomas Gleixner32c105c2009-10-14 22:54:46 +0000224static int __init nvram_write_header(struct nvram_partition * part)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 loff_t tmp_index;
227 int rc;
228
229 tmp_index = part->index;
230 rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
231
232 return rc;
233}
234
235
Thomas Gleixner32c105c2009-10-14 22:54:46 +0000236static unsigned char __init nvram_checksum(struct nvram_header *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 unsigned int c_sum, c_sum2;
239 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
240 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
241
242 /* The sum may have spilled into the 3rd byte. Fold it back. */
243 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
244 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
245 c_sum2 = (c_sum >> 8) + (c_sum << 8);
246 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
247 return c_sum;
248}
249
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000250/**
251 * nvram_remove_partition - Remove one or more partitions in nvram
252 * @name: name of the partition to remove, or NULL for a
253 * signature only match
254 * @sig: signature of the partition(s) to remove
255 */
256
257static int __init nvram_remove_partition(const char *name, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000259 struct nvram_partition *part, *prev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 int rc;
261
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000262 list_for_each_entry(part, &nvram_part->partition, partition) {
263 if (part->header.signature != sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 continue;
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000265 if (name && strncmp(name, part->header.name, 12))
266 continue;
267
268 /* Make partition a free partition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 part->header.signature = NVRAM_SIG_FREE;
270 sprintf(part->header.name, "wwwwwwwwwwww");
271 part->header.checksum = nvram_checksum(&part->header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 rc = nvram_write_header(part);
273 if (rc <= 0) {
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000274 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return rc;
276 }
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000279 /* Merge contiguous ones */
280 prev = NULL;
281 list_for_each_entry_safe(part, tmp, &nvram_part->partition, partition) {
282 if (part->header.signature != NVRAM_SIG_FREE) {
283 prev = NULL;
284 continue;
285 }
286 if (prev) {
287 prev->header.length += part->header.length;
288 prev->header.checksum = nvram_checksum(&part->header);
289 rc = nvram_write_header(part);
290 if (rc <= 0) {
291 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
292 return rc;
293 }
294 list_del(&part->partition);
295 kfree(part);
296 } else
297 prev = part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
300 return 0;
301}
302
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000303/**
304 * nvram_create_partition - Create a partition in nvram
305 * @name: name of the partition to create
306 * @sig: signature of the partition to create
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +1000307 * @req_size: size of data to allocate in bytes
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000308 * @min_size: minimum acceptable size (0 means req_size)
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000309 *
310 * Returns a negative error code or a positive nvram index
311 * of the beginning of the data area of the newly created
312 * partition. If you provided a min_size smaller than req_size
313 * you need to query for the actual size yourself after the
314 * call using nvram_partition_get_size().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000316static loff_t __init nvram_create_partition(const char *name, int sig,
317 int req_size, int min_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Arnd Bergmanna341ad92005-06-28 20:33:49 +1000319 struct nvram_partition *part;
320 struct nvram_partition *new_part;
akpm@osdl.org0339ad72005-05-01 08:58:44 -0700321 struct nvram_partition *free_part = NULL;
Benjamin Herrenschmidtcef0d5a2010-07-29 17:22:34 +1000322 static char nv_init_vals[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 loff_t tmp_index;
324 long size = 0;
325 int rc;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000326
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +1000327 /* Convert sizes from bytes to blocks */
328 req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
329 min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
330
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000331 /* If no minimum size specified, make it the same as the
332 * requested size
333 */
334 if (min_size == 0)
335 min_size = req_size;
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000336 if (min_size > req_size)
337 return -EINVAL;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000338
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +1000339 /* Now add one block to each for the header */
340 req_size += 1;
341 min_size += 1;
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* Find a free partition that will give us the maximum needed size
344 If can't find one that will give us the minimum size needed */
Arnd Bergmanna341ad92005-06-28 20:33:49 +1000345 list_for_each_entry(part, &nvram_part->partition, partition) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (part->header.signature != NVRAM_SIG_FREE)
347 continue;
348
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000349 if (part->header.length >= req_size) {
350 size = req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 free_part = part;
352 break;
353 }
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000354 if (part->header.length > size &&
355 part->header.length >= min_size) {
356 size = part->header.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 free_part = part;
358 }
359 }
akpm@osdl.org0339ad72005-05-01 08:58:44 -0700360 if (!size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* Create our OS partition */
akpm@osdl.org0339ad72005-05-01 08:58:44 -0700364 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (!new_part) {
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000366 pr_err("nvram_create_os_partition: kmalloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return -ENOMEM;
368 }
369
370 new_part->index = free_part->index;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000371 new_part->header.signature = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 new_part->header.length = size;
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000373 strncpy(new_part->header.name, name, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 new_part->header.checksum = nvram_checksum(&new_part->header);
375
376 rc = nvram_write_header(new_part);
377 if (rc <= 0) {
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000378 pr_err("nvram_create_os_partition: nvram_write_header "
379 "failed (%d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return rc;
381 }
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000382 list_add_tail(&new_part->partition, &free_part->partition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000384 /* Adjust or remove the partition we stole the space from */
385 if (free_part->header.length > size) {
386 free_part->index += size * NVRAM_BLOCK_LEN;
387 free_part->header.length -= size;
388 free_part->header.checksum = nvram_checksum(&free_part->header);
389 rc = nvram_write_header(free_part);
390 if (rc <= 0) {
391 pr_err("nvram_create_os_partition: nvram_write_header "
392 "failed (%d)\n", rc);
393 return rc;
394 }
395 } else {
396 list_del(&free_part->partition);
397 kfree(free_part);
398 }
399
400 /* Clear the new partition */
Benjamin Herrenschmidtcef0d5a2010-07-29 17:22:34 +1000401 for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
402 tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
403 tmp_index += NVRAM_BLOCK_LEN) {
404 rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
405 if (rc <= 0) {
406 pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
407 return rc;
408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000411 return new_part->index + NVRAM_HEADER_LEN;
412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000414/**
415 * nvram_get_partition_size - Get the data size of an nvram partition
416 * @data_index: This is the offset of the start of the data of
417 * the partition. The same value that is returned by
418 * nvram_create_partition().
419 */
420static int nvram_get_partition_size(loff_t data_index)
421{
422 struct nvram_partition *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000424 list_for_each_entry(part, &nvram_part->partition, partition) {
425 if (part->index + NVRAM_HEADER_LEN == data_index)
426 return (part->header.length - 1) * NVRAM_BLOCK_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000428 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431
432/* nvram_setup_partition
433 *
434 * This will setup the partition we need for buffering the
435 * error logs and cleanup partitions if needed.
436 *
437 * The general strategy is the following:
438 * 1.) If there is ppc64,linux partition large enough then use it.
439 * 2.) If there is not a ppc64,linux partition large enough, search
440 * for a free partition that is large enough.
441 * 3.) If there is not a free partition large enough remove
442 * _all_ OS partitions and consolidate the space.
443 * 4.) Will first try getting a chunk that will satisfy the maximum
444 * error log size (NVRAM_MAX_REQ).
445 * 5.) If the max chunk cannot be allocated then try finding a chunk
446 * that will satisfy the minum needed (NVRAM_MIN_REQ).
447 */
Thomas Gleixner32c105c2009-10-14 22:54:46 +0000448static int __init nvram_setup_partition(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct list_head * p;
451 struct nvram_partition * part;
452 int rc;
453
454 /* For now, we don't do any of this on pmac, until I
455 * have figured out if it's worth killing some unused stuffs
456 * in our nvram, as Apple defined partitions use pretty much
457 * all of the space
458 */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100459 if (machine_is(powermac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return -ENOSPC;
461
462 /* see if we have an OS partition that meets our needs.
463 will try getting the max we need. If not we'll delete
464 partitions and try again. */
465 list_for_each(p, &nvram_part->partition) {
466 part = list_entry(p, struct nvram_partition, partition);
467 if (part->header.signature != NVRAM_SIG_OS)
468 continue;
469
470 if (strcmp(part->header.name, "ppc64,linux"))
471 continue;
472
Benjamin Herrenschmidt36673302010-07-29 18:18:44 +1000473 if ((part->header.length - 1) * NVRAM_BLOCK_LEN >= NVRAM_MIN_REQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* found our partition */
475 nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
476 nvram_error_log_size = ((part->header.length - 1) *
477 NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
478 return 0;
479 }
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000480
481 /* Found one but it's too small, remove it */
482 nvram_remove_partition("ppc64,linux", NVRAM_SIG_OS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484
485 /* try creating a partition with the free space we have */
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000486 rc = nvram_create_partition("ppc64,linux", NVRAM_SIG_OS,
Benjamin Herrenschmidt4e7c77a2010-07-29 15:28:20 +1000487 NVRAM_MAX_REQ, NVRAM_MIN_REQ);
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000488 if (rc < 0) {
Benjamin Herrenschmidtfa2b4e52010-07-29 18:19:59 +1000489 /* need to free up some space, remove any "OS" partition */
490 nvram_remove_partition(NULL, NVRAM_SIG_OS);
491
492 /* Try again */
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000493 rc = nvram_create_partition("ppc64,linux", NVRAM_SIG_OS,
494 NVRAM_MAX_REQ, NVRAM_MIN_REQ);
495 if (rc < 0) {
496 pr_err("nvram_create_partition: Could not find"
497 " enough space in NVRAM for partition\n");
498 return rc;
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
Benjamin Herrenschmidte49e2e82010-07-29 17:38:55 +1000502 nvram_error_log_index = rc;
503 nvram_error_log_size = nvram_get_partition_size(rc) - sizeof(struct err_log_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return 0;
505}
506
Thomas Gleixner32c105c2009-10-14 22:54:46 +0000507static int __init nvram_scan_partitions(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 loff_t cur_index = 0;
510 struct nvram_header phead;
511 struct nvram_partition * tmp_part;
512 unsigned char c_sum;
513 char * header;
514 int total_size;
515 int err;
516
517 if (ppc_md.nvram_size == NULL)
518 return -ENODEV;
519 total_size = ppc_md.nvram_size();
520
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800521 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!header) {
523 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
524 return -ENOMEM;
525 }
526
527 while (cur_index < total_size) {
528
529 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
530 if (err != NVRAM_HEADER_LEN) {
531 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
532 "nvram partitions\n");
533 goto out;
534 }
535
536 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
537
538 memcpy(&phead, header, NVRAM_HEADER_LEN);
539
540 err = 0;
541 c_sum = nvram_checksum(&phead);
542 if (c_sum != phead.checksum) {
543 printk(KERN_WARNING "WARNING: nvram partition checksum"
544 " was %02x, should be %02x!\n",
545 phead.checksum, c_sum);
546 printk(KERN_WARNING "Terminating nvram partition scan\n");
547 goto out;
548 }
549 if (!phead.length) {
550 printk(KERN_WARNING "WARNING: nvram corruption "
551 "detected: 0-length partition\n");
552 goto out;
553 }
554 tmp_part = (struct nvram_partition *)
555 kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
556 err = -ENOMEM;
557 if (!tmp_part) {
558 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
559 goto out;
560 }
561
562 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
563 tmp_part->index = cur_index;
564 list_add_tail(&tmp_part->partition, &nvram_part->partition);
565
566 cur_index += phead.length * NVRAM_BLOCK_LEN;
567 }
568 err = 0;
569
570 out:
571 kfree(header);
572 return err;
573}
574
575static int __init nvram_init(void)
576{
577 int error;
578 int rc;
579
Benjamin Herrenschmidt578914c2010-07-29 17:21:17 +1000580 BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
583 return -ENODEV;
584
585 rc = misc_register(&nvram_dev);
586 if (rc != 0) {
587 printk(KERN_ERR "nvram_init: failed to register device\n");
588 return rc;
589 }
590
591 /* initialize our anchor for the nvram partition list */
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800592 nvram_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!nvram_part) {
594 printk(KERN_ERR "nvram_init: Failed kmalloc\n");
595 return -ENOMEM;
596 }
597 INIT_LIST_HEAD(&nvram_part->partition);
598
599 /* Get all the NVRAM partitions */
600 error = nvram_scan_partitions();
601 if (error) {
602 printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
603 return error;
604 }
605
606 if(nvram_setup_partition())
607 printk(KERN_WARNING "nvram_init: Could not find nvram partition"
608 " for nvram buffered error logging.\n");
609
610#ifdef DEBUG_NVRAM
611 nvram_print_partitions("NVRAM Partitions");
612#endif
613
614 return rc;
615}
616
617void __exit nvram_cleanup(void)
618{
619 misc_deregister( &nvram_dev );
620}
621
622
623#ifdef CONFIG_PPC_PSERIES
624
625/* nvram_write_error_log
626 *
627 * We need to buffer the error logs into nvram to ensure that we have
628 * the failure information to decode. If we have a severe error there
629 * is no way to guarantee that the OS or the machine is in a state to
630 * get back to user land and write the error to disk. For example if
631 * the SCSI device driver causes a Machine Check by writing to a bad
632 * IO address, there is no way of guaranteeing that the device driver
633 * is in any state that is would also be able to write the error data
634 * captured to disk, thus we buffer it in NVRAM for analysis on the
635 * next boot.
636 *
637 * In NVRAM the partition containing the error log buffer will looks like:
638 * Header (in bytes):
639 * +-----------+----------+--------+------------+------------------+
640 * | signature | checksum | length | name | data |
641 * |0 |1 |2 3|4 15|16 length-1|
642 * +-----------+----------+--------+------------+------------------+
643 *
644 * The 'data' section would look like (in bytes):
645 * +--------------+------------+-----------------------------------+
646 * | event_logged | sequence # | error log |
647 * |0 3|4 7|8 nvram_error_log_size-1|
648 * +--------------+------------+-----------------------------------+
649 *
650 * event_logged: 0 if event has not been logged to syslog, 1 if it has
651 * sequence #: The unique sequence # for each event. (until it wraps)
652 * error log: The error log from event_scan
653 */
Linas Vepstas0f2342c2007-08-10 06:56:41 +1000654int nvram_write_error_log(char * buff, int length,
655 unsigned int err_type, unsigned int error_log_cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 int rc;
658 loff_t tmp_index;
659 struct err_log_info info;
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (nvram_error_log_index == -1) {
662 return -ESPIPE;
663 }
664
665 if (length > nvram_error_log_size) {
666 length = nvram_error_log_size;
667 }
668
669 info.error_type = err_type;
670 info.seq_num = error_log_cnt;
671
672 tmp_index = nvram_error_log_index;
673
674 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
675 if (rc <= 0) {
676 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
677 return rc;
678 }
679
680 rc = ppc_md.nvram_write(buff, length, &tmp_index);
681 if (rc <= 0) {
682 printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
683 return rc;
684 }
685
686 return 0;
687}
688
689/* nvram_read_error_log
690 *
691 * Reads nvram for error log for at most 'length'
692 */
Linas Vepstas0f2342c2007-08-10 06:56:41 +1000693int nvram_read_error_log(char * buff, int length,
694 unsigned int * err_type, unsigned int * error_log_cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 int rc;
697 loff_t tmp_index;
698 struct err_log_info info;
699
700 if (nvram_error_log_index == -1)
701 return -1;
702
703 if (length > nvram_error_log_size)
704 length = nvram_error_log_size;
705
706 tmp_index = nvram_error_log_index;
707
708 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
709 if (rc <= 0) {
710 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
711 return rc;
712 }
713
714 rc = ppc_md.nvram_read(buff, length, &tmp_index);
715 if (rc <= 0) {
716 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
717 return rc;
718 }
719
Linas Vepstas0f2342c2007-08-10 06:56:41 +1000720 *error_log_cnt = info.seq_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 *err_type = info.error_type;
722
723 return 0;
724}
725
726/* This doesn't actually zero anything, but it sets the event_logged
727 * word to tell that this event is safely in syslog.
728 */
729int nvram_clear_error_log(void)
730{
731 loff_t tmp_index;
732 int clear_word = ERR_FLAG_ALREADY_LOGGED;
733 int rc;
734
Thomas Gleixnerfd62c6c2009-10-14 22:54:40 +0000735 if (nvram_error_log_index == -1)
736 return -1;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 tmp_index = nvram_error_log_index;
739
740 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
741 if (rc <= 0) {
742 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
743 return rc;
744 }
745
746 return 0;
747}
748
749#endif /* CONFIG_PPC_PSERIES */
750
751module_init(nvram_init);
752module_exit(nvram_cleanup);
753MODULE_LICENSE("GPL");